public void actionTreatments() { List<Treatment> treatments = Treatment.getToBeActioned(); // Get all Treatments yet to be processed for (Treatment treatment : treatments) { Long ageInMins = (new Date().getTime() - treatment.date_requested) / 1000 / 60; if (ageInMins > 10) { // Treatment was requested as > 10mins old, its too old to be processed treatment.state = "error"; treatment.details = "Treatment is older than 10mins, too old to be automated"; treatment.delivered = false; treatment.rejected = true; treatment.happ_update = true; treatment.save(); } else { Boolean isPumpOnline = true; // TODO: 16/01/2016 this should be a function to check if the pump is online Boolean isPumpBusy = false; // TODO: 16/01/2016 this should be a function to check if the pump is busy, for // example if delivering a treatment if (!isPumpOnline || isPumpBusy) { // Cannot use pump right now treatment.state = "delayed"; treatment.details = "Pump online: " + isPumpOnline + " | Pump busy: " + isPumpBusy; treatment.delivered = false; treatment.happ_update = true; treatment.save(); } else { // Pump is online and available to accept a treatment // TODO: 16/01/2016 a function should be here to action the treatment treatment.state = "delivered"; treatment.details = "Treatment has been sent to the pump"; treatment.delivered = true; // TODO: 16/01/2016 logic should be set to check that the treatment was // successfully delivered before setting this to true treatment.happ_update = true; treatment.save(); } } } if (treatments.size() > 0) { connect_to_HAPP(); Intent intent = new Intent("UPDATE_TREATMENTS"); // sends result to update UI if loaded MainApp.instance().sendBroadcast(intent); } }
@Override public void handleMessage(Message msg) { String action = ""; Long requested = 0L; String dataString = ""; Bundle data = new Bundle(); Toast.makeText(MainApp.instance(), "START", Toast.LENGTH_LONG).show(); try { data = msg.getData(); action = data.getString("ACTION"); Log.d("RECEIVED: ACTION", action); requested = data.getLong("DATE_REQUESTED", 0); Log.d("RECEIVED: DATE", requested.toString()); dataString = data.getString("DATA"); Log.d("RECEIVED: DATA", dataString); } catch (Exception e) { e.printStackTrace(); // TODO: 16/01/2016 Issue getting treatment details from HAPPs msg } finally { // Toast.makeText(MainApp.instance(), action, Toast.LENGTH_LONG).show(); switch (action) { case "TEST_MSG": Resources appR = MainApp.instance().getResources(); CharSequence txt = appR.getText( appR.getIdentifier("app_name", "string", MainApp.instance().getPackageName())); Toast.makeText( MainApp.instance(), txt + ": HAPP has connected successfully. ", Toast.LENGTH_LONG) .show(); break; case "temp_basal": case "cancel_temp_basal": ObjectToSync basalSync = new Gson().fromJson(dataString, ObjectToSync.class); Basal basal = new Basal(); basal.rate = basalSync.value1; basal.ratePercent = Integer.getInteger(basalSync.value2, 0); basal.duration = Integer.getInteger(basalSync.value3, 0); basal.start_time = basalSync.requested; basal.action = basalSync.action; basal.been_set = false; basal.happ_int_id = basalSync.happ_integration_id; basal.auth_code = basalSync.integrationSecretCode; basal.state = "received"; basal.save(); // We have now saved the requested treatments from HAPP to our local DB, now action them connect_to_HAPP(); actionBasal(); break; case "bolus_delivery": Type type = new TypeToken<List<ObjectToSync>>() {}.getType(); List<ObjectToSync> bolusList = new Gson().fromJson(dataString, type); for (ObjectToSync newBolus : bolusList) { try { Treatment newTreatment = new Treatment(); newTreatment.type = newBolus.value3; newTreatment.date_requested = newBolus.requested.getTime(); newTreatment.value = newBolus.value1; newTreatment.delivered = false; newTreatment.happ_int_id = newBolus.happ_integration_id; newTreatment.auth_code = newBolus.integrationSecretCode; newTreatment.state = "received"; newTreatment.save(); } catch (Exception e) { e.printStackTrace(); // TODO: 16/01/2016 Issue getting treatment details } } // We have now saved the requested treatments from HAPP to our local DB, now action them connect_to_HAPP(); actionTreatments(); break; } } }