blueReader { public static byte[] decodeblueReaderPacket(byte[] buffer, int len) { int cmdFound = 0; long timestamp = new Date().getTime(); String bufferstring; if (buffer == null) { Log.e(TAG, "null buffer passed to decodeblueReaderPacket"); return null; } else { bufferstring=new String(buffer); } if (bufferstring.startsWith("not ready for") ) { counterHibernated++; Log.e(TAG, "Found blueReader in a ugly State (" + counterHibernated + "/3), send hibernate to reset! If this does not help in the next 5 Minutes, then turn the bluereader manually off and on!"); if (counterHibernated > 2) { Log.wtf(TAG, "Ugly state not resolveable. Bluereader will be shut down! Please restart it!"); Home.toaststatic("BlueReader ugly state not resolveable, bluereader will be shut down. Please restart it!"); if (!Pref.getBooleanDefaultFalse("blueReader_suppressuglystatemsg")) { Notifications.RiseDropAlert(xdrip.getAppContext(),true,"BlueReader Alarm", xdrip.getAppContext().getString(R.string.bluereaderuglystate),1); } return shutdown; } else { Home.toaststatic("Found blueReader in a ugly State, send hibernate to reset!"); return goHybernate; } } else if (bufferstring.startsWith("IDR")){ Log.i(TAG, bufferstring); PersistentStore.setString("blueReaderFirmware", bufferstring ); tempVers=Pattern.compile(".*\\|blue(.*)-.*").matcher(bufferstring); tempVers.find(); PersistentStore.setDouble("blueReaderFirmwareValue",Double.parseDouble(tempVers.group(1))); Log.i(TAG, "bluereader-Firmware-Version: " + tempVers); if (BgReading.last() == null || BgReading.last().timestamp + (4 * 60 * 1000) < System.currentTimeMillis()) { return requestValue; } else { return null; } } else if (bufferstring.startsWith("WAKE")) { Log.d (TAG, "blueReader was set to wakeup-mode manually..."); return null; } else if (bufferstring.startsWith("ECHO")) { Log.d (TAG, "blueReader was set to Echo-Mode manually..."); return null; } else if (bufferstring.startsWith("NFC READY")) { Log.d (TAG, "blueReader notice that NFC is active..."); return null; } else if (bufferstring.startsWith("NFC_DISABLED")) { Log.d (TAG, "blueReader notice that NFC is now hibernated..."); return null; } else if (bufferstring.startsWith("HYBERNATE SUCCESS")) { Log.i (TAG, "blueReader notice that NFC is now really hibernated..."); if (counterHibernated > 0) { Log.w (TAG,"Found hibernation after wrong read. Resend read-command..."); return requestValue; } else { return null; } } else if (bufferstring.startsWith("-r 0:")) { Log.d (TAG, "blueReader sends an unknown reaction: '" + bufferstring + "'"); return null; } else if (bufferstring.startsWith("TRANS_FAILED")) { Log.w (TAG, "Attention: check position of blueReader on the sensor, as it was not able to read!"); Home.toaststatic(xdrip.getAppContext().getString(R.string.bluereader_position)); return null; } else if (bufferstring.startsWith("battery: ")) { if (BgReading.last() == null || BgReading.last().timestamp + (4 * 60 * 1000) < System.currentTimeMillis()) { return requestValue; } } else { counterHibernated = 0; processNewTransmitterData(TransmitterData.create(buffer, len, timestamp), timestamp); if (Pref.getBooleanDefaultFalse("blueReader_turn_off")) { if (Pref.getInt("blueReader_turn_off_value",5) > Pref.getInt("bridge_battery",100)) { Log.w (TAG, "blueReader will be turn off, as the battery is lower then " + Pref.getInt("blueReader_turn_off_value",5) +"%"); Home.toaststatic(xdrip.getAppContext().getString(R.string.bluereaderoff) + Pref.getInt("blueReader_turn_off_value",5) +"%"); return shutdown; } } } return null; }  static boolean isblueReader(); static byte[] decodeblueReaderPacket(byte[] buffer, int len); static ByteBuffer initialize();  }
@Test public void decodeBlueReaderPacket_TransFailed() { byte[] buffer = "TRANS_FAILED".getBytes(); byte[] reply = blueReader.decodeblueReaderPacket(buffer, -1); assertThat(reply).isNull(); assertThat(getLogs()).contains("W/blueReader: Attention: check position of blueReader on the sensor, as it was not able to read!"); }
@Test public void decodeBlueReaderPacket_Wake() { byte[] buffer = "WAKE".getBytes(); byte[] reply = blueReader.decodeblueReaderPacket(buffer, -1); assertThat(reply).isNull(); assertThat(getLogs()).contains("D/blueReader: blueReader was set to wakeup-mode manually..."); }
@Test public void decodeBlueReaderPacket_Echo() { byte[] buffer = "ECHO".getBytes(); byte[] reply = blueReader.decodeblueReaderPacket(buffer, -1); assertThat(reply).isNull(); assertThat(getLogs()).contains("D/blueReader: blueReader was set to Echo-Mode manually..."); }
@Test public void decodeBlueReaderPacket_NfcReady() { byte[] buffer = "NFC READY".getBytes(); byte[] reply = blueReader.decodeblueReaderPacket(buffer, -1); assertThat(reply).isNull(); assertThat(getLogs()).contains("D/blueReader: blueReader notice that NFC is active..."); }
@Test public void decodeBlueReaderPacket_NfcDisabled() { byte[] buffer = "NFC_DISABLED".getBytes(); byte[] reply = blueReader.decodeblueReaderPacket(buffer, -1); assertThat(reply).isNull(); assertThat(getLogs()).contains("D/blueReader: blueReader notice that NFC is now hibernated..."); }
@Test public void decodeBlueReaderPacket_HybernateSuccess_HibernatedCounterZero() { byte[] buffer = "HYBERNATE SUCCESS".getBytes(); byte[] reply = blueReader.decodeblueReaderPacket(buffer, -1); assertThat(reply).isNull(); assertThat(getLogs()).contains("I/blueReader: blueReader notice that NFC is now really hibernated..."); }
@Test public void decodeBlueReaderPacket_HybernateSuccess_HibernatedCounterNotZero() { byte[] buffer = "HYBERNATE SUCCESS".getBytes(); blueReader.decodeblueReaderPacket("not ready for".getBytes(), -1); byte[] reply = blueReader.decodeblueReaderPacket(buffer, -1); assertThat(reply).isNotNull(); assertThat(new String(reply)).isEqualTo("l"); String log = getLogs(); assertThat(log).contains("I/blueReader: blueReader notice that NFC is now really hibernated..."); assertThat(log).contains("W/blueReader: Found hibernation after wrong read. Resend read-command..."); }
@Test public void decodeBlueReaderPacket_Unknown() { byte[] buffer = "-r 0:ASDF".getBytes(); byte[] reply = blueReader.decodeblueReaderPacket(buffer, -1); assertThat(reply).isNull(); assertThat(getLogs()).contains("D/blueReader: blueReader sends an unknown reaction: '-r 0:ASDF'"); }
@Test public void decodeBlueReaderPacket_Battery_NoLastBgReading() { byte[] buffer = "battery: 222".getBytes(); byte[] reply = blueReader.decodeblueReaderPacket(buffer, -1); assertThat(reply).isNotNull(); assertThat(new String(reply)).isEqualTo("l"); }
@Test public void decodeBlueReaderPacket_Battery_OldBgReading() { byte[] buffer = "battery: 4000 980".getBytes(); Sensor mockSensor = createMockSensor(); addMockBgReading(125, 10, mockSensor); byte[] reply = blueReader.decodeblueReaderPacket(buffer, -1); assertThat(reply).isNotNull(); assertThat(new String(reply)).isEqualTo("l"); }
@Test public void decodeBlueReaderPacket_Battery_NewBgReading() { byte[] buffer = "battery: 4000 980".getBytes(); Sensor mockSensor = createMockSensor(); addMockBgReading(125, 2, mockSensor); byte[] reply = blueReader.decodeblueReaderPacket(buffer, -1); assertThat(reply).isNull(); }
@Test public void decodeBlueReaderPacket_IDR_noBgReading() { String inputString = "IDR0|blue131-a1"; Double resultVersion = 131d; byte[] reply = blueReader.decodeblueReaderPacket(inputString.getBytes(), -1); assertThat(reply).isNotNull(); assertThat(new String(reply)).isEqualTo("l"); assertThat(PersistentStore.getString("blueReaderFirmware")).isEqualTo(inputString); assertThat(PersistentStore.getDouble("blueReaderFirmwareValue")).isWithin(0.01d).of(resultVersion) ; }
@Test public void decodeBlueReaderPacket_IDR_OldBgReading() { String inputString = "IDR0|blue131-a1"; Sensor mockSensor = createMockSensor(); addMockBgReading(125, 10, mockSensor); byte[] reply = blueReader.decodeblueReaderPacket(inputString.getBytes(), -1); assertThat(reply).isNotNull(); assertThat(new String(reply)).isEqualTo("l"); assertThat(PersistentStore.getString("blueReaderFirmware")).isEqualTo(inputString); }
@Test public void decodeBlueReaderPacket_IDR_NewBgReading() { String inputString = "IDR0|blue131-a1"; Sensor mockSensor = createMockSensor(); addMockBgReading(125, 2, mockSensor); byte[] reply = blueReader.decodeblueReaderPacket(inputString.getBytes(), -1); assertThat(reply).isNull(); assertThat(PersistentStore.getString("blueReaderFirmware")).isEqualTo(inputString); }
@Test public void decodeBlueReaderPacket_null() { byte[] reply = blueReader.decodeblueReaderPacket(null, -1); assertThat(reply).isNull(); assertThat(getLogs()).contains("E/blueReader: null buffer passed to decodeblueReaderPacket"); }
@Test public void decodeBlueReaderPacket_notReadyFor_oneTime() { byte[] buffer = "not ready for".getBytes(); byte[] reply = blueReader.decodeblueReaderPacket(buffer, -1); assertThat(reply).isNotNull(); assertThat(new String(reply)).isEqualTo("h"); assertThat(getLogs()).contains("E/blueReader: Found blueReader in a ugly State (1/3), send hibernate to reset! If this does not help in the next 5 Minutes, then turn the bluereader manually off and on!"); }
@Test public void decodeBlueReaderPacket_notReadyFor_threeTime() { byte[] buffer = "not ready for".getBytes(); Pref.setBoolean("blueReader_suppressuglystatemsg", true); byte[] reply1 = blueReader.decodeblueReaderPacket(buffer, -1); byte[] reply2 = blueReader.decodeblueReaderPacket(buffer, -1); byte[] reply3 = blueReader.decodeblueReaderPacket(buffer, -1); assertThat(reply1).isNotNull(); assertThat(reply2).isNotNull(); assertThat(reply3).isNotNull(); assertThat(new String(reply1)).isEqualTo("h"); assertThat(new String(reply2)).isEqualTo("h"); assertThat(new String(reply3)).isEqualTo("k"); String log = getLogs(); assertThat(log).contains("E/blueReader: Found blueReader in a ugly State (1/3), send hibernate to reset! If this does not help in the next 5 Minutes, then turn the bluereader manually off and on!"); assertThat(log).contains("E/blueReader: Found blueReader in a ugly State (2/3), send hibernate to reset! If this does not help in the next 5 Minutes, then turn the bluereader manually off and on!"); assertThat(log).contains("E/blueReader: Found blueReader in a ugly State (3/3), send hibernate to reset! If this does not help in the next 5 Minutes, then turn the bluereader manually off and on!"); assertThat(log).contains("A/blueReader: Ugly state not resolveable. Bluereader will be shut down! Please restart it!"); }
@Test public void processNewTransmitterData_NormalBg_FullBattery() { String message = "150000 4300"; Sensor mockSensor = createMockSensor(); addMockBgReading(150, 15, mockSensor); addMockBgReading(150, 10, mockSensor); addMockBgReading(150, 5, mockSensor); Calibration.initialCalibration(150, 150, RuntimeEnvironment.application.getApplicationContext()); byte[] bytes = blueReader.decodeblueReaderPacket(message.getBytes(), message.length()); assertThat(bytes).isNull(); assertThat(PersistentStore.getLong("blueReader_Full_Battery")).isEqualTo(4300); assertThat(PersistentStore.getString("bridge_battery_days")).isEqualTo("6.1"); assertThat(Pref.getInt("bridge_battery", -1)).isEqualTo(100); assertThat(getLogs()).contains("BgReading.create: new BG reading at "); assertThat(Sensor.currentSensor().latest_battery_level).isEqualTo(100); TransmitterData lastTransmitterData = DexCollectionService.last_transmitter_Data; assertThat(lastTransmitterData.filtered_data).isEqualTo(150000.0); assertThat(lastTransmitterData.raw_data).isEqualTo(150000.0); assertThat(lastTransmitterData.sensor_battery_level).isEqualTo(4300); BgReading lastBgReading = BgReading.last(); assertThat(lastBgReading.raw_data).isEqualTo(150.0d); assertThat(lastBgReading.filtered_data).isEqualTo(150.0d); }
@Test public void processNewTransmitterData_NormalBg_SomeBattery() { String message = "150000 3900"; Sensor mockSensor = createMockSensor(); addMockBgReading(150, 15, mockSensor); addMockBgReading(150, 10, mockSensor); addMockBgReading(150, 5, mockSensor); Calibration.initialCalibration(150, 150, RuntimeEnvironment.application.getApplicationContext()); byte[] bytes = blueReader.decodeblueReaderPacket(message.getBytes(), message.length()); assertThat(bytes).isNull(); assertThat(PersistentStore.getLong("blueReader_Full_Battery")).isEqualTo(4100); assertThat(PersistentStore.getString("bridge_battery_days")).isEqualTo("5.5"); assertThat(Pref.getInt("bridge_battery", -1)).isEqualTo(75); assertThat(getLogs()).contains("BgReading.create: new BG reading at "); assertThat(Sensor.currentSensor().latest_battery_level).isEqualTo(75); TransmitterData lastTransmitterData = DexCollectionService.last_transmitter_Data; assertThat(lastTransmitterData.filtered_data).isEqualTo(150000.0); assertThat(lastTransmitterData.raw_data).isEqualTo(150000.0); assertThat(lastTransmitterData.sensor_battery_level).isEqualTo(3900); BgReading lastBgReading = BgReading.last(); assertThat(lastBgReading.raw_data).isEqualTo(150.0d); assertThat(lastBgReading.filtered_data).isEqualTo(150.0d); }
@Test public void processNewTransmitterData_NormalBg_LowBattery() { Pref.setBoolean("blueReader_turn_off", false); String message = "150000 3300"; Sensor mockSensor = createMockSensor(); addMockBgReading(150, 15, mockSensor); addMockBgReading(150, 10, mockSensor); addMockBgReading(150, 5, mockSensor); Calibration.initialCalibration(150, 150, RuntimeEnvironment.application.getApplicationContext()); byte[] bytes = blueReader.decodeblueReaderPacket(message.getBytes(), message.length()); assertThat(bytes).isNull(); assertThat(PersistentStore.getLong("blueReader_Full_Battery")).isEqualTo(4100); assertThat(PersistentStore.getString("bridge_battery_days")).isEqualTo("0.1"); assertThat(Pref.getInt("bridge_battery", -1)).isEqualTo(0); assertThat(getLogs()).contains("BgReading.create: new BG reading at "); assertThat(Sensor.currentSensor().latest_battery_level).isEqualTo(0); TransmitterData lastTransmitterData = DexCollectionService.last_transmitter_Data; assertThat(lastTransmitterData.filtered_data).isEqualTo(150000.0); assertThat(lastTransmitterData.raw_data).isEqualTo(150000.0); assertThat(lastTransmitterData.sensor_battery_level).isEqualTo(3300); BgReading lastBgReading = BgReading.last(); assertThat(lastBgReading.raw_data).isEqualTo(150.0d); assertThat(lastBgReading.filtered_data).isEqualTo(150.0d); }
@Test public void processNewTransmitterData_NormalBg_LowBatteryShutdown() { Pref.setBoolean("blueReader_turn_off", true); String message = "150000 3300"; Sensor mockSensor = createMockSensor(); addMockBgReading(150, 15, mockSensor); addMockBgReading(150, 10, mockSensor); addMockBgReading(150, 5, mockSensor); Calibration.initialCalibration(150, 150, RuntimeEnvironment.application.getApplicationContext()); byte[] reply = blueReader.decodeblueReaderPacket(message.getBytes(), message.length()); assertThat(new String(reply)).isEqualTo("k"); }
@Test public void processNewTransmitterData_NormalBg_BatteryLifeTest() { Sensor mockSensor = createMockSensor(); addMockBgReading(150, 15, mockSensor); addMockBgReading(150, 10, mockSensor); addMockBgReading(150, 5, mockSensor); Calibration.initialCalibration(150, 150, RuntimeEnvironment.application.getApplicationContext()); int bgLevelBase = 150000; int minBatteryLevel = 3300; int maxBatteryLevel = 4500; int batteryRange = maxBatteryLevel - minBatteryLevel; for (int batteryLevel = maxBatteryLevel; batteryLevel >= minBatteryLevel; batteryLevel -= 50) { String message = (bgLevelBase + batteryLevel) + " " + batteryLevel; blueReader.decodeblueReaderPacket(message.getBytes(), message.length()); assertWithMessage("batteryLevel: " + batteryLevel) .that(PersistentStore.getLong("blueReader_Full_Battery")) .isEqualTo(maxBatteryLevel); double days = 6.13d / (1d + Math.pow(batteryLevel / 3764d, -61d)); if (batteryLevel < 3600) { days += 0.1; } String compare = BigDecimal.valueOf(days) .setScale(1, RoundingMode.HALF_UP) .toString(); assertWithMessage("batteryLevel: " + batteryLevel) .that(PersistentStore.getString("bridge_battery_days")) .isEqualTo(compare); int batteryPercentage = ((batteryLevel - minBatteryLevel) * 100 / batteryRange); assertWithMessage("batteryLevel: " + batteryLevel) .that(Sensor.currentSensor().latest_battery_level) .isEqualTo(batteryPercentage); } }