working on rest controller. something with Collection<DeviceUpdate> is not working...

This commit is contained in:
Lucas Pleß 2012-03-12 01:17:52 +01:00
parent 68b44c76a8
commit 9c54e5d73c
6 changed files with 62 additions and 70 deletions

View File

@ -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;
}

View File

@ -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<String, Object> options = new HashMap<String, Object>();
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<BuntiDevice> getAll() {
@ -49,4 +62,29 @@ public class RestController {
return "redirect:devices/" + id;
}
@RequestMapping(value = "/devices", method = RequestMethod.PUT)
public String setDevices(@RequestBody ArrayList<DeviceUpdate> 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";
}
}

View File

@ -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<String, Object> options;

View File

@ -0,0 +1,10 @@
package de.ctdo.bunti.webmodel;
import java.util.Collection;
public class DeviceUpdates {
}

View File

@ -11,34 +11,18 @@
</layout>
</appender>
<!-- 3rdparty Loggers -->
<logger name="org.springframework.core">
<level value="info" />
<logger name="org.springframework">
<level value="debug" />
</logger>
<logger name="org.springframework.beans">
<level value="info" />
</logger>
<logger name="org.springframework.context">
<level value="info" />
</logger>
<logger name="org.springframework.web">
<level value="info" />
</logger>
<logger name="de.ctdo">
<level value="debug" />
</logger>
<logger name="org.atmosphere">
<level value="debug" />
</logger>
<!-- Root Logger -->
<root>
<priority value="info" />
<priority value="debug" />
<appender-ref ref="console" />
</root>

View File

@ -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("<p>" + message.data + "</p>");
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("<p>WS opened</p>");
};
ws.onclose = function () {
$("#messages").append("<p>WS closed</p>");
};
$("#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,13 +48,10 @@
</script>
</head>
<body>
<h1>Hello World!</h1>
<script>
<h1>Bunti Steuerung</h1>
</script>
<div class="demo">
<div id="slider1" style="width: 300px"></div>