Initial commit

This commit is contained in:
Ziver Koc 2015-03-26 21:22:26 +00:00
commit 12f2c3eeee
78 changed files with 4776 additions and 0 deletions

View file

@ -0,0 +1,91 @@
/*
* Copyright (c) 2015 Ziver
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package se.koc.hal.plugin.tellstick;
import se.koc.hal.plugin.tellstick.protocols.NexaSelfLearning;
import zutil.converters.Converter;
import java.util.HashMap;
/**
* Created by Ziver on 2015-02-18.
*/
public class TellstickParser {
private static HashMap<String, Class<? extends TellstickProtocol>> protocolMap;
static {
registerProtocol(NexaSelfLearning.class);
}
public void decode(String data) {
if (data.startsWith("+W")) {
data = data.substring(2);
HashMap<String, String> map = new HashMap<String, String>();
String[] parameters = data.split(";");
for (String parameter : parameters) {
String[] keyValue = parameter.split(":");
map.put(keyValue[0], keyValue[1]);
}
Class<? extends TellstickProtocol> protClass =
getProtocolClass(map.get("protocol"), map.get("model"));
if (protClass != null) {
try {
TellstickProtocol protocol = protClass.newInstance();
String binData = map.get("data");
protocol.decode(Converter.hexToByte(binData));
System.out.println("Decoded: " + protocol);
} catch (Exception e) {
e.printStackTrace();
}
} else {
System.out.println("Unknown protocol: " + data);
}
} else if (data.startsWith("+S") || data.startsWith("+T")) {
// This is confirmation of send commands
}else {
System.out.println("Unknown prefix: " + data);
}
}
public static void registerProtocol(Class<? extends TellstickProtocol> protClass) {
try {
if (protocolMap == null)
protocolMap = new HashMap<String, Class<? extends TellstickProtocol>>();
TellstickProtocol tmp = protClass.newInstance();
protocolMap.put(
tmp.getProtocolName() + "-" + tmp.getModelName(),
protClass);
} catch (Exception e) {
e.printStackTrace();
}
}
public static Class<? extends TellstickProtocol> getProtocolClass(String protocol, String model) {
return protocolMap.get(protocol + "-" + model);
}
}

View file

@ -0,0 +1,35 @@
/*
* Copyright (c) 2015 Ziver
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package se.koc.hal.plugin.tellstick;
/**
* Created by Ziver on 2015-02-18.
*/
public interface TellstickProtocol {
public String encode();
public void decode(byte[] data);
public String getProtocolName();
public String getModelName();
}

View file

@ -0,0 +1,121 @@
/*
* Copyright (c) 2015 Ziver
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package se.koc.hal.plugin.tellstick;
import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import se.koc.hal.plugin.tellstick.protocols.NexaSelfLearning;
import java.io.*;
/**
* This version of the TwoWaySerialComm example makes use of the
* SerialPortEventListener to avoid polling.
*/
public class TwoWaySerialComm extends Thread{
private BufferedReader in;
private OutputStream out;
private TellstickParser parser = new TellstickParser();
public void connect(String portName) throws Exception {
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
if (portIdentifier.isCurrentlyOwned()) {
System.out.println("Error: Port is currently in use");
} else {
CommPort commPort = portIdentifier.open(this.getClass().getName(), 2000);
if (commPort instanceof SerialPort) {
SerialPort serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
in = new BufferedReader(new InputStreamReader (serialPort.getInputStream()));
out = new BufferedOutputStream(serialPort.getOutputStream());
serialPort.disableReceiveTimeout();
serialPort.enableReceiveThreshold(1);
this.start();
} else {
System.out.println("Error: Only serial ports are handled by this example.");
}
}
}
public void run() {
try {
String data;
while ((data = in.readLine()) != null) {
System.out.println("> "+data);
parser.decode(data);
}
} catch (IOException e) {
e.printStackTrace();
}
}
private void write(TellstickProtocol prot) {
write(prot.encode());
}
public void write(String data) {
try {
System.out.println("< "+data);
for(int i=0; i<data.length();i++)
out.write(0xFF & data.charAt(i));
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
try {
// http://developer.telldus.com/doxygen/TellStick.html
TwoWaySerialComm comm = new TwoWaySerialComm();
comm.connect("COM6");
try{Thread.sleep(1000);}catch(Exception e){}
NexaSelfLearning nexa = new NexaSelfLearning();
nexa.setHouse(11772006);
nexa.setGroup(0);
nexa.setUnit(1);
while(true) {
nexa.setEnable(true);
comm.write(nexa);
Thread.sleep(2000);
nexa.setEnable(false);
comm.write(nexa);
Thread.sleep(4000);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

View file

@ -0,0 +1,155 @@
/*
* Copyright (c) 2015 Ziver
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package se.koc.hal.plugin.tellstick.protocols;
import se.koc.hal.plugin.tellstick.TellstickProtocol;
/**
* Created by Ziver on 2015-02-18.
*/
public class NexaSelfLearning implements TellstickProtocol {
private int house = 0;
private int group = 0;
private int unit = 0;
private boolean enable = false;
public String encode(){
StringBuilder enc = new StringBuilder();
enc.append(new char[]{'T', 127, 255, 24, 1});
enc.append((char)132);
// House
StringBuilder m = new StringBuilder();
for (int i = 25; i >= 0; --i) {
m.append( (house & (1 << i)) == 0 ? "01" : "10" );
}
// Group
m.append("01");
// On or OFF
if (enable)
m.append("10");
else
m.append("01");
// Unit
for (int i = 3; i >= 0; --i) {
m.append( (unit & (1 << i)) == 0 ? "01" : "10" );
}
// The number of data is odd add this to make it even
m.append("0");
//01011001101001011010100110010101010101101001011010 01 01 1001011010 0
char code = 9; // b1001, startcode
for (int i = 0; i < m.length(); ++i) {
code <<= 4;
if (m.charAt(i) == '1') {
code |= 0x08; // b1000
} else {
code |= 0x0A; // b1010
}
if (i % 2 == 0) {
enc.append(code);
code = 0x00;
}
}
enc.append("+");
return enc.toString();
}
public void decode(byte[] data){
// Data positions
// house = 0xFFFFFFC0
// group = 0x00000020
// method = 0x00000010
// unit = 0x0000000F
// ----------------h------------ g m --u-
// 0x2CE81990 - 00101100_11101000_00011001_10 0 1 0000 - ON
// 0x2CE81980 - 00101100_11101000_00011001_10 0 0 0000 - OFF
house = 0;
house |= (data[3] & 0xFF) << 18;
house |= (data[2] & 0xFF) << 10;
house |= (data[1] & 0xFF) << 2;
house |= (data[0] & 0xC0) >>> 6;
group = data[0] & 0x20;
group >>>= 5;
enable = (data[0] & 0x10) != 0;
unit = data[0] & 0x0F;
unit++;
}
public int getHouse() {
return house;
}
public void setHouse(int house) {
this.house = house;
}
public int getGroup() {
return group;
}
public void setGroup(int group) {
this.group = group;
}
public int getUnit() {
return unit;
}
public void setUnit(int unit) {
this.unit = unit;
}
public boolean isEnabled() {
return enable;
}
public void setEnable(boolean enable) {
this.enable = enable;
}
@Override
public String getProtocolName() {
return "arctech";
}
@Override
public String getModelName() {
return "selflearning";
}
public String toString(){
return "class:command;protocol:arctech;model:selflearning;" +
"house:"+house+
";group:"+group+
";unit:"+unit+
";method:"+enable;
}
}