bunti/src/main/java/de/ctdo/bunti/web/RestController.java

91 lines
3.1 KiB
Java

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;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.validation.Validator;
import java.util.*;
@Controller
@RequestMapping(value = "/control")
public class RestController {
private static final Logger LOGGER = LoggerFactory.getLogger(RestController.class);
// private Validator validator;
private BuntiController controller;
// @Autowired
// public void setValidator(Validator validator) {
// this.validator = validator;
// }
@Autowired
public final void setController(BuntiController controller) {
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() {
LOGGER.info("handle GET /devices request");
return controller.getAllDevices();
}
@RequestMapping(value = "/devices/{id}", method = RequestMethod.GET)
public @ResponseBody BuntiDevice getDeviceById(@PathVariable("id") int id) {
LOGGER.info("handle GET /devices/" + id + " request");
return controller.getDeviceById(id);
}
@RequestMapping(value = "/devices/{id}", method = RequestMethod.PUT)
public String setDeviceById(@PathVariable("id") int id, @RequestBody Map<String, Object> update) {
LOGGER.info("handle PUT /devices/" + id + " request update=" + update.toString() );
controller.updateDeviceData(id, update);
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";
}
}