Merge tag 'upstream/1.0.1'
[psensor-pkg-debian.git] / src / lib / psensor_json.c
1 /*
2  * Copyright (C) 2010-2014 jeanfi@gmail.com
3  *
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.
8  *
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.
13  *
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
17  * 02110-1301 USA
18  */
19 #include <stdlib.h>
20 #include <string.h>
21
22 #include "psensor_json.h"
23 #include "url.h"
24
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"
34
35 static json_object *
36 measure_to_json_object(struct measure *m)
37 {
38         json_object *o = json_object_new_object();
39
40         json_object_object_add(o,
41                                ATT_MEASURE_VALUE,
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));
45         return o;
46 }
47
48 static json_object *
49 measures_to_json_object(struct psensor *s)
50 {
51         json_object *o;
52         int i;
53
54         o = json_object_new_array();
55
56         for (i = 0; i < s->values_max_length; i++)
57                 if (s->measures[i].time.tv_sec)
58                         json_object_array_add
59                                 (o, measure_to_json_object(&s->measures[i]));
60
61
62         return o;
63 }
64
65 static json_object *sensor_to_json(struct psensor *s)
66 {
67         json_object *mo, *obj;
68         struct measure *m;
69
70         obj = json_object_new_object();
71
72         json_object_object_add(obj,
73                                ATT_SENSOR_ID,
74                                json_object_new_string(s->id));
75         json_object_object_add(obj,
76                                ATT_SENSOR_NAME,
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,
85                                ATT_SENSOR_MEASURES,
86                                measures_to_json_object(s));
87
88         m = psensor_get_current_measure(s);
89         mo = json_object_new_object();
90         json_object_object_add(mo,
91                                ATT_MEASURE_VALUE,
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);
96
97         return obj;
98 }
99
100 char *sensor_to_json_string(struct psensor *s)
101 {
102         char *str;
103         json_object *obj = sensor_to_json(s);
104
105         str = strdup(json_object_to_json_string(obj));
106
107         json_object_put(obj);
108
109         return str;
110 }
111
112 char *sensors_to_json_string(struct psensor **sensors)
113 {
114         struct psensor **sensors_cur;
115         char *str;
116         json_object *obj = json_object_new_array();
117
118         sensors_cur = sensors;
119
120         while (*sensors_cur) {
121                 struct psensor *s = *sensors_cur;
122
123                 json_object_array_add(obj, sensor_to_json(s));
124
125                 sensors_cur++;
126         }
127
128         str = strdup(json_object_to_json_string(obj));
129
130         json_object_put(obj);
131
132         return str;
133 }
134
135 struct psensor *psensor_new_from_json(json_object *o,
136                                       const char *sensors_url,
137                                       int values_max_length)
138 {
139         json_object *oid, *oname, *otype;
140         struct psensor *s;
141         char *eid, *url;
142
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);
146
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);
150
151         s = psensor_create(strdup(url),
152                            strdup(json_object_get_string(oname)),
153                            NULL,
154                            json_object_get_int(otype) | SENSOR_TYPE_REMOTE,
155                            values_max_length);
156         s->url = url;
157
158         free(eid);
159
160         return s;
161 }
162