2 * Copyright (C) 2010-2014 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)
27 #include <curl/curl.h>
29 #include "psensor_json.h"
31 #include "server/server.h"
41 static size_t cbk_curl(void *buffer, size_t size, size_t nmemb, void *userp)
46 realsize = size * nmemb;
47 mem = (struct ucontent *)userp;
49 mem->data = realloc(mem->data, mem->len + realsize + 1);
51 memcpy(&(mem->data[mem->len]), buffer, realsize);
53 mem->data[mem->len] = 0;
58 static char *create_api_1_1_sensors_url(const char *base_url)
63 nurl = url_normalize(base_url);
64 n = strlen(nurl) + strlen(URL_BASE_API_1_1_SENSORS) + 1;
68 strcat(ret, URL_BASE_API_1_1_SENSORS);
75 void rsensor_init(void)
77 curl = curl_easy_init();
80 void rsensor_cleanup(void)
82 curl_easy_cleanup(curl);
85 static json_object *get_json_object(const char *url)
87 struct ucontent chunk;
95 chunk.data = malloc(1);
98 curl_easy_setopt(curl, CURLOPT_URL, url);
99 curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);
100 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, cbk_curl);
101 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
103 log_debug("HTTP request %s", url);
104 if (curl_easy_perform(curl) == CURLE_OK)
105 obj = json_tokener_parse(chunk.data);
107 log_printf(LOG_ERR, _("Fail to connect to: %s"), url);
114 struct psensor **get_remote_sensors(const char *server_url,
115 int values_max_length)
117 struct psensor **sensors, *s;
124 url = create_api_1_1_sensors_url(server_url);
126 obj = get_json_object(url);
128 if (obj && !is_error(obj)) {
129 n = json_object_array_length(obj);
130 sensors = malloc((n + 1) * sizeof(struct psensor *));
132 for (i = 0; i < n; i++) {
133 s = psensor_new_from_json
134 (json_object_array_get_idx(obj, i),
142 json_object_put(obj);
144 log_printf(LOG_ERR, _("Invalid content: %s"), url);
150 sensors = malloc(sizeof(struct psensor *));
157 static void remote_psensor_update(struct psensor *s)
161 obj = get_json_object(s->url);
163 if (obj && !is_error(obj)) {
166 json_object_object_get_ex(obj, "last_measure", &om);
168 if (!is_error(obj)) {
169 json_object *ov, *ot;
172 json_object_object_get_ex(om, "value", &ov);
173 json_object_object_get_ex(om, "time", &ot);
175 tv.tv_sec = json_object_get_int(ot);
178 psensor_set_current_measure
179 (s, json_object_get_double(ov), tv);
182 json_object_put(obj);
184 log_printf(LOG_ERR, _("Invalid JSON: %s"), s->url);
189 void remote_psensor_list_update(struct psensor **sensors)
191 struct psensor **cur;
195 struct psensor *s = *cur;
197 if (s->type & SENSOR_TYPE_REMOTE)
198 remote_psensor_update(s);