json_object * api_json_tuples(Buffer::Ptr buf) { json_object *json_tuples = json_object_new_array(); Buffer::iterator it; print(log_debug, "==> number of tuples: %d", "api", buf->size()); for (it = buf->begin(); it != buf->end(); it++) { struct json_object *json_tuple = json_object_new_array(); buf->lock(); // TODO use long int of new json-c version // API requires milliseconds => * 1000 double timestamp = it->tvtod() * 1000; double value = it->value(); buf->unlock(); json_object_array_add(json_tuple, json_object_new_double(timestamp)); json_object_array_add(json_tuple, json_object_new_double(value)); json_object_array_add(json_tuples, json_tuple); } return json_tuples; }
json_object * vz::api::Volkszaehler::api_json_tuples(Buffer::Ptr buf) { json_object *json_tuples = json_object_new_array(); Buffer::iterator it; print(log_debug, "==> number of tuples: %d", channel()->name(), buf->size()); uint64_t timestamp = 1; // copy all values to local buffer queue buf->lock(); for (it = buf->begin(); it != buf->end(); it++) { timestamp = round(it->tvtod() * 1000); print(log_debug, "compare: %llu %llu %f", channel()->name(), _last_timestamp, timestamp, it->tvtod() * 1000); if (_last_timestamp < timestamp ) { _values.push_back(*it); _last_timestamp = timestamp; } it->mark_delete(); } buf->unlock(); buf->clean(); if (_values.size() < 1 ) { return NULL; } for (it = _values.begin(); it != _values.end(); it++) { struct json_object *json_tuple = json_object_new_array(); // TODO use long int of new json-c version // API requires milliseconds => * 1000 double timestamp = it->tvtod() * 1000; double value = it->value(); json_object_array_add(json_tuple, json_object_new_double(timestamp)); json_object_array_add(json_tuple, json_object_new_double(value)); json_object_array_add(json_tuples, json_tuple); } return json_tuples; }