import psensor trunk from private svn
[psensor.git] / src / libpsensor_json / psensor_json.c
1 /*
2     Copyright (C) 2010-2011 wpitchoune@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_MEASURE_VALUE "value"
31 #define ATT_MEASURE_TIME "time"
32
33 json_object *sensor_to_json_object(struct psensor *s)
34 {
35         json_object *mo;
36         json_object *obj = json_object_new_object();
37         struct measure *m;
38
39         json_object_object_add(obj,
40                                ATT_SENSOR_ID,
41                                json_object_new_string(s->id));
42         json_object_object_add(obj,
43                                ATT_SENSOR_NAME,
44                                json_object_new_string(s->name));
45         json_object_object_add(obj,
46                                ATT_SENSOR_TYPE, json_object_new_int(s->type));
47         json_object_object_add(obj,
48                                ATT_SENSOR_MIN, json_object_new_double(s->min));
49         json_object_object_add(obj,
50                                ATT_SENSOR_MAX, json_object_new_double(s->max));
51
52         m = psensor_get_current_measure(s);
53         mo = json_object_new_object();
54         json_object_object_add(mo,
55                                ATT_MEASURE_VALUE,
56                                json_object_new_double(m->value));
57         json_object_object_add(mo, ATT_MEASURE_TIME,
58                                json_object_new_int((m->time).tv_sec));
59         json_object_object_add(obj, ATT_SENSOR_LAST_MEASURE, mo);
60
61         return obj;
62 }
63
64 char *sensor_to_json_string(struct psensor *s)
65 {
66         char *str;
67         json_object *obj = sensor_to_json_object(s);
68
69         str = strdup(json_object_to_json_string(obj));
70
71         json_object_put(obj);
72
73         return str;
74 }
75
76 char *sensors_to_json_string(struct psensor **sensors)
77 {
78         struct psensor **sensors_cur;
79         char *str;
80
81         json_object *obj = json_object_new_array();
82
83         sensors_cur = sensors;
84
85         while (*sensors_cur) {
86                 struct psensor *s = *sensors_cur;
87
88                 json_object_array_add(obj, sensor_to_json_object(s));
89
90                 sensors_cur++;
91         }
92
93         str = strdup(json_object_to_json_string(obj));
94
95         json_object_put(obj);
96
97         return str;
98 }
99