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 | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/app/src/main/java/io/trygvis/soilmoisture/SmDevice.java b/app/src/main/java/io/trygvis/soilmoisture/SmDevice.java index e24416c..b51b3fa 100644 --- a/app/src/main/java/io/trygvis/soilmoisture/SmDevice.java +++ b/app/src/main/java/io/trygvis/soilmoisture/SmDevice.java @@ -2,11 +2,19 @@ package io.trygvis.soilmoisture; import android.util.Log; +import java.nio.charset.Charset; import java.util.ArrayList; import java.util.List; import io.trygvis.android.bt.BtDevice; +import static io.trygvis.soilmoisture.SmDevice.SmCmdCode.GET_SENSOR_COUNT; +import static io.trygvis.soilmoisture.SmDevice.SmCmdCode.GET_SENSOR_NAME; +import static io.trygvis.soilmoisture.SmDevice.SmCmdCode.GET_VALUE; +import static io.trygvis.soilmoisture.SmDevice.SmCmdCode.GET_WARNING_VALUE; +import static io.trygvis.soilmoisture.SmDevice.SmCmdCode.SET_SENSOR_NAME; +import static io.trygvis.soilmoisture.SmDevice.SmCmdCode.SET_WARNING_VALUE; + class SmDevice implements BtDevice.BtDeviceWrapper<SmDevice> { private final static String TAG = SmDevice.class.getSimpleName(); @@ -57,4 +65,109 @@ class SmDevice implements BtDevice.BtDeviceWrapper<SmDevice> { public List<SmSensor> getSensors() { return sensors; } + + // ----------------------------------------------------------------------- + // + // ----------------------------------------------------------------------- + + @SuppressWarnings("unchecked") + public static <T> T parseResponse(byte[] bytes, SmCmdCode code, Class<T> klass) { + byte c = bytes[0]; + + if (c != code.code) { + 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) { + } else if (c == SET_WARNING_VALUE.code) { + } else if (c == GET_WARNING_VALUE.code) { + } else if (c == SET_SENSOR_NAME.code) { + } else if (c == GET_SENSOR_NAME.code) { + } + + 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; + + public GetSensorCountRes(int count) { + this.count = count; + } + } + + public static byte[] createGetSensorCountReq() { + return new byte[]{ + GET_SENSOR_COUNT.code + }; + } + + public static byte[] createGetValueReq(byte sensor) { + return new byte[]{ + GET_VALUE.code, + sensor + }; + } + + public static byte[] createSetWarningValueReq(byte sensor, short warningValue) { + return new byte[]{ + SET_WARNING_VALUE.code, + sensor, + (byte) (warningValue & 0xff), + (byte) ((warningValue >> 8) & 0xff) + }; + } + + public static byte[] createGetWarningValueReq(byte sensor) { + return new byte[]{ + GET_WARNING_VALUE.code, + sensor + }; + } + + public static byte[] createSetSensorNameReq(byte sensor, String name) { + byte[] tmp = name.getBytes(Charset.defaultCharset()); + + byte[] bytes = new byte[3 + tmp.length]; + bytes[0] = GET_WARNING_VALUE.code; + bytes[1] = sensor; + bytes[2] = (byte) tmp.length; + System.arraycopy(tmp, 0, bytes, 1, tmp.length); + return bytes; + } + + public static byte[] createGetSensorNameReq(byte sensor) { + return new byte[]{ + GET_SENSOR_NAME.code, + sensor + }; + } + + public static final int SENSOR_NAME_LEN = 10; + + public enum SmCmdCode { + GET_SENSOR_COUNT(1), + GET_VALUE(2), + SET_WARNING_VALUE(3), + GET_WARNING_VALUE(4), + SET_SENSOR_NAME(5), + GET_SENSOR_NAME(6), + FAIL(255); + + public final byte code; + + SmCmdCode(int value) { + this.code = (byte) value; + } + } } |