diff --git a/Project/applications/smartcities/include/json.h b/Project/applications/smartcities/include/json.h index 2bc11e6..efcf765 100644 --- a/Project/applications/smartcities/include/json.h +++ b/Project/applications/smartcities/include/json.h @@ -3,6 +3,9 @@ #include #include +#include "http.h" +#include "libwismart.h" +#include "ch.h" #define JSON_POST_OK 0 #define JSON_COMM_ERROR 1 @@ -13,7 +16,7 @@ char* prepare_json_statement(char** data, uint32_t nObservations); char* prepare_observation(char* observation, uint32_t length); -uint8_t send_json(char* statement, uint32_t length); +uint8_t send_json(char* statement, uint32_t length, char* ID); uint32_t find_next_index(char* string, uint32_t length, char delimiter); char* join_strings(char* str1, char* str2, uint32_t len1, uint32_t len2, bool) diff --git a/Project/applications/smartcities/json.c b/Project/applications/smartcities/json.c index ab381fe..c70b019 100644 --- a/Project/applications/smartcities/json.c +++ b/Project/applications/smartcities/json.c @@ -1,6 +1,6 @@ #include "json.h" -char* prepare_json_statement(char** data, uint32_t nObservations) +char* prepare_json_observation_statement(char** data, uint32_t nObservations) { unsigned int i, length, observation_length; char *json_statement, *str_aux, *str_aux2; @@ -10,7 +10,7 @@ char* prepare_json_statement(char** data, uint32_t nObservations) { str_aux2 = prepare_observation(data[i],strlen(data[i])); observation_length = strlen(str_aux2); - str_aux = join_strings(str_aux,str_aux2,length,observation_length,JOIN_FREE_MEM); //FREES DATA FROM SENSORS!!! + str_aux = join_strings(str_aux,str_aux2,length,observation_length,JOIN_FREE_MEM); length += observation_length; } length--; //REMOVE LAST ',' @@ -40,18 +40,46 @@ char* prepare_observation(char* observation, uint32_t length) return json_data; } -uint8_t send_json(char* statement, uint32_t length) +uint8_t send_json(char* statement, uint32_t length, char* provider_ID, char* sensor_ID) { - int connectivity; + int connectivity, response_code; + char* URL; connectivity = libwismart_IsConnected(); if(connectivity != WISMART_CONNECT) { return JSON_COMM_ERROR; } - //PREPARE HTTP POST - //SEND POST - //RETURN JSON_OTHER_ERROR - //RETURN JSON_POST_OK + if(sensor_ID == NULL) + { + URL = (char*) chHeapAlloc(NULL,17+SERVER_HOSTNAME_LENGTH+strlen(provider_ID)); + strcpy(URL,"http://"); + strcpy(URL+7,SERVER_HOSTNAME); + strcpy(URL+7+SERVER_HOSTNAME_LENGTH,"/catalog/"); + strcpy(URL+16+SERVER_HOSTNAME_LENGTH,provider_ID); + } + else + { + URL = (char*) chHeapAlloc(NULL,18+SERVER_HOSTNAME_LENGTH+strlen(provider_ID)+strlen(sensor_ID)); + strcpy(URL,"http://"); + strcpy(URL+7,SERVER_HOSTNAME); + strcpy(URL+7+SERVER_HOSTNAME_LENGTH,"/data/"); + strcpy(URL+16+SERVER_HOSTNAME_LENGTH,provider_ID); + strcpy(URL+16+SERVER_HOSTNAME_LENGTH+strlen(provider_ID),"/"); + strcpy(URL+17+SERVER_HOSTNAME_LENGTH+strlen(provider_ID),sensor_ID); + } + response_code = http_post(URL,statement,URL_length,length); + if(response code == 200) + { + return JSON_POST_OK; + } + else if((response_code >= 400) && (response_code < 500)) + { + return JSON_COMM_ERROR; + } + else + { + return JSON_OTHER_ERROR; + } } uint32_t find_next_index(char* string, uint32_t length, char delimiter)