added msg log
[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_cleanup()
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         log_printf(LOG_DEBUG, "HTTP request %s", url);
135         if (curl_easy_perform(curl) == CURLE_OK)
136                 obj = json_tokener_parse(chunk.data);
137         else
138                 log_printf(LOG_ERR, _("Fail to connect to: %s"), url);
139
140         free(chunk.data);
141
142         return obj;
143 }
144
145 struct psensor **get_remote_sensors(const char *server_url,
146                                     int values_max_length)
147 {
148         struct psensor **sensors, *s;
149         char *url;
150         json_object *obj;
151         int i, n;
152
153         sensors = NULL;
154
155         url = create_api_1_0_sensors_url(server_url);
156
157         obj = get_json_object(url);
158
159         if (obj && !is_error(obj)) {
160                 n = json_object_array_length(obj);
161                 sensors = malloc((n + 1) * sizeof(struct psensor *));
162
163                 for (i = 0; i < n; i++) {
164                         s = json_object_to_psensor
165                                 (json_object_array_get_idx(obj, i),
166                                  url,
167                                  values_max_length);
168                         sensors[i] = s;
169                 }
170
171                 sensors[n] = NULL;
172
173                 json_object_put(obj);
174         } else {
175                 log_printf(LOG_ERR, _("Invalid content: %s"), url);
176         }
177
178         free(url);
179
180         if (!sensors) {
181                 sensors = malloc(sizeof(struct psensor *));
182                 *sensors = NULL;
183         }
184
185         return sensors;
186 }
187
188 void remote_psensor_update(struct psensor *s)
189 {
190         json_object *obj;
191
192         obj = get_json_object(s->url);
193
194         if (obj && !is_error(obj)) {
195                 json_object *om;
196
197                 om = json_object_object_get(obj, "last_measure");
198
199                 if (!is_error(obj)) {
200                         json_object *ov, *ot;
201                         struct timeval tv;
202
203                         ov = json_object_object_get(om, "value");
204                         ot = json_object_object_get(om, "time");
205
206                         tv.tv_sec = json_object_get_int(ot);
207                         tv.tv_usec = 0;
208
209                         psensor_set_current_measure
210                             (s, json_object_get_double(ov), tv);;
211                 }
212
213                 json_object_put(obj);
214         } else {
215                 log_printf(LOG_ERR, _("Invalid JSON: %s"), s->url);
216         }
217
218 }
219
220 void remote_psensor_list_update(struct psensor **sensors)
221 {
222         struct psensor **cur;
223
224         cur = sensors;
225         while (*cur) {
226                 struct psensor *s = *cur;
227
228                 if (s->type & SENSOR_TYPE_REMOTE)
229                         remote_psensor_update(s);
230
231                 cur++;
232         }
233 }