Commit 4686809bec1016a00e0822879cd18d0602dbf4cd
1 parent
32ede649
--no commit message
Showing
2 changed files
with
40 additions
and
9 deletions
Project/applications/smartcities/include/json.h
@@ -3,6 +3,9 @@ | @@ -3,6 +3,9 @@ | ||
3 | 3 | ||
4 | #include <string.h> | 4 | #include <string.h> |
5 | #include <stdint.h> | 5 | #include <stdint.h> |
6 | +#include "http.h" | ||
7 | +#include "libwismart.h" | ||
8 | +#include "ch.h" | ||
6 | 9 | ||
7 | #define JSON_POST_OK 0 | 10 | #define JSON_POST_OK 0 |
8 | #define JSON_COMM_ERROR 1 | 11 | #define JSON_COMM_ERROR 1 |
@@ -13,7 +16,7 @@ | @@ -13,7 +16,7 @@ | ||
13 | 16 | ||
14 | char* prepare_json_statement(char** data, uint32_t nObservations); | 17 | char* prepare_json_statement(char** data, uint32_t nObservations); |
15 | char* prepare_observation(char* observation, uint32_t length); | 18 | char* prepare_observation(char* observation, uint32_t length); |
16 | -uint8_t send_json(char* statement, uint32_t length); | 19 | +uint8_t send_json(char* statement, uint32_t length, char* ID); |
17 | uint32_t find_next_index(char* string, uint32_t length, char delimiter); | 20 | uint32_t find_next_index(char* string, uint32_t length, char delimiter); |
18 | char* join_strings(char* str1, char* str2, uint32_t len1, uint32_t len2, bool) | 21 | char* join_strings(char* str1, char* str2, uint32_t len1, uint32_t len2, bool) |
19 | 22 |
Project/applications/smartcities/json.c
1 | #include "json.h" | 1 | #include "json.h" |
2 | 2 | ||
3 | -char* prepare_json_statement(char** data, uint32_t nObservations) | 3 | +char* prepare_json_observation_statement(char** data, uint32_t nObservations) |
4 | { | 4 | { |
5 | unsigned int i, length, observation_length; | 5 | unsigned int i, length, observation_length; |
6 | char *json_statement, *str_aux, *str_aux2; | 6 | char *json_statement, *str_aux, *str_aux2; |
@@ -10,7 +10,7 @@ char* prepare_json_statement(char** data, uint32_t nObservations) | @@ -10,7 +10,7 @@ char* prepare_json_statement(char** data, uint32_t nObservations) | ||
10 | { | 10 | { |
11 | str_aux2 = prepare_observation(data[i],strlen(data[i])); | 11 | str_aux2 = prepare_observation(data[i],strlen(data[i])); |
12 | observation_length = strlen(str_aux2); | 12 | observation_length = strlen(str_aux2); |
13 | - str_aux = join_strings(str_aux,str_aux2,length,observation_length,JOIN_FREE_MEM); //FREES DATA FROM SENSORS!!! | 13 | + str_aux = join_strings(str_aux,str_aux2,length,observation_length,JOIN_FREE_MEM); |
14 | length += observation_length; | 14 | length += observation_length; |
15 | } | 15 | } |
16 | length--; //REMOVE LAST ',' | 16 | length--; //REMOVE LAST ',' |
@@ -40,18 +40,46 @@ char* prepare_observation(char* observation, uint32_t length) | @@ -40,18 +40,46 @@ char* prepare_observation(char* observation, uint32_t length) | ||
40 | return json_data; | 40 | return json_data; |
41 | } | 41 | } |
42 | 42 | ||
43 | -uint8_t send_json(char* statement, uint32_t length) | 43 | +uint8_t send_json(char* statement, uint32_t length, char* provider_ID, char* sensor_ID) |
44 | { | 44 | { |
45 | - int connectivity; | 45 | + int connectivity, response_code; |
46 | + char* URL; | ||
46 | connectivity = libwismart_IsConnected(); | 47 | connectivity = libwismart_IsConnected(); |
47 | if(connectivity != WISMART_CONNECT) | 48 | if(connectivity != WISMART_CONNECT) |
48 | { | 49 | { |
49 | return JSON_COMM_ERROR; | 50 | return JSON_COMM_ERROR; |
50 | } | 51 | } |
51 | - //PREPARE HTTP POST | ||
52 | - //SEND POST | ||
53 | - //RETURN JSON_OTHER_ERROR | ||
54 | - //RETURN JSON_POST_OK | 52 | + if(sensor_ID == NULL) |
53 | + { | ||
54 | + URL = (char*) chHeapAlloc(NULL,17+SERVER_HOSTNAME_LENGTH+strlen(provider_ID)); | ||
55 | + strcpy(URL,"http://"); | ||
56 | + strcpy(URL+7,SERVER_HOSTNAME); | ||
57 | + strcpy(URL+7+SERVER_HOSTNAME_LENGTH,"/catalog/"); | ||
58 | + strcpy(URL+16+SERVER_HOSTNAME_LENGTH,provider_ID); | ||
59 | + } | ||
60 | + else | ||
61 | + { | ||
62 | + URL = (char*) chHeapAlloc(NULL,18+SERVER_HOSTNAME_LENGTH+strlen(provider_ID)+strlen(sensor_ID)); | ||
63 | + strcpy(URL,"http://"); | ||
64 | + strcpy(URL+7,SERVER_HOSTNAME); | ||
65 | + strcpy(URL+7+SERVER_HOSTNAME_LENGTH,"/data/"); | ||
66 | + strcpy(URL+16+SERVER_HOSTNAME_LENGTH,provider_ID); | ||
67 | + strcpy(URL+16+SERVER_HOSTNAME_LENGTH+strlen(provider_ID),"/"); | ||
68 | + strcpy(URL+17+SERVER_HOSTNAME_LENGTH+strlen(provider_ID),sensor_ID); | ||
69 | + } | ||
70 | + response_code = http_post(URL,statement,URL_length,length); | ||
71 | + if(response code == 200) | ||
72 | + { | ||
73 | + return JSON_POST_OK; | ||
74 | + } | ||
75 | + else if((response_code >= 400) && (response_code < 500)) | ||
76 | + { | ||
77 | + return JSON_COMM_ERROR; | ||
78 | + } | ||
79 | + else | ||
80 | + { | ||
81 | + return JSON_OTHER_ERROR; | ||
82 | + } | ||
55 | } | 83 | } |
56 | 84 | ||
57 | uint32_t find_next_index(char* string, uint32_t length, char delimiter) | 85 | uint32_t find_next_index(char* string, uint32_t length, char delimiter) |