avoid to use cpp in code
[psensor.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 <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, 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,
87                                ATT_SENSOR_MEASURES,
88                                measures_to_json_object(s));
89
90         m = psensor_get_current_measure(s);
91         mo = json_object_new_object();
92         json_object_object_add(mo,
93                                ATT_MEASURE_VALUE,
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);
98
99         return obj;
100 }
101
102 char *sensor_to_json_string(struct psensor *s)
103 {
104         char *str;
105         json_object *obj = sensor_to_json(s);
106
107         str = strdup(json_object_to_json_string(obj));
108
109         json_object_put(obj);
110
111         return str;
112 }
113
114 char *sensors_to_json_string(struct psensor **sensors)
115 {
116         struct psensor **sensors_cur;
117         char *str;
118         json_object *obj = json_object_new_array();
119
120         sensors_cur = sensors;
121
122         while (*sensors_cur) {
123                 struct psensor *s = *sensors_cur;
124
125                 json_object_array_add(obj, sensor_to_json(s));
126
127                 sensors_cur++;
128         }
129
130         str = strdup(json_object_to_json_string(obj));
131
132         json_object_put(obj);
133
134         return str;
135 }
136
137 struct psensor *psensor_new_from_json(json_object *o,
138                                       const char *sensors_url,
139                                       int values_max_length)
140 {
141         json_object *oid, *oname, *otype;
142         struct psensor *s;
143         char *eid, *url;
144
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);
148
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);
152
153         s = psensor_create(strdup(url),
154                            strdup(json_object_get_string(oname)),
155                            NULL,
156                            json_object_get_int(otype) | SENSOR_TYPE_REMOTE,
157                            values_max_length);
158         s->url = url;
159
160         free(eid);
161
162         return s;
163 }
164