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
22 #include "psensor_json.h"
25 #define ATT_SENSOR_ID "id"
26 #define ATT_SENSOR_NAME "name"
27 #define ATT_SENSOR_TYPE "type"
28 #define ATT_SENSOR_MIN "min"
29 #define ATT_SENSOR_MAX "max"
30 #define ATT_SENSOR_LAST_MEASURE "last_measure"
31 #define ATT_SENSOR_MEASURES "measures"
32 #define ATT_MEASURE_VALUE "value"
33 #define ATT_MEASURE_TIME "time"
36 measure_to_json_object(struct measure *m)
38 json_object *o = json_object_new_object();
40 json_object_object_add(o,
42 json_object_new_double(m->value));
43 json_object_object_add(o, ATT_MEASURE_TIME,
44 json_object_new_int((m->time).tv_sec));
49 measures_to_json_object(struct psensor *s)
54 o = json_object_new_array();
56 for (i = 0; i < s->values_max_length; i++)
57 if (s->measures[i].time.tv_sec)
59 (o, measure_to_json_object(&s->measures[i]));
65 static json_object *sensor_to_json(struct psensor *s)
67 json_object *mo, *obj;
70 obj = json_object_new_object();
72 json_object_object_add(obj,
74 json_object_new_string(s->id));
75 json_object_object_add(obj,
77 json_object_new_string(s->name));
78 json_object_object_add(obj,
79 ATT_SENSOR_TYPE, json_object_new_int(s->type));
80 json_object_object_add(obj,
81 ATT_SENSOR_MIN, json_object_new_double(s->min));
82 json_object_object_add(obj,
83 ATT_SENSOR_MAX, json_object_new_double(s->max));
84 json_object_object_add(obj,
86 measures_to_json_object(s));
88 m = psensor_get_current_measure(s);
89 mo = json_object_new_object();
90 json_object_object_add(mo,
92 json_object_new_double(m->value));
93 json_object_object_add(mo, ATT_MEASURE_TIME,
94 json_object_new_int((m->time).tv_sec));
95 json_object_object_add(obj, ATT_SENSOR_LAST_MEASURE, mo);
100 char *sensor_to_json_string(struct psensor *s)
103 json_object *obj = sensor_to_json(s);
105 str = strdup(json_object_to_json_string(obj));
107 json_object_put(obj);
112 char *sensors_to_json_string(struct psensor **sensors)
114 struct psensor **sensors_cur;
116 json_object *obj = json_object_new_array();
118 sensors_cur = sensors;
120 while (*sensors_cur) {
121 struct psensor *s = *sensors_cur;
123 json_object_array_add(obj, sensor_to_json(s));
128 str = strdup(json_object_to_json_string(obj));
130 json_object_put(obj);
135 struct psensor *psensor_new_from_json(json_object *o,
136 const char *sensors_url,
137 int values_max_length)
139 json_object *oid, *oname, *otype;
143 json_object_object_get_ex(o, "id", &oid);
144 json_object_object_get_ex(o, "name", &oname);
145 json_object_object_get_ex(o, "type", &otype);
147 eid = url_encode(json_object_get_string(oid));
148 url = malloc(strlen(sensors_url) + 1 + strlen(eid) + 1);
149 sprintf(url, "%s/%s", sensors_url, eid);
151 s = psensor_create(strdup(url),
152 strdup(json_object_get_string(oname)),
154 json_object_get_int(otype) | SENSOR_TYPE_REMOTE,