diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2015-01-11 12:28:55 +0100 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2015-01-11 12:28:55 +0100 |
commit | 17a1f7227c8c3872fce7bbcc2f5cd46540f9ac52 (patch) | |
tree | 20c2c99e710fd1db438a49029177552204a63591 /app/src/main/java/io/trygvis/soilmoisture/SmDevice.java | |
parent | 4a2ca2d94c827566f8682e8dbd6fbdf17d70b4dd (diff) | |
download | io.trygvis.soilmoisture-android-17a1f7227c8c3872fce7bbcc2f5cd46540f9ac52.tar.gz io.trygvis.soilmoisture-android-17a1f7227c8c3872fce7bbcc2f5cd46540f9ac52.tar.bz2 io.trygvis.soilmoisture-android-17a1f7227c8c3872fce7bbcc2f5cd46540f9ac52.tar.xz io.trygvis.soilmoisture-android-17a1f7227c8c3872fce7bbcc2f5cd46540f9ac52.zip |
o Reading values from the soil sensor.
o Rewrote the database schema to match the new device+sensors model.
o Storing samples in the database.
o To be able to reuse BT callbacks, added a way to always to directly to the next step instead of waiting for an event.
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 |