diff --git a/Project/applications/smartcities/Makefile b/Project/applications/smartcities/Makefile index 4b371da..3179a82 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 +USER_SRC = main.c httpClient.c callbacks.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 b92ddd9..0691b8f 100644 --- a/Project/applications/smartcities/include/globals.h +++ b/Project/applications/smartcities/include/globals.h @@ -1,6 +1,7 @@ #ifndef GLOBALS_H #define GLOBALS_H -#define SERVER_IP "10.0.60.241" +#define SERVER_IP "10.0.60.241" +#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 c797e89..aeae30c 100644 --- a/Project/applications/smartcities/include/json.h +++ b/Project/applications/smartcities/include/json.h @@ -17,7 +17,7 @@ char* prepare_json_statement(char** data, uint32_t nObservations); char* prepare_json_register_statement(char* module_ID, uint8_t sensor_type); char* prepare_observation(char* observation, uint32_t length); -uint8_t send_observation(char* statement, uint32_t length, char* ID); +uint8_t send_observation(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, bool) diff --git a/Project/applications/smartcities/include/module.h b/Project/applications/smartcities/include/module.h index e0a2659..84be2f8 100644 --- a/Project/applications/smartcities/include/module.h +++ b/Project/applications/smartcities/include/module.h @@ -1,6 +1,11 @@ #ifndef MODULE_H #define MODULE_H -#define SERIAL_NUMBER "123456" +typedef struct module_info { + char* ID; + char* geoloc; +} module_info; + +module_info module = {"123456", "41.39479 2.148768"} /* append 0/1 flag for read write */ #endif \ No newline at end of file diff --git a/Project/applications/smartcities/include/sensors.h b/Project/applications/smartcities/include/sensors.h index 59746a7..6ad402d 100644 --- a/Project/applications/smartcities/include/sensors.h +++ b/Project/applications/smartcities/include/sensors.h @@ -1,5 +1,14 @@ #ifndef SENSORS_H #define SENSORS_H +typedef struct sensor_info { + uint8_t ID; + char* description; + char* type; + char* unit; +} sensor_info; -#endif +sensor_info humidity_sensor = {ID = 0x77, "Humidity sensor", "humidity", "%"} /* append 0/1 flag for read write */ + + +#endif \ No newline at end of file diff --git a/Project/applications/smartcities/json.c b/Project/applications/smartcities/json.c index 6f091fd..f5d4ab2 100644 --- a/Project/applications/smartcities/json.c +++ b/Project/applications/smartcities/json.c @@ -14,48 +14,48 @@ char* prepare_json_observation_statement(char** data, uint32_t nObservations) length += observation_length; } length--; //REMOVE LAST ',' - json_statement = join_strings(str_aux,"]}",length,2,JOIN_NO_FREE); + json_statement = join_strings(str_aux,"]}\0",length,3,JOIN_NO_FREE); chHeapFree(str_aux); return json_statement; } -char* prepare_json_register_statement(char* module_ID, uint8_t sensor_type) +char* prepare_json_register_statement(module_info module, sensor_info sensor) { - //id, desc, type, unit - unsigned int i, length, observation_length; char *json_statement, *str_aux, *str_aux2; - length = 17; - str_aux = join_strings("{\"observations\":[","",length,0,JOIN_NO_FREE); - for(i = 0; i < nObservations; i++) - { - 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); - length += observation_length; - } - length--; - json_statement = join_strings(str_aux,"]}",length,2,JOIN_NO_FREE); + length = 23; + char* ID[MODULE_ID_LENGTH+3]; + strcpy(ID,module.ID); + sprintf(ID+MODULE_ID_LENGTH,"%d",sensor.ID); + str_aux = join_strings("{\"sensors\":[{\"sensor\":\"",ID,length,MODULE_ID_LENGTH+2,JOIN_NO_FREE); + length += MODULE_ID_LENGTH+2; + str_aux2 = join_strings(str_aux,"\",\"description\":\"",length,17,JOIN_NO_FREE); chHeapFree(str_aux); - return json_statement; - - char *json_data, *str_aux, *str_aux2; - int value_length = find_next_index(observation,length,','); - int timestamp_length = length - (value_length + 1); - char *value = (char*) chHeapAlloc(NULL,value_length+1); - char *timestamp = (char*) chHeapAlloc(NULL,timestamp_length+1); - strncpy(value,observation,value_length); - strncpy(timestamp,observation+(value_length+1),timestamp_length); - value[value_length] = '\0'; - timestamp[timestamp_length] = '\0'; - str_aux = join_strings("{\"value\":\"",value,10,value_length,JOIN_NO_FREE); - str_aux2 = join_strings(str_aux,"\",\"timestamp\":\"",10+value_length,15,JOIN_NO_FREE); - str_aux2 = join_strings(str_aux2,timestamp,25+value_length,timestamp_length,JOIN_FREE_MEM); - json_data = join_strings(str_aux2,"\"},",25+value_length+timestamp_length,3,JOIN_NO_FREE); + length += 17; + str_aux = join_strings(str_aux2,sensor.description,length,strlen(sensor.description),JOIN_NO_FREE); + chHeapFree(str_aux2); + length += strlen(sensor.description); + str_aux2 = join_strings(str_aux,"\",\"type\":\"",length,10,JOIN_NO_FREE); chHeapFree(str_aux); + length += 10; + str_aux = join_strings(str_aux2,sensor.type,length,strlen(sensors.type),JOIN_NO_FREE); chHeapFree(str_aux2); - chHeapFree(value); - return json_data; + length += strlen(sensors.type); + str_aux2 = join_strings(str_aux,"\",\"unit\":\"",length,10,JOIN_NO_FREE); + chHeapFree(str_aux); + length += 10; + str_aux = join_strings(str_aux2,sensor.unit,length,strlen(sensor.unit),JOIN_NO_FREE); + chHeapFree(str_aux2); + length += strlen(sensor.unit); + str_aux2 = join_strings(str_aux,"\",\"location\":\"",length,14,JOIN_NO_FREE); + chHeapFree(str_aux); + length += 14; + str_aux = join_strings(str_aux2,module.geoloc,length,strlen(module.geoloc),JOIN_NO_FREE); + chHeapFree(str_aux2); + length += strlen(module.geoloc); + json_statement = join_strings(str_aux,"\"}]}\0",length,5,JOIN_NO_FREE); + chHeapFree(str_aux); + return json_statement; } char* prepare_observation(char* observation, uint32_t length) @@ -161,4 +161,7 @@ char* join_strings(char* str1, char* str2, uint32_t len1, uint32_t len2, uint8_t free(str2); } return str; -} \ No newline at end of file +} + +//check if exists +//subscribe \ No newline at end of file