private boolean backFillIfNeeded(final AnnexARx annex) { return backFillIfNeeded(annex, 0); }
private void onConnectionFailure(Throwable throwable) { if (listen_connected) { status("Disconnected"); } else { status("Connection failure"); } // TODO under what circumstances should we change state or do something here? UserError.Log.d(TAG, "Connection Disconnected/Failed: " + throwable); stopConnect(); changeState(CLOSE); setRetryTimer(); }
private void connect_to_device() { if (JoH.quietratelimit("medtrum-connect-cooldown", 2)) { status("Connecting"); UserError.Log.d(TAG, "Connecting to device: " + address); connect_to_device(false); } }
private void onConnectionReceived(final RxBleConnection this_connection) { listen_connected = true; status("Connected"); // TODO close off existing connection? connection = this_connection; if (this_connection != null) { changeState(ENABLE); } else { UserError.Log.d(TAG, "New connection null!"); changeState(CLOSE); } }
private synchronized void onConnectionStateChange(RxBleConnection.RxBleConnectionState newState) { String connection_state = "Unknown"; switch (newState) { case CONNECTING: connection_state = "Connecting"; // connecting_time = JoH.tsl(); break; case CONNECTED: connection_state = "Connected"; retry_backoff = 0; break; case DISCONNECTING: connection_state = "Disconnecting"; break; case DISCONNECTED: connection_state = "Disconnected"; status("Disconnected"); changeState(CLOSE); break; } status(connection_state); if (connection_state.equals("Disconnecting")) { tryGattRefresh(connection); } }
initialize(); break; case SCAN: scan_for_device(); break; case CONNECT: connect_to_device(); break; case ENABLE: retry_backoff = 0; // we have a connection enable_features_and_listen(); break; case DISCOVER: changeState(state.next()); break; case CALIBRATE: check_calibrate(); break; case GET_DATA: get_data(); break; case SET_TIME: if (JoH.pratelimit("medtrum-set-time-" + serial, 60)) { sendTx(new TimeTx()); } else { changeState(state.next()); sendTx(new ConnParamTx()); break;
@Override public int onStartCommand(Intent intent, int flags, int startId) { xdrip.checkAppContext(getApplicationContext()); if (shouldServiceRun()) { final PowerManager.WakeLock wl = JoH.getWakeLock("medtrum-start-service", 600000); try { UserError.Log.d(TAG, "WAKE UP WAKE UP WAKE UP WAKE UP @ " + JoH.dateTimeText(JoH.tsl()) + " State: " + state); setFailOverTimer(); processInitialState(); background_automata(); } else { UserError.Log.d(TAG, "Should not be running so shutting down"); stopSelf(); return START_NOT_STICKY;
private synchronized void enable_features_and_listen() { UserError.Log.d(TAG, "enable features - enter"); stopListening(); if (connection != null) { notificationSubscription = connection.setupNotification(Const.CGM_CHARACTERISTIC_NOTIFY) UserError.Log.d(TAG, "Received notification bytes: " + JoH.bytesToHex(bytes)); lastInteractionTime = JoH.tsl(); setFailOverTimer(); lastAnnex = new AnnexARx(bytes); UserError.Log.d(TAG, "Notification: " + lastAnnex.toS()); createRecordFromAnnexData(lastAnnex); backFillIfNeeded(lastAnnex); sendTx(new AuthTx(serial)); if (!checkAndProcessInboundStream(inboundStream)) { Inevitable.task("mt-reset-stream-no-data", 3000, () -> { if (inboundStream.hasSomeData()) {
final AuthRx authrx = new AuthRx(packet); if (authrx.isValid()) { status("Authenticated"); } else { errorStatus("AUTHENTICATION FAILED!"); if (JoH.ratelimit("medtrum-auth-fail", 600)) { UserError.Log.wtf(TAG, "Auth packet failure: " + serial + authrx.toS()); changeState(state.next()); break; createRecordFromAnnexData(statusrx.getAnnex()); asking_backfill = backFillIfNeeded(statusrx.getAnnex()); changeState(state.next(), asking_backfill ? 1500 : DEFAULT_AUTOMATA_DELAY); break; final String msg = "Got time set reply: " + (timeRx.isValid() ? "VALID" : "INVALID"); if (timeRx.isValid()) { status("Set time"); UserError.Log.d(TAG, msg); } else { status("Error setting time"); UserError.Log.e(TAG, msg); changeState(SET_CONN_PARAM); break; UserError.Log.d(TAG, "Got connection parameter reply");
private void check_calibrate() { final Pair<Long, Integer> calibration = Medtrum.getCalibration(); if (calibration != null) { status("Calibrating"); try { sendTx(new CalibrateTx(serial, calibration.first, calibration.second)); } catch (InvalidAlgorithmParameterException e) { UserError.Log.wtf(TAG, "Cannot calibrate: " + e); } } else { changeState(state.next()); } }
status("Connecting"); stopConnect(); changeState(SCAN);
if (BgReading.getForPreciseTimestamp(timestamp, Constants.MINUTE_IN_MS * 2.5) == null) { if (isNative()) { if (JoH.ratelimit("mt-backfill-repeat", 60)) { UserError.Log.d(TAG, "Requesting additional backfill with offset: " + backsies.size()); backFillIfNeeded(lastAnnex, backsies.size());
public void changeState(STATE new_state) { changeState(new_state, DEFAULT_AUTOMATA_DELAY); }
@Override public void onCreate() { super.onCreate(); if (scanner == null) { scanner = new Scanner(); } scanner.setTag(TAG); scanner.addCallBack(this, TAG); DisconnectReceiver.addCallBack(this, TAG); UserError.Log.d(TAG, "SERVICE CREATED - SERVICE CREATED"); enableBuggySamsungIfNeeded(TAG); }
public void changeState(STATE new_state, int timeout) { if ((state == CLOSED || state == CLOSE) && new_state == CLOSE) { UserError.Log.d(TAG, "Not closing as already closed"); } else { if (new_state != state) { UserError.Log.d(TAG, "Changing state from: " + state + " to " + new_state); state = new_state; background_automata(timeout); } else { UserError.Log.d(TAG, "Not changing state as already in state: " + new_state); } } }
break; case "test medtrum calibrate": MedtrumCollectionService.calibratePing(); break; case "delete all desert sync data":
initialize(); break; case SCAN: scan_for_device(); break; case CONNECT: connect_to_device(); break; case ENABLE: retry_backoff = 0; // we have a connection enable_features_and_listen(); break; case DISCOVER: changeState(state.next()); break; case CALIBRATE: check_calibrate(); break; case GET_DATA: get_data(); break; case SET_TIME: if (JoH.pratelimit("medtrum-set-time-" + serial, 60)) { sendTx(new TimeTx()); } else { changeState(state.next()); sendTx(new ConnParamTx()); break;
@Override public int onStartCommand(Intent intent, int flags, int startId) { xdrip.checkAppContext(getApplicationContext()); if (shouldServiceRun()) { final PowerManager.WakeLock wl = JoH.getWakeLock("medtrum-start-service", 600000); try { UserError.Log.d(TAG, "WAKE UP WAKE UP WAKE UP WAKE UP @ " + JoH.dateTimeText(JoH.tsl()) + " State: " + state); setFailOverTimer(); processInitialState(); background_automata(); } else { UserError.Log.d(TAG, "Should not be running so shutting down"); stopSelf(); return START_NOT_STICKY;
private synchronized void enable_features_and_listen() { UserError.Log.d(TAG, "enable features - enter"); stopListening(); if (connection != null) { notificationSubscription = connection.setupNotification(Const.CGM_CHARACTERISTIC_NOTIFY) UserError.Log.d(TAG, "Received notification bytes: " + JoH.bytesToHex(bytes)); lastInteractionTime = JoH.tsl(); setFailOverTimer(); lastAnnex = new AnnexARx(bytes); UserError.Log.d(TAG, "Notification: " + lastAnnex.toS()); createRecordFromAnnexData(lastAnnex); backFillIfNeeded(lastAnnex); sendTx(new AuthTx(serial)); if (!checkAndProcessInboundStream(inboundStream)) { Inevitable.task("mt-reset-stream-no-data", 3000, () -> { if (inboundStream.hasSomeData()) {
final AuthRx authrx = new AuthRx(packet); if (authrx.isValid()) { status("Authenticated"); } else { errorStatus("AUTHENTICATION FAILED!"); if (JoH.ratelimit("medtrum-auth-fail", 600)) { UserError.Log.wtf(TAG, "Auth packet failure: " + serial + authrx.toS()); changeState(state.next()); break; createRecordFromAnnexData(statusrx.getAnnex()); asking_backfill = backFillIfNeeded(statusrx.getAnnex()); changeState(state.next(), asking_backfill ? 1500 : DEFAULT_AUTOMATA_DELAY); break; final String msg = "Got time set reply: " + (timeRx.isValid() ? "VALID" : "INVALID"); if (timeRx.isValid()) { status("Set time"); UserError.Log.d(TAG, msg); } else { status("Error setting time"); UserError.Log.e(TAG, msg); changeState(SET_CONN_PARAM); break; UserError.Log.d(TAG, "Got connection parameter reply");