X-Git-Url: https://git.wpitchoune.net/gitweb/?a=blobdiff_plain;f=src%2Frsensor.c;h=32c182c25165b75fd6cda34da120e7be4cde0c2e;hb=f3b05dae619a7909bd7422b3a82422c9442aa114;hp=8b3eb1292a18a8c2734f19a623d370f1dabcc489;hpb=cde76fd1723bbdad164adac96d105111b831bf50;p=psensor.git diff --git a/src/rsensor.c b/src/rsensor.c index 8b3eb12..32c182c 100644 --- a/src/rsensor.c +++ b/src/rsensor.c @@ -1,37 +1,35 @@ /* - Copyright (C) 2010-2011 jeanfi@gmail.com - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA - 02110-1301 USA -*/ - + * Copyright (C) 2010-2014 jeanfi@gmail.com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ #include #include #define _(str) gettext(str) -#include "plib/url.h" -#include "server/server.h" - #include #include #include #include -#include +#include "psensor_json.h" #include "rsensor.h" +#include "server/server.h" +#include "url.h" struct ucontent { char *data; @@ -40,10 +38,13 @@ struct ucontent { static CURL *curl; -size_t cbk_curl(void *buffer, size_t size, size_t nmemb, void *userp) +static size_t cbk_curl(void *buffer, size_t size, size_t nmemb, void *userp) { - size_t realsize = size * nmemb; - struct ucontent *mem = (struct ucontent *)userp; + size_t realsize; + struct ucontent *mem; + + realsize = size * nmemb; + mem = (struct ucontent *)userp; mem->data = realloc(mem->data, mem->len + realsize + 1); @@ -54,65 +55,39 @@ size_t cbk_curl(void *buffer, size_t size, size_t nmemb, void *userp) return realsize; } -char *create_api_1_0_sensors_url(const char *base_url) +static char *create_api_1_1_sensors_url(const char *base_url) { - char *nurl = url_normalize(base_url); - int n = strlen(nurl) + strlen(URL_BASE_API_1_0_SENSORS) + 1; - char *ret = malloc(n); + char *nurl, *ret; + int n; + + nurl = url_normalize(base_url); + n = strlen(nurl) + strlen(URL_BASE_API_1_1_SENSORS) + 1; + ret = malloc(n); strcpy(ret, nurl); - strcat(ret, URL_BASE_API_1_0_SENSORS); + strcat(ret, URL_BASE_API_1_1_SENSORS); free(nurl); return ret; } -struct psensor *json_object_to_psensor(json_object * o, - const char *sensors_url, - int values_max_length) -{ - json_object *oid; - json_object *oname; - json_object *otype; - struct psensor *s; - char *eid; - char *url; - - oid = json_object_object_get(o, "id"); - oname = json_object_object_get(o, "name"); - otype = json_object_object_get(o, "type"); - - eid = url_encode(json_object_get_string(oid)); - url = malloc(strlen(sensors_url) + 1 + strlen(eid) + 1); - sprintf(url, "%s/%s", sensors_url, eid); - - s = psensor_create(strdup(url), - strdup(json_object_get_string(oname)), - json_object_get_int(otype) | SENSOR_TYPE_REMOTE, - values_max_length); - s->url = url; - - free(eid); - - return s; -} - void rsensor_init() { curl = curl_easy_init(); } -void rsensor_end() +void rsensor_cleanup() { curl_easy_cleanup(curl); } -json_object *get_json_object(const char *url) +static json_object *get_json_object(const char *url) { - struct ucontent chunk; - json_object *obj = NULL; + json_object *obj; + + obj = NULL; if (!curl) return NULL; @@ -125,10 +100,11 @@ json_object *get_json_object(const char *url) curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, cbk_curl); curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk); + log_debug("HTTP request %s", url); if (curl_easy_perform(curl) == CURLE_OK) obj = json_tokener_parse(chunk.data); else - fprintf(stderr, _("ERROR: Fail to connect to: %s\n"), url); + log_printf(LOG_ERR, _("Fail to connect to: %s"), url); free(chunk.data); @@ -138,24 +114,26 @@ json_object *get_json_object(const char *url) struct psensor **get_remote_sensors(const char *server_url, int values_max_length) { - struct psensor **sensors = NULL; + struct psensor **sensors, *s; char *url; json_object *obj; + int i, n; - url = create_api_1_0_sensors_url(server_url); + sensors = NULL; + + url = create_api_1_1_sensors_url(server_url); obj = get_json_object(url); if (obj && !is_error(obj)) { - int i; - int n = json_object_array_length(obj); + n = json_object_array_length(obj); sensors = malloc((n + 1) * sizeof(struct psensor *)); for (i = 0; i < n; i++) { - struct psensor *s = json_object_to_psensor - (json_object_array_get_idx(obj, i), - url, - values_max_length); + s = psensor_new_from_json + (json_object_array_get_idx(obj, i), + url, + values_max_length); sensors[i] = s; } @@ -163,7 +141,7 @@ struct psensor **get_remote_sensors(const char *server_url, json_object_put(obj); } else { - fprintf(stderr, _("ERROR: Invalid content: %s\n"), url); + log_printf(LOG_ERR, _("Invalid content: %s"), url); } free(url); @@ -178,7 +156,9 @@ struct psensor **get_remote_sensors(const char *server_url, void remote_psensor_update(struct psensor *s) { - json_object *obj = get_json_object(s->url); + json_object *obj; + + obj = get_json_object(s->url); if (obj && !is_error(obj)) { json_object *om; @@ -196,20 +176,21 @@ void remote_psensor_update(struct psensor *s) tv.tv_usec = 0; psensor_set_current_measure - (s, json_object_get_double(ov), tv);; + (s, json_object_get_double(ov), tv); } json_object_put(obj); } else { - printf(_("ERROR: Invalid JSON: %s\n"), s->url); + log_printf(LOG_ERR, _("Invalid JSON: %s"), s->url); } } void remote_psensor_list_update(struct psensor **sensors) { - struct psensor **cur = sensors; + struct psensor **cur; + cur = sensors; while (*cur) { struct psensor *s = *cur;