bunti/src/main/java/de/ctdo/bunti/model/BuntiDMXDevice.java

150 lines
3.7 KiB
Java

package de.ctdo.bunti.model;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import de.ctdo.bunti.dmx.*;
public abstract class BuntiDMXDevice extends BuntiDevice {
protected int startAddress;
private long lastSendOut;
protected DMXChannels dmxChannels = new DMXChannels();
public BuntiDMXDevice(int deviceId, int startAddress, String name) {
this.startAddress = startAddress;
this.deviceName = name;
this.deviceId = deviceId;
}
public long getLastSendOut() {
return lastSendOut;
}
public void setSendOutNow() {
this.lastSendOut = System.currentTimeMillis();
}
public int getStartAddress() {
return startAddress;
}
public void setStartAddress(int startAddress) {
this.startAddress = startAddress;
}
/**
* Set channel value by given channel name.
* @param name The channel name to change the value.
* @param value The channel value to set.
*/
protected void setChannelValueByName(String name, int value) {
DMXChannel dx = dmxChannels.getChannelByName(name);
if(dx != null) {
dx.setValue(DMX.sanitizeDMXValue(value));
}
lastChangedNow();
}
/**
* Returns the channel value identified by channel name.
* @param name The channel name to get the value from.
* @return The desired channel value.
*/
protected int getChannelValueByName(String name) {
DMXChannel dx = dmxChannels.getChannelByName(name);
if(dx != null) {
return dx.getValue();
}
return 0;
}
// /**
// * Merge the DMX values from this device into a global DMX512 Data Array.
// * @param dmxData DMX512 Data array to merge the local values into.
// * @return True on success, false otherwise.
// */
// public boolean mergeDMXData(int[] dmxData) {
//
// if(dmxData == null || dmxData.length == 0) {
// return false;
// }
//
// for (DMXChannel channel : dmxChannels.getAllChannels()) {
// int index = channel.getOffset() + (startAddress - DMX.DMX_STARTADDRESS_OFFSET);
//
// if(index >= 0 && index < dmxData.length){
// dmxData[index] = channel.getValue();
// } else {
// return false;
// }
// }
//
// return true;
// }
//
// /**
// * Merge the DMX values from this device into a global DMX512 Data Array.
// * @param dmxData DMX512 Data to merge the local values into.
// * @return True on success, false otherwise.
// */
// public boolean mergeDMXData(Map<Integer,Integer> dmxData) {
//
// if(dmxData == null || dmxData.size() == 0) {
// return false;
// }
//
// for (DMXChannel channel : dmxChannels.getAllChannels()) {
// int index = channel.getOffset() + (startAddress - DMX.DMX_STARTADDRESS_OFFSET);
//
// if(index >= DMX.DMX_CHANNELS_MIN && index <= DMX.DMX_CHANNELS_MAX){
// dmxData.put(index, channel.getValue());
// } else {
// return false;
// }
// }
//
// return true;
// }
public Map<Integer,Integer> getChannelData() {
Map<Integer,Integer> map = new HashMap<Integer, Integer>();
for (DMXChannel channel : dmxChannels.getAllChannels()) {
int index = channel.getOffset() + (startAddress - DMX.DMX_STARTADDRESS_OFFSET);
if(index >= DMX.DMX_CHANNELS_MIN && index <= DMX.DMX_CHANNELS_MAX){
map.put(index, channel.getValue());
}
}
return map;
}
@Override
public boolean setValuesFromOptions(Map<String, Object> options) {
for (Entry<String, Object> opt : options.entrySet()) {
DMXChannel channel = dmxChannels.getChannelByName(opt.getKey());
if(channel != null) {
try {
byte value = Byte.parseByte(opt.getValue().toString());
setChannelValueByName(channel.getName(), value);
} catch (Exception e) {
return false;
}
}
}
return true;
}
@Override
public String toString() {
return "BuntiDMXDevice " + deviceId + ", " + deviceName;
}
}