From 1cb2d64324af2a763f1e8d004e428deb045bff12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Ple=C3=9F?= Date: Sat, 3 Mar 2012 02:29:06 +0100 Subject: [PATCH] =?UTF-8?q?Ein=20aufruf=20des=20TestServlet=20unter=20http?= =?UTF-8?q?://localhost:8080/bunti=5Fserver/foobar=20macht=20einen=20Aufru?= =?UTF-8?q?f=20an=20den=20Controller,=20und=20der=20mergend=20mit=20dem=20?= =?UTF-8?q?DMXMixer=20die=20Werte=20dann=20in=20den=20DMX=20Array.=20Als?= =?UTF-8?q?=20n=C3=A4chstes=20ist=20dann=20DeviceValuesChanged=20Notificat?= =?UTF-8?q?ion=20dran.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/de/ctdo/bunti/Mixer.java | 11 + .../ctdo/bunti/control/BuntiController.java | 53 +---- .../bunti/control/BuntiControllerImpl.java | 94 ++++++++ .../de/ctdo/bunti/dao/BuntiDevicesDAO.java | 5 +- .../ctdo/bunti/dao/BuntiDevicesDAOImpl.java | 74 +++---- .../de/ctdo/bunti/devices/DeviceMixer.java | 10 + .../ctdo/bunti/devices/DeviceMixerImpl.java | 24 +++ .../src/main/java/de/ctdo/bunti/dmx/DMX.java | 2 + .../java/de/ctdo/bunti/dmx/DMXChannel.java | 50 ++++- .../java/de/ctdo/bunti/dmx/DMXChannels.java | 117 ++++++++++ .../java/de/ctdo/bunti/dmx/DMXMixerImpl.java | 99 +++++++++ .../de/ctdo/bunti/model/BuntiDMXDevice.java | 203 ++++++++++++------ .../java/de/ctdo/bunti/model/BuntiDevice.java | 19 +- .../bunti/model/BuntiSwitchingDevice.java | 27 +++ .../java/de/ctdo/bunti/model/Par56Spot.java | 69 ++++++ .../java/de/ctdo/bunti/model/Strobe1500.java | 61 ++++++ .../de/ctdo/bunti/web/TestController.java | 69 ++++++ .../websocket/BuntiControllerApplication.java | 8 +- 18 files changed, 814 insertions(+), 181 deletions(-) create mode 100644 bunti_server/src/main/java/de/ctdo/bunti/Mixer.java create mode 100644 bunti_server/src/main/java/de/ctdo/bunti/control/BuntiControllerImpl.java create mode 100644 bunti_server/src/main/java/de/ctdo/bunti/devices/DeviceMixer.java create mode 100644 bunti_server/src/main/java/de/ctdo/bunti/devices/DeviceMixerImpl.java create mode 100644 bunti_server/src/main/java/de/ctdo/bunti/dmx/DMXChannels.java create mode 100644 bunti_server/src/main/java/de/ctdo/bunti/dmx/DMXMixerImpl.java create mode 100644 bunti_server/src/main/java/de/ctdo/bunti/model/BuntiSwitchingDevice.java create mode 100644 bunti_server/src/main/java/de/ctdo/bunti/model/Par56Spot.java create mode 100644 bunti_server/src/main/java/de/ctdo/bunti/model/Strobe1500.java diff --git a/bunti_server/src/main/java/de/ctdo/bunti/Mixer.java b/bunti_server/src/main/java/de/ctdo/bunti/Mixer.java new file mode 100644 index 0000000..ee16656 --- /dev/null +++ b/bunti_server/src/main/java/de/ctdo/bunti/Mixer.java @@ -0,0 +1,11 @@ +package de.ctdo.bunti; + +import java.util.Map; + +import de.ctdo.bunti.model.BuntiDevice; + +public interface Mixer { + + public boolean setDevice(BuntiDevice device, Map options); + +} diff --git a/bunti_server/src/main/java/de/ctdo/bunti/control/BuntiController.java b/bunti_server/src/main/java/de/ctdo/bunti/control/BuntiController.java index 0fab845..cbaa09d 100644 --- a/bunti_server/src/main/java/de/ctdo/bunti/control/BuntiController.java +++ b/bunti_server/src/main/java/de/ctdo/bunti/control/BuntiController.java @@ -1,56 +1,9 @@ package de.ctdo.bunti.control; -import java.util.ArrayList; -import java.util.List; +import java.util.Map; -import net.sf.json.JSONObject; -import de.ctdo.bunti.dmx.DMXMixer; - -public class BuntiController { - static BuntiController instance = new BuntiController(); - DMXMixer mixer = null; - protected final List listeners = new ArrayList(); - - public static BuntiController getInstance() { - return instance; - } - - private BuntiController() { - mixer = new DMXMixer(); - - } - - public void addListener(BroadcastListener l) { - synchronized (listeners) { - listeners.add(l); - } - } - public void removeListener(BroadcastListener l) { - synchronized (listeners) { - listeners.remove(l); - } - } +public interface BuntiController { + boolean setDevice(int deviceID, Map options); - public void performJSONString(String json) { - - JSONObject jsonobj = JSONObject.fromObject(json); - - if (jsonobj.containsKey("command")) { - - String command = jsonobj.get("command").toString(); - if (command.equals("setdmxchannels")) { - - } else if (command.equals("switchdevice")) { - - } - } - - } - - public void sendBroadcastMessage(String message) { - for (BroadcastListener l : listeners) { - l.Broadcast(message); - } - } } diff --git a/bunti_server/src/main/java/de/ctdo/bunti/control/BuntiControllerImpl.java b/bunti_server/src/main/java/de/ctdo/bunti/control/BuntiControllerImpl.java new file mode 100644 index 0000000..bf4dfdd --- /dev/null +++ b/bunti_server/src/main/java/de/ctdo/bunti/control/BuntiControllerImpl.java @@ -0,0 +1,94 @@ +package de.ctdo.bunti.control; + +import java.util.Map; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import net.sf.json.JSONObject; +import de.ctdo.bunti.dao.BuntiDevicesDAO; +import de.ctdo.bunti.devices.*; +import de.ctdo.bunti.dmx.*; +import de.ctdo.bunti.model.*; + +@Component +public class BuntiControllerImpl implements BuntiController { + Logger logger = LoggerFactory.getLogger(getClass()); + BuntiDevicesDAO devicesDAO; + DMXMixer dmxMixer; + DeviceMixer deviceMixer; + + // protected final List listeners = new ArrayList(); + + @Autowired + public void setDmxMixer(DMXMixer dmxMixer) { + this.dmxMixer = dmxMixer; + } + + @Autowired + public void setDeviceMixer(DeviceMixer deviceMixer) { + this.deviceMixer = deviceMixer; + } + + @Autowired + public void setDevicesDAO(BuntiDevicesDAO devicesDAO) { + this.devicesDAO = devicesDAO; + } + + // public void addListener(BroadcastListener l) { + // synchronized (listeners) { + // listeners.add(l); + // } + // } + // public void removeListener(BroadcastListener l) { + // synchronized (listeners) { + // listeners.remove(l); + // } + // } + + public void performJSONString(String json) { + + JSONObject jsonobj = JSONObject.fromObject(json); + + if (jsonobj.containsKey("command")) { + + String command = jsonobj.get("command").toString(); + if (command.equals("setdmxchannels")) { + + } else if (command.equals("switchdevice")) { + + } + } + + } + + @Override + public boolean setDevice(int deviceId, Map options) { + + BuntiDevice device = devicesDAO.getDeviceById(deviceId); + + if (device != null) { + + //TODO hier dann DeviceChangedEvent feuern + + if (device instanceof BuntiDMXDevice) { + dmxMixer.setDevice(device, options); + } else if (device instanceof BuntiSwitchingDevice) { + deviceMixer.setDevice(device, options); + } + return true; + } + + + + return false; + } + + // public void sendBroadcastMessage(String message) { + // for (BroadcastListener l : listeners) { + // l.Broadcast(message); + // } + // } +} diff --git a/bunti_server/src/main/java/de/ctdo/bunti/dao/BuntiDevicesDAO.java b/bunti_server/src/main/java/de/ctdo/bunti/dao/BuntiDevicesDAO.java index c8d9388..6d90cfa 100644 --- a/bunti_server/src/main/java/de/ctdo/bunti/dao/BuntiDevicesDAO.java +++ b/bunti_server/src/main/java/de/ctdo/bunti/dao/BuntiDevicesDAO.java @@ -1,11 +1,12 @@ package de.ctdo.bunti.dao; -import java.util.List; +import java.util.Collection; import de.ctdo.bunti.model.*; public interface BuntiDevicesDAO { - public List getAllDMXDevices(); + public Collection getAllDMXDevices(); + public BuntiDevice getDeviceById(int deviceId); } diff --git a/bunti_server/src/main/java/de/ctdo/bunti/dao/BuntiDevicesDAOImpl.java b/bunti_server/src/main/java/de/ctdo/bunti/dao/BuntiDevicesDAOImpl.java index d737110..c4cb960 100644 --- a/bunti_server/src/main/java/de/ctdo/bunti/dao/BuntiDevicesDAOImpl.java +++ b/bunti_server/src/main/java/de/ctdo/bunti/dao/BuntiDevicesDAOImpl.java @@ -1,14 +1,18 @@ package de.ctdo.bunti.dao; import java.util.ArrayList; +import java.util.Collection; import java.util.List; -import de.ctdo.bunti.dmx.DMXChannel; -import de.ctdo.bunti.model.BuntiDMXDevice; -import de.ctdo.bunti.model.BuntiDevice; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; +import de.ctdo.bunti.model.*; + +@Component public class BuntiDevicesDAOImpl implements BuntiDevicesDAO { - + Logger logger = LoggerFactory.getLogger(getClass()); List devices = new ArrayList(); public BuntiDevicesDAOImpl() { @@ -17,62 +21,36 @@ public class BuntiDevicesDAOImpl implements BuntiDevicesDAO { private void addDummyDevices() { - BuntiDMXDevice d1 = new BuntiDMXDevice(); - d1.setDeviceID(1); - d1.setDeviceName("Par65 Lampe 1"); - d1.setStartAddress(1); - d1.addChannel(new DMXChannel(0, "mode")); - d1.addChannel(new DMXChannel(1, "red")); - d1.addChannel(new DMXChannel(2, "green")); - d1.addChannel(new DMXChannel(3, "blue")); - d1.addChannel(new DMXChannel(4, "speed")); - devices.add(d1); + int deviceID = 0; - d1 = new BuntiDMXDevice(); - d1.setDeviceID(6); - d1.setDeviceName("Par65 Lampe 2"); - d1.setStartAddress(1); - d1.addChannel(new DMXChannel(0, "mode")); - d1.addChannel(new DMXChannel(1, "red")); - d1.addChannel(new DMXChannel(2, "green")); - d1.addChannel(new DMXChannel(3, "blue")); - d1.addChannel(new DMXChannel(4, "speed")); - devices.add(d1); - - d1 = new BuntiDMXDevice(); - d1.setDeviceID(11); - d1.setDeviceName("Par65 Lampe 4"); - d1.setStartAddress(1); - d1.addChannel(new DMXChannel(0, "mode")); - d1.addChannel(new DMXChannel(1, "red")); - d1.addChannel(new DMXChannel(2, "green")); - d1.addChannel(new DMXChannel(3, "blue")); - d1.addChannel(new DMXChannel(4, "speed")); - devices.add(d1); - - d1 = new BuntiDMXDevice(); - d1.setDeviceID(16); - d1.setDeviceName("Par65 Lampe 4"); - d1.setStartAddress(1); - d1.addChannel(new DMXChannel(0, "mode")); - d1.addChannel(new DMXChannel(1, "red")); - d1.addChannel(new DMXChannel(2, "green")); - d1.addChannel(new DMXChannel(3, "blue")); - d1.addChannel(new DMXChannel(4, "speed")); - devices.add(d1); + devices.add(new Par56Spot(deviceID++, 1, "Par56 Lampe 1")); + devices.add(new Par56Spot(deviceID++, 6, "Par56 Lampe 2")); + devices.add(new Par56Spot(deviceID++, 11, "Par56 Lampe 3")); + devices.add(new Par56Spot(deviceID++, 16, "Par56 Lampe 4")); + devices.add(new Strobe1500(deviceID++, 21, "Stroboskop 1")); } @Override - public List getAllDMXDevices() { + public Collection getAllDMXDevices() { List liste = new ArrayList(); for (BuntiDevice device : devices) { - if( device.getClass().equals(BuntiDMXDevice.class) ) { + if( device instanceof BuntiDMXDevice ) { liste.add((BuntiDMXDevice) device); } } return liste; } + + @Override + public BuntiDevice getDeviceById(int deviceId) { + for (BuntiDevice dev : devices) { + if(dev.getDeviceID() == deviceId) { + return dev; + } + } + return null; + } } diff --git a/bunti_server/src/main/java/de/ctdo/bunti/devices/DeviceMixer.java b/bunti_server/src/main/java/de/ctdo/bunti/devices/DeviceMixer.java new file mode 100644 index 0000000..cdbec9d --- /dev/null +++ b/bunti_server/src/main/java/de/ctdo/bunti/devices/DeviceMixer.java @@ -0,0 +1,10 @@ +package de.ctdo.bunti.devices; + + +import de.ctdo.bunti.Mixer; + +public interface DeviceMixer extends Mixer { + + + +} diff --git a/bunti_server/src/main/java/de/ctdo/bunti/devices/DeviceMixerImpl.java b/bunti_server/src/main/java/de/ctdo/bunti/devices/DeviceMixerImpl.java new file mode 100644 index 0000000..abf3680 --- /dev/null +++ b/bunti_server/src/main/java/de/ctdo/bunti/devices/DeviceMixerImpl.java @@ -0,0 +1,24 @@ +package de.ctdo.bunti.devices; + +import java.util.Map; + +import org.springframework.stereotype.Component; + +import de.ctdo.bunti.model.BuntiDMXDevice; +import de.ctdo.bunti.model.BuntiDevice; + + +@Component +public class DeviceMixerImpl implements DeviceMixer { + + + @Override + public boolean setDevice(BuntiDevice device, Map options) { + + BuntiDMXDevice dmxDev = (BuntiDMXDevice)device; + return dmxDev.setValuesFromOptions(options); + } + + + +} diff --git a/bunti_server/src/main/java/de/ctdo/bunti/dmx/DMX.java b/bunti_server/src/main/java/de/ctdo/bunti/dmx/DMX.java index 5986b0c..7f23849 100644 --- a/bunti_server/src/main/java/de/ctdo/bunti/dmx/DMX.java +++ b/bunti_server/src/main/java/de/ctdo/bunti/dmx/DMX.java @@ -18,4 +18,6 @@ public class DMX { return (channel >= DMX_CHANNELS_MIN && channel <= DMX_CHANNELS_MAX); } + + } diff --git a/bunti_server/src/main/java/de/ctdo/bunti/dmx/DMXChannel.java b/bunti_server/src/main/java/de/ctdo/bunti/dmx/DMXChannel.java index 15b0623..ee629ba 100644 --- a/bunti_server/src/main/java/de/ctdo/bunti/dmx/DMXChannel.java +++ b/bunti_server/src/main/java/de/ctdo/bunti/dmx/DMXChannel.java @@ -1,13 +1,53 @@ package de.ctdo.bunti.dmx; public class DMXChannel { - public int offset; - public String description; - public int value; + int offset; + String name; + int value; + long lastChangedTimestamp = 0; - public DMXChannel(int offset, String description) { - this.description = description; + public DMXChannel(int offset, String name) { + this.name = name; this.offset = offset; this.value = 0; } + + public int getValue() { + return value; + } + + public void setValue(int value) { + this.value = value; + lastChangedTimestamp = System.currentTimeMillis(); + } + + public long getLastChangedTimestamp() { + return lastChangedTimestamp; + } + + public int getOffset() { + return offset; + } + + public void setOffset(int offset) { + this.offset = offset; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public void hasbeenSendOut() { + this.lastChangedTimestamp = System.currentTimeMillis(); + } + + @Override + public String toString() { + return "DMXChannel " + getName() + "," + getOffset() + "," + getValue(); + } + } diff --git a/bunti_server/src/main/java/de/ctdo/bunti/dmx/DMXChannels.java b/bunti_server/src/main/java/de/ctdo/bunti/dmx/DMXChannels.java new file mode 100644 index 0000000..cc71f45 --- /dev/null +++ b/bunti_server/src/main/java/de/ctdo/bunti/dmx/DMXChannels.java @@ -0,0 +1,117 @@ +package de.ctdo.bunti.dmx; + +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +import org.springframework.stereotype.Component; + +/** +* DMXChannel container +*/ +public class DMXChannels { + + protected Map channelByNumber = new HashMap(); + protected Map channelByName = new HashMap(); + + /** + * Creates a new DMXChannel container + */ + public DMXChannels() { + } + + /** + * Returns the number of channels + * @return number of channels + */ + public int getCount() { + return this.channelByNumber.size(); + } + + /** + * Returns a channel by name + * @param name channel name + * @return channel or null if not found + */ + public DMXChannel getChannelByName(String name) { + if (name == null) { + return null; + } + return this.channelByName.get(name); + } + + /** + * Returns a channel by offset + * @param number number + * @return channel or null if not found + */ + public DMXChannel getChannelByNumber(int number) { + return this.channelByNumber.get(number); + } + + /** + * Adds a channel + * @param channel channel to add + * @return true on success, false on error + */ + public boolean addChannel(DMXChannel channel) { + // object cannot be null + if (channel == null) { + return false; + } + // description cannot be null + if (channel.name == null) { + return false; + } + // entry must not exist by offset + if (this.channelByNumber.containsKey(channel.offset) == true) { + return false; + } + // entry must not exist by name + if (this.channelByName.containsKey(channel.name) == true) { + return false; + } + this.channelByNumber.put(channel.offset, channel); + this.channelByName.put(channel.name, channel); + return true; + } + +// /** +// * Removes a channel by offset +// * @param offset offset +// * @return removed channel or null if it does not exist +// */ +// public DMXChannel removeChannel(int offset) { +// DMXChannel tmpChannel = this.channelByNumber.remove(offset); +// if (tmpChannel != null) { +// this.channelByName.remove(tmpChannel.name); +// } +// return tmpChannel; +// } + +// /** +// * Removes a channel by name +// * @param name channel name +// * @return removed channel or null if it does not exist +// */ +// public DMXChannel removeChannel(String name) { +// if (name == null) { +// return null; +// } +// DMXChannel tmpChannel = this.channelByName.remove(name); +// if (tmpChannel != null) { +// this.channelByOffset.remove(tmpChannel.offset); +// } +// return tmpChannel; +// } + + /** + * Returns an (unmodifiable) collection of all channels + * @return unmodifiable collection of all channels + */ + public Collection getAllChannels() { + return Collections.unmodifiableCollection(this.channelByNumber.values()); + } + +} diff --git a/bunti_server/src/main/java/de/ctdo/bunti/dmx/DMXMixerImpl.java b/bunti_server/src/main/java/de/ctdo/bunti/dmx/DMXMixerImpl.java new file mode 100644 index 0000000..ba15ab0 --- /dev/null +++ b/bunti_server/src/main/java/de/ctdo/bunti/dmx/DMXMixerImpl.java @@ -0,0 +1,99 @@ +package de.ctdo.bunti.dmx; + +import java.util.Map; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import de.ctdo.bunti.dao.BuntiDevicesDAO; +import de.ctdo.bunti.model.*; + +@Component +public class DMXMixerImpl implements DMXMixer { + Logger logger = LoggerFactory.getLogger(getClass()); + final int TICKS_BETWEEN_DMX_SEND = 20; + final String ARTNET_DEVICE_ADDRESS = "192.168.0.90"; + + int[] dmx512databuffer = new int[DMX.DMX_CHANNELS_MAX+1]; + + static int sequenceID = 0; + long ticksLastBufferFlush = 0; + BuntiDevicesDAO devicesDAO; + + @Autowired + public void setDevicesDAO(BuntiDevicesDAO devicesDAO) { + this.devicesDAO = devicesDAO; + } + + private void sendOutDMXBuffer() { + + StringBuilder sb = new StringBuilder(); + sb.append("DMX Data: "); + + byte[] arr = new byte[dmx512databuffer.length]; + for (int i = 0; i < dmx512databuffer.length; i++) { + arr[i] = (byte)dmx512databuffer[i]; + sb.append(i); + sb.append("="); + sb.append(dmx512databuffer[i]); + sb.append(", "); + } + + logger.debug(sb.toString()); + + + } + + public void sendOutDMXBufferIfNeeded() { + if(ticksLastBufferFlush + TICKS_BETWEEN_DMX_SEND < System.currentTimeMillis()) { + + mergeDevicesIntoBuffer(); + sendOutDMXBuffer(); + ticksLastBufferFlush = System.currentTimeMillis(); + } + } + + private void mergeDevicesIntoBuffer() { + + for (BuntiDMXDevice buntiDMXDevice : devicesDAO.getAllDMXDevices()) { + long lastchanged = buntiDMXDevice.getLastChanged(); + long lastSend = buntiDMXDevice.getLastSendOut(); + + if(lastchanged >= lastSend ) { + buntiDMXDevice.mergeDMXData(dmx512databuffer); + logger.debug("merged " + buntiDMXDevice + " into dmx buffer"); + buntiDMXDevice.setSendOutNow(); + } + } + + } + +// public void setDMX512Channel(int channel, int value) { +// if(!DMX.checkChannelBoundaries(channel)) return; +// value = DMX.sanitizeDMXValue(value); +// +// dmx512databuffer[channel] = value; +// +// sendOutDMXBufferIfNeeded(); +// } + + @Override + public boolean setDevice(BuntiDevice device, Map options) { + boolean retval = false; + + if( device instanceof BuntiDMXDevice) { + + BuntiDMXDevice dmxDev = (BuntiDMXDevice)device; + retval = dmxDev.setValuesFromOptions(options); + + if( retval ) { + sendOutDMXBufferIfNeeded(); + } + } + + return retval; + } + +} diff --git a/bunti_server/src/main/java/de/ctdo/bunti/model/BuntiDMXDevice.java b/bunti_server/src/main/java/de/ctdo/bunti/model/BuntiDMXDevice.java index 8375ceb..3301390 100644 --- a/bunti_server/src/main/java/de/ctdo/bunti/model/BuntiDMXDevice.java +++ b/bunti_server/src/main/java/de/ctdo/bunti/model/BuntiDMXDevice.java @@ -1,99 +1,162 @@ package de.ctdo.bunti.model; import java.util.*; +import java.util.Map.Entry; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import de.ctdo.bunti.dmx.*; -public class BuntiDMXDevice extends BuntiDevice { +public abstract class BuntiDMXDevice extends BuntiDevice { + Logger logger = LoggerFactory.getLogger(getClass()); int startAddress; - List dmxValues = new ArrayList(); - - public DMXChannel getDMXChannelByName(String channel) { - for (DMXChannel dx : dmxValues) { - if(channel.equals(dx.description)) { - return dx; - } - } - return null; - } - - public DMXChannel getDMXChannelByOffset(int offset) { - for (DMXChannel dx : dmxValues) { - if(offset == dx.offset) { - return dx; - } - } - return null; + long lastSendOut; + DMXChannels dmxChannels = new DMXChannels(); + + public int getStartAddress() { + return startAddress+1; } -// public DMXChannel getDMXChannelByAddress(int address) { -// int offset = address - startAddress; -// return getDMXChannelByOffset(offset); -// } + public long getLastSendOut() { + return lastSendOut; + } + public void setSendOutNow() { + this.lastSendOut = System.currentTimeMillis(); + } + + + /** + * set the DMX Start Address (1 to 512) + * @param startAddress + */ + public void setStartAddress(int startAddress) { + this.startAddress = startAddress-1; + } + + + public void addChannel(DMXChannel channel) { + //dmxChannels.add(channel); + dmxChannels.addChannel(channel); + } + + public void setChannelValueByName(String name, int value) { - DMXChannel dx = getDMXChannelByName(name); + DMXChannel dx = dmxChannels.getChannelByName(name); if(dx != null) { - dx.value = DMX.sanitizeDMXValue(value); + dx.setValue(DMX.sanitizeDMXValue(value)); } + lastChangedNow(); } -// public void setChannelValueByOffset(int offsetChannel, int value) { -// DMXChannel dx = getDMXChannelByOffset(offsetChannel); -// if(dx != null) { -// dx.value = DMX.sanitizeDMXValue(value); -// } -// } - -// public void setChannelValueByAddress(int address, int value) { -// DMXChannel dx = getDMXChannelByAddress(address); -// if(dx != null) { -// dx.value = DMX.sanitizeDMXValue(value); -// } -// } - public int getChannelValueByName(String name) { - DMXChannel dx = getDMXChannelByName(name); + DMXChannel dx = dmxChannels.getChannelByName(name); if(dx != null) { - return dx.value; + return dx.getValue(); } return 0; } -// public int getChannelValueByOffset(int offsetChannel) { -// DMXChannel dx = getDMXChannelByOffset(offsetChannel) -// if(dx != null) { -// return dx.value; -// } -// return 0; -// } - -// public int getChannelValueByAddress(int address) { -// DMXChannel dx = getDMXChannelByAddress(address); -// if(dx != null) { -// return dx.value; -// } -// return 0; -// } - public int getStartAddress() { - return startAddress; - } - - public void setStartAddress(int startAddress) { - this.startAddress = startAddress; - } - public void addChannel(DMXChannel channel) { - dmxValues.add(channel); - } - public void removeChannel(int offset) { - DMXChannel dx = getDMXChannelByOffset(offset); - if(dx != null) { - dmxValues.remove(offset); + + + /** + * Merge the DMX values from this device into a global DMX512 Data Array + * @param dmxData + * @return + */ + public boolean mergeDMXData(int[] dmxData) { + + if(dmxData == null) { + return false; } + + for (DMXChannel channel : dmxChannels.getAllChannels()) { + int index = channel.getOffset() + startAddress; + + if(index >= DMX.DMX_CHANNEL_VALUE_MIN && index <= DMX.DMX_CHANNELS_MAX){ + dmxData[index] = channel.getValue(); + } else { + return false; + } + } + + return true; } + + @Override + public void switchOff() { + + } + + @Override + public void switchOn() { + + } + + + @Override + public boolean setValuesFromOptions(Map options) { + + for (Entry opt : options.entrySet()) { + + DMXChannel channel = dmxChannels.getChannelByName(opt.getKey()); + + if(channel != null) { + try { + int value = Integer.parseInt(opt.getValue().toString()); + + setChannelValueByName(channel.getName(), value); + + } catch (Exception e) { + return false; + } + } + } + return true; + } + + + +// public void setChannelValueByOffset(int offsetChannel, int value) { +// DMXChannel dx = getDMXChannelByOffset(offsetChannel); +// if(dx != null) { +// dx.value = DMX.sanitizeDMXValue(value); +// } +//} + +//public void setChannelValueByAddress(int address, int value) { +// DMXChannel dx = getDMXChannelByAddress(address); +// if(dx != null) { +// dx.value = DMX.sanitizeDMXValue(value); +// } +//} + +//public int getChannelValueByOffset(int offsetChannel) { +// DMXChannel dx = getDMXChannelByOffset(offsetChannel) +// if(dx != null) { +// return dx.value; +// } +// return 0; +//} + +//public int getChannelValueByAddress(int address) { +// DMXChannel dx = getDMXChannelByAddress(address); +// if(dx != null) { +// return dx.value; +// } +// return 0; +//} + + @Override + public String toString() { + return "BuntiDMXDevice " + getDeviceID() + ", " + getDeviceName(); + } + + } diff --git a/bunti_server/src/main/java/de/ctdo/bunti/model/BuntiDevice.java b/bunti_server/src/main/java/de/ctdo/bunti/model/BuntiDevice.java index b55c63f..ffbeac3 100644 --- a/bunti_server/src/main/java/de/ctdo/bunti/model/BuntiDevice.java +++ b/bunti_server/src/main/java/de/ctdo/bunti/model/BuntiDevice.java @@ -1,9 +1,12 @@ package de.ctdo.bunti.model; -public class BuntiDevice { +import java.util.Map; + +public abstract class BuntiDevice { int deviceID; String deviceName; + long lastChanged; public int getDeviceID() { return deviceID; @@ -17,6 +20,20 @@ public class BuntiDevice { public void setDeviceName(String deviceName) { this.deviceName = deviceName; } + public long getLastChanged() { + return lastChanged; + } + public void setLastChanged(long lastChanged) { + this.lastChanged = lastChanged; + } + protected void lastChangedNow() { + this.lastChanged = System.currentTimeMillis(); + } + + public abstract void switchOff(); + public abstract void switchOn(); + + public abstract boolean setValuesFromOptions(Map options); } diff --git a/bunti_server/src/main/java/de/ctdo/bunti/model/BuntiSwitchingDevice.java b/bunti_server/src/main/java/de/ctdo/bunti/model/BuntiSwitchingDevice.java new file mode 100644 index 0000000..cd86fb8 --- /dev/null +++ b/bunti_server/src/main/java/de/ctdo/bunti/model/BuntiSwitchingDevice.java @@ -0,0 +1,27 @@ +package de.ctdo.bunti.model; + +import java.util.Map; + +public class BuntiSwitchingDevice extends BuntiDevice { + + @Override + public void switchOff() { + // TODO Auto-generated method stub + + } + + @Override + public void switchOn() { + // TODO Auto-generated method stub + + } + + @Override + public boolean setValuesFromOptions(Map options) { + // TODO Auto-generated method stub + return false; + } + + // zum Beispiel Lampel, also nen Hostname und HTTP Krams hier rein + +} diff --git a/bunti_server/src/main/java/de/ctdo/bunti/model/Par56Spot.java b/bunti_server/src/main/java/de/ctdo/bunti/model/Par56Spot.java new file mode 100644 index 0000000..1af8af6 --- /dev/null +++ b/bunti_server/src/main/java/de/ctdo/bunti/model/Par56Spot.java @@ -0,0 +1,69 @@ +package de.ctdo.bunti.model; + +import de.ctdo.bunti.dmx.DMX; +import de.ctdo.bunti.dmx.DMXChannel; + +public class Par56Spot extends BuntiDMXDevice { + + public final static String CHANNEL_MODE = "mode"; + public final static String CHANNEL_RED = "red"; + public final static String CHANNEL_GREEN = "green"; + public final static String CHANNEL_BLUE = "blue"; + public final static String CHANNEL_SPEED = "speed"; + + public Par56Spot(int deviceId, int startAddress, String name) { + addChannel(new DMXChannel(0, CHANNEL_MODE)); + addChannel(new DMXChannel(1, "red")); + addChannel(new DMXChannel(2, CHANNEL_GREEN)); + addChannel(new DMXChannel(3, CHANNEL_BLUE)); + addChannel(new DMXChannel(4, CHANNEL_SPEED)); + setStartAddress(startAddress); + setDeviceName(name); + setDeviceID(deviceId); + } + + public void setColorRed(int value) { + setChannelValueByName(CHANNEL_RED, value); + } + public void setColorGreen(int value) { + setChannelValueByName(CHANNEL_GREEN, value); + } + public void setColorBlue(int value) { + setChannelValueByName(CHANNEL_BLUE, value); + } + + public int getColorRed() { + return getChannelValueByName(CHANNEL_RED); + } + public int getColorGreen() { + return getChannelValueByName(CHANNEL_GREEN); + } + public int getColorBlue() { + return getChannelValueByName(CHANNEL_BLUE); + } + + @Override + public void switchOff() { + setChannelValueByName(CHANNEL_MODE, DMX.DMX_CHANNEL_VALUE_MIN); + setColorRed(DMX.DMX_CHANNEL_VALUE_MIN); + setColorGreen(DMX.DMX_CHANNEL_VALUE_MIN); + setColorBlue(DMX.DMX_CHANNEL_VALUE_MIN); + setChannelValueByName(CHANNEL_SPEED, DMX.DMX_CHANNEL_VALUE_MIN); + } + + @Override + public void switchOn() { + setChannelValueByName(CHANNEL_MODE, DMX.DMX_CHANNEL_VALUE_MIN); + setColorRed(DMX.DMX_CHANNEL_VALUE_MAX); + setColorGreen(DMX.DMX_CHANNEL_VALUE_MAX); + setColorBlue(DMX.DMX_CHANNEL_VALUE_MAX);; + setChannelValueByName(CHANNEL_SPEED, DMX.DMX_CHANNEL_VALUE_MIN); + } + + @Override + public String toString() { + return "Par56Spot " + getDeviceID() + ", " + getDeviceName() + + "[" + getColorRed() + "," + getColorGreen() + "," + getColorBlue() + "]"; + } + +} diff --git a/bunti_server/src/main/java/de/ctdo/bunti/model/Strobe1500.java b/bunti_server/src/main/java/de/ctdo/bunti/model/Strobe1500.java new file mode 100644 index 0000000..034212a --- /dev/null +++ b/bunti_server/src/main/java/de/ctdo/bunti/model/Strobe1500.java @@ -0,0 +1,61 @@ +package de.ctdo.bunti.model; + +import de.ctdo.bunti.dmx.DMX; +import de.ctdo.bunti.dmx.DMXChannel; + +public class Strobe1500 extends BuntiDMXDevice { + + public final static String CHANNEL_SPEED = "speed"; + public final static String CHANNEL_INTENSITY = "intensity"; + public final static String CHANNEL_MODE = "mode"; + + public Strobe1500(int deviceId, int startAddress, String name) { + addChannel(new DMXChannel(0, CHANNEL_SPEED)); + addChannel(new DMXChannel(1, CHANNEL_INTENSITY)); + addChannel(new DMXChannel(2, CHANNEL_MODE)); + setStartAddress(startAddress); + setDeviceName(name); + setDeviceID(deviceId); + } + + public void setSpeed(int value) { + setChannelValueByName(CHANNEL_SPEED, value); + } + public void setIntensity(int value) { + setChannelValueByName(CHANNEL_INTENSITY, value); + } + public void setMode(int value) { + setChannelValueByName(CHANNEL_MODE, value); + } + + public int getSpeed() { + return getChannelValueByName(CHANNEL_SPEED); + } + public int getIntensity() { + return getChannelValueByName(CHANNEL_INTENSITY); + } + public int getMode() { + return getChannelValueByName(CHANNEL_MODE); + } + + @Override + public void switchOff() { + setChannelValueByName(CHANNEL_MODE, DMX.DMX_CHANNEL_VALUE_MIN); + setChannelValueByName(CHANNEL_SPEED, DMX.DMX_CHANNEL_VALUE_MIN); + setChannelValueByName(CHANNEL_INTENSITY, DMX.DMX_CHANNEL_VALUE_MIN); + } + + @Override + public void switchOn() { + setChannelValueByName(CHANNEL_MODE, DMX.DMX_CHANNEL_VALUE_MIN); + setChannelValueByName(CHANNEL_SPEED, DMX.DMX_CHANNEL_VALUE_MAX); + setChannelValueByName(CHANNEL_INTENSITY, DMX.DMX_CHANNEL_VALUE_MAX); + } + + @Override + public String toString() { + return "Strobe1500 " + getDeviceID() + ", " + getDeviceName() + + "[" + getSpeed() + "," + getIntensity() + "," + getMode() + "]"; + } + +} diff --git a/bunti_server/src/main/java/de/ctdo/bunti/web/TestController.java b/bunti_server/src/main/java/de/ctdo/bunti/web/TestController.java index e169c55..a773bf8 100644 --- a/bunti_server/src/main/java/de/ctdo/bunti/web/TestController.java +++ b/bunti_server/src/main/java/de/ctdo/bunti/web/TestController.java @@ -1,16 +1,85 @@ package de.ctdo.bunti.web; +import java.util.HashMap; +import java.util.Map; + +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.RequestMapping; import org.springframework.web.servlet.ModelAndView; +import de.ctdo.bunti.control.BuntiController; + @Controller public class TestController { + Logger logger = LoggerFactory.getLogger(getClass()); + BuntiController buntiController; + + @Autowired + public void setBuntiController(BuntiController buntiController) { + this.buntiController = buntiController; + } @RequestMapping("/foobar") public ModelAndView blafasel() { + + Map options = new HashMap(); + + options.put("red", 124); + options.put("green", 23); + options.put("blue", 217); + + buntiController.setDevice(0, options); + + options.put("red", 0); + options.put("green", 255); + options.put("blue", 255); + buntiController.setDevice(1, options); + + options.put("red", 42); + options.put("green", 222); + options.put("blue", 111); + buntiController.setDevice(2, options); + + options.put("red", 10); + options.put("green", 25); + options.put("blue", 33); + buntiController.setDevice(3, options); + + options.clear(); + options.put("speed", 128); + options.put("intensity", 255); + buntiController.setDevice(4, options); + + + ModelAndView mav = new ModelAndView("hello.jsp"); + return mav; + + } + + @RequestMapping("/foobar2") + public ModelAndView blafasel2() { + + + Map options = new HashMap(); + + + options.put("red", 0); + options.put("green", 23); + options.put("blue", 42); + buntiController.setDevice(2, options); + + options.put("red", 111); + options.put("green", 78); + options.put("blue", 255); + buntiController.setDevice(3, options); + + + ModelAndView mav = new ModelAndView("hello.jsp"); return mav; diff --git a/bunti_server/src/main/java/de/ctdo/bunti/websocket/BuntiControllerApplication.java b/bunti_server/src/main/java/de/ctdo/bunti/websocket/BuntiControllerApplication.java index 4c17e40..bf4959e 100644 --- a/bunti_server/src/main/java/de/ctdo/bunti/websocket/BuntiControllerApplication.java +++ b/bunti_server/src/main/java/de/ctdo/bunti/websocket/BuntiControllerApplication.java @@ -10,8 +10,6 @@ import com.sun.grizzly.websockets.WebSocket; import com.sun.grizzly.websockets.WebSocketApplication; import com.sun.grizzly.websockets.WebSocketListener; -import de.ctdo.bunti.control.BuntiController; - @Component public class BuntiControllerApplication extends WebSocketApplication { Logger logger = LoggerFactory.getLogger(BuntiControllerApplication.class); @@ -19,7 +17,7 @@ public class BuntiControllerApplication extends WebSocketApplication { @Override public WebSocket createWebSocket(ProtocolHandler protocolHandler, WebSocketListener... listeners) { BuntiControllerWebSocket socket = new BuntiControllerWebSocket(protocolHandler, listeners); - BuntiController.getInstance().addListener(socket); +// BuntiControllerImpl.getInstance().addListener(socket); return socket; } @@ -32,14 +30,14 @@ public class BuntiControllerApplication extends WebSocketApplication { @Override public void onClose(WebSocket socket, com.sun.grizzly.websockets.DataFrame frame) { BuntiControllerWebSocket ws = (BuntiControllerWebSocket) socket; - BuntiController.getInstance().removeListener(ws); +// BuntiControllerImpl.getInstance().removeListener(ws); } @Override public void onMessage(WebSocket socket, String text) { - BuntiController.getInstance().performJSONString(text); +// BuntiControllerImpl.getInstance().performJSONString(text); // for (final WebSocket webSocket : getWebSockets()) {