2 Copyright (C) 2010-2011 jeanfi@gmail.com
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU 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
22 #define _(str) gettext(str)
25 #include "server/server.h"
31 #include <curl/curl.h>
32 #include <json/json.h>
43 size_t cbk_curl(void *buffer, size_t size, size_t nmemb, void *userp)
45 size_t realsize = size * nmemb;
46 struct ucontent *mem = (struct ucontent *)userp;
48 mem->data = realloc(mem->data, mem->len + realsize + 1);
50 memcpy(&(mem->data[mem->len]), buffer, realsize);
52 mem->data[mem->len] = 0;
57 char *create_api_1_0_sensors_url(const char *base_url)
59 char *nurl = url_normalize(base_url);
60 int n = strlen(nurl) + strlen(URL_BASE_API_1_0_SENSORS) + 1;
61 char *ret = malloc(n);
64 strcat(ret, URL_BASE_API_1_0_SENSORS);
71 struct psensor *json_object_to_psensor(json_object * o,
72 const char *sensors_url,
73 int values_max_length)
82 oid = json_object_object_get(o, "id");
83 oname = json_object_object_get(o, "name");
84 otype = json_object_object_get(o, "type");
86 eid = url_encode(json_object_get_string(oid));
87 url = malloc(strlen(sensors_url) + 1 + strlen(eid) + 1);
88 sprintf(url, "%s/%s", sensors_url, eid);
90 s = psensor_create(strdup(url),
91 strdup(json_object_get_string(oname)),
92 json_object_get_int(otype) | SENSOR_TYPE_REMOTE,
103 curl = curl_easy_init();
108 curl_easy_cleanup(curl);
111 json_object *get_json_object(const char *url)
114 struct ucontent chunk;
115 json_object *obj = NULL;
120 chunk.data = malloc(1);
123 curl_easy_setopt(curl, CURLOPT_URL, url);
124 curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);
125 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, cbk_curl);
126 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
128 if (curl_easy_perform(curl) == CURLE_OK)
129 obj = json_tokener_parse(chunk.data);
131 fprintf(stderr, _("ERROR: Fail to connect to: %s\n"), url);
138 struct psensor **get_remote_sensors(const char *server_url,
139 int values_max_length)
141 struct psensor **sensors = NULL;
145 url = create_api_1_0_sensors_url(server_url);
147 obj = get_json_object(url);
149 if (obj && !is_error(obj)) {
151 int n = json_object_array_length(obj);
152 sensors = malloc((n + 1) * sizeof(struct psensor *));
154 for (i = 0; i < n; i++) {
155 struct psensor *s = json_object_to_psensor
156 (json_object_array_get_idx(obj, i),
164 json_object_put(obj);
166 fprintf(stderr, _("ERROR: Invalid content: %s\n"), url);
172 sensors = malloc(sizeof(struct psensor *));
179 void remote_psensor_update(struct psensor *s)
181 json_object *obj = get_json_object(s->url);
183 if (obj && !is_error(obj)) {
186 om = json_object_object_get(obj, "last_measure");
188 if (!is_error(obj)) {
189 json_object *ov, *ot;
192 ov = json_object_object_get(om, "value");
193 ot = json_object_object_get(om, "time");
195 tv.tv_sec = json_object_get_int(ot);
198 psensor_set_current_measure
199 (s, json_object_get_double(ov), tv);;
202 json_object_put(obj);
204 printf(_("ERROR: Invalid JSON: %s\n"), s->url);
209 void remote_psensor_list_update(struct psensor **sensors)
211 struct psensor **cur = sensors;
214 struct psensor *s = *cur;
216 if (s->type & SENSOR_TYPE_REMOTE)
217 remote_psensor_update(s);