int M2XStreamClient::readLocation(location_read_callback callback,
                                  void* context) {
  const int BUF_LEN = 40;
  char buf[BUF_LEN];

  int length = readContentLength();
  if (length < 0) {
    close();
    return length;
  }

  int index = skipHttpHeader();
  if (index != E_OK) {
    close();
    return index;
  }
  index = 0;

  location_parsing_context_state state;
  state.state = state.index = 0;
  state.callback = callback;
  state.context = context;

  jsonlite_parser_callbacks cbs = jsonlite_default_callbacks;
  cbs.key_found = on_location_key_found;
  cbs.string_found = on_location_string_found;
  cbs.context.client_state = &state;

  jsonlite_parser p = jsonlite_parser_init(jsonlite_parser_estimate_size(5));
  jsonlite_parser_set_callback(p, &cbs);

  jsonlite_result result = jsonlite_result_unknown;
  while (index < length) {
    int i = 0;

#ifdef DEBUG
    printf("Received Data: ");
#endif
    while ((i < BUF_LEN) && _client->available()) {
      buf[i++] = _client->read();
#ifdef DEBUG
      printf("%c", buf[i - 1]);
#endif
    }
#ifdef DEBUG
    printf("\n");
#endif

    if ((!_client->connected()) &&
        (!_client->available()) &&
        ((index + i) < length)) {
      jsonlite_parser_release(p);
      close();
      return E_NOCONNECTION;
    }

    result = jsonlite_parser_tokenize(p, buf, i);
    if ((result != jsonlite_result_ok) &&
        (result != jsonlite_result_end_of_stream)) {
      jsonlite_parser_release(p);
      close();
      return E_JSON_INVALID;
    }

    index += i;
  }

  jsonlite_parser_release(p);
  close();
  return (result == jsonlite_result_ok) ? (E_OK) : (E_JSON_INVALID);
}
Example #2
0
int main()
{
    setbuf(stdout, NULL); // no buffering for this filehandle
    char json[1024];

    int content_length;
    int response_code;
    char buf[1024];

    float amb_temp;

    setupEthernet();

    // set time
    lcd.printf("Reading Time... ");
    ntp.setTime("time-c.nist.gov");
    lcd.printf("Time set");


    while(1) {  // Loop
        char streamName[] = "amb-temp";  // Set value for stream you are using
     
        // GET Stream value
        response_code = getStream(streamName, json);
        content_length = readContentLength(json);
        pc.printf("GET Stream\r\nResponse: %d\r\nContent Length: %d\r\njson: %s\r\n\r\n", response_code, content_length, json);
        Thread::wait(5000);

/////
        // PUT value to Strem
        amb_temp = tmp.read();

        sprintf(json, "{\"value\":\"%0.2f\"}", amb_temp);

        response_code = putStream(streamName, json);
        content_length = readContentLength(json);
        pc.printf("PUT Stream\r\nResponse: %d\r\nContent Length: %d\r\njson: %s\r\n\r\n", response_code, content_length, json);
        Thread::wait(5000);


//////
        // POST value(s) to Stream
        time_t seconds;

        seconds = time(NULL);   // get current time from mbed RTC
        strftime(buf,40, "%Y-%m-%dT%H:%M:%S%z", localtime(&seconds));

        amb_temp = tmp.read();

        sprintf(json, "{ \"values\": [ {\"at\": \"%s\", \"value\":\"%0.2f\"} ] }", buf, amb_temp);

        response_code = postStream(streamName, json);
        content_length = readContentLength(json);

        pc.printf("POST Stream\r\nResponse: %d\r\nContent Length: %d\r\njson: %s\r\n\r\n", response_code, content_length, json);
        Thread::wait(5000);

///////
        // PUT Location to Feed
        sprintf(json, "{ \"name\": \"%s\", \"latitude\": \"%0.8f\", \"longitude\": \"%0.8f\", \"elevation\": \"%0.2f\" }", LOC_NAME, LOC_LAT, LOC_LONG, LOC_ELEV);

        response_code = putLocation(json);
        content_length = readContentLength(json);

        pc.printf("PUT Location\r\nResponse: %d\r\nContent Length: %d\r\njson: %s\r\n\r\n", response_code, content_length, json);
        Thread::wait(5000);

///////
        // GET Location of Feed
        response_code = getLocation(json);
        content_length = readContentLength(json);

        pc.printf("GET Location\r\nResponse: %d\r\nContent Length: %d\r\njson: %s\r\n\r\n", response_code, content_length, json);
        Thread::wait(5000);

    }

    eth.disconnect();

    while(1) {}
}