2 * Copyright (C) 2010-2011 jeanfi@gmail.com
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.
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.
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
21 #define _(str) gettext(str)
24 #include "server/server.h"
30 #include <curl/curl.h>
31 #include <json/json.h>
42 static size_t cbk_curl(void *buffer, size_t size, size_t nmemb, void *userp)
47 realsize = size * nmemb;
48 mem = (struct ucontent *)userp;
50 mem->data = realloc(mem->data, mem->len + realsize + 1);
52 memcpy(&(mem->data[mem->len]), buffer, realsize);
54 mem->data[mem->len] = 0;
59 static char *create_api_1_0_sensors_url(const char *base_url)
64 nurl = url_normalize(base_url);
65 n = strlen(nurl) + strlen(URL_BASE_API_1_0_SENSORS) + 1;
69 strcat(ret, URL_BASE_API_1_0_SENSORS);
76 static struct psensor *json_object_to_psensor(json_object *o,
77 const char *sensors_url,
78 int values_max_length)
80 json_object *oid, *oname, *otype;
84 oid = json_object_object_get(o, "id");
85 oname = json_object_object_get(o, "name");
86 otype = json_object_object_get(o, "type");
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);
92 s = psensor_create(strdup(url),
93 strdup(json_object_get_string(oname)),
94 json_object_get_int(otype) | SENSOR_TYPE_REMOTE,
105 curl = curl_easy_init();
108 void rsensor_cleanup()
110 curl_easy_cleanup(curl);
113 static json_object *get_json_object(const char *url)
115 struct ucontent chunk;
123 chunk.data = malloc(1);
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);
131 log_printf(LOG_DEBUG, "HTTP request %s", url);
132 if (curl_easy_perform(curl) == CURLE_OK)
133 obj = json_tokener_parse(chunk.data);
135 log_printf(LOG_ERR, _("Fail to connect to: %s"), url);
142 struct psensor **get_remote_sensors(const char *server_url,
143 int values_max_length)
145 struct psensor **sensors, *s;
152 url = create_api_1_0_sensors_url(server_url);
154 obj = get_json_object(url);
156 if (obj && !is_error(obj)) {
157 n = json_object_array_length(obj);
158 sensors = malloc((n + 1) * sizeof(struct psensor *));
160 for (i = 0; i < n; i++) {
161 s = json_object_to_psensor
162 (json_object_array_get_idx(obj, i),
170 json_object_put(obj);
172 log_printf(LOG_ERR, _("Invalid content: %s"), url);
178 sensors = malloc(sizeof(struct psensor *));
185 void remote_psensor_update(struct psensor *s)
189 obj = get_json_object(s->url);
191 if (obj && !is_error(obj)) {
194 om = json_object_object_get(obj, "last_measure");
196 if (!is_error(obj)) {
197 json_object *ov, *ot;
200 ov = json_object_object_get(om, "value");
201 ot = json_object_object_get(om, "time");
203 tv.tv_sec = json_object_get_int(ot);
206 psensor_set_current_measure
207 (s, json_object_get_double(ov), tv);;
210 json_object_put(obj);
212 log_printf(LOG_ERR, _("Invalid JSON: %s"), s->url);
217 void remote_psensor_list_update(struct psensor **sensors)
219 struct psensor **cur;
223 struct psensor *s = *cur;
225 if (s->type & SENSOR_TYPE_REMOTE)
226 remote_psensor_update(s);