private boolean hasSubmittedToday(String concept) {
   String result = null;
   String obsResult = null;
   try {
     result = ApiAuthRest.getRequestGet("obs?patient=" + USER);
   } catch (Exception e) {
     e.printStackTrace();
   }
   int startPoint = 0;
   int endPoint = 0;
   while ((startPoint = result.indexOf("uuid", startPoint)) != -1) {
     startPoint += 7;
     endPoint = result.indexOf("\"", startPoint);
     UUID = result.substring(startPoint, endPoint);
     try {
       obsResult = ApiAuthRest.getRequestGet("obs/" + UUID);
     } catch (Exception e) {
       e.printStackTrace();
     }
     if (isCorrectConcept(obsResult, concept)) {
       if (syncedToday(obsResult)) return true;
       else return false;
     }
   }
   return false;
 }
  @Override
  protected void onHandleIntent(Intent intent) {

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);

    ApiAuthRest.setURLBase(URLBase);
    ApiAuthRest.setUsername(username);
    ApiAuthRest.setPassword(password);

    /*if (hasSubmittedToday("STEPS"))
        syncGoogleFit(2);
    else
        syncGoogleFit(1);

    if (hasSubmittedToday("CALORIES"))
        syncGoogleFit(4);
    else
        syncGoogleFit(3);*/
    syncGoogleFit(1);
    syncGoogleFit(3);
  }
  private void syncGoogleFit(int type) {
    String JSON;
    StringEntity input = null;
    switch (type) {
      case 1:
        JSON =
            "{\"obsDatetime\": \""
                + SyncData.getDate()
                + "\""
                + ", \"concept\": \""
                + STEPS
                + "\""
                + ", \"value\": \""
                + SyncData.getStep()
                + "\""
                + ", \"person\": \""
                + USER
                + "\"}";
        try {
          input = new StringEntity(JSON);
        } catch (UnsupportedEncodingException e) {
          e.printStackTrace();
        }

        input.setContentType("application/json");
        try {
          Log.i("OpenMRS response", "AddGoogleFit = " + ApiAuthRest.getRequestPost("obs", input));
        } catch (Exception e) {
          e.printStackTrace();
        }
        break;
      case 2:
        JSON = "{\"value\" : \"" + SyncData.getStep() + "\"}";
        try {
          input = new StringEntity(JSON);
        } catch (UnsupportedEncodingException e) {
          e.printStackTrace();
        }

        input.setContentType("application/json");
        try {
          Log.i(
              "OpenMRS response",
              "AddGoogleFit = " + ApiAuthRest.getRequestPost("obs/" + UUID, input));
        } catch (Exception e) {
          e.printStackTrace();
        }
        break;
      case 3:
        JSON =
            "{\"obsDatetime\": \""
                + SyncData.getDate()
                + "\""
                + ", \"concept\": \""
                + CALORIES
                + "\""
                + ", \"value\": \""
                + Math.round(SyncData.getCal())
                + "\""
                + ", \"person\": \""
                + USER
                + "\"}";
        try {
          input = new StringEntity(JSON);
        } catch (UnsupportedEncodingException e) {
          e.printStackTrace();
        }

        input.setContentType("application/json");
        try {
          Log.i("OpenMRS response", "AddGoogleFit = " + ApiAuthRest.getRequestPost("obs", input));
        } catch (Exception e) {
          e.printStackTrace();
        }
        break;
      case 4:
        JSON = "{\"value\" : \"" + Math.round(SyncData.getCal()) + "\"}";
        try {
          input = new StringEntity(JSON);
        } catch (UnsupportedEncodingException e) {
          e.printStackTrace();
        }

        input.setContentType("application/json");
        try {
          Log.i(
              "OpenMRS response",
              "AddGoogleFit = " + ApiAuthRest.getRequestPost("obs/" + UUID, input));
        } catch (Exception e) {
          e.printStackTrace();
        }
        break;
    }
  }
Ejemplo n.º 4
0
  public void sendData() {
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);

    ApiAuthRest.setURLBase(Container.URLBase);
    ApiAuthRest.setUsername(Container.username);
    ApiAuthRest.setPassword(Container.password);

    Calendar c = Calendar.getInstance();
    String date =
        c.get(Calendar.YEAR)
            + "-"
            + c.get(Calendar.MONTH)
            + "-"
            + c.get(Calendar.DAY_OF_MONTH)
            + " "
            + c.get(Calendar.HOUR)
            + ":"
            + c.get(Calendar.MINUTE)
            + ":"
            + c.get(Calendar.SECOND);

    final String JSONHeight =
        "{\"obsDatetime\": \""
            + date
            + "\""
            + ", \"concept\": \""
            + Container.height_uuid
            + "\""
            + ", \"value\": \""
            + Container.height
            + "\""
            + ", \"person\": \""
            + Container.user_uuid
            + "\"}";

    final String JSONWeight =
        "{\"obsDatetime\": \""
            + date
            + "\""
            + ", \"concept\": \""
            + Container.weight_uuid
            + "\""
            + ", \"value\": \""
            + Container.weight
            + "\""
            + ", \"person\": \""
            + Container.user_uuid
            + "\"}";

    StringEntity heightV = null;
    StringEntity weightV = null;
    try {
      heightV = new StringEntity(JSONHeight);
      weightV = new StringEntity(JSONWeight);
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    }

    heightV.setContentType("application/json");
    weightV.setContentType("application/json");
    try {
      Log.i("OpenMRS response", "AddCalories = " + ApiAuthRest.getRequestPost("obs", heightV));
      Log.i("OpenMRS response", "AddCalories = " + ApiAuthRest.getRequestPost("obs", weightV));
    } catch (Exception e) {
      e.printStackTrace();
    }
  }