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

92 lines
2.1 KiB
Java

package de.ctdo.bunti.model;
import de.ctdo.bunti.model.BuntiDevice;
import org.hibernate.annotations.Entity;
import javax.persistence.Transient;
import java.util.HashMap;
import java.util.Map;
@Entity
public abstract class BuntiEthersexDevice extends BuntiDevice {
private String hostname;
private int port;
private final Map<String, Integer> ports = new HashMap<String, Integer>();
public BuntiEthersexDevice() {
}
public String getHostname() {
return hostname;
}
public void setHostname(String hostname) {
this.hostname = hostname;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public final boolean setPortByName(String name, int value) {
if (ports.containsKey(name)) {
ports.put(name, value);
return true;
}
return false;
}
@Transient
public final int getPortByName(String name) {
if (ports.containsKey(name)) {
return ports.get(name);
}
return 0;
}
@Override
public boolean setValuesFromOptions(Map<String, Object> options) {
for (Map.Entry<String, Object> opt : options.entrySet()) {
try {
int value = Integer.parseInt(opt.getValue().toString());
if (!setPortByName(opt.getKey(), value)) {
return false;
}
} catch (Exception e) {
return false;
}
}
return true;
}
@Override
public Map<String, Object> getOptions() {
Map<String, Object> options = new HashMap<String, Object>();
for(Map.Entry<String, Integer> p: ports.entrySet()) {
options.put(p.getKey(), p.getValue());
}
return options;
}
/**
* 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 void addPort(String channel) {
ports.put(channel, 0x00);
}
}