diff options
Diffstat (limited to 'app/src/main/java/io/trygvis/soilmoisture/SmDevice.java')
-rw-r--r-- | app/src/main/java/io/trygvis/soilmoisture/SmDevice.java | 36 |
1 files changed, 25 insertions, 11 deletions
diff --git a/app/src/main/java/io/trygvis/soilmoisture/SmDevice.java b/app/src/main/java/io/trygvis/soilmoisture/SmDevice.java index b51b3fa..4cb25f3 100644 --- a/app/src/main/java/io/trygvis/soilmoisture/SmDevice.java +++ b/app/src/main/java/io/trygvis/soilmoisture/SmDevice.java @@ -18,16 +18,22 @@ import static io.trygvis.soilmoisture.SmDevice.SmCmdCode.SET_WARNING_VALUE; class SmDevice implements BtDevice.BtDeviceWrapper<SmDevice> { private final static String TAG = SmDevice.class.getSimpleName(); + final DefaultSoilMoistureService smService; + private final BtDevice<SmDevice> btDevice; + final long id; + private String name; private Boolean isUseful; private List<SmSensor> sensors = new ArrayList<>(); - public SmDevice(BtDevice<SmDevice> btDevice) { + public SmDevice(DefaultSoilMoistureService smService, BtDevice<SmDevice> btDevice, long id) { + this.smService = smService; this.btDevice = btDevice; + this.id = id; Log.i(TAG, "new device"); name = btDevice.getName(); @@ -63,14 +69,21 @@ class SmDevice implements BtDevice.BtDeviceWrapper<SmDevice> { } public List<SmSensor> getSensors() { + if (!isUseful()) { + throw new IllegalStateException("Not a useful device"); + } return sensors; } + void addSensor(SmSensor sensor) { + sensors.add(sensor); + } + // ----------------------------------------------------------------------- - // + // Message parsing and handling. // ----------------------------------------------------------------------- - @SuppressWarnings("unchecked") + @SuppressWarnings({"unchecked", "StatementWithEmptyBody"}) public static <T> T parseResponse(byte[] bytes, SmCmdCode code, Class<T> klass) { byte c = bytes[0]; @@ -78,10 +91,10 @@ class SmDevice implements BtDevice.BtDeviceWrapper<SmDevice> { throw new RuntimeException("Expected response of type " + code + ", got " + c); } - Object value = null; if (c == GET_SENSOR_COUNT.code) { return (T) new GetSensorCountRes(bytes[1]); } else if (c == GET_VALUE.code) { + return (T) new GetValueRes((bytes[2] & 0xff) << 8 | (bytes[1] & 0xff)); } else if (c == SET_WARNING_VALUE.code) { } else if (c == GET_WARNING_VALUE.code) { } else if (c == SET_SENSOR_NAME.code) { @@ -91,13 +104,6 @@ class SmDevice implements BtDevice.BtDeviceWrapper<SmDevice> { throw new RuntimeException("Unknown code: " + c); } - public void setSensorCount(int count) { - sensors = new ArrayList<>(); - for (int index = 0; index < count; index++) { - sensors.add(new SmSensor(this, index)); - } - } - public static class GetSensorCountRes { public final int count; @@ -106,6 +112,14 @@ class SmDevice implements BtDevice.BtDeviceWrapper<SmDevice> { } } + public static class GetValueRes { + public final int value; + + public GetValueRes(int value) { + this.value = value; + } + } + public static byte[] createGetSensorCountReq() { return new byte[]{ GET_SENSOR_COUNT.code |