public void updateHAPPBolus() {

    List<Treatment> treatments = Treatment.getToUpdateHAPP();
    Log.d("UPDATE HAPP:", treatments.size() + " treatments");

    for (Treatment bolus : treatments) {

      ObjectToSync bolusSync = new ObjectToSync(bolus, null);

      Message msg = Message.obtain();
      Bundle bundle = new Bundle();
      bundle.putString("ACTION", "bolus_delivery");
      bundle.putString("UPDATE", bolusSync.asJSONString());
      msg.setData(bundle);

      try {
        Log.d("UPDATE HAPP:", "HAPP INT ID " + bolusSync.happ_integration_id);
        myService.send(msg);
      } catch (RemoteException e) {
        e.printStackTrace();
        Log.d("UPDATE HAPP:", "Update FAILED for " + bolusSync.happ_integration_id);
      } finally {
        Log.d("UPDATE HAPP:", "Update sent for " + bolusSync.happ_integration_id);
        bolus.happ_update = false;
        bolus.save();
      }
    }

    try {
      if (isBound) IncomingService.this.unbindService(myConnection);
    } catch (IllegalArgumentException e) {
      // catch if service was killed in a unclean way
    }
  }
  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);
    }
  }