Updated Tellstick drivers and Switched from System.ou.print to Logger
Former-commit-id: 62d5bf468e366ecf4056602de469c151311c9a12
This commit is contained in:
parent
c2138902f0
commit
aa656cdc05
27 changed files with 125 additions and 199 deletions
|
|
@ -25,13 +25,17 @@ package se.koc.hal.plugin.tellstick;
|
|||
import se.koc.hal.plugin.tellstick.protocols.NexaSelfLearning;
|
||||
import se.koc.hal.plugin.tellstick.protocols.Oregon0x1A2D;
|
||||
import zutil.converters.Converter;
|
||||
import zutil.log.LogUtil;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* Created by Ziver on 2015-02-18.
|
||||
*/
|
||||
public class TellstickParser {
|
||||
private static final Logger logger = LogUtil.getLogger();
|
||||
private static HashMap<String, Class<? extends TellstickProtocol>> protocolMap;
|
||||
|
||||
static {
|
||||
|
|
@ -61,19 +65,19 @@ public class TellstickParser {
|
|||
protocol.decode(Converter.hexToByte(binData));
|
||||
if(!protocol.equals(previus)) {
|
||||
previus = protocol;
|
||||
System.out.println("Decoded: " + protocol);
|
||||
logger.finest("Decoded: " + protocol);
|
||||
return protocol;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
logger.log(Level.WARNING, null, e);
|
||||
}
|
||||
} else {
|
||||
System.out.println("Unknown protocol: " + data);
|
||||
logger.warning("Unknown protocol: " + data);
|
||||
}
|
||||
} else if (data.startsWith("+S") || data.startsWith("+T")) {
|
||||
// This is confirmation of send commands
|
||||
}else {
|
||||
System.out.println("Unknown prefix: " + data);
|
||||
logger.severe("Unknown prefix: " + data);
|
||||
}
|
||||
|
||||
return null;
|
||||
|
|
@ -90,7 +94,7 @@ public class TellstickParser {
|
|||
tmp.getProtocolName() + "-" + tmp.getModelName(),
|
||||
protClass);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
logger.log(Level.SEVERE, null, e);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -23,53 +23,72 @@
|
|||
package se.koc.hal.plugin.tellstick;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import com.fazecast.jSerialComm.SerialPort;
|
||||
import zutil.log.InputStreamLogger;
|
||||
import zutil.log.LogUtil;
|
||||
import zutil.log.OutputStreamLogger;
|
||||
import zutil.struct.TimedHashSet;
|
||||
|
||||
/**
|
||||
* This version of the TwoWaySerialComm example makes use of the
|
||||
* SerialPortEventListener to avoid polling.
|
||||
*/
|
||||
public class TellstickSerialComm extends Thread{
|
||||
public static TellstickSerialComm instance;
|
||||
private static final long TRANSMISSION_UNIQUENESS_TTL = 100; // milliseconds
|
||||
private static final Logger logger = LogUtil.getLogger();
|
||||
private static TellstickSerialComm instance;
|
||||
|
||||
private com.fazecast.jSerialComm.SerialPort serial;
|
||||
private SerialPort serial;
|
||||
private BufferedReader in;
|
||||
private Writer out;
|
||||
private BufferedWriter out;
|
||||
private TimedHashSet set; // To check for retransmissions
|
||||
|
||||
private TellstickParser parser = new TellstickParser();
|
||||
private TellstickChangeListener listener;
|
||||
|
||||
|
||||
public TellstickSerialComm(){
|
||||
set = new TimedHashSet(TRANSMISSION_UNIQUENESS_TTL);
|
||||
}
|
||||
|
||||
public void connect(String portName) throws Exception {
|
||||
serial = SerialPort.getCommPort(portName);
|
||||
serial.setBaudRate(115200);
|
||||
serial.setBaudRate(9600);
|
||||
if(!serial.openPort())
|
||||
throw new IOException("Could not open port: "+portName);
|
||||
serial.setComPortTimeouts(
|
||||
SerialPort.TIMEOUT_READ_SEMI_BLOCKING,
|
||||
5000, 0);
|
||||
SerialPort.TIMEOUT_READ_SEMI_BLOCKING, 0, 0);
|
||||
|
||||
in = new BufferedReader(new InputStreamReader(serial.getInputStream(), "UTF-8"));
|
||||
out = new BufferedWriter(new OutputStreamWriter(serial.getOutputStream(), "UTF-8"));
|
||||
in = new BufferedReader(new InputStreamReader(new InputStreamLogger(serial.getInputStream()), "UTF-8"));
|
||||
out = new BufferedWriter(new OutputStreamWriter(new OutputStreamLogger(serial.getOutputStream()), "UTF-8"));
|
||||
this.start();
|
||||
}
|
||||
|
||||
|
||||
public void run() {
|
||||
try {
|
||||
String data;
|
||||
while ((data = in.readLine()) != null) {
|
||||
System.out.println("> " + data);
|
||||
TellstickProtocol protocol = parser.decode(data);
|
||||
if (protocol == null && (data.startsWith("+S") || data.startsWith("+T"))) {
|
||||
if ((data.startsWith("+S") || data.startsWith("+T"))) {
|
||||
synchronized (this) {
|
||||
this.notifyAll();
|
||||
}
|
||||
}
|
||||
else if(listener != null){
|
||||
listener.stateChange(protocol);
|
||||
else {
|
||||
if(!set.contains(data)) {
|
||||
TellstickProtocol protocol = parser.decode(data);
|
||||
set.add(data);
|
||||
if (listener != null) {
|
||||
listener.stateChange(protocol);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
logger.log(Level.SEVERE, null, e);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -78,18 +97,18 @@ public class TellstickSerialComm extends Thread{
|
|||
try {
|
||||
this.wait();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
logger.log(Level.SEVERE, null, e);
|
||||
}
|
||||
}
|
||||
|
||||
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.write('\n');
|
||||
out.flush();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
logger.log(Level.SEVERE, null, e);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -104,7 +123,7 @@ public class TellstickSerialComm extends Thread{
|
|||
instance = new TellstickSerialComm();
|
||||
instance.connect("COM6");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
logger.log(Level.SEVERE, null, e);
|
||||
}
|
||||
}
|
||||
return instance;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue