示例#1
0
 public static ShimDataResponse empty(String shimKey) {
   ShimDataResponse response = new ShimDataResponse();
   response.setShim(shimKey);
   response.setBody(null);
   response.setTimeStamp(Calendar.getInstance().getTimeInMillis() / 1000);
   return response;
 }
示例#2
0
 public static ShimDataResponse result(String shimKey, Object object) {
   ShimDataResponse response = new ShimDataResponse();
   response.setShim(shimKey);
   response.setBody(object);
   response.setTimeStamp(Calendar.getInstance().getTimeInMillis() / 1000);
   return response;
 }
示例#3
0
  protected ResponseEntity<ShimDataResponse> getData(
      OAuth2RestOperations restTemplate, ShimDataRequest shimDataRequest) throws ShimException {

    final JawboneDataTypes jawboneDataType;
    try {
      jawboneDataType =
          JawboneDataTypes.valueOf(shimDataRequest.getDataTypeKey().trim().toUpperCase());
    } catch (NullPointerException | IllegalArgumentException e) {
      throw new ShimException(
          "Null or Invalid data type parameter: "
              + shimDataRequest.getDataTypeKey()
              + " in shimDataRequest, cannot retrieve data.");
    }

    // FIXME this needs to get changed or documented
    long numToReturn = 100;
    if (shimDataRequest.getNumToReturn() != null) {
      numToReturn = shimDataRequest.getNumToReturn();
    }

    OffsetDateTime today = OffsetDateTime.now();

    OffsetDateTime startDateTime =
        shimDataRequest.getStartDateTime() == null
            ? today.minusDays(1)
            : shimDataRequest.getStartDateTime();
    long startTimeInEpochSecond = startDateTime.toEpochSecond();

    OffsetDateTime endDateTime =
        shimDataRequest.getEndDateTime() == null
            ? today.plusDays(1)
            : shimDataRequest.getEndDateTime();
    long endTimeInEpochSecond = endDateTime.toEpochSecond();

    UriComponentsBuilder uriComponentsBuilder =
        UriComponentsBuilder.fromUriString(DATA_URL)
            .path(jawboneDataType.getEndPoint())
            .queryParam("start_time", startTimeInEpochSecond)
            .queryParam("end_time", endTimeInEpochSecond)
            .queryParam("limit", numToReturn);

    ResponseEntity<JsonNode> responseEntity;
    try {
      responseEntity =
          restTemplate.getForEntity(uriComponentsBuilder.build().encode().toUri(), JsonNode.class);
    } catch (HttpClientErrorException | HttpServerErrorException e) {
      // FIXME figure out how to handle this
      logger.error("A request for Jawbone data failed.", e);
      throw e;
    }

    if (shimDataRequest.getNormalize()) {

      JawboneDataPointMapper mapper;
      switch (jawboneDataType) {
        case WEIGHT:
          mapper = new JawboneBodyWeightDataPointMapper();
          break;
        case STEPS:
          mapper = new JawboneStepCountDataPointMapper();
          break;
        case BODY_MASS_INDEX:
          mapper = new JawboneBodyMassIndexDataPointMapper();
          break;
        case ACTIVITY:
          mapper = new JawbonePhysicalActivityDataPointMapper();
          break;
        case SLEEP:
          mapper = new JawboneSleepDurationDataPointMapper();
          break;
        case HEART_RATE:
          mapper = new JawboneHeartRateDataPointMapper();
          break;
        default:
          throw new UnsupportedOperationException();
      }

      return ResponseEntity.ok()
          .body(
              ShimDataResponse.result(
                  JawboneShim.SHIM_KEY,
                  mapper.asDataPoints(singletonList(responseEntity.getBody()))));

    } else {

      return ResponseEntity.ok()
          .body(ShimDataResponse.result(JawboneShim.SHIM_KEY, responseEntity.getBody()));
    }
  }