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

126 lines
3.6 KiB
Java
Raw Normal View History

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;
import org.codehaus.jackson.annotate.JsonIgnore;
2012-03-06 23:42:30 +00:00
import javax.persistence.Column;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.Transient;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
@Entity
public abstract class BuntiDMXDevice extends BuntiDevice {
2012-03-06 23:42:30 +00:00
private int startAddress;
private final DMXChannels dmxChannels = new DMXChannels();
public BuntiDMXDevice() {
}
/**
* 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;
}
/**
* 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.
* @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));
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.
*/
@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
*/
@JsonIgnore
@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()) {
try {
int value = Integer.parseInt(opt.getValue().toString());
2012-03-06 23:42:30 +00:00
if (!setChannelValueByName(opt.getKey(), value)) {
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
}