moved fct creating psensor struct from json to appropriate file
[psensor.git] / src / lib / psensor_json.c
1 /*
2     Copyright (C) 2010-2011 jeanfi@gmail.com
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU 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
20 #include <stdlib.h>
21 #include <string.h>
22
23 #include "psensor_json.h"
24 #include "url.h"
25
26 #define ATT_SENSOR_ID "id"
27 #define ATT_SENSOR_NAME "name"
28 #define ATT_SENSOR_TYPE "type"
29 #define ATT_SENSOR_MIN "min"
30 #define ATT_SENSOR_MAX "max"
31 #define ATT_SENSOR_LAST_MEASURE "last_measure"
32 #define ATT_SENSOR_MEASURES "measures"
33 #define ATT_MEASURE_VALUE "value"
34 #define ATT_MEASURE_TIME "time"
35
36 static json_object *
37 measure_to_json_object(struct measure *m)
38 {
39         json_object *o = json_object_new_object();
40
41         json_object_object_add(o,
42                                ATT_MEASURE_VALUE,
43                                json_object_new_double(m->value.d_num));
44         json_object_object_add(o, ATT_MEASURE_TIME,
45                                json_object_new_int((m->time).tv_sec));
46         return o;
47 }
48
49 static json_object *
50 measures_to_json_object(struct psensor *s)
51 {
52         json_object *o;
53         int i;
54
55         o = json_object_new_array();
56
57         for (i = 0; i < s->values_max_length; i++)
58                 if (s->measures[i].time.tv_sec)
59                         json_object_array_add
60                                 (o, measure_to_json_object(&s->measures[i]));
61
62
63         return o;
64 }
65
66 static json_object *sensor_to_json(struct psensor *s)
67 {
68         json_object *mo, *obj;
69         struct measure *m;
70
71         obj = json_object_new_object();
72
73         json_object_object_add(obj,
74                                ATT_SENSOR_ID,
75                                json_object_new_string(s->id));
76         json_object_object_add(obj,
77                                ATT_SENSOR_NAME,
78                                json_object_new_string(s->name));
79         json_object_object_add(obj,
80                                ATT_SENSOR_TYPE, json_object_new_int(s->type));
81         json_object_object_add(obj,
82                                ATT_SENSOR_MIN, json_object_new_double(s->min));
83         json_object_object_add(obj,
84                                ATT_SENSOR_MAX, json_object_new_double(s->max));
85         json_object_object_add(obj,
86                                ATT_SENSOR_MEASURES,
87                                measures_to_json_object(s));
88
89         m = psensor_get_current_measure(s);
90         mo = json_object_new_object();
91         json_object_object_add(mo,
92                                ATT_MEASURE_VALUE,
93                                json_object_new_double(m->value.d_num));
94         json_object_object_add(mo, ATT_MEASURE_TIME,
95                                json_object_new_int((m->time).tv_sec));
96         json_object_object_add(obj, ATT_SENSOR_LAST_MEASURE, mo);
97
98         return obj;
99 }
100
101 char *sensor_to_json_string(struct psensor *s)
102 {
103         char *str;
104         json_object *obj = sensor_to_json(s);
105
106         str = strdup(json_object_to_json_string(obj));
107
108         json_object_put(obj);
109
110         return str;
111 }
112
113 char *sensors_to_json_string(struct psensor **sensors)
114 {
115         struct psensor **sensors_cur;
116         char *str;
117         json_object *obj = json_object_new_array();
118
119         sensors_cur = sensors;
120
121         while (*sensors_cur) {
122                 struct psensor *s = *sensors_cur;
123
124                 json_object_array_add(obj, sensor_to_json(s));
125
126                 sensors_cur++;
127         }
128
129         str = strdup(json_object_to_json_string(obj));
130
131         json_object_put(obj);
132
133         return str;
134 }
135
136 struct psensor *psensor_new_from_json(json_object *o,
137                                       const char *sensors_url,
138                                       int values_max_length)
139 {
140         json_object *oid, *oname, *otype;
141         struct psensor *s;
142         char *eid, *url;
143
144         oid = json_object_object_get(o, "id");
145         oname = json_object_object_get(o, "name");
146         otype = json_object_object_get(o, "type");
147
148         eid = url_encode(json_object_get_string(oid));
149         url = malloc(strlen(sensors_url) + 1 + strlen(eid) + 1);
150         sprintf(url, "%s/%s", sensors_url, eid);
151
152         s = psensor_create(strdup(url),
153                            strdup(json_object_get_string(oname)),
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