Compare commits

...

14 Commits

Author SHA1 Message Date
Lucas Pleß c0cd519a4f working on cache and controllers 2012-05-26 01:56:44 +02:00
Lucas Pleß 892a7e6905 working on ethersex stuff 2012-05-25 18:02:59 +02:00
Lucas Ple 8c363fbf64 Anderungen am Controller. Die Kanalwerte werden nicht in der DB gespeichert, deswegen bekommt man immer 0 zurueck wenn man sich die Geräte holt.
Leider nicht funktional soweit
2012-05-25 15:52:42 +02:00
Lucas Pleß f98ab7c225 blau 2012-05-11 00:44:30 +02:00
Lucas Pleß 6b41c9cb92 Merge remote-tracking branch 'remotes/origin/maint/henne' into maint/lp 2012-05-11 00:43:07 +02:00
Lucas Pleß 0c356f3bdb corrected buntiethersexdevice. 2012-03-30 17:14:29 +02:00
Lucas Pleß 2c7694036d clean up 2012-03-30 17:00:30 +02:00
Lucas Pleß a5329a0bcc moved model classes, extended Ethersex Device section 2012-03-26 23:21:44 +02:00
Lucas Pleß ed69f6a0e3 removed Devices classes 2012-03-26 23:02:58 +02:00
Lucas Pleß 4f288256e8 moved model classes to their packages 2012-03-26 23:02:42 +02:00
Lucas Pleß 41bfcd33c3 first approach to connect to ethersex devices like lampel 2012-03-26 01:26:52 +02:00
Lucas Pleß 53f6e29eb3 added devices to rooms 2012-03-25 19:56:09 +02:00
Lucas Pleß b224e1b0ba changed dao dependencies from hibernate to JPA api. 2012-03-25 19:47:16 +02:00
Lucas Pleß 8f2610314e moved devicechange event and the webmodel to web.model 2012-03-25 18:30:51 +02:00
43 changed files with 613 additions and 277 deletions

View File

@ -1,6 +1,7 @@
package de.ctdo.bunti.control;
import de.ctdo.bunti.model.BuntiDevice;
import de.ctdo.bunti.model.DeviceUpdate;
import de.ctdo.bunti.model.Room;
import java.util.Collection;
@ -8,12 +9,7 @@ import java.util.Map;
public interface BuntiController {
Collection<BuntiDevice> getAllDevices();
BuntiDevice getDeviceById(int deviceId);
boolean updateDeviceData(int deviceId, Map<String, Object> options);
Collection<Room> getAllRooms();
Room getRoomById(int roomId);
DeviceUpdate getDeviceValues(int deviceId);
}

View File

@ -1,9 +1,8 @@
package de.ctdo.bunti.control;
import java.util.Collection;
import java.util.Map;
import de.ctdo.bunti.dao.RoomsDAO;
import de.ctdo.bunti.model.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@ -11,34 +10,24 @@ import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Component;
import de.ctdo.bunti.DeviceChangedEvent;
import de.ctdo.bunti.dao.BuntiDevicesDAO;
import de.ctdo.bunti.model.*;
@Component
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;
@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<String, Object> options) {
BuntiDevice device = devicesDAO.getDeviceById(deviceId);
@ -46,6 +35,10 @@ public class BuntiControllerImpl implements BuntiController, ApplicationEventPu
if (device != null) {
LOGGER.debug("publishEvent in BuntiController");
deviceCache.updateData(deviceId, options);
device.setValuesFromOptions(options) ;
this.applicationEventPublisher.publishEvent(new DeviceChangedEvent(this, device, options));
return true;
@ -55,23 +48,8 @@ public class BuntiControllerImpl implements BuntiController, ApplicationEventPu
}
@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);
public DeviceUpdate getDeviceValues(int deviceId) {
return deviceCache.getData(deviceId);
}
}

View File

@ -1,4 +1,4 @@
package de.ctdo.bunti;
package de.ctdo.bunti.control;
import java.util.Map;

