Converting to BinaryStructs (Incomplete)

This commit is contained in:
Ziver Koc 2016-05-13 17:03:51 +02:00
parent 1c62ef7c18
commit 467b58d7ca
2 changed files with 68 additions and 25 deletions

View file

@ -25,21 +25,35 @@ package se.hal.plugin.tellstick.protocols;
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.SwitchEventData; import se.hal.struct.SwitchEventData;
import zutil.ByteUtil;
import zutil.parser.binary.BinaryStruct;
import zutil.parser.binary.BinaryStructInputStream;
import zutil.parser.binary.BinaryStructOutputStream;
import zutil.ui.Configurator; import zutil.ui.Configurator;
import java.io.IOException;
/** /**
* Created by Ziver on 2015-02-18. * Created by Ziver on 2015-02-18.
*/ */
public class NexaSelfLearning extends TellstickProtocol implements SwitchEventData,TellstickGroupProtocol { public class NexaSelfLearning extends TellstickProtocol
implements SwitchEventData,TellstickGroupProtocol,BinaryStruct {
@BinaryField(index=1, length=26)
@Configurator.Configurable("House code") @Configurator.Configurable("House code")
private int house = 0; private int house = 0;
@BinaryField(index=2, length=1)
@Configurator.Configurable("Group code") @Configurator.Configurable("Group code")
private boolean group = false; private boolean group = false;
@BinaryField(index=3, length=1)
private boolean enable = false;
@BinaryField(index=4, length=4)
@Configurator.Configurable("Unit code") @Configurator.Configurable("Unit code")
private int unit = 0; private int unit = 0;
private boolean enable = false;
public NexaSelfLearning() { public NexaSelfLearning() {
@ -48,12 +62,12 @@ public class NexaSelfLearning extends TellstickProtocol implements SwitchEventDa
public String encode(){ public String encode(){
/*
// Binary 0 => "01" // Binary 0 => "01"
// Binary 1 => "10" // Binary 1 => "10"
StringBuilder enc = new StringBuilder(); StringBuilder enc = new StringBuilder();
enc.append(new char[]{'T', 127, 255, 24, 1}); enc.append(new char[]{'T', 127, 255, 24, 1});
enc.append((char)132);
// House // House
StringBuilder m = new StringBuilder(); StringBuilder m = new StringBuilder();
@ -97,6 +111,31 @@ public class NexaSelfLearning extends TellstickProtocol implements SwitchEventDa
enc.append("+"); enc.append("+");
return enc.toString(); return enc.toString();
*/
try {
StringBuilder enc = new StringBuilder();
enc.append(new char[]{'T', 127, 255, 24, 1});
enc.append((char)132); // length
enc.append((char)0b0000_1001); // preamble
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
}
}
enc.append("+");
return enc.toString();
} catch (IOException e) {
e.printStackTrace();
}
return "";
} }
public void decode(byte[] data){ public void decode(byte[] data){
@ -109,18 +148,7 @@ public class NexaSelfLearning extends TellstickProtocol implements SwitchEventDa
// 0x2CE81990 - 00101100_11101000_00011001_10 0 1 0000 - ON // 0x2CE81990 - 00101100_11101000_00011001_10 0 1 0000 - ON
// 0x2CE81980 - 00101100_11101000_00011001_10 0 0 0000 - OFF // 0x2CE81980 - 00101100_11101000_00011001_10 0 0 0000 - OFF
house = 0; BinaryStructInputStream.read(this, data);
house |= (data[3] & 0xFF) << 18;
house |= (data[2] & 0xFF) << 10;
house |= (data[1] & 0xFF) << 2;
house |= (data[0] & 0xC0) >>> 6;
int tmpGroup = data[0] & 0x20; // >>> 5
group = tmpGroup != 0;
enable = (data[0] & 0x10) != 0;
unit = data[0] & 0x0F;
} }

View file

@ -22,8 +22,11 @@
package se.hal.plugin.tellstick.protocols; package se.hal.plugin.tellstick.protocols;
import zutil.ByteUtil;
import zutil.converter.Converter; import zutil.converter.Converter;
import java.nio.charset.StandardCharsets;
import static org.junit.Assert.*; import static org.junit.Assert.*;
public class NexaSelfLearningTest { public class NexaSelfLearningTest {
@ -31,18 +34,30 @@ public class NexaSelfLearningTest {
@org.junit.Test @org.junit.Test
public void testEncode() throws Exception { public void testEncode() throws Exception {
NexaSelfLearning nexa = new NexaSelfLearning(); NexaSelfLearning nexa = new NexaSelfLearning();
nexa.setHouse(11772006); nexa.setHouse(11_772_006);
nexa.setUnit(3); nexa.setUnit(0);
nexa.turnOn(); nexa.turnOn();
assertArrayEquals( byte[] expected = Converter.toBytes(new char[]{
new char[]{ 84, // T
84, 127, 255, 24, 1, 132, 154, 138, 136, 170, 127, 255, 24, 1, 132, // timings
136, 168, 170, 138, 136, 168, 168, 170, 136, 170,
138, 138, 138, 138, 138, 136, 168, 170, 138, 136, 9, // preamble
168, 170, 138, 136, 170, 138, 136, 168, 170, 43}, 168, 168, 138, 168, 138, 138, 168, 168, 138, 138,
nexa.encode().toCharArray() 138, 168, 138, 168, 168, 168, 168, 168, 168, 138,
); 138, 168, 168, 138, 138, 168, 168, 138, 168, 168,
168, 168,
/*154, 138, 136, 170, 136, 168, 170, 138, 136, 168,
168, 170, 136, 170, 138, 138, 138, 138, 138, 136,
168, 170, 138, 136, 168, 170, 138, 136, 170, 138,
136, 168, 170,*/
43}); // +
byte[] actual = nexa.encode().getBytes(StandardCharsets.ISO_8859_1);
System.out.println("Expected: "+Converter.toHexString(expected).toUpperCase());
System.out.println("Actual : "+Converter.toHexString(actual).toUpperCase());
assertArrayEquals(expected, actual);
} }