Beispiel #1
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);
    }
  }