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 size_t cbk_curl(void *buffer, size_t size, size_t nmemb, void *userp)
44 size_t realsize = size * nmemb;
45 struct ucontent *mem = (struct ucontent *)userp;
47 mem->data = realloc(mem->data, mem->len + realsize + 1);
49 memcpy(&(mem->data[mem->len]), buffer, realsize);
51 mem->data[mem->len] = 0;
56 char *create_api_1_0_sensors_url(const char *base_url)
58 char *nurl = url_normalize(base_url);
59 int n = strlen(nurl) + strlen(URL_BASE_API_1_0_SENSORS) + 1;
60 char *ret = malloc(n);
63 strcat(ret, URL_BASE_API_1_0_SENSORS);
70 struct psensor *json_object_to_psensor(json_object * o,
71 const char *sensors_url,
72 int values_max_length)
81 oid = json_object_object_get(o, "id");
82 oname = json_object_object_get(o, "name");
83 otype = json_object_object_get(o, "type");
85 eid = url_encode(json_object_get_string(oid));
86 url = malloc(strlen(sensors_url) + 1 + strlen(eid) + 1);
87 sprintf(url, "%s/%s", sensors_url, eid);
89 s = psensor_create(strdup(url),
90 strdup(json_object_get_string(oname)),
91 json_object_get_int(otype) | SENSOR_TYPE_REMOTE,
102 curl = curl_easy_init();
107 curl_easy_cleanup(curl);
110 json_object *get_json_object(const char *url)
113 struct ucontent chunk;
114 json_object *obj = NULL;
119 chunk.data = malloc(1);
122 curl_easy_setopt(curl, CURLOPT_URL, url);
123 curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);
124 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, cbk_curl);
125 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
127 if (curl_easy_perform(curl) == CURLE_OK)
128 obj = json_tokener_parse(chunk.data);
130 fprintf(stderr, _("ERROR: Fail to connect to: %s\n"), url);
137 struct psensor **get_remote_sensors(const char *server_url,
138 int values_max_length)
140 struct psensor **sensors = NULL;
144 url = create_api_1_0_sensors_url(server_url);
146 obj = get_json_object(url);
148 if (obj && !is_error(obj)) {
150 int n = json_object_array_length(obj);
151 sensors = malloc((n + 1) * sizeof(struct psensor *));
153 for (i = 0; i < n; i++) {
154 struct psensor *s = json_object_to_psensor
155 (json_object_array_get_idx(obj, i),
163 json_object_put(obj);
165 fprintf(stderr, _("ERROR: Invalid content: %s\n"), url);
171 sensors = malloc(sizeof(struct psensor *));
178 void remote_psensor_update(struct psensor *s)
180 json_object *obj = get_json_object(s->url);
182 if (obj && !is_error(obj)) {
185 om = json_object_object_get(obj, "last_measure");
187 if (!is_error(obj)) {
188 json_object *ov, *ot;
191 ov = json_object_object_get(om, "value");
192 ot = json_object_object_get(om, "time");
194 tv.tv_sec = json_object_get_int(ot);
197 psensor_set_current_measure
198 (s, json_object_get_double(ov), tv);;
201 json_object_put(obj);
203 printf(_("ERROR: Invalid JSON: %s\n"), s->url);
208 void remote_psensor_list_update(struct psensor **sensors)
210 struct psensor **cur = sensors;
213 struct psensor *s = *cur;
215 if (s->type & SENSOR_TYPE_REMOTE)
216 remote_psensor_update(s);