flag static fcts
[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;
81         json_object *oname;
82         json_object *otype;
83         struct psensor *s;
84         char *eid;
85         char *url;
86
87         oid = json_object_object_get(o, "id");
88         oname = json_object_object_get(o, "name");
89         otype = json_object_object_get(o, "type");
90
91         eid = url_encode(json_object_get_string(oid));
92         url = malloc(strlen(sensors_url) + 1 + strlen(eid) + 1);
93         sprintf(url, "%s/%s", sensors_url, eid);
94
95         s = psensor_create(strdup(url),
96                            strdup(json_object_get_string(oname)),
97                            json_object_get_int(otype) | SENSOR_TYPE_REMOTE,
98                            values_max_length);
99         s->url = url;
100
101         free(eid);
102
103         return s;
104 }
105
106 void rsensor_init()
107 {
108         curl = curl_easy_init();
109 }
110
111 void rsensor_end()
112 {
113         curl_easy_cleanup(curl);
114 }
115
116 static json_object *get_json_object(const char *url)
117 {
118         struct ucontent chunk;
119         json_object *obj;
120
121         obj = NULL;
122
123         if (!curl)
124                 return NULL;
125
126         chunk.data = malloc(1);
127         chunk.len = 0;
128
129         curl_easy_setopt(curl, CURLOPT_URL, url);
130         curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);
131         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, cbk_curl);
132         curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
133
134         if (curl_easy_perform(curl) == CURLE_OK)
135                 obj = json_tokener_parse(chunk.data);
136         else
137                 fprintf(stderr, _("ERROR: Fail to connect to: %s\n"), url);
138
139         free(chunk.data);
140
141         return obj;
142 }
143
144 struct psensor **get_remote_sensors(const char *server_url,
145                                     int values_max_length)
146 {
147         struct psensor **sensors;
148         char *url;
149         json_object *obj;
150
151         sensors = NULL;
152
153         url = create_api_1_0_sensors_url(server_url);
154
155         obj = get_json_object(url);
156
157         if (obj && !is_error(obj)) {
158                 int i;
159                 int n = json_object_array_length(obj);
160                 sensors = malloc((n + 1) * sizeof(struct psensor *));
161
162                 for (i = 0; i < n; i++) {
163                         struct psensor *s = json_object_to_psensor
164                             (json_object_array_get_idx(obj, i),
165                              url,
166                              values_max_length);
167                         sensors[i] = s;
168                 }
169
170                 sensors[n] = NULL;
171
172                 json_object_put(obj);
173         } else {
174                 fprintf(stderr, _("ERROR: Invalid content: %s\n"), url);
175         }
176
177         free(url);
178
179         if (!sensors) {
180                 sensors = malloc(sizeof(struct psensor *));
181                 *sensors = NULL;
182         }
183
184         return sensors;
185 }
186
187 void remote_psensor_update(struct psensor *s)
188 {
189         json_object *obj;
190
191         obj = get_json_object(s->url);
192
193         if (obj && !is_error(obj)) {
194                 json_object *om;
195
196                 om = json_object_object_get(obj, "last_measure");
197
198                 if (!is_error(obj)) {
199                         json_object *ov, *ot;
200                         struct timeval tv;
201
202                         ov = json_object_object_get(om, "value");
203                         ot = json_object_object_get(om, "time");
204
205                         tv.tv_sec = json_object_get_int(ot);
206                         tv.tv_usec = 0;
207
208                         psensor_set_current_measure
209                             (s, json_object_get_double(ov), tv);;
210                 }
211
212                 json_object_put(obj);
213         } else {
214                 printf(_("ERROR: Invalid JSON: %s\n"), s->url);
215         }
216
217 }
218
219 void remote_psensor_list_update(struct psensor **sensors)
220 {
221         struct psensor **cur;
222
223         cur = sensors;
224         while (*cur) {
225                 struct psensor *s = *cur;
226
227                 if (s->type & SENSOR_TYPE_REMOTE)
228                         remote_psensor_update(s);
229
230                 cur++;
231         }
232 }