2
Copyright (C) 2010-2011 wpitchoune@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
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
20
19
#include <locale.h>
21
20
#include <libintl.h>
22
21
#define _(str) gettext(str)
25
#include "server/server.h"
28
24
#include <stdlib.h>
29
25
#include <string.h>
31
27
#include <curl/curl.h>
32
#include <json/json.h>
29
#include "psensor_json.h"
34
30
#include "rsensor.h"
31
#include "server/server.h"
43
size_t cbk_curl(void *buffer, size_t size, size_t nmemb, void *userp)
41
static 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;
46
realsize = size * nmemb;
47
mem = (struct ucontent *)userp;
48
49
mem->data = realloc(mem->data, mem->len + realsize + 1);
57
char *create_api_1_0_sensors_url(const char *base_url)
58
static 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);
63
nurl = url_normalize(base_url);
64
n = strlen(nurl) + strlen(URL_BASE_API_1_0_SENSORS) + 1;
64
68
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,
101
75
void rsensor_init()
103
77
curl = curl_easy_init();
80
void rsensor_cleanup()
108
82
curl_easy_cleanup(curl);
111
json_object *get_json_object(const char *url)
85
static json_object *get_json_object(const char *url)
114
87
struct ucontent chunk;
115
json_object *obj = NULL;
125
100
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, cbk_curl);
126
101
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
103
log_debug("HTTP request %s", url);
128
104
if (curl_easy_perform(curl) == CURLE_OK)
129
105
obj = json_tokener_parse(chunk.data);
131
fprintf(stderr, _("ERROR: Fail to connect to: %s\n"), url);
107
log_printf(LOG_ERR, _("Fail to connect to: %s"), url);
133
109
free(chunk.data);
138
114
struct psensor **get_remote_sensors(const char *server_url,
139
115
int values_max_length)
141
struct psensor **sensors = NULL;
117
struct psensor **sensors, *s;
143
119
json_object *obj;
145
124
url = create_api_1_0_sensors_url(server_url);
147
126
obj = get_json_object(url);
149
128
if (obj && !is_error(obj)) {
151
int n = json_object_array_length(obj);
129
n = json_object_array_length(obj);
152
130
sensors = malloc((n + 1) * sizeof(struct psensor *));
154
132
for (i = 0; i < n; i++) {
155
struct psensor *s = json_object_to_psensor
156
(json_object_array_get_idx(obj, i),
133
s = psensor_new_from_json
134
(json_object_array_get_idx(obj, i),
198
178
psensor_set_current_measure
199
(s, json_object_get_double(ov), tv);;
179
(s, json_object_get_double(ov), tv);
202
182
json_object_put(obj);
204
printf(_("ERROR: Invalid JSON: %s\n"), s->url);
184
log_printf(LOG_ERR, _("Invalid JSON: %s"), s->url);
209
189
void remote_psensor_list_update(struct psensor **sensors)
211
struct psensor **cur = sensors;
191
struct psensor **cur;
214
195
struct psensor *s = *cur;