diff --git a/src/main/java/de/ctdo/bunti/control/BuntiController.java b/src/main/java/de/ctdo/bunti/control/BuntiController.java index 79e0f72..c83ddd8 100644 --- a/src/main/java/de/ctdo/bunti/control/BuntiController.java +++ b/src/main/java/de/ctdo/bunti/control/BuntiController.java @@ -9,14 +9,7 @@ import java.util.Map; public interface BuntiController { - Collection getAllDevices(); - BuntiDevice getDeviceById(int deviceId); - boolean updateDeviceData(int deviceId, Map options); - - Collection getAllRooms(); - Room getRoomById(int roomId); - DeviceUpdate getDeviceValues(int deviceId); } diff --git a/src/main/java/de/ctdo/bunti/control/BuntiControllerImpl.java b/src/main/java/de/ctdo/bunti/control/BuntiControllerImpl.java index 3dc4a0a..e2eddde 100644 --- a/src/main/java/de/ctdo/bunti/control/BuntiControllerImpl.java +++ b/src/main/java/de/ctdo/bunti/control/BuntiControllerImpl.java @@ -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; - private BuntiDevicesDAO devicesDAO; - private RoomsDAO roomsDAO; - private Map> deviceCache = new HashMap>(); - - - @Autowired - public final void setDevicesDAO(BuntiDevicesDAO devicesDAO) { - this.devicesDAO = devicesDAO; - } @Autowired - public final void setRoomsDAO(RoomsDAO roomsDAO) { - this.roomsDAO = roomsDAO; - } + private BuntiDevicesDAO devicesDAO; + + @Autowired + private DeviceValueCache deviceCache; @Override public final void setApplicationEventPublisher(ApplicationEventPublisher publisher) { this.applicationEventPublisher = publisher; } - @Override public final boolean updateDeviceData(int deviceId, Map 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 cached = deviceCache.get(deviceId); - if(cached == null ) { - cached = new HashMap(); - 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 cached = deviceCache.get(deviceId); - - DeviceUpdate du = new DeviceUpdate(); - du.setDeviceId(deviceId); - du.setOptions(cached); - return du; - } - - @Override - public Collection getAllRooms() { - return roomsDAO.getRooms(); - } - - @Override - public Room getRoomById(int roomId) { - return roomsDAO.getRoom(roomId); - } - - @Override - public Collection getAllDevices() { - return devicesDAO.getAllDevices(); - } - - @Override - public BuntiDevice getDeviceById(int deviceId) { - return devicesDAO.getDeviceById(deviceId); + return deviceCache.getData(deviceId); } } diff --git a/src/main/java/de/ctdo/bunti/control/DeviceValueCache.java b/src/main/java/de/ctdo/bunti/control/DeviceValueCache.java new file mode 100644 index 0000000..d6aa5b7 --- /dev/null +++ b/src/main/java/de/ctdo/bunti/control/DeviceValueCache.java @@ -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> deviceCache; + + private void init() { + deviceCache = new HashMap>(); + + for(BuntiDevice device: devicesDAO.getAllDevices()) { + deviceCache.put(device.getId(), device.getOptions()); + } + + } + + public void updateData(int deviceId, Map options) { + if(deviceCache == null) init(); + + Map cached = deviceCache.get(deviceId); + if(cached == null ) { + cached = new HashMap(); + deviceCache.put(deviceId, cached); + } + + cached.putAll(options); + } + + + public DeviceUpdate getData(int deviceId) { + if(deviceCache == null) init(); + + Map cached = deviceCache.get(deviceId); + + DeviceUpdate du = new DeviceUpdate(); + du.setDeviceId(deviceId); + du.setOptions(cached); + return du; + } + + +} diff --git a/src/main/java/de/ctdo/bunti/dao/BuntiDevicesDAO.java b/src/main/java/de/ctdo/bunti/dao/BuntiDevicesDAO.java index 4394618..4397cf9 100644 --- a/src/main/java/de/ctdo/bunti/dao/BuntiDevicesDAO.java +++ b/src/main/java/de/ctdo/bunti/dao/BuntiDevicesDAO.java @@ -13,5 +13,7 @@ public interface BuntiDevicesDAO { void addDevice(BuntiDevice device); void removeDevice(int deviceId); + + } diff --git a/src/main/java/de/ctdo/bunti/dao/BuntiDevicesDAOImpl.java b/src/main/java/de/ctdo/bunti/dao/BuntiDevicesDAOImpl.java index bef44fe..25fe455 100644 --- a/src/main/java/de/ctdo/bunti/dao/BuntiDevicesDAOImpl.java +++ b/src/main/java/de/ctdo/bunti/dao/BuntiDevicesDAOImpl.java @@ -44,4 +44,6 @@ public final class BuntiDevicesDAOImpl implements BuntiDevicesDAO { em.remove(getDeviceById(deviceId)); } + + } diff --git a/src/main/java/de/ctdo/bunti/web/DeviceControlController.java b/src/main/java/de/ctdo/bunti/web/DeviceControlController.java index 751445a..beb19a5 100644 --- a/src/main/java/de/ctdo/bunti/web/DeviceControlController.java +++ b/src/main/java/de/ctdo/bunti/web/DeviceControlController.java @@ -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); - } } diff --git a/src/main/java/de/ctdo/bunti/web/DevicesController.java b/src/main/java/de/ctdo/bunti/web/DevicesController.java index 6bf563d..61f7402 100644 --- a/src/main/java/de/ctdo/bunti/web/DevicesController.java +++ b/src/main/java/de/ctdo/bunti/web/DevicesController.java @@ -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 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\"}"; } } diff --git a/src/main/java/de/ctdo/bunti/web/RoomsController.java b/src/main/java/de/ctdo/bunti/web/RoomsController.java index 0bb9ee3..8c1f927 100644 --- a/src/main/java/de/ctdo/bunti/web/RoomsController.java +++ b/src/main/java/de/ctdo/bunti/web/RoomsController.java @@ -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 diff --git a/src/test/java/de/ctdo/bunti/control/BuntiControllerImplTest.java b/src/test/java/de/ctdo/bunti/control/BuntiControllerImplTest.java index a474047..81130fb 100644 --- a/src/test/java/de/ctdo/bunti/control/BuntiControllerImplTest.java +++ b/src/test/java/de/ctdo/bunti/control/BuntiControllerImplTest.java @@ -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 data = new HashMap(); + data.put("red", 10); + data.put("green", 20); + data.put("blue", 30); + + dut.updateDeviceData(2, data); + + upt = dut.getDeviceValues(2); + + return; + } + + +}