first approach to connect to ethersex devices like lampel

This commit is contained in:
Lucas Pleß 2012-03-26 01:26:52 +02:00
parent 53f6e29eb3
commit 41bfcd33c3
5 changed files with 204 additions and 0 deletions

View File

@ -0,0 +1,30 @@
package de.ctdo.bunti.ethersex;
import java.util.ArrayList;
import java.util.List;
/**
* @author: lucas
* @date: 26.03.12 00:52
*/
public class ECMDCommand {
private String command;
private List<String> parameters = new ArrayList<String>();
public String getCommand() {
return command;
}
public void setCommand(String command) {
this.command = command;
}
public List<String> getParameters() {
return parameters;
}
public void setParameters(List<String> parameters) {
this.parameters = parameters;
}
}

View File

@ -0,0 +1,19 @@
package de.ctdo.bunti.ethersex;
/**
* @author: lucas
* @date: 26.03.12 00:52
*/
public class ECMDResult {
private String resultString;
public String getResultString() {
return resultString;
}
public void setResultString(String resultString) {
this.resultString = resultString;
}
}

View File

@ -0,0 +1,12 @@
package de.ctdo.bunti.ethersex;
import java.io.IOException;
/**
* @author: lucas
* @date: 26.03.12 00:47
*/
public interface ECMDSender {
ECMDResult sendCommand(ECMDCommand command, String dest) throws IOException;
}

View File

@ -0,0 +1,54 @@
package de.ctdo.bunti.ethersex;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
/**
* @author: lucas
* @date: 26.03.12 00:54
*/
@Component
public class SimpleECMDSender implements ECMDSender {
private static final Logger LOGGER = LoggerFactory.getLogger(SimpleECMDSender.class);
private static final int ECMD_TCP_PORT = 2701;
@Override
public ECMDResult sendCommand(ECMDCommand command, String dest) {
try {
Socket client = new Socket(dest, ECMD_TCP_PORT);
DataOutputStream outToServer = new DataOutputStream(client.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(client.getInputStream()));
StringBuilder params = new StringBuilder();
for(String param: command.getParameters()) {
params.append(" ");
params.append(param);
}
outToServer.writeBytes(command.getCommand() + params.toString() + '\n');
ECMDResult result = new ECMDResult();
result.setResultString(inFromServer.readLine());
client.close();
return result;
} catch (IOException e) {
LOGGER.error("Could not send ECMDCommand to " + dest + " on port " + ECMD_TCP_PORT);
}
return null;
}
}

View File

@ -0,0 +1,89 @@
package de.ctdo.bunti.model;
import de.ctdo.bunti.dmx.DMX;
import de.ctdo.bunti.dmx.model.DMXChannel;
import org.codehaus.jackson.annotate.JsonIgnore;
import javax.persistence.Entity;
import javax.persistence.Transient;
import java.util.HashMap;
import java.util.Map;
@Entity
public class Lampel extends BuntiDevice {
private static final String CHANNEL_RED = "red";
private static final String CHANNEL_GREEN = "green";
private static final String CHANNEL_YELLOW = "yellow";
private static final Map<String, Boolean> values = new HashMap<String, Boolean>();
@Override
public final void switchOff() {
setValueByName(CHANNEL_GREEN, false);
setValueByName(CHANNEL_YELLOW, false);
setValueByName(CHANNEL_RED, false);
}
@Override
public final void switchOn() {
setValueByName(CHANNEL_GREEN, true);
setValueByName(CHANNEL_YELLOW, true);
setValueByName(CHANNEL_RED, true);
}
protected final boolean setValueByName(String name, boolean value) {
if (values.containsKey(name)) {
values.put(name, value);
return true;
}
return false;
}
@Transient
protected final boolean getValueByName(String name) {
if (values.containsKey(name)) {
return values.get(name);
}
return false;
}
@Override
public boolean setValuesFromOptions(Map<String, Object> options) {
for (Map.Entry<String, Object> opt : options.entrySet()) {
try {
Boolean value = Boolean.parseBoolean(opt.getValue().toString());
if (!setValueByName(opt.getKey(), value)) {
return false;
}
} catch (Exception e) {
return false;
}
}
return true;
}
@Override
public Map<String, Object> getOptions() {
return null;
}
@Override
public final String toString() {
StringBuilder sb = new StringBuilder();
sb.append("Lampel ");
sb.append(getId());
sb.append(", ");
sb.append(getDeviceName());
sb.append(" [");
sb.append(getValueByName(CHANNEL_RED));
sb.append(",");
sb.append(getValueByName(CHANNEL_YELLOW));
sb.append(",");
sb.append(getValueByName(CHANNEL_GREEN));
sb.append("]");
return sb.toString();
}
}