added json parsing (fnordlicht output pending...)
This commit is contained in:
parent
f9750baaa8
commit
aa0346cdb5
1 changed files with 44 additions and 25 deletions
69
src/fnpom.c
69
src/fnpom.c
|
@ -9,11 +9,11 @@
|
|||
|
||||
#include "../lib/libfn.h"
|
||||
|
||||
char * http_get_body(char * buffer) {
|
||||
char * http_get_body(char * response) {
|
||||
char * body;
|
||||
|
||||
for (body = buffer; body != '\0'; body++) {
|
||||
if (body - buffer > 4) {
|
||||
for (body = response; body != '\0'; body++) {
|
||||
if (body - response > 4) {
|
||||
if (body[0] == '\r' && body[1] == '\n' && body[2] == '\r' && body[3] == '\n') {
|
||||
return body + 4;
|
||||
}
|
||||
|
@ -24,18 +24,10 @@ char * http_get_body(char * buffer) {
|
|||
return NULL;
|
||||
}
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
char * http_get(char * response, size_t bytes, char * host, char * port, char * path) {
|
||||
int sd, count;
|
||||
|
||||
char host[] = "volkszaehler.org";
|
||||
char port[] = "80";
|
||||
char path[] = "/demo/backend.php/data/";
|
||||
char uuid[] = "12345678-1234-1234-1234-123456789012";
|
||||
char buffer[8192];
|
||||
|
||||
char request[1024];
|
||||
struct addrinfo hints, *res;
|
||||
struct json_tokener *tok;
|
||||
struct json_object *obj;
|
||||
|
||||
/* set up connection */
|
||||
memset(&hints, 0, sizeof hints);
|
||||
|
@ -52,26 +44,53 @@ int main(int argc, char * argv[]) {
|
|||
}
|
||||
|
||||
/* send request */
|
||||
sprintf(buffer, "GET %s/%s.json?from=yesterday&tuples=1 HTTP/1.0\r\n\r\n", path, uuid);
|
||||
send(sd, buffer, strlen(buffer), 0);
|
||||
sprintf(request, "GET %s HTTP/1.0\r\n\r\n", path);
|
||||
send(sd, request, strlen(request), 0);
|
||||
|
||||
/* receive data */
|
||||
do { count = recv(sd, buffer, sizeof(buffer), 0); } while (count > 0);
|
||||
do { count = recv(sd, response, bytes, 0); } while (count > 0);
|
||||
/* close socket */
|
||||
close(sd);
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
char * json = http_get_body(buffer);
|
||||
printf("%s\n", json);
|
||||
int main(int argc, char * argv[]) {
|
||||
char host[] = "volkszaehler.org";
|
||||
char port[] = "80";
|
||||
char path[] = "/demo/backend.php/data/";
|
||||
char uuid[] = "12345678-1234-1234-1234-123456789012";
|
||||
char response[8192], url[1024], * json;
|
||||
|
||||
struct json_tokener * json_tok;
|
||||
struct json_object * json_obj;
|
||||
|
||||
tok = json_tokener_new();
|
||||
obj = json_tokener_parse_ex(tok, json, strlen(json));
|
||||
sprintf(url, "%s/%s.txt?from=yesterday&tuples=1", path, uuid); /* store request uri in buffer */
|
||||
http_get(response, sizeof(response), host, port, url); /* fetch json data to buffer */
|
||||
json = http_get_body(response); /* get pointer to http body */
|
||||
|
||||
if (tok->err != json_tokener_success)
|
||||
obj = error_ptr(-tok->err);
|
||||
json_tok = json_tokener_new();
|
||||
json_obj = json_tokener_parse_ex(json_tok, json, strlen(json));
|
||||
|
||||
if (json_tok->err != json_tokener_success) {
|
||||
json_obj = error_ptr(-json_tok->err);
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
printf("\n%s\n", json_object_to_json_string(obj));
|
||||
/* printf("%s\n", json_object_to_json_string(json_obj)); */
|
||||
|
||||
json_obj = json_object_object_get(json_obj, "data");
|
||||
json_obj = json_object_object_get(json_obj, "last");
|
||||
|
||||
if (json_object_get_type(json_obj) != json_type_double) {
|
||||
fprintf(stderr, "invalid json!\n");
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
printf("Last value: %.2f\n", json_object_get_double(json_obj));
|
||||
|
||||
/* housekeeping */
|
||||
json_tokener_free(tok);
|
||||
close(sd);
|
||||
json_tokener_free(json_tok);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue