Some progress

This commit is contained in:
Ziver Koc 2016-08-17 16:45:02 +02:00
parent 52afc53660
commit 3bdb4ea043
14 changed files with 212 additions and 173 deletions

View file

@ -50,17 +50,17 @@ public class HalSpeechClient {
bot.initialize(); bot.initialize();
/********************************************************************/ /********************************************************************/
/* NexaSelfLearning nexa1 = new NexaSelfLearning(); /* NexaSelfLearningProtocol nexa1 = new NexaSelfLearningProtocol();
nexa1.setHouse(15087918); nexa1.setHouse(15087918);
nexa1.setUnit(0); nexa1.setUnit(0);
switches.put("livingroom", new SwitchEventData("livingroom", nexa1)); switches.put("livingroom", new SwitchEventData("livingroom", nexa1));
NexaSelfLearning nexa2 = new NexaSelfLearning(); NexaSelfLearningProtocol nexa2 = new NexaSelfLearningProtocol();
nexa2.setHouse(15087918); nexa2.setHouse(15087918);
nexa2.setUnit(1); nexa2.setUnit(1);
switches.put("bedroom", new SwitchEventData("bedroom", nexa2)); switches.put("bedroom", new SwitchEventData("bedroom", nexa2));
NexaSelfLearning nexa3 = new NexaSelfLearning(); NexaSelfLearningProtocol nexa3 = new NexaSelfLearningProtocol();
nexa3.setHouse(15087918); nexa3.setHouse(15087918);
nexa3.setUnit(3); nexa3.setUnit(3);
switches.put("kitchen", new SwitchEventData("kitchen", nexa3)); switches.put("kitchen", new SwitchEventData("kitchen", nexa3));
@ -70,7 +70,7 @@ public class HalSpeechClient {
public void stateChange(TellstickProtocol protocol) { public void stateChange(TellstickProtocol protocol) {
for(SwitchEventData s : switches.values()) { for(SwitchEventData s : switches.values()) {
if(s.equals(protocol)) { if(s.equals(protocol)) {
String response = s.getName()+" window is "+(((NexaSelfLearning)protocol).isEnabled() ? "open": "closed"); String response = s.getName()+" window is "+(((NexaSelfLearningProtocol)protocol).isEnabled() ? "open": "closed");
System.out.println(">>> " + response); System.out.println(">>> " + response);
tts.speak(response); tts.speak(response);
return; return;

View file

@ -0,0 +1,25 @@
package se.hal.intf;
/**
* Interface representing one report from an event
*
* Created by Ziver on 2016-08-17.
*/
public abstract class HalDeviceData {
private long timestamp = -1;
public long getTimestamp(){
return timestamp;
}
public void setTimestamp(long timestamp){
this.timestamp = timestamp;
}
/**
* @return serialized event data converted to double that will be saved in DB.
*/
public abstract double getData();
}

View file

@ -5,21 +5,6 @@ package se.hal.intf;
* *
* Created by Ziver on 2016-08-17. * Created by Ziver on 2016-08-17.
*/ */
public abstract class HalEventData { public abstract class HalEventData extends HalDeviceData{
private long timestamp = -1;
public long getTimestamp(){
return timestamp;
}
public void setTimestamp(long timestamp){
this.timestamp = timestamp;
}
/**
* @return serialized event data converted to double that will be saved in DB.
*/
public abstract double getData();
} }

View file

@ -5,21 +5,6 @@ package se.hal.intf;
* *
* Created by Ziver on 2016-08-17. * Created by Ziver on 2016-08-17.
*/ */
public abstract class HalSensorData { public abstract class HalSensorData extends HalDeviceData{
private long timestamp = -1;
public long getTimestamp(){
return timestamp;
}
public void setTimestamp(long timestamp){
this.timestamp = timestamp;
}
/**
* @return serialized sensor data converted to double that will be saved in DB.
*/
public abstract double getData();
} }

View file

@ -34,7 +34,7 @@ public interface TellstickGroupProtocol {
* Protocols should extend this method if it has group functionality. * Protocols should extend this method if it has group functionality.
* @return true if this object an the input is in the same group. * @return true if this object an the input is in the same group.
*/ */
public boolean equalsGroup(TellstickProtocol obj); public boolean equalsGroup(TellstickGroupProtocol obj);
/** /**
* Copy the state data from the group to this object. * Copy the state data from the group to this object.

View file

@ -22,8 +22,8 @@
package se.hal.plugin.tellstick; package se.hal.plugin.tellstick;
import se.hal.plugin.tellstick.protocols.NexaSelfLearning; import se.hal.plugin.tellstick.protocol.NexaSelfLearningProtocol;
import se.hal.plugin.tellstick.protocols.Oregon0x1A2D; import se.hal.plugin.tellstick.protocol.Oregon0x1A2DProtocol;
import zutil.converter.Converter; import zutil.converter.Converter;
import zutil.log.LogUtil; import zutil.log.LogUtil;
@ -41,8 +41,8 @@ public class TellstickParser {
private static HashMap<String, Class<? extends TellstickProtocol>> protocolMap; private static HashMap<String, Class<? extends TellstickProtocol>> protocolMap;
static { static {
registerProtocol(NexaSelfLearning.class); registerProtocol(NexaSelfLearningProtocol.class);
registerProtocol(Oregon0x1A2D.class); registerProtocol(Oregon0x1A2DProtocol.class);
} }
private int firmwareVersion = -1; private int firmwareVersion = -1;

View file

@ -19,20 +19,18 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE. * THE SOFTWARE.
*/ */
package se.hal.plugin.tellstick; package se.hal.plugin.tellstick;
import se.hal.intf.HalEventController; import se.hal.intf.HalEventConfig;
import se.hal.intf.HalSensorController; import se.hal.intf.HalEventData;
/** /**
* Created by Ziver on 2015-02-18. * Created by Ziver on 2015-02-18.
*/ */
public abstract class TellstickProtocol { public abstract class TellstickProtocol {
private String protocol; private final String protocol;
private String model; private final String model;
private long timestamp = -1;
public TellstickProtocol(String protocol, String model){ public TellstickProtocol(String protocol, String model){
@ -48,22 +46,7 @@ public abstract class TellstickProtocol {
return model; return model;
} }
public long getTimestamp() { public String encode(HalEventConfig deviceConfig, HalEventData deviceData){ return null; }
return timestamp;
}
public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}
public Class<? extends HalEventController> getEventController() {
return TellstickSerialComm.class;
}
public Class<? extends HalSensorController> getSensorController() {
return TellstickSerialComm.class;
}
public abstract String encode();
public abstract void decode(byte[] data); public abstract void decode(byte[] data);
} }

View file

@ -20,12 +20,13 @@
* THE SOFTWARE. * THE SOFTWARE.
*/ */
package se.hal.plugin.tellstick.protocols; package se.hal.plugin.tellstick.device;
import se.hal.intf.HalEventConfig; import se.hal.intf.HalEventConfig;
import se.hal.intf.HalEventController;
import se.hal.plugin.tellstick.TellstickGroupProtocol; import se.hal.plugin.tellstick.TellstickGroupProtocol;
import se.hal.plugin.tellstick.TellstickProtocol; import se.hal.plugin.tellstick.TellstickProtocol;
import se.hal.struct.devicedata.SwitchEventData; import se.hal.plugin.tellstick.TellstickSerialComm;
import zutil.ByteUtil; import zutil.ByteUtil;
import zutil.parser.binary.BinaryStruct; import zutil.parser.binary.BinaryStruct;
import zutil.parser.binary.BinaryStructInputStream; import zutil.parser.binary.BinaryStructInputStream;
@ -37,8 +38,7 @@ import java.io.IOException;
/** /**
* Created by Ziver on 2015-02-18. * Created by Ziver on 2015-02-18.
*/ */
public class NexaSelfLearning extends TellstickProtocol public class NexaSelfLearning implements HalEventConfig,TellstickGroupProtocol,BinaryStruct {
implements HalEventConfig,TellstickGroupProtocol,BinaryStruct {
@BinaryField(index=1, length=26) @BinaryField(index=1, length=26)
@Configurator.Configurable("House code") @Configurator.Configurable("House code")
@ -56,59 +56,6 @@ public class NexaSelfLearning extends TellstickProtocol
private int unit = 0; private int unit = 0;
public NexaSelfLearning() {
super("arctech", "selflearning");
}
public String encode(){
try {
// T[t0][t1][t2][t3][length][d1]..[dn]+
StringBuilder enc = new StringBuilder(90); // Tellstick supports max 74 bytes
enc.append(new char[]{'T', 127, 255, 24, 0});
enc.append((char)0); // length
enc.append((char)0b0000_1001); // preamble
int length = 4;
byte[] data = BinaryStructOutputStream.serialize(this);
for (byte b : data){
for (int i=7; i>=0; --i){
if (ByteUtil.getBits(b, i, 1) == 0)
enc.append((char) 0b1010_1000); // 0b1010_1000
else // 1
enc.append((char) 0b1000_1010); // 0b1000_1010
length += 4;
}
}
enc.append((char)0b0000_0000); // postemble
length += 2;
enc.setCharAt(5, (char)length); // Set calculated length
enc.append("+");
return enc.toString();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
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
BinaryStructInputStream.read(this, data);
}
public int getHouse() { public int getHouse() {
return house; return house;
} }
@ -146,7 +93,7 @@ public class NexaSelfLearning extends TellstickProtocol
return false; return false;
} }
@Override @Override
public boolean equalsGroup(TellstickProtocol obj) { public boolean equalsGroup(TellstickGroupProtocol obj) {
if(obj instanceof NexaSelfLearning) if(obj instanceof NexaSelfLearning)
return ((NexaSelfLearning) obj).house == house && return ((NexaSelfLearning) obj).house == house &&
(((NexaSelfLearning) obj).group || group ); (((NexaSelfLearning) obj).group || group );
@ -158,4 +105,8 @@ public class NexaSelfLearning extends TellstickProtocol
this.enable = ((NexaSelfLearning) group).enable; this.enable = ((NexaSelfLearning) group).enable;
} }
public Class<? extends HalEventController> getEventController() {
return TellstickSerialComm.class;
}
} }

View file

@ -0,0 +1,55 @@
package se.hal.plugin.tellstick.device;
import se.hal.intf.HalEventController;
import se.hal.intf.HalSensorConfig;
import se.hal.intf.HalSensorController;
import se.hal.plugin.tellstick.TellstickProtocol;
import se.hal.plugin.tellstick.TellstickSerialComm;
import zutil.log.LogUtil;
import zutil.ui.Configurator;
import java.util.logging.Logger;
/**
* Created by Ziver on 2015-11-19.
*/
public class Oregon0x1A2D implements HalSensorConfig {
private static final Logger logger = LogUtil.getLogger();
@Configurator.Configurable("Address")
private int address = 0;
@Configurator.Configurable("Report Interval(ms)")
private int interval = 60*1000; // default 1 min
@Override
public boolean equals(Object obj){
if(! (obj instanceof Oregon0x1A2D))
return false;
return ((Oregon0x1A2D)obj).address == this.address;
}
public String toString(){
return "address:"+address;
}
@Override
public long getDataInterval() {
return interval;
}
@Override
public AggregationMethod getAggregationMethod() {
return AggregationMethod.SUM;
}
@Override
public Class<? extends HalSensorController> getSensorController() {
return TellstickSerialComm.class;
}
}

View file

@ -0,0 +1,95 @@
/*
* 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.hal.plugin.tellstick.protocol;
import se.hal.intf.HalEventConfig;
import se.hal.plugin.tellstick.TellstickGroupProtocol;
import se.hal.plugin.tellstick.TellstickProtocol;
import zutil.ByteUtil;
import zutil.parser.binary.BinaryStruct;
import zutil.parser.binary.BinaryStructInputStream;
import zutil.parser.binary.BinaryStructOutputStream;
import zutil.ui.Configurator;
import java.io.IOException;
/**
* Created by Ziver on 2015-02-18.
*/
public class NexaSelfLearningProtocol extends TellstickProtocol {
public NexaSelfLearningProtocol() {
super("arctech", "selflearning");
}
@Override
public String encode(){
try {
// T[t0][t1][t2][t3][length][d1]..[dn]+
StringBuilder enc = new StringBuilder(90); // Tellstick supports max 74 bytes
enc.append(new char[]{'T', 127, 255, 24, 0});
enc.append((char)0); // length
enc.append((char)0b0000_1001); // preamble
int length = 4;
byte[] data = BinaryStructOutputStream.serialize(this);
for (byte b : data){
for (int i=7; i>=0; --i){
if (ByteUtil.getBits(b, i, 1) == 0)
enc.append((char) 0b1010_1000); // 0b1010_1000
else // 1
enc.append((char) 0b1000_1010); // 0b1000_1010
length += 4;
}
}
enc.append((char)0b0000_0000); // postemble
length += 2;
enc.setCharAt(5, (char)length); // Set calculated length
enc.append("+");
return enc.toString();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
@Override
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
BinaryStructInputStream.read(this, data);
}
}

View file

@ -1,8 +1,7 @@
package se.hal.plugin.tellstick.protocols; package se.hal.plugin.tellstick.protocol;
import se.hal.intf.HalSensorConfig; import se.hal.intf.HalSensorConfig;
import se.hal.plugin.tellstick.TellstickProtocol; import se.hal.plugin.tellstick.TellstickProtocol;
import se.hal.struct.devicedata.PowerConsumptionSensorData;
import zutil.log.LogUtil; import zutil.log.LogUtil;
import zutil.ui.Configurator; import zutil.ui.Configurator;
@ -11,29 +10,16 @@ import java.util.logging.Logger;
/** /**
* Created by Ziver on 2015-11-19. * Created by Ziver on 2015-11-19.
*/ */
public class Oregon0x1A2D extends TellstickProtocol implements HalSensorConfig { public class Oregon0x1A2DProtocol extends TellstickProtocol {
private static final Logger logger = LogUtil.getLogger(); private static final Logger logger = LogUtil.getLogger();
@Configurator.Configurable("Address")
private int address = 0;
@Configurator.Configurable("Report Interval(ms)")
private int interval = 60*1000; // default 1 min
private double temperature = 0;
private double humidity = 0;
public Oregon0x1A2DProtocol(){
public Oregon0x1A2D(){
super("oregon", "0x1A2D"); super("oregon", "0x1A2D");
} }
@Override
public String encode() {
return null;
}
@Override @Override
public void decode(byte[] data) { public void decode(byte[] data) {
//class:sensor;protocol:oregon;model:0x1A2D;data:20BA000000002700; //class:sensor;protocol:oregon;model:0x1A2D;data:20BA000000002700;
@ -68,29 +54,4 @@ public class Oregon0x1A2D extends TellstickProtocol implements HalSensorConfig {
} }
@Override
public boolean equals(Object obj){
if(! (obj instanceof Oregon0x1A2D))
return false;
return ((Oregon0x1A2D)obj).address == this.address;
}
public String toString(){
return "address:"+address+
", temperature:"+temperature+
", humidity:"+humidity;
}
@Override
public long getDataInterval() {
return interval;
}
@Override
public AggregationMethod getAggregationMethod() {
return AggregationMethod.SUM;
}
} }

View file

@ -42,7 +42,7 @@ public class SensorDataAggregationDeamonTest {
System.out.println("Adding user to database"); System.out.println("Adding user to database");
db.exec("INSERT INTO user(id, external, username) VALUES(222, 0, 'test')"); //adding user db.exec("INSERT INTO user(id, external, username) VALUES(222, 0, 'test')"); //adding user
System.out.println("Adding sensor to database"); System.out.println("Adding sensor to database");
db.exec("INSERT INTO sensor(id, user_id, external_id, type) VALUES(111, 222, 333, 'se.hal.plugin.tellstick.protocols.Oregon0x1A2D')"); //adding sensor db.exec("INSERT INTO sensor(id, user_id, external_id, type) VALUES(111, 222, 333, 'se.hal.plugin.tellstick.protocol.Oregon0x1A2DProtocol')"); //adding sensor
System.out.println("Generating raw data and saving it to the database..."); System.out.println("Generating raw data and saving it to the database...");
PreparedStatement stmt = db.getPreparedStatement("INSERT INTO sensor_data_raw (timestamp, sensor_id, data) VALUES(?, ?, ?)"); PreparedStatement stmt = db.getPreparedStatement("INSERT INTO sensor_data_raw (timestamp, sensor_id, data) VALUES(?, ?, ?)");
try{ try{

View file

@ -1,6 +1,6 @@
package se.hal.plugin.tellstick; package se.hal.plugin.tellstick;
import se.hal.plugin.tellstick.protocols.NexaSelfLearning; import se.hal.plugin.tellstick.protocol.NexaSelfLearningProtocol;
/** /**
* Created by Ziver on 2015-11-19. * Created by Ziver on 2015-11-19.
@ -17,7 +17,7 @@ public class TelstickSerialCommNexaOnOffTest {
Thread.sleep(1000); Thread.sleep(1000);
NexaSelfLearning nexa = new NexaSelfLearning(); NexaSelfLearningProtocol nexa = new NexaSelfLearningProtocol();
//nexa.setHouse(11772006); //nexa.setHouse(11772006);
nexa.setHouse(14160770); nexa.setHouse(14160770);
nexa.setGroup(false); nexa.setGroup(false);

View file

@ -20,9 +20,8 @@
* THE SOFTWARE. * THE SOFTWARE.
*/ */
package se.hal.plugin.tellstick.protocols; package se.hal.plugin.tellstick.protocol;
import zutil.ByteUtil;
import zutil.converter.Converter; import zutil.converter.Converter;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
@ -33,7 +32,7 @@ public class NexaSelfLearningTest {
@org.junit.Test @org.junit.Test
public void testEncode() throws Exception { public void testEncode() throws Exception {
NexaSelfLearning nexa = new NexaSelfLearning(); NexaSelfLearningProtocol nexa = new NexaSelfLearningProtocol();
nexa.setHouse(11_772_006); nexa.setHouse(11_772_006);
nexa.setUnit(0); nexa.setUnit(0);
nexa.turnOn(); nexa.turnOn();
@ -61,7 +60,7 @@ public class NexaSelfLearningTest {
@org.junit.Test @org.junit.Test
public void decode_ON() throws Exception { public void decode_ON() throws Exception {
NexaSelfLearning nexa = decode("0x2CE81990"); NexaSelfLearningProtocol nexa = decode("0x2CE81990");
assertEquals("House Code", 11772006, nexa.getHouse()); assertEquals("House Code", 11772006, nexa.getHouse());
assertEquals("Unit Code", 0, nexa.getUnit()); assertEquals("Unit Code", 0, nexa.getUnit());
@ -70,15 +69,15 @@ public class NexaSelfLearningTest {
@org.junit.Test @org.junit.Test
public void decode_OFF() throws Exception { public void decode_OFF() throws Exception {
NexaSelfLearning nexa = decode("0x2CE81980"); NexaSelfLearningProtocol nexa = decode("0x2CE81980");
assertEquals("House Code", 11772006, nexa.getHouse()); assertEquals("House Code", 11772006, nexa.getHouse());
assertEquals("Unit Code", 0, nexa.getUnit()); assertEquals("Unit Code", 0, nexa.getUnit());
assertFalse("Enabled", nexa.isOn()); assertFalse("Enabled", nexa.isOn());
} }
private NexaSelfLearning decode(String data){ private NexaSelfLearningProtocol decode(String data){
NexaSelfLearning nexa = new NexaSelfLearning(); NexaSelfLearningProtocol nexa = new NexaSelfLearningProtocol();
nexa.decode(Converter.hexToByte(data)); nexa.decode(Converter.hexToByte(data));
return nexa; return nexa;
} }