diff --git a/Project/applications/smartcities/Makefile b/Project/applications/smartcities/Makefile index 5c71871..ea03010 100644 --- a/Project/applications/smartcities/Makefile +++ b/Project/applications/smartcities/Makefile @@ -10,7 +10,7 @@ SDK_ROOT = ../../ PROJECT_OUT = smartcities -USER_SRC = main.c httpClient.c callbacks.c module.c sensors.c +USER_SRC = main.c httpClient.c callbacks.c module.c sensors.c json.c USER_INCDIR = include/ # if you need to add build Defines options add to USER_DEFS define diff --git a/Project/applications/smartcities/include/globals.h b/Project/applications/smartcities/include/globals.h index 0691b8f..591d1f9 100644 --- a/Project/applications/smartcities/include/globals.h +++ b/Project/applications/smartcities/include/globals.h @@ -1,7 +1,8 @@ #ifndef GLOBALS_H #define GLOBALS_H -#define SERVER_IP "10.0.60.241" -#define MODULE_ID_LENGTH 6 +#define SERVER_IP "10.0.60.241" +#define SERVER_HOSTNAME SERVER_IP +#define MODULE_ID_LENGTH 6 #endif \ No newline at end of file diff --git a/Project/applications/smartcities/include/json.h b/Project/applications/smartcities/include/json.h index 073cb9a..1b472b3 100644 --- a/Project/applications/smartcities/include/json.h +++ b/Project/applications/smartcities/include/json.h @@ -8,6 +8,7 @@ #include "module.h" #include #include +#include "libwismart.h" #define JSON_POST_OK 0 #define JSON_COMM_ERROR 1 @@ -19,7 +20,7 @@ char* prepare_json_statement(char** data, uint32_t nObservations); char* prepare_json_register_statement(module mod, sensor sens); char* prepare_observation(char* observation, uint32_t length); -uint8_t send_observation(char* statement, uint32_t length, char* provider_ID, char* sensor_ID); +uint8_t send_json(char* statement, uint32_t length, char* provider_ID, char* sensor_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, uint8_t free_mem); diff --git a/Project/applications/smartcities/json.c b/Project/applications/smartcities/json.c index 9a34008..2285787 100644 --- a/Project/applications/smartcities/json.c +++ b/Project/applications/smartcities/json.c @@ -21,7 +21,7 @@ char* prepare_json_observation_statement(char** data, uint32_t nObservations) char* prepare_json_register_statement(module mod, sensor sens) { - unsigned int i, length, observation_length; + unsigned int length; char *json_statement, *str_aux, *str_aux2; char ID[MODULE_ID_LENGTH+3]; length = 23; @@ -46,7 +46,7 @@ char* prepare_json_register_statement(module mod, sensor sens) length += 10; str_aux = join_strings(str_aux2,sens.unit,length,strlen(sens.unit),JOIN_NO_FREE); chHeapFree(str_aux2); - length += strlen(sensor.unit); + length += strlen(sens.unit); str_aux2 = join_strings(str_aux,"\",\"location\":\"",length,14,JOIN_NO_FREE); chHeapFree(str_aux); length += 14; @@ -86,32 +86,33 @@ char* prepare_observation(char* observation, uint32_t length) return json_data; } -uint8_t send_observation(char* statement, uint32_t length, char* provider_ID, char* sensor_ID) +uint8_t send_json(char* statement, uint32_t length, char* provider_ID, char* sensor_ID) { int connectivity, response_code; char* URL; + int server_hostname_length = strlen(SERVER_HOSTNAME); connectivity = libwismart_IsConnected(); - if(connectivity != WISMART_CONNECT) + if(connectivity != WISMART_CONNECTED) { return JSON_COMM_ERROR; } - if(sensor_ID == NULL) + if(sensor_ID == NULL) //Register sensor { - URL = (char*) chHeapAlloc(NULL,17+SERVER_HOSTNAME_LENGTH+strlen(provider_ID)); + 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); + strcpy(URL+7+server_hostname_length,"/catalog/"); + strcpy(URL+16+server_hostname_length,provider_ID); } - else + else //Post data { - URL = (char*) chHeapAlloc(NULL,18+SERVER_HOSTNAME_LENGTH+strlen(provider_ID)+strlen(sensor_ID)); + 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); + 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); } /* SELF-REMINDER @@ -121,8 +122,9 @@ uint8_t send_observation(char* statement, uint32_t length, char* provider_ID, ch * --Imanol */ - response_code = http_post(URL,statement,URL_length,length); - if(response code == 200) + //response_code = http_post(URL,statement,URL_length,length); + response_code = 200; //placeholder + if(response_code == 200) { return JSON_POST_OK; }