From 41bfcd33c39d998a68005981e499b363fe06187a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Ple=C3=9F?= Date: Mon, 26 Mar 2012 01:26:52 +0200 Subject: [PATCH] first approach to connect to ethersex devices like lampel --- .../de/ctdo/bunti/ethersex/ECMDCommand.java | 30 +++++++ .../de/ctdo/bunti/ethersex/ECMDResult.java | 19 ++++ .../de/ctdo/bunti/ethersex/ECMDSender.java | 12 +++ .../ctdo/bunti/ethersex/SimpleECMDSender.java | 54 +++++++++++ src/main/java/de/ctdo/bunti/model/Lampel.java | 89 +++++++++++++++++++ 5 files changed, 204 insertions(+) create mode 100644 src/main/java/de/ctdo/bunti/ethersex/ECMDCommand.java create mode 100644 src/main/java/de/ctdo/bunti/ethersex/ECMDResult.java create mode 100644 src/main/java/de/ctdo/bunti/ethersex/ECMDSender.java create mode 100644 src/main/java/de/ctdo/bunti/ethersex/SimpleECMDSender.java create mode 100644 src/main/java/de/ctdo/bunti/model/Lampel.java diff --git a/src/main/java/de/ctdo/bunti/ethersex/ECMDCommand.java b/src/main/java/de/ctdo/bunti/ethersex/ECMDCommand.java new file mode 100644 index 0000000..21b4b27 --- /dev/null +++ b/src/main/java/de/ctdo/bunti/ethersex/ECMDCommand.java @@ -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 parameters = new ArrayList(); + + public String getCommand() { + return command; + } + + public void setCommand(String command) { + this.command = command; + } + + public List getParameters() { + return parameters; + } + + public void setParameters(List parameters) { + this.parameters = parameters; + } +} diff --git a/src/main/java/de/ctdo/bunti/ethersex/ECMDResult.java b/src/main/java/de/ctdo/bunti/ethersex/ECMDResult.java new file mode 100644 index 0000000..4cddd51 --- /dev/null +++ b/src/main/java/de/ctdo/bunti/ethersex/ECMDResult.java @@ -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; + } +} diff --git a/src/main/java/de/ctdo/bunti/ethersex/ECMDSender.java b/src/main/java/de/ctdo/bunti/ethersex/ECMDSender.java new file mode 100644 index 0000000..65d913c --- /dev/null +++ b/src/main/java/de/ctdo/bunti/ethersex/ECMDSender.java @@ -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; + +} diff --git a/src/main/java/de/ctdo/bunti/ethersex/SimpleECMDSender.java b/src/main/java/de/ctdo/bunti/ethersex/SimpleECMDSender.java new file mode 100644 index 0000000..6cda7ea --- /dev/null +++ b/src/main/java/de/ctdo/bunti/ethersex/SimpleECMDSender.java @@ -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; + } + + +} diff --git a/src/main/java/de/ctdo/bunti/model/Lampel.java b/src/main/java/de/ctdo/bunti/model/Lampel.java new file mode 100644 index 0000000..8080986 --- /dev/null +++ b/src/main/java/de/ctdo/bunti/model/Lampel.java @@ -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 values = new HashMap(); + + @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 options) { + for (Map.Entry 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 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(); + } + +}