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)
87 oid = json_object_object_get(o, "id");
88 oname = json_object_object_get(o, "name");
89 otype = json_object_object_get(o, "type");
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);
95 s = psensor_create(strdup(url),
96 strdup(json_object_get_string(oname)),
97 json_object_get_int(otype) | SENSOR_TYPE_REMOTE,
108 curl = curl_easy_init();
113 curl_easy_cleanup(curl);
116 static json_object *get_json_object(const char *url)
118 struct ucontent chunk;
126 chunk.data = malloc(1);
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);
134 if (curl_easy_perform(curl) == CURLE_OK)
135 obj = json_tokener_parse(chunk.data);
137 fprintf(stderr, _("ERROR: Fail to connect to: %s\n"), url);
144 struct psensor **get_remote_sensors(const char *server_url,
145 int values_max_length)
147 struct psensor **sensors;
153 url = create_api_1_0_sensors_url(server_url);
155 obj = get_json_object(url);
157 if (obj && !is_error(obj)) {
159 int n = json_object_array_length(obj);
160 sensors = malloc((n + 1) * sizeof(struct psensor *));
162 for (i = 0; i < n; i++) {
163 struct psensor *s = json_object_to_psensor
164 (json_object_array_get_idx(obj, i),
172 json_object_put(obj);
174 fprintf(stderr, _("ERROR: Invalid content: %s\n"), url);
180 sensors = malloc(sizeof(struct psensor *));
187 void remote_psensor_update(struct psensor *s)
191 obj = get_json_object(s->url);
193 if (obj && !is_error(obj)) {
196 om = json_object_object_get(obj, "last_measure");
198 if (!is_error(obj)) {
199 json_object *ov, *ot;
202 ov = json_object_object_get(om, "value");
203 ot = json_object_object_get(om, "time");
205 tv.tv_sec = json_object_get_int(ot);
208 psensor_set_current_measure
209 (s, json_object_get_double(ov), tv);;
212 json_object_put(obj);
214 printf(_("ERROR: Invalid JSON: %s\n"), s->url);
219 void remote_psensor_list_update(struct psensor **sensors)
221 struct psensor **cur;
225 struct psensor *s = *cur;
227 if (s->type & SENSOR_TYPE_REMOTE)
228 remote_psensor_update(s);