2012-03-02 21:09:43 +00:00
|
|
|
package de.ctdo.bunti.model;
|
|
|
|
|
2012-03-06 23:42:30 +00:00
|
|
|
import de.ctdo.bunti.dmx.DMX;
|
|
|
|
import de.ctdo.bunti.dmx.DMXChannel;
|
|
|
|
import de.ctdo.bunti.dmx.DMXChannels;
|
|
|
|
|
2012-03-03 16:08:40 +00:00
|
|
|
import java.util.HashMap;
|
2012-03-03 12:00:53 +00:00
|
|
|
import java.util.Map;
|
2012-03-03 01:29:06 +00:00
|
|
|
import java.util.Map.Entry;
|
|
|
|
|
|
|
|
public abstract class BuntiDMXDevice extends BuntiDevice {
|
2012-03-06 23:42:30 +00:00
|
|
|
private int startAddress;
|
|
|
|
private long lastSendOut;
|
|
|
|
private final DMXChannels dmxChannels = new DMXChannels();
|
2012-03-03 12:00:53 +00:00
|
|
|
|
2012-03-06 23:42:30 +00:00
|
|
|
public BuntiDMXDevice(int deviceId, int startAddress, String name) {
|
|
|
|
super(deviceId, name);
|
2012-03-04 09:50:50 +00:00
|
|
|
this.startAddress = startAddress;
|
2012-03-06 23:42:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public final long getLastSendOut() {
|
|
|
|
return lastSendOut;
|
|
|
|
}
|
|
|
|
|
|
|
|
public final void setSendOutNow() {
|
|
|
|
this.lastSendOut = System.currentTimeMillis();
|
|
|
|
}
|
|
|
|
|
|
|
|
public final int getStartAddress() {
|
|
|
|
return startAddress;
|
|
|
|
}
|
|
|
|
|
|
|
|
public final 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.
|
2012-03-06 16:09:25 +00:00
|
|
|
* @return True on success, otherwise false
|
2012-03-06 23:42:30 +00:00
|
|
|
*/
|
|
|
|
protected final boolean setChannelValueByName(String name, int value) {
|
|
|
|
DMXChannel dx = dmxChannels.getChannelByName(name);
|
|
|
|
if (dx != null) {
|
|
|
|
dx.setValue(DMX.sanitizeDMXValue(value));
|
2012-03-06 16:09:25 +00:00
|
|
|
lastChangedNow();
|
|
|
|
return true;
|
2012-03-06 23:42:30 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the channel value identified by channel name.
|
|
|
|
*
|
|
|
|
* @param name The channel name to get the value from.
|
|
|
|
* @return The desired channel value.
|
|
|
|
*/
|
|
|
|
protected final int getChannelValueByName(String name) {
|
|
|
|
DMXChannel dx = dmxChannels.getChannelByName(name);
|
|
|
|
if (dx != null) {
|
|
|
|
return dx.getValue();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Collect the DMX Channel Data with correct DMX512 calculated offsets
|
|
|
|
* @return The channel data with startaddress+offset of every channel
|
|
|
|
*/
|
|
|
|
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 final boolean setValuesFromOptions(Map<String, Object> options) {
|
|
|
|
|
|
|
|
for (Entry<String, Object> opt : options.entrySet()) {
|
2012-03-06 16:58:11 +00:00
|
|
|
try {
|
|
|
|
int value = Integer.parseInt(opt.getValue().toString());
|
|
|
|
|
2012-03-06 23:42:30 +00:00
|
|
|
if (!setChannelValueByName(opt.getKey(), value)) {
|
2012-03-06 16:58:11 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-03-06 23:42:30 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2012-03-04 09:50:50 +00:00
|
|
|
|
2012-03-06 23:42:30 +00:00
|
|
|
/**
|
|
|
|
* 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 boolean addChannel(DMXChannel channel) {
|
2012-03-04 09:50:50 +00:00
|
|
|
return dmxChannels.addChannel(channel);
|
|
|
|
}
|
2012-03-02 21:09:43 +00:00
|
|
|
}
|