public static void setSuggested_Temp_Basal(JSONObject openAPSSuggest, Context c) {
      try {
        Suggested_Temp_Basal = new TempBasal();
        Notifications.clear(MainActivity.activity); // Clears any open notifications
        if (openAPSSuggest.has("rate")) { // Temp Basal suggested
          Suggested_Temp_Basal.rate = openAPSSuggest.getDouble("rate");
          Suggested_Temp_Basal.ratePercent = openAPSSuggest.getInt("ratePercent");
          Suggested_Temp_Basal.duration = openAPSSuggest.getInt("duration");
          Suggested_Temp_Basal.basal_type = openAPSSuggest.getString("temp");
          Suggested_Temp_Basal.basal_adjustemnt = openAPSSuggest.getString("basal_adjustemnt");

          if (openAPSSuggest
              .getString("openaps_mode")
              .equals("closed")) { // OpenAPS mode is closed, send command direct to pump
            pumpAction.setTempBasal(
                openAPSFragment.getSuggested_Temp_Basal(), MainActivity.activity);
          } else { // Make notification (Wear & Phone)
            Notifications.newTemp(openAPSSuggest, c);
          }
        }
      } catch (Exception e) {
        Crashlytics.logException(e);
        Toast.makeText(
                MainActivity.activity, "Crash in setSuggested_Temp_Basal", Toast.LENGTH_SHORT)
            .show();
      }
      currentOpenAPSSuggest = openAPSSuggest;
      update();
    }
 public static String age() {
   if (Suggested_Temp_Basal.age() > 1) {
     return Suggested_Temp_Basal.age() + " mins ago";
   } else {
     return Suggested_Temp_Basal.age() + " min ago";
   }
 }
Exemple #3
0
  public static void uploadTempBasals(Context c, SharedPreferences prefs) {
    // Will grab the last 20 suggested TempBasals and check they have all been uploaded to NS

    List<TempBasal> tempBasals = TempBasal.latestTempBasals(20);
    JSONArray tempBasalsJSONArray = new JSONArray();
    String url = prefs.getString("nightscout_url", "") + "/treatments/";
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mmZ");
    String dateAsISO8601;

    for (TempBasal tempBasal : tempBasals) {
      try {

        JSONObject tempBasalJSON = new JSONObject();
        JSONObject tempBasalIntegration = tools.getJSONO(tempBasal.integration);

        if (!tempBasalIntegration.has("ns_upload_id")) {

          tempBasalJSON.put("happ_id", tempBasal.getId());
          tempBasalJSON.put("enterdBy", "HAPP_APP");
          dateAsISO8601 = df.format(tempBasal.start_time);
          tempBasalJSON.put("created_at", dateAsISO8601);
          tempBasalJSON.put("eventType", "Temp Basal");
          tempBasalJSON.put("duration", tempBasal.duration);

          // if (tempBasal.basal_type.equals("percent")) {                             //Percent is
          // not supported in NS as expected
          //    tempBasalJSON.put("percent", tempBasal.ratePercent);                  //Basal 1U /
          // Hour
          // } else {                                                                  //NS = 50%
          // means * 1.5 ~~ HAPP 50% means * 0.5
          tempBasalJSON.put("absolute", tempBasal.rate);
          // }

          tempBasalsJSONArray.put(tempBasalJSON);
        } else if (tempBasalIntegration.has("ns_temp_basal_stop")) {
          // This Temp Basal has been stopped, insert a record to NS so it knows
          if (tempBasalIntegration.getString("ns_temp_basal_stop").equals("dirty")) {
            tempBasalJSON.put("happ_id", tempBasal.getId());
            tempBasalJSON.put("enterdBy", "HAPP_APP");
            dateAsISO8601 = df.format(tempBasal.endDate());
            tempBasalJSON.put("created_at", dateAsISO8601);
            tempBasalJSON.put("eventType", "Temp Basal");
            // tempBasalJSON.put("happ_note", "temp_basal_stop");

            tempBasalsJSONArray.put(tempBasalJSON);
          }
        }

      } catch (JSONException | NullPointerException e) {
        Crashlytics.logException(e);
      }
    }

    if (tempBasalsJSONArray.length() > 0) {
      jsonPost(tempBasalsJSONArray, url, c);
    }
  }
  public void displayCurrentInfo() {
    final TextView currentBgValueText = (TextView) findViewById(R.id.currentBgValueRealTime);
    final TextView notificationText = (TextView) findViewById(R.id.notices);
    final TextView deltaText = (TextView) findViewById(R.id.bgDelta);
    if ((currentBgValueText.getPaintFlags() & Paint.STRIKE_THRU_TEXT_FLAG) > 0) {
      currentBgValueText.setPaintFlags(
          currentBgValueText.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));
    }
    Bg lastBgreading = Bg.last();

    if (lastBgreading != null) {
      notificationText.setText(lastBgreading.readingAge());
      String bgDelta = new String(String.format(Locale.ENGLISH, "%.2f", lastBgreading.bgdelta));
      if (lastBgreading.bgdelta >= 0) bgDelta = "+" + bgDelta;
      deltaText.setText(bgDelta);
      currentBgValueText.setText(
          extendedGraphBuilder.unitized_string(lastBgreading.sgv_double())
              + " "
              + lastBgreading.slopeArrow());

      if ((new Date().getTime()) - (60000 * 16) - lastBgreading.datetime > 0) {
        notificationText.setTextColor(Color.parseColor("#C30909"));
        currentBgValueText.setPaintFlags(
            currentBgValueText.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
      } else {
        notificationText.setTextColor(Color.WHITE);
      }
      double estimate = lastBgreading.sgv_double();
      if (extendedGraphBuilder.unitized(estimate) <= extendedGraphBuilder.lowMark) {
        currentBgValueText.setTextColor(Color.parseColor("#C30909"));
      } else if (extendedGraphBuilder.unitized(estimate) >= extendedGraphBuilder.highMark) {
        currentBgValueText.setTextColor(Color.parseColor("#FFBB33"));
      } else {
        currentBgValueText.setTextColor(Color.WHITE);
      }
    }

    // Stats age update
    statsAge = (TextView) findViewById(R.id.statsAge);
    Stats lastRun = Stats.last();
    if (lastRun != null) statsAge.setText(lastRun.statAge());

    // OpenAPS age update
    openAPSAgeTextView = (TextView) findViewById(R.id.openapsAge);
    openAPSAgeTextView.setText(openAPSFragment.age());

    // Temp Basal running update
    Date timeNow = new Date();
    sysMsg = (TextView) findViewById(R.id.sysmsg);
    TempBasal lastTempBasal = TempBasal.last();
    String appStatus;
    if (lastTempBasal.isactive(null)) { // Active temp Basal
      appStatus =
          lastTempBasal.basal_adjustemnt
              + " Temp active: "
              + lastTempBasal.rate
              + "U("
              + lastTempBasal.ratePercent
              + "%) "
              + lastTempBasal.durationLeft()
              + "mins left";
    } else { // No temp Basal running, show default
      Double currentBasal = Profile.ProfileAsOf(timeNow, this.getBaseContext()).current_basal;
      appStatus = "No temp basal, current basal: " + currentBasal + "U";
    }
    sysMsg.setText(appStatus);
  }