X-Git-Url: http://git.wpitchoune.net/gitweb/?p=psensor.git;a=blobdiff_plain;f=src%2Flib%2Fpsensor_json.c;h=ca12fc262dff04216f04ce78d0f35d4f41b5454b;hp=129e24cf9925f7be545b3d801e9dc66653234044;hb=dd9cbee87e002a9954d5f9dd743311cccd4cedef;hpb=9bffa7f1d797d8d4dfd3d5176fe1066d5e4ffd4c diff --git a/src/lib/psensor_json.c b/src/lib/psensor_json.c index 129e24c..ca12fc2 100644 --- a/src/lib/psensor_json.c +++ b/src/lib/psensor_json.c @@ -1,25 +1,28 @@ /* - Copyright (C) 2010-2011 jeanfi@gmail.com - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA - 02110-1301 USA -*/ - + * Copyright (C) 2010-2014 jeanfi@gmail.com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ +#include #include +#include + #include "psensor_json.h" +#include "url.h" #define ATT_SENSOR_ID "id" #define ATT_SENSOR_NAME "name" @@ -38,7 +41,7 @@ measure_to_json_object(struct measure *m) json_object_object_add(o, ATT_MEASURE_VALUE, - json_object_new_double(m->value.d_num)); + json_object_new_double(m->value)); json_object_object_add(o, ATT_MEASURE_TIME, json_object_new_int((m->time).tv_sec)); return o; @@ -77,9 +80,11 @@ static json_object *sensor_to_json(struct psensor *s) json_object_object_add(obj, ATT_SENSOR_TYPE, json_object_new_int(s->type)); json_object_object_add(obj, - ATT_SENSOR_MIN, json_object_new_double(s->min)); + ATT_SENSOR_MIN, + json_object_new_double(s->sess_lowest)); json_object_object_add(obj, - ATT_SENSOR_MAX, json_object_new_double(s->max)); + ATT_SENSOR_MAX, + json_object_new_double(s->sess_highest)); json_object_object_add(obj, ATT_SENSOR_MEASURES, measures_to_json_object(s)); @@ -88,7 +93,7 @@ static json_object *sensor_to_json(struct psensor *s) mo = json_object_new_object(); json_object_object_add(mo, ATT_MEASURE_VALUE, - json_object_new_double(m->value.d_num)); + json_object_new_double(m->value)); json_object_object_add(mo, ATT_MEASURE_TIME, json_object_new_int((m->time).tv_sec)); json_object_object_add(obj, ATT_SENSOR_LAST_MEASURE, mo); @@ -114,14 +119,16 @@ char *sensors_to_json_string(struct psensor **sensors) char *str; json_object *obj = json_object_new_array(); - sensors_cur = sensors; + if (sensors) { + sensors_cur = sensors; - while (*sensors_cur) { - struct psensor *s = *sensors_cur; + while (*sensors_cur) { + struct psensor *s = *sensors_cur; - json_object_array_add(obj, sensor_to_json(s)); + json_object_array_add(obj, sensor_to_json(s)); - sensors_cur++; + sensors_cur++; + } } str = strdup(json_object_to_json_string(obj)); @@ -131,3 +138,31 @@ char *sensors_to_json_string(struct psensor **sensors) return str; } +struct psensor *psensor_new_from_json(json_object *o, + const char *sensors_url, + int values_max_length) +{ + json_object *oid, *oname, *otype; + struct psensor *s; + char *eid, *url; + + json_object_object_get_ex(o, "id", &oid); + json_object_object_get_ex(o, "name", &oname); + json_object_object_get_ex(o, "type", &otype); + + eid = url_encode(json_object_get_string(oid)); + url = malloc(strlen(sensors_url) + 1 + strlen(eid) + 1); + sprintf(url, "%s/%s", sensors_url, eid); + + s = psensor_create(strdup(url), + strdup(json_object_get_string(oname)), + NULL, + json_object_get_int(otype) | SENSOR_TYPE_REMOTE, + values_max_length); + s->provider_data = url; + + free(eid); + + return s; +} +