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-09 22:37:26 +00:00
|
|
|
import org.codehaus.jackson.annotate.JsonIgnore;
|
2012-03-06 23:42:30 +00:00
|
|
|
|
2012-03-21 22:43:00 +00:00
|
|
|
import javax.persistence.Column;
|
|
|
|
import javax.persistence.DiscriminatorValue;
|
|
|
|
import javax.persistence.Entity;
|
|
|
|
import javax.persistence.Transient;
|
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;
|
|
|
|
|
2012-03-21 22:43:00 +00:00
|
|
|
@Entity
|
2012-03-03 01:29:06 +00:00
|
|
|
public abstract class BuntiDMXDevice extends BuntiDevice {
|
2012-03-06 23:42:30 +00:00
|
|
|
private int startAddress;
|
|
|
|
private final DMXChannels dmxChannels = new DMXChannels();
|
2012-03-03 12:00:53 +00:00
|
|
|
|
2012-03-21 22:43:00 +00:00
|
|
|
public BuntiDMXDevice() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-03-09 22:37:26 +00:00
|
|
|
/**
|
|
|
|
* Gets the DMX start address for this device
|
|
|
|
* @return The address value from 1 to max 512
|
|
|
|
*/
|
2012-03-25 14:23:45 +00:00
|
|
|
public int getStartAddress() {
|
2012-03-06 23:42:30 +00:00
|
|
|
return startAddress;
|
|
|
|
}
|
|
|
|
|
2012-03-09 22:37:26 +00:00
|
|
|
/**
|
|
|
|
* Gets the DMX start address for this device
|
|
|
|
* @param startAddress The address value from 1 to max 512
|
|
|
|
* @return True on success, false if start address is wrong
|
|
|
|
*/
|
2012-03-25 14:23:45 +00:00
|
|
|
public boolean setStartAddress(int startAddress) {
|
2012-03-07 18:17:43 +00:00
|
|
|
if(startAddress < DMX.DMX_CHANNEL_INDEX_MIN ||
|
|
|
|
startAddress - 1 + dmxChannels.getCount() > DMX.DMX_CHANNEL_INDEX_MAX) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-03-06 23:42:30 +00:00
|
|
|
this.startAddress = startAddress;
|
2012-03-07 18:17:43 +00:00
|
|
|
return true;
|
2012-03-06 23:42:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
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.
|
|
|
|
*/
|
2012-03-21 22:43:00 +00:00
|
|
|
@JsonIgnore
|
|
|
|
@Transient
|
2012-03-06 23:42:30 +00:00
|
|
|
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
|
|
|
|
*/
|
2012-03-09 22:37:26 +00:00
|
|
|
@JsonIgnore
|
2012-03-21 22:43:00 +00:00
|
|
|
@Transient
|
2012-03-06 23:42:30 +00:00
|
|
|
public Map<Integer, Integer> getChannelData() {
|
|
|
|
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
|
|
|
|
|
|
|
|
for (DMXChannel channel : dmxChannels.getAllChannels()) {
|
2012-03-07 18:17:43 +00:00
|
|
|
int index = channel.getOffset() + startAddress;
|
2012-03-06 23:42:30 +00:00
|
|
|
|
2012-03-07 18:17:43 +00:00
|
|
|
// if (index >= DMX.DMX_CHANNEL_INDEX_MIN && index <= DMX.DMX_CHANNEL_INDEX_MAX) {
|
2012-03-06 23:42:30 +00:00
|
|
|
map.put(index, channel.getValue());
|
2012-03-07 18:17:43 +00:00
|
|
|
// }
|
2012-03-06 23:42:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|