View File

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

View File

@ -1,9 +1,9 @@
package de.ctdo.bunti.dao;
import java.util.Collection;
import java.util.List;
import de.ctdo.bunti.model.*;
import de.ctdo.bunti.model.BuntiDMXDevice;
import de.ctdo.bunti.model.BuntiDevice;
public interface BuntiDevicesDAO {
@ -14,4 +14,6 @@ public interface BuntiDevicesDAO {
void addDevice(BuntiDevice device);
void removeDevice(int deviceId);
}

View File

@ -2,35 +2,48 @@ package de.ctdo.bunti.dao;
import java.util.List;
import de.ctdo.bunti.model.*;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import de.ctdo.bunti.model.BuntiDMXDevice;
import de.ctdo.bunti.model.BuntiDevice;
import org.springframework.stereotype.Repository;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
public final class BuntiDevicesDAOImpl extends HibernateDaoSupport implements BuntiDevicesDAO {
@Repository
public final class BuntiDevicesDAOImpl implements BuntiDevicesDAO {
private EntityManager em;
@PersistenceContext
public void setEntityManager(EntityManager entityManager) {
this.em = entityManager;
}
@Override
public List<BuntiDMXDevice> getAllDMXDevices() {
return getHibernateTemplate().loadAll(BuntiDMXDevice.class);
//TODO: hier noch nur die DMX Geräte suchen!
return em.createQuery("SELECT d FROM BuntiDevice d").getResultList();
}
@Override
public List<BuntiDevice> getAllDevices() {
return getHibernateTemplate().loadAll(BuntiDevice.class);
return em.createQuery("SELECT d FROM BuntiDevice d").getResultList();
}
@Override
public BuntiDevice getDeviceById(int deviceId) {
return getHibernateTemplate().get(BuntiDevice.class,deviceId);
return em.find(BuntiDevice.class, deviceId);
}
@Override
public void addDevice(BuntiDevice device) {
getHibernateTemplate().save(device);
em.persist(device);
}
@Override
public void removeDevice(int deviceId) {
getHibernateTemplate().delete(getDeviceById(deviceId));
em.remove(getDeviceById(deviceId));
}
}

View File

@ -7,6 +7,6 @@ import java.util.List;
public interface RoomsDAO {
List<Room> getRooms();
Room getRoom(int id);
Room addRoom(Room room);
void addRoom(Room room);
void removeRoom(int id);
}

View File

@ -1,29 +1,38 @@
package de.ctdo.bunti.dao;
import de.ctdo.bunti.model.Room;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;
public final class RoomsDAOImpl extends HibernateDaoSupport implements RoomsDAO {
@Repository
public final class RoomsDAOImpl implements RoomsDAO {
private EntityManager em;
@PersistenceContext
public void setEntityManager(EntityManager entityManager) {
this.em = entityManager;
}
@Override
public List<Room> getRooms() {
return getHibernateTemplate().loadAll(Room.class);
return em.createQuery("SELECT r FROM Room r").getResultList();
}
@Override
public Room getRoom(int id) {
return getHibernateTemplate().get(Room.class, id);
return em.find(Room.class, id);
}
@Override
public Room addRoom(Room room) {
getHibernateTemplate().save(room);
return room;
public void addRoom(Room room) {
em.persist(room);
}
@Override
public void removeRoom(int id) {
getHibernateTemplate().delete(getRoom(id));
em.remove(getRoom(id));
}
}

View File

@ -1,9 +0,0 @@
package de.ctdo.bunti.devices;
public interface DeviceMixer {
}

View File

@ -1,25 +0,0 @@
package de.ctdo.bunti.devices;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
import de.ctdo.bunti.DeviceChangedEvent;
import de.ctdo.bunti.model.BuntiSwitchingDevice;
@Component
public class DeviceMixerImpl implements DeviceMixer, ApplicationListener<DeviceChangedEvent> {
@Override
public final void onApplicationEvent(DeviceChangedEvent arg0) {
if( arg0.getDevice() instanceof BuntiSwitchingDevice) {
BuntiSwitchingDevice switchDev = (BuntiSwitchingDevice)arg0.getDevice();
switchDev.setValuesFromOptions(arg0.getOptions());
}
}
}

View File

@ -1,6 +1,6 @@
package de.ctdo.bunti.dmx;
import de.ctdo.bunti.dmx.model.DMXChannel;
import de.ctdo.bunti.model.DMXChannel;
import java.util.Collection;
import java.util.Collections;

View File

@ -1,6 +1,6 @@
package de.ctdo.bunti.dmx;
import de.ctdo.bunti.DeviceChangedEvent;
import de.ctdo.bunti.control.DeviceChangedEvent;
import de.ctdo.bunti.artnet.SimpleArtNetSender;
import de.ctdo.bunti.model.BuntiDMXDevice;
import de.ctdo.bunti.model.BuntiDevice;
@ -40,6 +40,7 @@ public class DMXMixerImpl implements DMXMixer, ApplicationListener<DeviceChanged
@Scheduled(fixedDelay = NET_SEND_INTERVAL)
public final void sendOutDMXBuffer() {
/*
if (dmxMap.size() == 0) {
initDMXData();
}
@ -48,7 +49,7 @@ public class DMXMixerImpl implements DMXMixer, ApplicationListener<DeviceChanged
LOGGER.debug("sending DMX Data");
artNetSender.sendDMXData(dmxMap, artNetDeviceAddress);
hasDataChanged = false;
}
}*/
}
@Override
@ -59,16 +60,16 @@ public class DMXMixerImpl implements DMXMixer, ApplicationListener<DeviceChanged
BuntiDMXDevice dmxDev = (BuntiDMXDevice) device;
if (dmxDev.setValuesFromOptions(options)) {
//if (dmxDev.setValuesFromOptions(options)) {
dmxMap.putAll(dmxDev.getChannelData());
LOGGER.info("setValuesFromOptions on " + device);
return true;
}
//}
LOGGER.info("setValuesFromOptions on " + device + " failed");
return false;
// LOGGER.info("setValuesFromOptions on " + device + " failed");
// return false;
}
@Override

View File

@ -0,0 +1,30 @@
package de.ctdo.bunti.ethersex;
import java.util.ArrayList;
import java.util.List;
/**
* @author: lucas
* @date: 26.03.12 00:52
*/
public class ECMDCommand {
private String command;
private List<String> parameters = new ArrayList<String>();
public String getCommand() {
return command;
}
public void setCommand(String command) {
this.command = command;
}
public List<String> getParameters() {
return parameters;
}
public void setParameters(List<String> parameters) {
this.parameters = parameters;
}
}

View File

@ -0,0 +1,19 @@
package de.ctdo.bunti.ethersex;
/**
* @author: lucas
* @date: 26.03.12 00:52
*/
public class ECMDResult {
private String resultString;
public String getResultString() {
return resultString;
}
public void setResultString(String resultString) {
this.resultString = resultString;
}
}

View File

@ -0,0 +1,12 @@
package de.ctdo.bunti.ethersex;
import java.io.IOException;
/**
* @author: lucas
* @date: 26.03.12 00:47
*/
public interface ECMDSender {
ECMDResult sendCommand(ECMDCommand command, String dest) throws IOException;
}

View File

@ -0,0 +1,10 @@
package de.ctdo.bunti.ethersex;
import de.ctdo.bunti.model.BuntiDevice;
import java.util.Map;
public interface EthersexMixer {
boolean updateDevice(BuntiDevice device, Map<String, Object> options);
}

View File

@ -0,0 +1,57 @@
package de.ctdo.bunti.ethersex;
import de.ctdo.bunti.control.DeviceChangedEvent;
import de.ctdo.bunti.model.BuntiEthersexDevice;
import de.ctdo.bunti.model.BuntiDevice;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
import java.util.Map;
@Component
public class EthersexMixerImpl implements EthersexMixer, ApplicationListener<DeviceChangedEvent> {
private static final Logger LOGGER = LoggerFactory.getLogger(EthersexMixerImpl.class);
// private boolean hasDataChanged = true;
ECMDSender sender;
@Autowired
public void setECMDSender(ECMDSender sender) {
this.sender = sender;
}
@Override
public final boolean updateDevice(BuntiDevice device, Map<String, Object> options) {
if(device == null || options == null || options.size() == 0) {
return false;
}
BuntiEthersexDevice edev = (BuntiEthersexDevice) device;
// BuntiDMXDevice dmxDev = (BuntiDMXDevice) device;
//
// if (dmxDev.setValuesFromOptions(options)) {
//
// dmxMap.putAll(dmxDev.getChannelData());
//
// LOGGER.info("setValuesFromOptions on " + device);
// return true;
// }
LOGGER.info("setValuesFromOptions on " + device + " failed");
return false;
}
@Override
public final void onApplicationEvent(DeviceChangedEvent arg0) {
if (arg0.getDevice() instanceof BuntiEthersexDevice) {
updateDevice(arg0.getDevice(), arg0.getOptions());
}
}
}

View File

@ -0,0 +1,56 @@
package de.ctdo.bunti.ethersex;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
/**
* @author: lucas
* @date: 26.03.12 00:54
*/
@Component
public class SimpleECMDSender implements ECMDSender {
private static final Logger LOGGER = LoggerFactory.getLogger(SimpleECMDSender.class);
private static final int ECMD_TCP_PORT = 2701;
@Override
public ECMDResult sendCommand(ECMDCommand command, String dest) {
try {
Socket client = new Socket(dest, ECMD_TCP_PORT);
client.setSoTimeout(2000);
DataOutputStream outToServer = new DataOutputStream(client.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(client.getInputStream()));
StringBuilder params = new StringBuilder();
for(String param: command.getParameters()) {
params.append(" ");
params.append(param);
}
outToServer.writeBytes(command.getCommand() + params.toString() + '\n');
ECMDResult result = new ECMDResult();
result.setResultString(inFromServer.readLine());
client.close();
return result;
} catch (IOException e) {
LOGGER.error("Could not send ECMDCommand to " + dest + " on port " + ECMD_TCP_PORT);
}
return null;
}
}

View File

@ -1,7 +1,6 @@
package de.ctdo.bunti.model;
import de.ctdo.bunti.dmx.DMX;
import de.ctdo.bunti.dmx.model.DMXChannel;
import de.ctdo.bunti.dmx.DMXChannels;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.hibernate.annotations.Entity;
@ -49,7 +48,7 @@ public abstract class BuntiDMXDevice extends BuntiDevice {
* @param value The channel value to set.
* @return True on success, otherwise false
*/
protected final boolean setChannelValueByName(String name, int value) {
public final boolean setChannelValueByName(String name, int value) {
DMXChannel dx = dmxChannels.getChannelByName(name);
if (dx != null) {
dx.setValue(DMX.sanitizeDMXValue(value));
@ -66,7 +65,7 @@ public abstract class BuntiDMXDevice extends BuntiDevice {
*/
@JsonIgnore
@Transient
protected final int getChannelValueByName(String name) {
public final int getChannelValueByName(String name) {
DMXChannel dx = dmxChannels.getChannelByName(name);
if (dx != null) {
return dx.getValue();

View File

@ -11,7 +11,7 @@ import java.util.Map;
@Entity
@Table(name = "devices")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
public abstract class BuntiDevice {
public class BuntiDevice {
private int id;
private String deviceName;
private String picture;
@ -85,12 +85,12 @@ public abstract class BuntiDevice {
/**
* Switch this device off.
*/
public abstract void switchOff();
public void switchOff() {}
/**
* Switch this device on.
*/
public abstract void switchOn();
public void switchOn() {}
/**
* The the internal options corresponding to the given Key Value Map
@ -98,11 +98,15 @@ public abstract class BuntiDevice {
* @param options The options Map.
* @return True on success. False otherwise.
*/
public abstract boolean setValuesFromOptions(Map<String, Object> options);
public boolean setValuesFromOptions(Map<String, Object> options) {
return false;
}
@Transient
public abstract Map<String, Object> getOptions();
public Map<String, Object> getOptions() {
return null;
}
}

View File

@ -0,0 +1,91 @@
package de.ctdo.bunti.model;
import de.ctdo.bunti.model.BuntiDevice;
import org.hibernate.annotations.Entity;
import javax.persistence.Transient;
import java.util.HashMap;
import java.util.Map;
@Entity
public abstract class BuntiEthersexDevice extends BuntiDevice {
private String hostname;
private int port;
private final Map<String, Integer> ports = new HashMap<String, Integer>();
public BuntiEthersexDevice() {
}
public String getHostname() {
return hostname;
}
public void setHostname(String hostname) {
this.hostname = hostname;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public final boolean setPortByName(String name, int value) {
if (ports.containsKey(name)) {
ports.put(name, value);
return true;
}
return false;
}
@Transient
public final int getPortByName(String name) {
if (ports.containsKey(name)) {
return ports.get(name);
}
return 0;
}
@Override
public boolean setValuesFromOptions(Map<String, Object> options) {
for (Map.Entry<String, Object> opt : options.entrySet()) {
try {
int value = Integer.parseInt(opt.getValue().toString());
if (!setPortByName(opt.getKey(), value)) {
return false;
}
} catch (Exception e) {
return false;
}
}
return true;
}
@Override
public Map<String, Object> getOptions() {
Map<String, Object> options = new HashMap<String, Object>();
for(Map.Entry<String, Integer> p: ports.entrySet()) {
options.put(p.getKey(), p.getValue());
}
return options;
}
/**
* Add a channel to this DMX Device
* used internally by subclasses to define their structure
* @param channel DMXChannel to add (name and offset)
* @return True on success, false otherwise.
*/
public final void addPort(String channel) {
ports.put(channel, 0x00);
}
}

View File

@ -1,41 +0,0 @@
package de.ctdo.bunti.model;
import javax.persistence.Entity;
import javax.persistence.Transient;
import java.util.Map;
@Entity
public abstract class BuntiSwitchingDevice extends BuntiDevice {
private static final String OPTION_STATE = "state";
private boolean state = false;
public BuntiSwitchingDevice() {
}
@Override
public final boolean setValuesFromOptions(Map<String, Object> options) {
if(options.containsKey(OPTION_STATE)) {
try {
boolean value = Boolean.parseBoolean(options.get(OPTION_STATE).toString());
setState(value);
return true;
} catch (Exception e) {
return false;
}
}
return false;
}
@Transient
public boolean isState() {
return state;
}
public void setState(boolean state) {
this.state = state;
}
}

View File

@ -1,4 +1,4 @@
package de.ctdo.bunti.dmx.model;
package de.ctdo.bunti.model;
public class DMXChannel {
private int offset;

View File

@ -1,6 +1,5 @@
package de.ctdo.bunti.webmodel;
package de.ctdo.bunti.model;
import java.io.Serializable;
import java.util.Map;

View File

@ -0,0 +1,54 @@
package de.ctdo.bunti.model;
import javax.persistence.Entity;
import javax.persistence.Transient;
@Entity
public class Lampel extends BuntiEthersexDevice {
private static final String PORTC = "2"; // TODO: rausfinden welche PortNummer das ist
public static final int LAMPEL_OFF = 0x00;
public static final int LAMPEL_RED = 0x80; // TODO: rausfinden ob die Reihenfolge stimmt
public static final int LAMPEL_YELLOW = 0x40;
public static final int LAMPEL_GREEN = 0x20;
public Lampel() {
addPort(PORTC);
}
@Transient
public int getLampelState() {
return getPortByName(PORTC);
}
public void setLampelState(int value) {
setPortByName(PORTC, value);
}
@Override
public final void switchOff() {
setPortByName(PORTC, LAMPEL_OFF);
}
@Override
public final void switchOn() {
setPortByName(PORTC, LAMPEL_GREEN | LAMPEL_RED | LAMPEL_YELLOW);
}
@Override
public final String toString() {
StringBuilder sb = new StringBuilder();
sb.append("Lampel ");
sb.append(getId());
sb.append(", ");
sb.append(getDeviceName());
sb.append(" [");
sb.append(getPortByName(PORTC));
sb.append("]");
return sb.toString();
}
}

View File

@ -1,7 +1,6 @@
package de.ctdo.bunti.model;
import de.ctdo.bunti.dmx.DMX;
import de.ctdo.bunti.dmx.model.DMXChannel;
import org.codehaus.jackson.annotate.JsonIgnore;
import javax.persistence.Entity;

View File

@ -1,7 +1,6 @@
package de.ctdo.bunti.model;
import de.ctdo.bunti.dmx.DMX;
import de.ctdo.bunti.dmx.model.DMXChannel;
import org.codehaus.jackson.annotate.JsonIgnore;
import javax.persistence.Entity;

View File

@ -1,8 +1,8 @@
package de.ctdo.bunti.web;
import de.ctdo.bunti.control.BuntiController;
import de.ctdo.bunti.webmodel.DeviceUpdate;
import de.ctdo.bunti.webmodel.DeviceUpdates;
import de.ctdo.bunti.model.DeviceUpdate;
import de.ctdo.bunti.web.model.DeviceUpdates;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@ -13,21 +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) {
LOGGER.info("handle PUT /devices" + " request update=" + updates.toString() );
@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);
}
}

View File

@ -11,28 +11,26 @@ import java.util.Collection;
@Controller
@RequestMapping(value = "/devices")
public class DevicesController {
@Autowired
private BuntiDevicesDAO devicesDAO;
@Autowired
public void setDevicesDAO(BuntiDevicesDAO devicesDAO) {
this.devicesDAO = devicesDAO;
}
@RequestMapping(value = "", method = RequestMethod.GET)
public @ResponseBody Collection<BuntiDevice> getAll() {
@ResponseBody
public Collection<BuntiDevice> getAll() {
return devicesDAO.getAllDevices();
}
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public @ResponseBody BuntiDevice getDeviceById(@PathVariable("id") int id) {
@ResponseBody
public BuntiDevice getDeviceById(@PathVariable("id") int id) {
return devicesDAO.getDeviceById(id);
}
@RequestMapping(value = "", method = RequestMethod.POST)
public @ResponseBody BuntiDevice setDevices(@RequestBody BuntiDevice device) {
@ResponseBody
public String setDevices(@RequestBody BuntiDevice device) {
devicesDAO.addDevice(device);
return device;
return "{\"status\":\"OK\"}";
}
}

View File

@ -13,26 +13,24 @@ import java.util.List;
@Controller
@RequestMapping(value = "/rooms")
public class RoomsController {
@Autowired
private RoomsDAO roomsDAO;
@Autowired
public void setRoomsDAO(RoomsDAO roomsDAO) {
this.roomsDAO = roomsDAO;
}
@RequestMapping(value = "", method = RequestMethod.GET)
public @ResponseBody Collection<Room> getAll() {
@ResponseBody
public Collection<Room> getAll() {
return roomsDAO.getRooms();
}
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public @ResponseBody Room getRoomById(@PathVariable("id") int id) {
@ResponseBody
public Room getRoomById(@PathVariable("id") int id) {
return roomsDAO.getRoom(id);
}
@RequestMapping(value = "/{id}/devices", method = RequestMethod.GET)
public @ResponseBody
List<BuntiDevice> getDevicesFromRoom(@PathVariable("id") int id) {
@ResponseBody
public List<BuntiDevice> getDevicesFromRoom(@PathVariable("id") int id) {
Room room = roomsDAO.getRoom(id);
if(room != null) {

View File

@ -1,4 +1,6 @@
package de.ctdo.bunti.webmodel;
package de.ctdo.bunti.web.model;
import de.ctdo.bunti.model.DeviceUpdate;
import java.util.ArrayList;
import java.util.List;
@ -26,4 +28,5 @@ public class DeviceUpdates {
public void setTimeStamp(long timeStamp) {
this.timeStamp = timeStamp;
}
}

View File

@ -1,43 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"
xmlns:jdbc="http://www.springframework.org/schema/jdbc">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<jdbc:embedded-database id="dataSource" type="H2">
<jdbc:script location="classpath:init_schema.sql" />
<jdbc:script location="classpath:init_data.sql" />
</jdbc:embedded-database>
<bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="packagesToScan" value="de.ctdo.bunti.model"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<!--<prop key="hibernate.hbm2ddl.auto">create</prop>-->
</props>
</property>
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="roomsDAO" class="de.ctdo.bunti.dao.RoomsDAOImpl">
<property name="sessionFactory" ref="hibernateSessionFactory" />
</bean>
<bean id="devicesDAO" class="de.ctdo.bunti.dao.BuntiDevicesDAOImpl">
<property name="sessionFactory" ref="hibernateSessionFactory" />
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="packagesToScan" value="de.ctdo.bunti.dmx.model" />
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="generateDdl" value="false" />
<!-- <property name="database" value="DERBY" />-->
<property name="database" value="H2" />
</bean>
</property>
</bean>
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="hibernateSessionFactory" />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
</property>
</bean>
<tx:annotation-driven />
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
</beans>

View File

@ -6,14 +6,22 @@ insert into rooms (ROOM_ID, floor, roomName, xCord, yCord) values (null, '2. Eta
insert into rooms (ROOM_ID, floor, roomName, xCord, yCord) values (null, '2. Etage', 'Flur', '1', '1');
insert into devices (DTYPE, BUNTIDEVICE_ID, deviceName, picture, startAddress)
values ('Par56Spot',null,'Lampe1',null, 1);
insert into devices (DTYPE, BUNTIDEVICE_ID, deviceName, picture, startAddress)
values ('Par56Spot',null,'Lampe2',null, 6);
insert into devices (DTYPE, BUNTIDEVICE_ID, deviceName, picture, startAddress)
values ('Par56Spot',null,'Lampe3',null, 11);
insert into devices (DTYPE, BUNTIDEVICE_ID, deviceName, picture, startAddress)
values ('Par56Spot',null,'Lampe4',null, 16);
insert into devices (DTYPE, BUNTIDEVICE_ID, deviceName, picture, startAddress, hostname, port)
values ('Par56Spot',null,'Lampe1',null, 1, null, null);
insert into devices (DTYPE, BUNTIDEVICE_ID, deviceName, picture, startAddress, hostname, port)
values ('Par56Spot',null,'Lampe2',null, 6, null, null);
insert into devices (DTYPE, BUNTIDEVICE_ID, deviceName, picture, startAddress, hostname, port)
values ('Par56Spot',null,'Lampe3',null, 11, null, null);
insert into devices (DTYPE, BUNTIDEVICE_ID, deviceName, picture, startAddress, hostname, port)
values ('Par56Spot',null,'Lampe4',null, 16, null, null);
insert into devices (DTYPE, BUNTIDEVICE_ID, deviceName, picture, startAddress, hostname, port)
values ('Lampel',null,'Die Lampel',null, null, 'lampel.ctdo.de', 2701);
insert into ROOM_BUNTIDEVICE (ROOM_ID, BUNTIDEVICE_ID) VALUES (2, 1);
insert into ROOM_BUNTIDEVICE (ROOM_ID, BUNTIDEVICE_ID) VALUES (2, 2);
insert into ROOM_BUNTIDEVICE (ROOM_ID, BUNTIDEVICE_ID) VALUES (3, 3);
insert into ROOM_BUNTIDEVICE (ROOM_ID, BUNTIDEVICE_ID) VALUES (3, 4);

View File

@ -10,6 +10,8 @@ create table devices (DTYPE varchar(31) not null,
deviceName varchar(255),
picture varchar(255),
startAddress integer,
hostname varchar(255),
port integer,
primary key (BUNTIDEVICE_ID));
create table rooms (ROOM_ID integer generated by default as identity (start with 1),

View File

@ -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<String, Object> data = new HashMap<String, Object>();
data.put("red", 10);
data.put("green", 20);
data.put("blue", 30);
dut.updateDeviceData(2, data);
upt = dut.getDeviceValues(2);
return;
}
}

View File

@ -8,7 +8,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.Collection;
import java.util.List;
import static junit.framework.Assert.assertEquals;
@ -24,13 +23,13 @@ public class BuntiDevicesDAOImplTest {
@Test
public void testGetAllDMXDevices() throws Exception {
List<BuntiDMXDevice> deviceList = dut.getAllDMXDevices();
assertEquals(4, deviceList.size());
assertEquals(5, deviceList.size());
}
@Test
public void testGetAllDevices() throws Exception {
List<BuntiDevice> deviceList = dut.getAllDevices();
assertEquals(4, deviceList.size());
assertEquals(5, deviceList.size());
}
@Test

View File

@ -1,6 +1,6 @@
package de.ctdo.bunti.dmx;
import de.ctdo.bunti.dmx.model.DMXChannel;
import de.ctdo.bunti.model.DMXChannel;
import org.junit.Before;
import org.junit.Test;

View File

@ -1,6 +1,6 @@
package de.ctdo.bunti.dmx;
import de.ctdo.bunti.dmx.model.DMXChannel;
import de.ctdo.bunti.model.DMXChannel;
import org.junit.Before;
import org.junit.Test;

View File

@ -1,6 +1,5 @@
package de.ctdo.bunti.dmx;
import de.ctdo.bunti.model.BuntiDevice;
import de.ctdo.bunti.model.Par56Spot;
import org.junit.Before;
import org.junit.Test;

View File

@ -1,6 +1,8 @@
package de.ctdo.bunti.model;
import de.ctdo.bunti.dmx.model.DMXChannel;
import de.ctdo.bunti.model.BuntiDMXDevice;
import de.ctdo.bunti.model.DMXChannel;
import de.ctdo.bunti.model.Par56Spot;
import org.junit.Before;
import org.junit.Test;

View File

@ -1,5 +1,6 @@
package de.ctdo.bunti.model;
import de.ctdo.bunti.model.Par56Spot;
import org.junit.Before;
import org.junit.Test;
import static junit.framework.Assert.*;

View File

@ -1,5 +1,6 @@
package de.ctdo.bunti.model;
import de.ctdo.bunti.model.Strobe1500;
import org.junit.Before;
import org.junit.Test;

View File

@ -1,39 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<jdbc:embedded-database id="dataSource" type="H2">
<jdbc:script location="classpath:init_schema.sql" />
<jdbc:script location="classpath:init_data.sql" />
</jdbc:embedded-database>
<bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="packagesToScan" value="de.ctdo.bunti.model"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<!--<prop key="hibernate.hbm2ddl.auto">create</prop>-->
</props>
</property>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="packagesToScan" value="de.ctdo.bunti.model" />
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="generateDdl" value="false" />
<!-- <property name="database" value="DERBY" />-->
<property name="database" value="H2" />
</bean>
</property>
</bean>
<bean id="roomsDAO" class="de.ctdo.bunti.dao.RoomsDAOImpl">
<property name="sessionFactory" ref="hibernateSessionFactory" />
</bean>
<bean id="devicesDAO" class="de.ctdo.bunti.dao.BuntiDevicesDAOImpl">
<property name="sessionFactory" ref="hibernateSessionFactory" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="hibernateSessionFactory" />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
</property>
</bean>
<tx:annotation-driven />
<context:component-scan base-package="de.ctdo.bunti.dao" />
</beans>