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 { private final static String TAG = SmDevice.class.getSimpleName(); private final BtDevice btDevice; private String name; private Boolean isUseful; private List sensors = new ArrayList<>(); public SmDevice(BtDevice btDevice) { this.btDevice = btDevice; Log.i(TAG, "new device"); name = btDevice.getName(); if (name != null && name.trim().length() == 0) { name = null; } } public BtDevice getBtDevice() { return btDevice; } public boolean isUseful() { return isUseful != null && isUseful; } public Boolean getIsUseful() { return isUseful; } public boolean isProbed() { return isUseful != null; } public void setIsUseful(Boolean isUseful) { Log.i(TAG, "useful=" + isUseful); this.isUseful = isUseful; } public String getName() { return name; } public List getSensors() { return sensors; } // ----------------------------------------------------------------------- // // ----------------------------------------------------------------------- @SuppressWarnings("unchecked") public static T parseResponse(byte[] bytes, SmCmdCode code, Class 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; } } }