working on cache and controllers
This commit is contained in:
parent
892a7e6905
commit
c0cd519a4f
|
@ -9,14 +9,7 @@ import java.util.Map;
|
||||||
|
|
||||||
public interface BuntiController {
|
public interface BuntiController {
|
||||||
|
|
||||||
Collection<BuntiDevice> getAllDevices();
|
|
||||||
BuntiDevice getDeviceById(int deviceId);
|
|
||||||
|
|
||||||
boolean updateDeviceData(int deviceId, Map<String, Object> options);
|
boolean updateDeviceData(int deviceId, Map<String, Object> options);
|
||||||
|
|
||||||
Collection<Room> getAllRooms();
|
|
||||||
Room getRoomById(int roomId);
|
|
||||||
|
|
||||||
DeviceUpdate getDeviceValues(int deviceId);
|
DeviceUpdate getDeviceValues(int deviceId);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,8 @@
|
||||||
package de.ctdo.bunti.control;
|
package de.ctdo.bunti.control;
|
||||||
|
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import de.ctdo.bunti.dao.RoomsDAO;
|
|
||||||
import de.ctdo.bunti.model.*;
|
import de.ctdo.bunti.model.*;
|
||||||
import org.apache.commons.collections.map.HashedMap;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
@ -20,27 +16,18 @@ import de.ctdo.bunti.dao.BuntiDevicesDAO;
|
||||||
public class BuntiControllerImpl implements BuntiController, ApplicationEventPublisherAware {
|
public class BuntiControllerImpl implements BuntiController, ApplicationEventPublisherAware {
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(BuntiControllerImpl.class);
|
private static final Logger LOGGER = LoggerFactory.getLogger(BuntiControllerImpl.class);
|
||||||
private ApplicationEventPublisher applicationEventPublisher = null;
|
private ApplicationEventPublisher applicationEventPublisher = null;
|
||||||
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
|
@Autowired
|
||||||
public final void setRoomsDAO(RoomsDAO roomsDAO) {
|
private BuntiDevicesDAO devicesDAO;
|
||||||
this.roomsDAO = roomsDAO;
|
|
||||||
}
|
@Autowired
|
||||||
|
private DeviceValueCache deviceCache;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public final void setApplicationEventPublisher(ApplicationEventPublisher publisher) {
|
public final void setApplicationEventPublisher(ApplicationEventPublisher publisher) {
|
||||||
this.applicationEventPublisher = publisher;
|
this.applicationEventPublisher = publisher;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public final boolean updateDeviceData(int deviceId, Map<String, Object> options) {
|
public final boolean updateDeviceData(int deviceId, Map<String, Object> options) {
|
||||||
BuntiDevice device = devicesDAO.getDeviceById(deviceId);
|
BuntiDevice device = devicesDAO.getDeviceById(deviceId);
|
||||||
|
@ -48,12 +35,7 @@ public class BuntiControllerImpl implements BuntiController, ApplicationEventPu
|
||||||
if (device != null) {
|
if (device != null) {
|
||||||
LOGGER.debug("publishEvent in BuntiController");
|
LOGGER.debug("publishEvent in BuntiController");
|
||||||
|
|
||||||
Map<String, Object> cached = deviceCache.get(deviceId);
|
deviceCache.updateData(deviceId, options);
|
||||||
if(cached == null ) {
|
|
||||||
cached = new HashMap<String, Object>();
|
|
||||||
deviceCache.put(deviceId, cached);
|
|
||||||
}
|
|
||||||
cached.putAll(options);
|
|
||||||
|
|
||||||
device.setValuesFromOptions(options) ;
|
device.setValuesFromOptions(options) ;
|
||||||
|
|
||||||
|
@ -67,32 +49,7 @@ public class BuntiControllerImpl implements BuntiController, ApplicationEventPu
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public DeviceUpdate getDeviceValues(int deviceId) {
|
public DeviceUpdate getDeviceValues(int deviceId) {
|
||||||
Map<String, Object> cached = deviceCache.get(deviceId);
|
return deviceCache.getData(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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -13,5 +13,7 @@ public interface BuntiDevicesDAO {
|
||||||
|
|
||||||
void addDevice(BuntiDevice device);
|
void addDevice(BuntiDevice device);
|
||||||
void removeDevice(int deviceId);
|
void removeDevice(int deviceId);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,4 +44,6 @@ public final class BuntiDevicesDAOImpl implements BuntiDevicesDAO {
|
||||||
em.remove(getDeviceById(deviceId));
|
em.remove(getDeviceById(deviceId));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,29 +13,28 @@ import org.springframework.web.bind.annotation.*;
|
||||||
@RequestMapping(value = "/control")
|
@RequestMapping(value = "/control")
|
||||||
public class DeviceControlController {
|
public class DeviceControlController {
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(DeviceControlController.class);
|
private static final Logger LOGGER = LoggerFactory.getLogger(DeviceControlController.class);
|
||||||
private BuntiController controller;
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public final void setController(BuntiController controller) {
|
private BuntiController controller;
|
||||||
this.controller = controller;
|
|
||||||
}
|
|
||||||
|
|
||||||
@RequestMapping(value = "/devices", method = RequestMethod.POST)
|
@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() );
|
LOGGER.info("handle POST /devices" + " request update=" + updates.toString() );
|
||||||
|
|
||||||
for (DeviceUpdate update: updates.getUpdates()) {
|
for (DeviceUpdate update: updates.getUpdates()) {
|
||||||
LOGGER.info("Update deviceId=" + update.getDeviceId());
|
LOGGER.info("Update deviceId=" + update.getDeviceId());
|
||||||
controller.updateDeviceData(update.getDeviceId(), update.getOptions());
|
controller.updateDeviceData(update.getDeviceId(), update.getOptions());
|
||||||
}
|
}
|
||||||
|
return "{\"status\":\"OK\"}";
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping(value = "/devices/{id}", method = RequestMethod.GET)
|
@RequestMapping(value = "/devices/{id}", method = RequestMethod.GET)
|
||||||
|
@ResponseBody
|
||||||
public DeviceUpdate getDeviceValues(@PathVariable("id") int id) {
|
public DeviceUpdate getDeviceValues(@PathVariable("id") int id) {
|
||||||
LOGGER.info("handle GET /devices/{id} " + id );
|
LOGGER.info("handle GET /devices/{id} " + id );
|
||||||
|
|
||||||
return controller.getDeviceValues(id);
|
return controller.getDeviceValues(id);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
package de.ctdo.bunti.web;
|
package de.ctdo.bunti.web;
|
||||||
|
|
||||||
import de.ctdo.bunti.control.BuntiController;
|
|
||||||
import de.ctdo.bunti.dao.BuntiDevicesDAO;
|
import de.ctdo.bunti.dao.BuntiDevicesDAO;
|
||||||
import de.ctdo.bunti.model.BuntiDevice;
|
import de.ctdo.bunti.model.BuntiDevice;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
@ -12,35 +11,26 @@ import java.util.Collection;
|
||||||
@Controller
|
@Controller
|
||||||
@RequestMapping(value = "/devices")
|
@RequestMapping(value = "/devices")
|
||||||
public class DevicesController {
|
public class DevicesController {
|
||||||
|
@Autowired
|
||||||
private BuntiDevicesDAO devicesDAO;
|
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)
|
@RequestMapping(value = "", method = RequestMethod.GET)
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
public Collection<BuntiDevice> getAll() {
|
public Collection<BuntiDevice> getAll() {
|
||||||
return buntiController.getAllDevices();
|
return devicesDAO.getAllDevices();
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
|
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
public BuntiDevice getDeviceById(@PathVariable("id") int id) {
|
public BuntiDevice getDeviceById(@PathVariable("id") int id) {
|
||||||
return buntiController.getDeviceById(id);
|
return devicesDAO.getDeviceById(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping(value = "", method = RequestMethod.POST)
|
@RequestMapping(value = "", method = RequestMethod.POST)
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
public BuntiDevice setDevices(@RequestBody BuntiDevice device) {
|
public String setDevices(@RequestBody BuntiDevice device) {
|
||||||
//buntiController.addDevice(device);
|
devicesDAO.addDevice(device);
|
||||||
return device;
|
return "{\"status\":\"OK\"}";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,12 +13,8 @@ import java.util.List;
|
||||||
@Controller
|
@Controller
|
||||||
@RequestMapping(value = "/rooms")
|
@RequestMapping(value = "/rooms")
|
||||||
public class RoomsController {
|
public class RoomsController {
|
||||||
private RoomsDAO roomsDAO;
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public void setRoomsDAO(RoomsDAO roomsDAO) {
|
private RoomsDAO roomsDAO;
|
||||||
this.roomsDAO = roomsDAO;
|
|
||||||
}
|
|
||||||
|
|
||||||
@RequestMapping(value = "", method = RequestMethod.GET)
|
@RequestMapping(value = "", method = RequestMethod.GET)
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
|
|
|
@ -1,40 +1,44 @@
|
||||||
package de.ctdo.bunti.control;
|
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.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
|
||||||
//@ContextConfiguration(locations = { "classpath:/META-INF/spring/root-context.xml","classpath:/de/ctdo/bunti/hibernate-test-context.xml" })
|
import java.util.HashMap;
|
||||||
//@RunWith(SpringJUnit4ClassRunner.class)
|
import java.util.Map;
|
||||||
//public class BuntiControllerImplTest {
|
|
||||||
|
|
||||||
// @Autowired
|
@ContextConfiguration(locations = { "classpath:/META-INF/spring/root-context.xml","classpath:/de/ctdo/bunti/hibernate-test-context.xml" })
|
||||||
// BuntiController dut;
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
//
|
public class BuntiControllerImplTest {
|
||||||
// @Test
|
|
||||||
// public void testUpdateDeviceData() throws Exception {
|
@Autowired
|
||||||
//
|
BuntiController dut;
|
||||||
// }
|
|
||||||
//
|
@Autowired
|
||||||
// @Test
|
BuntiDevicesDAO dao;
|
||||||
// public void testGetAllRooms() throws Exception {
|
|
||||||
//
|
@Test
|
||||||
// }
|
public void test1() throws Exception {
|
||||||
//
|
|
||||||
// @Test
|
BuntiDevice dev = dao.getDeviceById(2);
|
||||||
// public void testGetRoomById() throws Exception {
|
DeviceUpdate upt = dut.getDeviceValues(2);
|
||||||
//
|
|
||||||
// }
|
Map<String, Object> data = new HashMap<String, Object>();
|
||||||
//
|
data.put("red", 10);
|
||||||
// @Test
|
data.put("green", 20);
|
||||||
// public void testGetAllDevices() throws Exception {
|
data.put("blue", 30);
|
||||||
//
|
|
||||||
// }
|
dut.updateDeviceData(2, data);
|
||||||
//
|
|
||||||
// @Test
|
upt = dut.getDeviceValues(2);
|
||||||
// public void testGetDeviceById() throws Exception {
|
|
||||||
//
|
return;
|
||||||
// }
|
}
|
||||||
//}
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue