code style
[psensor.git] / src / rsensor.c
1 /*
2  * Copyright (C) 2010-2011 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 <locale.h>
20 #include <libintl.h>
21 #define _(str) gettext(str)
22
23 #include "url.h"
24 #include "server/server.h"
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include <curl/curl.h>
31 #include <json/json.h>
32
33 #include "rsensor.h"
34
35 struct ucontent {
36         char *data;
37         size_t len;
38 };
39
40 static CURL *curl;
41
42 size_t cbk_curl(void *buffer, size_t size, size_t nmemb, void *userp)
43 {
44         size_t realsize = size * nmemb;
45         struct ucontent *mem = (struct ucontent *)userp;
46
47         mem->data = realloc(mem->data, mem->len + realsize + 1);
48
49         memcpy(&(mem->data[mem->len]), buffer, realsize);
50         mem->len += realsize;
51         mem->data[mem->len] = 0;
52
53         return realsize;
54 }
55
56 char *create_api_1_0_sensors_url(const char *base_url)
57 {
58         char *nurl = url_normalize(base_url);
59         int n = strlen(nurl) + strlen(URL_BASE_API_1_0_SENSORS) + 1;
60         char *ret = malloc(n);
61
62         strcpy(ret, nurl);
63         strcat(ret, URL_BASE_API_1_0_SENSORS);
64
65         free(nurl);
66
67         return ret;
68 }
69
70 struct psensor *json_object_to_psensor(json_object * o,
71                                        const char *sensors_url,
72                                        int values_max_length)
73 {
74         json_object *oid;
75         json_object *oname;
76         json_object *otype;
77         struct psensor *s;
78         char *eid;
79         char *url;
80
81         oid = json_object_object_get(o, "id");
82         oname = json_object_object_get(o, "name");
83         otype = json_object_object_get(o, "type");
84
85         eid = url_encode(json_object_get_string(oid));
86         url = malloc(strlen(sensors_url) + 1 + strlen(eid) + 1);
87         sprintf(url, "%s/%s", sensors_url, eid);
88
89         s = psensor_create(strdup(url),
90                            strdup(json_object_get_string(oname)),
91                            json_object_get_int(otype) | SENSOR_TYPE_REMOTE,
92                            values_max_length);
93         s->url = url;
94
95         free(eid);
96
97         return s;
98 }
99
100 void rsensor_init()
101 {
102         curl = curl_easy_init();
103 }
104
105 void rsensor_end()
106 {
107         curl_easy_cleanup(curl);
108 }
109
110 json_object *get_json_object(const char *url)
111 {
112
113         struct ucontent chunk;
114         json_object *obj = NULL;
115
116         if (!curl)
117                 return NULL;
118
119         chunk.data = malloc(1);
120         chunk.len = 0;
121
122         curl_easy_setopt(curl, CURLOPT_URL, url);
123         curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);
124         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, cbk_curl);
125         curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
126
127         if (curl_easy_perform(curl) == CURLE_OK)
128                 obj = json_tokener_parse(chunk.data);
129         else
130                 fprintf(stderr, _("ERROR: Fail to connect to: %s\n"), url);
131
132         free(chunk.data);
133
134         return obj;
135 }
136
137 struct psensor **get_remote_sensors(const char *server_url,
138                                     int values_max_length)
139 {
140         struct psensor **sensors = NULL;
141         char *url;
142         json_object *obj;
143
144         url = create_api_1_0_sensors_url(server_url);
145
146         obj = get_json_object(url);
147
148         if (obj && !is_error(obj)) {
149                 int i;
150                 int n = json_object_array_length(obj);
151                 sensors = malloc((n + 1) * sizeof(struct psensor *));
152
153                 for (i = 0; i < n; i++) {
154                         struct psensor *s = json_object_to_psensor
155                             (json_object_array_get_idx(obj, i),
156                              url,
157                              values_max_length);
158                         sensors[i] = s;
159                 }
160
161                 sensors[n] = NULL;
162
163                 json_object_put(obj);
164         } else {
165                 fprintf(stderr, _("ERROR: Invalid content: %s\n"), url);
166         }
167
168         free(url);
169
170         if (!sensors) {
171                 sensors = malloc(sizeof(struct psensor *));
172                 *sensors = NULL;
173         }
174
175         return sensors;
176 }
177
178 void remote_psensor_update(struct psensor *s)
179 {
180         json_object *obj = get_json_object(s->url);
181
182         if (obj && !is_error(obj)) {
183                 json_object *om;
184
185                 om = json_object_object_get(obj, "last_measure");
186
187                 if (!is_error(obj)) {
188                         json_object *ov, *ot;
189                         struct timeval tv;
190
191                         ov = json_object_object_get(om, "value");
192                         ot = json_object_object_get(om, "time");
193
194                         tv.tv_sec = json_object_get_int(ot);
195                         tv.tv_usec = 0;
196
197                         psensor_set_current_measure
198                             (s, json_object_get_double(ov), tv);;
199                 }
200
201                 json_object_put(obj);
202         } else {
203                 printf(_("ERROR: Invalid JSON: %s\n"), s->url);
204         }
205
206 }
207
208 void remote_psensor_list_update(struct psensor **sensors)
209 {
210         struct psensor **cur = sensors;
211
212         while (*cur) {
213                 struct psensor *s = *cur;
214
215                 if (s->type & SENSOR_TYPE_REMOTE)
216                         remote_psensor_update(s);
217
218                 cur++;
219         }
220 }