working on cache and controllers

This commit is contained in:
Lucas Pleß 2012-05-26 01:56:44 +02:00
parent 892a7e6905
commit c0cd519a4f
9 changed files with 109 additions and 114 deletions

View File

@ -9,14 +9,7 @@ import java.util.Map;
public interface BuntiController {
Collection<BuntiDevice> getAllDevices();
BuntiDevice getDeviceById(int deviceId);
boolean updateDeviceData(int deviceId, Map<String, Object> options);
Collection<Room> getAllRooms();
Room getRoomById(int roomId);
DeviceUpdate getDeviceValues(int deviceId);
}

View File

@ -1,12 +1,8 @@
package de.ctdo.bunti.control;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import de.ctdo.bunti.dao.RoomsDAO;
import de.ctdo.bunti.model.*;
import org.apache.commons.collections.map.HashedMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@ -20,27 +16,18 @@ import de.ctdo.bunti.dao.BuntiDevicesDAO;
public class BuntiControllerImpl implements BuntiController, ApplicationEventPublisherAware {
private static final Logger LOGGER = LoggerFactory.getLogger(BuntiControllerImpl.class);
private ApplicationEventPublisher applicationEventPublisher = null;
@Autowired
private BuntiDevicesDAO devicesDAO;
private RoomsDAO roomsDAO;
private Map<Integer, Map<String, Object>> deviceCache = new HashMap<Integer, Map<String, Object>>();
@Autowired
public final void setDevicesDAO(BuntiDevicesDAO devicesDAO) {
this.devicesDAO = devicesDAO;
}
@Autowired
public final void setRoomsDAO(RoomsDAO roomsDAO) {
this.roomsDAO = roomsDAO;
}
private DeviceValueCache deviceCache;
@Override
public final void setApplicationEventPublisher(ApplicationEventPublisher publisher) {
this.applicationEventPublisher = publisher;
}
@Override
public final boolean updateDeviceData(int deviceId, Map<String, Object> options) {
BuntiDevice device = devicesDAO.getDeviceById(deviceId);
@ -48,12 +35,7 @@ public class BuntiControllerImpl implements BuntiController, ApplicationEventPu
if (device != null) {
LOGGER.debug("publishEvent in BuntiController");
Map<String, Object> cached = deviceCache.get(deviceId);
if(cached == null ) {
cached = new HashMap<String, Object>();
deviceCache.put(deviceId, cached);
}
cached.putAll(options);
deviceCache.updateData(deviceId, options);
device.setValuesFromOptions(options) ;
@ -67,32 +49,7 @@ public class BuntiControllerImpl implements BuntiController, ApplicationEventPu
@Override
public DeviceUpdate getDeviceValues(int deviceId) {
Map<String, Object> cached = deviceCache.get(deviceId);
DeviceUpdate du = new DeviceUpdate();
du.setDeviceId(deviceId);
du.setOptions(cached);
return du;
}
@Override
public Collection<Room> getAllRooms() {
return roomsDAO.getRooms();
}
@Override
public Room getRoomById(int roomId) {
return roomsDAO.getRoom(roomId);
}
@Override
public Collection<BuntiDevice> getAllDevices() {
return devicesDAO.getAllDevices();
}
@Override
public BuntiDevice getDeviceById(int deviceId) {
return devicesDAO.getDeviceById(deviceId);
return deviceCache.getData(deviceId);
}
}

View File

@ -0,0 +1,52 @@
package de.ctdo.bunti.control;
import de.ctdo.bunti.dao.BuntiDevicesDAO;
import de.ctdo.bunti.model.BuntiDevice;
import de.ctdo.bunti.model.DeviceUpdate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
@Component
public class DeviceValueCache {
@Autowired
private BuntiDevicesDAO devicesDAO;
private Map<Integer, Map<String, Object>> deviceCache;
private void init() {
deviceCache = new HashMap<Integer, Map<String, Object>>();
for(BuntiDevice device: devicesDAO.getAllDevices()) {
deviceCache.put(device.getId(), device.getOptions());
}
}
public void updateData(int deviceId, Map<String, Object> options) {
if(deviceCache == null) init();
Map<String, Object> cached = deviceCache.get(deviceId);
if(cached == null ) {
cached = new HashMap<String, Object>();
deviceCache.put(deviceId, cached);
}
cached.putAll(options);
}
public DeviceUpdate getData(int deviceId) {
if(deviceCache == null) init();
Map<String, Object> cached = deviceCache.get(deviceId);
DeviceUpdate du = new DeviceUpdate();
du.setDeviceId(deviceId);
du.setOptions(cached);
return du;
}
}

View File

@ -14,4 +14,6 @@ public interface BuntiDevicesDAO {
void addDevice(BuntiDevice device);
void removeDevice(int deviceId);
}

View File

@ -44,4 +44,6 @@ public final class BuntiDevicesDAOImpl implements BuntiDevicesDAO {
em.remove(getDeviceById(deviceId));
}
}

View File

@ -13,29 +13,28 @@ import org.springframework.web.bind.annotation.*;
@RequestMapping(value = "/control")
public class DeviceControlController {
private static final Logger LOGGER = LoggerFactory.getLogger(DeviceControlController.class);
private BuntiController controller;
@Autowired
public final void setController(BuntiController controller) {
this.controller = controller;
}
private BuntiController controller;
@RequestMapping(value = "/devices", method = RequestMethod.POST)
public void setDevices(@RequestBody DeviceUpdates updates) {
@ResponseBody
public String setDevices(@RequestBody DeviceUpdates updates) {
LOGGER.info("handle POST /devices" + " request update=" + updates.toString() );
for (DeviceUpdate update: updates.getUpdates()) {
LOGGER.info("Update deviceId=" + update.getDeviceId());
controller.updateDeviceData(update.getDeviceId(), update.getOptions());
}
return "{\"status\":\"OK\"}";
}
@RequestMapping(value = "/devices/{id}", method = RequestMethod.GET)
@ResponseBody
public DeviceUpdate getDeviceValues(@PathVariable("id") int id) {
LOGGER.info("handle GET /devices/{id} " + id );
return controller.getDeviceValues(id);
}
}

View File

@ -1,6 +1,5 @@
package de.ctdo.bunti.web;
import de.ctdo.bunti.control.BuntiController;
import de.ctdo.bunti.dao.BuntiDevicesDAO;
import de.ctdo.bunti.model.BuntiDevice;
import org.springframework.beans.factory.annotation.Autowired;
@ -12,35 +11,26 @@ import java.util.Collection;
@Controller
@RequestMapping(value = "/devices")
public class DevicesController {
@Autowired
private BuntiDevicesDAO devicesDAO;
private BuntiController buntiController;
@Autowired
public void setDevicesDAO(BuntiDevicesDAO devicesDAO) {
this.devicesDAO = devicesDAO;
}
@Autowired
public void setBuntiController(BuntiController buntiController) {
this.buntiController = buntiController;
}
@RequestMapping(value = "", method = RequestMethod.GET)
@ResponseBody
public Collection<BuntiDevice> getAll() {
return buntiController.getAllDevices();
return devicesDAO.getAllDevices();
}
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
@ResponseBody
public BuntiDevice getDeviceById(@PathVariable("id") int id) {
return buntiController.getDeviceById(id);
return devicesDAO.getDeviceById(id);
}
@RequestMapping(value = "", method = RequestMethod.POST)
@ResponseBody
public BuntiDevice setDevices(@RequestBody BuntiDevice device) {
//buntiController.addDevice(device);
return device;
public String setDevices(@RequestBody BuntiDevice device) {
devicesDAO.addDevice(device);
return "{\"status\":\"OK\"}";
}
}

View File

@ -13,12 +13,8 @@ import java.util.List;
@Controller
@RequestMapping(value = "/rooms")
public class RoomsController {
private RoomsDAO roomsDAO;
@Autowired
public void setRoomsDAO(RoomsDAO roomsDAO) {
this.roomsDAO = roomsDAO;
}
private RoomsDAO roomsDAO;
@RequestMapping(value = "", method = RequestMethod.GET)
@ResponseBody

View File

@ -1,40 +1,44 @@
package de.ctdo.bunti.control;
import de.ctdo.bunti.dao.BuntiDevicesDAO;
import de.ctdo.bunti.model.BuntiDevice;
import de.ctdo.bunti.model.DeviceUpdate;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
//@ContextConfiguration(locations = { "classpath:/META-INF/spring/root-context.xml","classpath:/de/ctdo/bunti/hibernate-test-context.xml" })
//@RunWith(SpringJUnit4ClassRunner.class)
//public class BuntiControllerImplTest {
import java.util.HashMap;
import java.util.Map;
// @Autowired
// BuntiController dut;
//
// @Test
// public void testUpdateDeviceData() throws Exception {
//
// }
//
// @Test
// public void testGetAllRooms() throws Exception {
//
// }
//
// @Test
// public void testGetRoomById() throws Exception {
//
// }
//
// @Test
// public void testGetAllDevices() throws Exception {
//
// }
//
// @Test
// public void testGetDeviceById() throws Exception {
//
// }
//}
@ContextConfiguration(locations = { "classpath:/META-INF/spring/root-context.xml","classpath:/de/ctdo/bunti/hibernate-test-context.xml" })
@RunWith(SpringJUnit4ClassRunner.class)
public class BuntiControllerImplTest {
@Autowired
BuntiController dut;
@Autowired
BuntiDevicesDAO dao;
@Test
public void test1() throws Exception {
BuntiDevice dev = dao.getDeviceById(2);
DeviceUpdate upt = dut.getDeviceValues(2);
Map<String, Object> data = new HashMap<String, Object>();
data.put("red", 10);
data.put("green", 20);
data.put("blue", 30);
dut.updateDeviceData(2, data);
upt = dut.getDeviceValues(2);
return;
}
}