2 * Copyright (C) 2010-2014 jeanfi@gmail.com
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 #include "psensor_json.h"
27 #define ATT_SENSOR_ID "id"
28 #define ATT_SENSOR_NAME "name"
29 #define ATT_SENSOR_TYPE "type"
30 #define ATT_SENSOR_MIN "min"
31 #define ATT_SENSOR_MAX "max"
32 #define ATT_SENSOR_LAST_MEASURE "last_measure"
33 #define ATT_SENSOR_MEASURES "measures"
34 #define ATT_MEASURE_VALUE "value"
35 #define ATT_MEASURE_TIME "time"
38 measure_to_json_object(struct measure *m)
40 json_object *o = json_object_new_object();
42 json_object_object_add(o,
44 json_object_new_double(m->value));
45 json_object_object_add(o, ATT_MEASURE_TIME,
46 json_object_new_int((m->time).tv_sec));
51 measures_to_json_object(struct psensor *s)
56 o = json_object_new_array();
58 for (i = 0; i < s->values_max_length; i++)
59 if (s->measures[i].time.tv_sec)
61 (o, measure_to_json_object(&s->measures[i]));
67 static json_object *sensor_to_json(struct psensor *s)
69 json_object *mo, *obj;
72 obj = json_object_new_object();
74 json_object_object_add(obj,
76 json_object_new_string(s->id));
77 json_object_object_add(obj,
79 json_object_new_string(s->name));
80 json_object_object_add(obj,
81 ATT_SENSOR_TYPE, json_object_new_int(s->type));
82 json_object_object_add(obj,
83 ATT_SENSOR_MIN, json_object_new_double(s->min));
84 json_object_object_add(obj,
85 ATT_SENSOR_MAX, json_object_new_double(s->max));
86 json_object_object_add(obj,
88 measures_to_json_object(s));
90 m = psensor_get_current_measure(s);
91 mo = json_object_new_object();
92 json_object_object_add(mo,
94 json_object_new_double(m->value));
95 json_object_object_add(mo, ATT_MEASURE_TIME,
96 json_object_new_int((m->time).tv_sec));
97 json_object_object_add(obj, ATT_SENSOR_LAST_MEASURE, mo);
102 char *sensor_to_json_string(struct psensor *s)
105 json_object *obj = sensor_to_json(s);
107 str = strdup(json_object_to_json_string(obj));
109 json_object_put(obj);
114 char *sensors_to_json_string(struct psensor **sensors)
116 struct psensor **sensors_cur;
118 json_object *obj = json_object_new_array();
120 sensors_cur = sensors;
122 while (*sensors_cur) {
123 struct psensor *s = *sensors_cur;
125 json_object_array_add(obj, sensor_to_json(s));
130 str = strdup(json_object_to_json_string(obj));
132 json_object_put(obj);
137 struct psensor *psensor_new_from_json(json_object *o,
138 const char *sensors_url,
139 int values_max_length)
141 json_object *oid, *oname, *otype;
145 json_object_object_get_ex(o, "id", &oid);
146 json_object_object_get_ex(o, "name", &oname);
147 json_object_object_get_ex(o, "type", &otype);
149 eid = url_encode(json_object_get_string(oid));
150 url = malloc(strlen(sensors_url) + 1 + strlen(eid) + 1);
151 sprintf(url, "%s/%s", sensors_url, eid);
153 s = psensor_create(strdup(url),
154 strdup(json_object_get_string(oname)),
156 json_object_get_int(otype) | SENSOR_TYPE_REMOTE,