moved model classes, extended Ethersex Device section

This commit is contained in:
Lucas Pleß 2012-03-26 23:21:44 +02:00
parent ed69f6a0e3
commit a5329a0bcc
22 changed files with 186 additions and 41 deletions

View File

@ -4,6 +4,7 @@ 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;
@ -12,7 +13,6 @@ import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Component;
import de.ctdo.bunti.dao.BuntiDevicesDAO;
import de.ctdo.bunti.model.*;
@Component
public class BuntiControllerImpl implements BuntiController, ApplicationEventPublisherAware {

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 {

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

@ -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,42 @@
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.context.ApplicationListener;
import java.util.Map;
public class EthersexMixerImpl implements EthersexMixer, ApplicationListener<DeviceChangedEvent> {
private static final Logger LOGGER = LoggerFactory.getLogger(EthersexMixerImpl.class);
private boolean hasDataChanged = true;
@Override
public final boolean updateDevice(BuntiDevice device, Map<String, Object> options) {
if(device == null || options == null || options.size() == 0) {
return false;
}
// 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

@ -1,9 +1,7 @@
package de.ctdo.bunti.dmx.model;
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 de.ctdo.bunti.model.BuntiDevice;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.hibernate.annotations.Entity;

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 static 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,4 +1,4 @@
package de.ctdo.bunti.dmx.model;
package de.ctdo.bunti.model;
public class DMXChannel {
private int offset;

View File

@ -1,14 +1,7 @@
package de.ctdo.bunti.ethersex.model;
import de.ctdo.bunti.dmx.DMX;
import de.ctdo.bunti.dmx.model.DMXChannel;
import de.ctdo.bunti.model.BuntiDevice;
import org.codehaus.jackson.annotate.JsonIgnore;
package de.ctdo.bunti.model;
import javax.persistence.Entity;
import javax.persistence.Transient;
import java.util.HashMap;
import java.util.Map;
@Entity
public class Lampel extends BuntiEthersexDevice {
@ -40,7 +33,7 @@ public class Lampel extends BuntiEthersexDevice {
@Override
public final void switchOn() {
setPortByName(PORTC, LAMPEL_GREEN | LAMPEL_RED | LAMPEL_YELLOW );
setPortByName(PORTC, LAMPEL_GREEN | LAMPEL_RED | LAMPEL_YELLOW);
}

View File

@ -1,4 +1,4 @@
package de.ctdo.bunti.dmx.model;
package de.ctdo.bunti.model;
import de.ctdo.bunti.dmx.DMX;
import org.codehaus.jackson.annotate.JsonIgnore;

View File

@ -1,4 +1,4 @@
package de.ctdo.bunti.dmx.model;
package de.ctdo.bunti.model;
import de.ctdo.bunti.dmx.DMX;
import org.codehaus.jackson.annotate.JsonIgnore;

View File

@ -19,7 +19,7 @@
</jdbc:embedded-database>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="packagesToScan" value="de.ctdo.bunti.model" />
<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">
@ -40,7 +40,6 @@
<tx:annotation-driven />
<context:component-scan base-package="de.ctdo.bunti.dao" />
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

View File

@ -6,14 +6,18 @@ 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);

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

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