moved json code to src/lib
[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 <string.h>
21
22 #include "psensor_json.h"
23
24 #define ATT_SENSOR_ID "id"
25 #define ATT_SENSOR_NAME "name"
26 #define ATT_SENSOR_TYPE "type"
27 #define ATT_SENSOR_MIN "min"
28 #define ATT_SENSOR_MAX "max"
29 #define ATT_SENSOR_LAST_MEASURE "last_measure"
30 #define ATT_SENSOR_MEASURES "measures"
31 #define ATT_MEASURE_VALUE "value"
32 #define ATT_MEASURE_TIME "time"
33
34 static json_object *
35 measure_to_json_object(struct measure *m)
36 {
37         json_object *o = json_object_new_object();
38
39         json_object_object_add(o,
40                                ATT_MEASURE_VALUE,
41                                json_object_new_double(m->value.d_num));
42         json_object_object_add(o, ATT_MEASURE_TIME,
43                                json_object_new_int((m->time).tv_sec));
44         return o;
45 }
46
47 static json_object *
48 measures_to_json_object(struct psensor *s)
49 {
50         json_object *o;
51         int i;
52
53         o = json_object_new_array();
54
55         for (i = 0; i < s->values_max_length; i++)
56                 if (s->measures[i].time.tv_sec)
57                         json_object_array_add
58                                 (o, measure_to_json_object(&s->measures[i]));
59
60
61         return o;
62 }
63
64 static json_object *sensor_to_json(struct psensor *s)
65 {
66         json_object *mo, *obj;
67         struct measure *m;
68
69         obj = json_object_new_object();
70
71         json_object_object_add(obj,
72                                ATT_SENSOR_ID,
73                                json_object_new_string(s->id));
74         json_object_object_add(obj,
75                                ATT_SENSOR_NAME,
76                                json_object_new_string(s->name));
77         json_object_object_add(obj,
78                                ATT_SENSOR_TYPE, json_object_new_int(s->type));
79         json_object_object_add(obj,
80                                ATT_SENSOR_MIN, json_object_new_double(s->min));
81         json_object_object_add(obj,
82                                ATT_SENSOR_MAX, json_object_new_double(s->max));
83         json_object_object_add(obj,
84                                ATT_SENSOR_MEASURES,
85                                measures_to_json_object(s));
86
87         m = psensor_get_current_measure(s);
88         mo = json_object_new_object();
89         json_object_object_add(mo,
90                                ATT_MEASURE_VALUE,
91                                json_object_new_double(m->value.d_num));
92         json_object_object_add(mo, ATT_MEASURE_TIME,
93                                json_object_new_int((m->time).tv_sec));
94         json_object_object_add(obj, ATT_SENSOR_LAST_MEASURE, mo);
95
96         return obj;
97 }
98
99 char *sensor_to_json_string(struct psensor *s)
100 {
101         char *str;
102         json_object *obj = sensor_to_json(s);
103
104         str = strdup(json_object_to_json_string(obj));
105
106         json_object_put(obj);
107
108         return str;
109 }
110
111 char *sensors_to_json_string(struct psensor **sensors)
112 {
113         struct psensor **sensors_cur;
114         char *str;
115         json_object *obj = json_object_new_array();
116
117         sensors_cur = sensors;
118
119         while (*sensors_cur) {
120                 struct psensor *s = *sensors_cur;
121
122                 json_object_array_add(obj, sensor_to_json(s));
123
124                 sensors_cur++;
125         }
126
127         str = strdup(json_object_to_json_string(obj));
128
129         json_object_put(obj);
130
131         return str;
132 }
133