Merge tag 'upstream/1.2.0'
[psensor-pkg-debian.git] / src / lib / psensor_json.c
1 /*
2  * Copyright (C) 2010-2016 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 <stdio.h>
23
24 #include "psensor_json.h"
25 #include "url.h"
26
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"
36
37 static json_object *
38 measure_to_json_object(struct measure *m)
39 {
40         json_object *o = json_object_new_object();
41
42         json_object_object_add(o,
43                                ATT_MEASURE_VALUE,
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));
47         return o;
48 }
49
50 static json_object *
51 measures_to_json_object(struct psensor *s)
52 {
53         json_object *o;
54         int i;
55
56         o = json_object_new_array();
57
58         for (i = 0; i < s->values_max_length; i++)
59                 if (s->measures[i].time.tv_sec)
60                         json_object_array_add
61                                 (o, measure_to_json_object(&s->measures[i]));
62
63
64         return o;
65 }
66
67 static json_object *sensor_to_json(struct psensor *s)
68 {
69         json_object *mo, *obj;
70         struct measure *m;
71
72         obj = json_object_new_object();
73
74         json_object_object_add(obj,
75                                ATT_SENSOR_ID,
76                                json_object_new_string(s->id));
77         json_object_object_add(obj,
78                                ATT_SENSOR_NAME,
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,
84                                json_object_new_double(s->sess_lowest));
85         json_object_object_add(obj,
86                                ATT_SENSOR_MAX,
87                                json_object_new_double(s->sess_highest));
88         json_object_object_add(obj,
89                                ATT_SENSOR_MEASURES,
90                                measures_to_json_object(s));
91
92         m = psensor_get_current_measure(s);
93         mo = json_object_new_object();
94         json_object_object_add(mo,
95                                ATT_MEASURE_VALUE,
96                                json_object_new_double(m->value));
97         json_object_object_add(mo, ATT_MEASURE_TIME,
98                                json_object_new_int((m->time).tv_sec));
99         json_object_object_add(obj, ATT_SENSOR_LAST_MEASURE, mo);
100
101         return obj;
102 }
103
104 char *sensor_to_json_string(struct psensor *s)
105 {
106         char *str;
107         json_object *obj = sensor_to_json(s);
108
109         str = strdup(json_object_to_json_string(obj));
110
111         json_object_put(obj);
112
113         return str;
114 }
115
116 char *sensors_to_json_string(struct psensor **sensors)
117 {
118         struct psensor **sensors_cur;
119         char *str;
120         json_object *obj = json_object_new_array();
121
122         if (sensors) {
123                 sensors_cur = sensors;
124
125                 while (*sensors_cur) {
126                         struct psensor *s = *sensors_cur;
127
128                         json_object_array_add(obj, sensor_to_json(s));
129
130                         sensors_cur++;
131                 }
132         }
133
134         str = strdup(json_object_to_json_string(obj));
135
136         json_object_put(obj);
137
138         return str;
139 }
140
141 struct psensor *psensor_new_from_json(json_object *o,
142                                       const char *sensors_url,
143                                       int values_max_length)
144 {
145         json_object *oid, *oname, *otype;
146         struct psensor *s;
147         char *eid, *url;
148
149         json_object_object_get_ex(o, "id", &oid);
150         json_object_object_get_ex(o, "name", &oname);
151         json_object_object_get_ex(o, "type", &otype);
152
153         eid = url_encode(json_object_get_string(oid));
154         url = malloc(strlen(sensors_url) + 1 + strlen(eid) + 1);
155         sprintf(url, "%s/%s", sensors_url, eid);
156
157         s = psensor_create(strdup(url),
158                            strdup(json_object_get_string(oname)),
159                            NULL,
160                            json_object_get_int(otype) | SENSOR_TYPE_REMOTE,
161                            values_max_length);
162         s->provider_data = url;
163
164         free(eid);
165
166         return s;
167 }
168