SensorSanity { public synchronized static boolean checkLibreSensorChange(final String currentSerial) { Log.i(TAG, "checkLibreSensorChange called currentSerial = " + currentSerial); if ((currentSerial == null) || currentSerial.length() < 4) return false; final Sensor this_sensor = Sensor.currentSensor(); if(this_sensor == null || this_sensor.uuid == null|| this_sensor.uuid.length() < 4) { Log.i(TAG, "no senosr open, deleting everything"); PersistentStore.setString(PREF_LIBRE_SENSOR_UUID, ""); PersistentStore.setString(PREF_LIBRE_SN, ""); return false; } final String lastSn = PersistentStore.getString(PREF_LIBRE_SN); final String last_uuid = PersistentStore.getString(PREF_LIBRE_SENSOR_UUID); Log.i(TAG, "checkLibreSensorChange Initial values: lastSn = " + lastSn + " last_uuid = " + last_uuid); if(lastSn.length() < 4 || last_uuid.length() < 4) { Log.i(TAG, "lastSn or last_uuid not valid, writing current values."); PersistentStore.setString(PREF_LIBRE_SENSOR_UUID, this_sensor.uuid); PersistentStore.setString(PREF_LIBRE_SN, currentSerial); return false; } if(lastSn.equals(currentSerial)) { if(this_sensor.uuid.equals(last_uuid)) { Log.i(TAG, "checkLibreSensorChange returning false 1"); return false; } else { Log.e(TAG, "A new xdrip sensor was found, updating state."); PersistentStore.setString(PREF_LIBRE_SENSOR_UUID, this_sensor.uuid); return false; } } else { if(this_sensor.uuid.equals(last_uuid)) { Log.e(TAG, String.format("Different sensor serial number for same sensor uuid: %s :: %s vs %s", last_uuid, lastSn, currentSerial)); Sensor.stopSensor(); JoH.static_toast_long("Stopping sensor due to serial number change"); PersistentStore.setString(PREF_LIBRE_SENSOR_UUID, ""); PersistentStore.setString(PREF_LIBRE_SN, ""); return true; } else { Log.i(TAG, "This is the first time we see this sensor uuid = " + this_sensor.uuid); PersistentStore.setString(PREF_LIBRE_SENSOR_UUID, this_sensor.uuid); PersistentStore.setString(PREF_LIBRE_SN, currentSerial); return false; } } }  static boolean isRawValueSane(double raw_value); static boolean isRawValueSane(double raw_value, boolean hard); static boolean isRawValueSane(double raw_value, DexCollectionType type); static boolean isRawValueSane(double raw_value, DexCollectionType type, boolean hard_check); static boolean allowTestingWithDeadSensor(); static void clearEnviorment(); static boolean checkLibreSensorChangeIfEnabled(final String sn); synchronized static boolean checkLibreSensorChange(final String currentSerial); static final double DEXCOM_MIN_RAW; static final double DEXCOM_MAX_RAW; static final double DEXCOM_G6_MIN_RAW; static final double DEXCOM_G6_MAX_RAW; static final double LIBRE_MIN_RAW; }
@Test public void checkLibreSensorChangeTest() { final String[] testSerials = {"12345678", "12345678", "", "12345678", "12345678", "12345679", "", "12345679", "12345679", "12345680", "12345680", "12345680", "12345680", "12345680"}; Sensor.shutdownAllSensors(); JoH.clearCache(); sensorCheck("all null check ", null, false, true); for (int i = 0; i < testSerials.length; i++) { sensorCheck("Null sensor check " + i, testSerials[i], false, true); } Sensor sensor = Sensor.create(JoH.tsl()); for (int i = 0; i < 4; i++) { sensorCheck("Standard failure check (a) " + i, testSerials[i], false, false); } for (int i = 5; i < 6; i++) { sensorCheck("Standard failure check (b) " + i, testSerials[i], true, true); } for (int i = 6; i < testSerials.length; i++) { sensorCheck("Standard failure check (b) " + i, testSerials[i], false, true); } final String[] testSerials2 = {"12345678", "12345678", "12345678", "12345678", null, "12345678", "12345678", "12345678", "12345678", "", "12345678", "12345678", "12345678", "12345678"}; final long ts = JoH.tsl(); Sensor.create(ts + 1); for (int i = 0; i < testSerials2.length; i++) { sensorCheck("new sensors (" + i + ") ", testSerials2[i], false, false); } }