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