From 9c54e5d73ced06d6a938bc6324f0af875492fee1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Ple=C3=9F?= Date: Mon, 12 Mar 2012 01:17:52 +0100 Subject: [PATCH] working on rest controller. something with Collection is not working... --- .../bunti/control/BuntiControllerImpl.java | 5 +- .../de/ctdo/bunti/web/RestController.java | 42 ++++++++++++++- .../de/ctdo/bunti/webmodel/DeviceUpdate.java | 2 +- .../de/ctdo/bunti/webmodel/DeviceUpdates.java | 10 ++++ src/main/resources/log4j.xml | 22 ++------ src/main/webapp/WEB-INF/jsp/index.jsp | 51 ++----------------- 6 files changed, 62 insertions(+), 70 deletions(-) create mode 100644 src/main/java/de/ctdo/bunti/webmodel/DeviceUpdates.java diff --git a/src/main/java/de/ctdo/bunti/control/BuntiControllerImpl.java b/src/main/java/de/ctdo/bunti/control/BuntiControllerImpl.java index a4b67b4..5e584c8 100644 --- a/src/main/java/de/ctdo/bunti/control/BuntiControllerImpl.java +++ b/src/main/java/de/ctdo/bunti/control/BuntiControllerImpl.java @@ -36,9 +36,10 @@ public class BuntiControllerImpl implements BuntiController, ApplicationEventPub BuntiDevice device = devicesDAO.getDeviceById(deviceId); if (device != null) { - this.applicationEventPublisher.publishEvent(new DeviceChangedEvent(this, device, options)); - LOGGER.debug("publishEvent in BuntiController"); + + this.applicationEventPublisher.publishEvent(new DeviceChangedEvent(this, device, options)); + return true; } diff --git a/src/main/java/de/ctdo/bunti/web/RestController.java b/src/main/java/de/ctdo/bunti/web/RestController.java index cc34cbc..e7a62c7 100644 --- a/src/main/java/de/ctdo/bunti/web/RestController.java +++ b/src/main/java/de/ctdo/bunti/web/RestController.java @@ -2,6 +2,8 @@ package de.ctdo.bunti.web; import de.ctdo.bunti.control.BuntiController; import de.ctdo.bunti.model.BuntiDevice; +import de.ctdo.bunti.webmodel.DeviceUpdate; +import de.ctdo.bunti.webmodel.DeviceUpdates; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -9,10 +11,10 @@ import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import javax.validation.Validator; -import java.util.Collection; -import java.util.Map; +import java.util.*; @Controller +@RequestMapping(value = "/control") public class RestController { private static final Logger LOGGER = LoggerFactory.getLogger(RestController.class); // private Validator validator; @@ -28,6 +30,17 @@ public class RestController { this.controller = controller; } + @RequestMapping(value = "/devices2", method = RequestMethod.GET) + public @ResponseBody DeviceUpdate testGet() { + DeviceUpdate update = new DeviceUpdate(); + update.setDeviceId(23); + Map options = new HashMap(); + options.put("red", 111); + options.put("green", 2); + options.put("blue", 33); + update.setOptions(options); + return update; + } @RequestMapping(value = "/devices", method = RequestMethod.GET) public @ResponseBody Collection getAll() { @@ -49,4 +62,29 @@ public class RestController { return "redirect:devices/" + id; } + @RequestMapping(value = "/devices", method = RequestMethod.PUT) + public String setDevices(@RequestBody ArrayList updates) { + LOGGER.info("handle PUT /devices" + " request update=" + updates.toString() ); + + for (DeviceUpdate update: updates) { + LOGGER.info("Update deviceId=" + update.getDeviceId()); + + controller.updateDeviceData(update.getDeviceId(), update.getOptions()); + } + + return "redirect:devices"; + } + + @RequestMapping(value = "/devices2", method = RequestMethod.PUT) + public String setDevices2(@RequestBody DeviceUpdate update) { + LOGGER.info("handle PUT /devices" + " request update=" + update.toString() ); + + LOGGER.info("Update deviceId=" + update.getDeviceId()); + + controller.updateDeviceData(update.getDeviceId(), update.getOptions()); + + + return "redirect:devices"; + } + } diff --git a/src/main/java/de/ctdo/bunti/webmodel/DeviceUpdate.java b/src/main/java/de/ctdo/bunti/webmodel/DeviceUpdate.java index 469ce0f..4437c5c 100644 --- a/src/main/java/de/ctdo/bunti/webmodel/DeviceUpdate.java +++ b/src/main/java/de/ctdo/bunti/webmodel/DeviceUpdate.java @@ -4,7 +4,7 @@ import java.io.Serializable; import java.util.Map; -public class DeviceUpdate implements Serializable { +public class DeviceUpdate { private int deviceId; private Map options; diff --git a/src/main/java/de/ctdo/bunti/webmodel/DeviceUpdates.java b/src/main/java/de/ctdo/bunti/webmodel/DeviceUpdates.java new file mode 100644 index 0000000..f8abcd0 --- /dev/null +++ b/src/main/java/de/ctdo/bunti/webmodel/DeviceUpdates.java @@ -0,0 +1,10 @@ +package de.ctdo.bunti.webmodel; + +import java.util.Collection; + +public class DeviceUpdates { + + + + +} diff --git a/src/main/resources/log4j.xml b/src/main/resources/log4j.xml index a9be4bd..f299a85 100644 --- a/src/main/resources/log4j.xml +++ b/src/main/resources/log4j.xml @@ -11,34 +11,18 @@ - - - + + - - - - - - - - - - - - - - - - + diff --git a/src/main/webapp/WEB-INF/jsp/index.jsp b/src/main/webapp/WEB-INF/jsp/index.jsp index c285108..fcf046e 100644 --- a/src/main/webapp/WEB-INF/jsp/index.jsp +++ b/src/main/webapp/WEB-INF/jsp/index.jsp @@ -19,57 +19,19 @@ $(document).ready( function() { - var Socket = "MozWebSocket" in window ? MozWebSocket : WebSocket; - ws = new Socket("ws://" + window.location.hostname + ":" + window.location.port + "/buntisocket"); - ws.onmessage = function (message) { - //$("#messages").append("

" + message.data + "

"); - - var obj = jQuery.parseJSON(message.data); - - if( obj.dmx512values != null) { - dmxData = obj.dmx512values; - - // das direkt zu machen ist evtl etwas unklug, da das sliden des sliders im - // gleichen browser dann hier zu ner aenderung fuehrt und der slider dann - // ruckelt. Aber es tut :D - $("#slider1").slider("value", dmxData[1]); - $("#slider2").slider("value", dmxData[2]); - $("#slider3").slider("value", dmxData[3]); - } - - - - }; - - ws.onopen = function () { - $("#messages").append("

WS opened

"); - }; - - ws.onclose = function () { - $("#messages").append("

WS closed

"); - }; $("#slider1").slider({ min: 0, max: 255, slide: function(event, ui) { - ws.send("channel:2=" + ui.value); - ws.send("channel:7=" + ui.value); - ws.send("channel:12=" + ui.value); - ws.send("channel:17=" + ui.value); + } }); $("#slider2").slider({ min: 0, max: 255, slide: function(event, ui) { - ws.send("channel:3=" + ui.value); - ws.send("channel:8=" + ui.value); - ws.send("channel:13=" + ui.value); - ws.send("channel:18=" + ui.value); + } }); $("#slider3").slider({ min: 0, max: 255, slide: function(event, ui) { - ws.send("channel:4=" + ui.value); - ws.send("channel:9=" + ui.value); - ws.send("channel:14=" + ui.value); - ws.send("channel:19=" + ui.value); + } }); $("#buttonLampe1").click(function() { @@ -86,14 +48,11 @@ -

Hello World!

- - -