diff --git a/Project/applications/smartcities/include/json.h b/Project/applications/smartcities/include/json.h index aeae30c..073cb9a 100644 --- a/Project/applications/smartcities/include/json.h +++ b/Project/applications/smartcities/include/json.h @@ -3,9 +3,11 @@ #include #include -#include "httpClient.h" -#include "libwismart.h" -#include "ch.h" +#include "globals.h" +#include "sensors.h" +#include "module.h" +#include +#include #define JSON_POST_OK 0 #define JSON_COMM_ERROR 1 @@ -15,10 +17,10 @@ #define JOIN_FREE_MEM 1 char* prepare_json_statement(char** data, uint32_t nObservations); -char* prepare_json_register_statement(char* module_ID, uint8_t sensor_type); +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); 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) +char* join_strings(char* str1, char* str2, uint32_t len1, uint32_t len2, uint8_t free_mem); -#endif +#endif \ No newline at end of file diff --git a/Project/applications/smartcities/include/module.h b/Project/applications/smartcities/include/module.h index 84be2f8..dbda733 100644 --- a/Project/applications/smartcities/include/module.h +++ b/Project/applications/smartcities/include/module.h @@ -1,11 +1,12 @@ #ifndef MODULE_H #define MODULE_H -typedef struct module_info { +typedef struct module_info module; +struct module_info { char* ID; char* geoloc; -} module_info; +}; -module_info module = {"123456", "41.39479 2.148768"} /* append 0/1 flag for read write */ +extern module mod; -#endif \ No newline at end of file +#endif diff --git a/Project/applications/smartcities/include/sensors.h b/Project/applications/smartcities/include/sensors.h index 6ad402d..b4192e4 100644 --- a/Project/applications/smartcities/include/sensors.h +++ b/Project/applications/smartcities/include/sensors.h @@ -1,14 +1,18 @@ #ifndef SENSORS_H #define SENSORS_H -typedef struct sensor_info { +#include + + +typedef struct sensor_info sensor; +struct sensor_info { uint8_t ID; char* description; char* type; char* unit; -} sensor_info; +}; -sensor_info humidity_sensor = {ID = 0x77, "Humidity sensor", "humidity", "%"} /* append 0/1 flag for read write */ +extern sensor humidity_sensor; #endif \ No newline at end of file diff --git a/Project/applications/smartcities/json.c b/Project/applications/smartcities/json.c index f5d4ab2..9a34008 100644 --- a/Project/applications/smartcities/json.c +++ b/Project/applications/smartcities/json.c @@ -19,40 +19,40 @@ char* prepare_json_observation_statement(char** data, uint32_t nObservations) return json_statement; } -char* prepare_json_register_statement(module_info module, sensor_info sensor) +char* prepare_json_register_statement(module mod, sensor sens) { unsigned int i, length, observation_length; char *json_statement, *str_aux, *str_aux2; + char ID[MODULE_ID_LENGTH+3]; length = 23; - char* ID[MODULE_ID_LENGTH+3]; - strcpy(ID,module.ID); - sprintf(ID+MODULE_ID_LENGTH,"%d",sensor.ID); + strcpy(ID,mod.ID); + sprintf(ID+MODULE_ID_LENGTH,"%d",sens.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); length += 17; - str_aux = join_strings(str_aux2,sensor.description,length,strlen(sensor.description),JOIN_NO_FREE); + str_aux = join_strings(str_aux2,sens.description,length,strlen(sens.description),JOIN_NO_FREE); chHeapFree(str_aux2); - length += strlen(sensor.description); + length += strlen(sens.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); + str_aux = join_strings(str_aux2,sens.type,length,strlen(sens.type),JOIN_NO_FREE); chHeapFree(str_aux2); - length += strlen(sensors.type); + length += strlen(sens.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); + str_aux = join_strings(str_aux2,sens.unit,length,strlen(sens.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); + str_aux = join_strings(str_aux2,mod.geoloc,length,strlen(mod.geoloc),JOIN_NO_FREE); chHeapFree(str_aux2); - length += strlen(module.geoloc); + length += strlen(mod.geoloc); json_statement = join_strings(str_aux,"\"}]}\0",length,5,JOIN_NO_FREE); chHeapFree(str_aux); return json_statement; diff --git a/Project/applications/smartcities/module.c b/Project/applications/smartcities/module.c new file mode 100644 index 0000000..527afc7 --- /dev/null +++ b/Project/applications/smartcities/module.c @@ -0,0 +1,3 @@ +#include "module.h" + +module mod = {"123456", "41.39479 2.148768"}; /* append 0/1 flag for read write */ \ No newline at end of file diff --git a/Project/applications/smartcities/sensors.c b/Project/applications/smartcities/sensors.c new file mode 100644 index 0000000..b4517b4 --- /dev/null +++ b/Project/applications/smartcities/sensors.c @@ -0,0 +1,3 @@ +#include "sensors.h" + +sensor humidity_sensor = {0x77, "Humidity sensor", "humidity", "%"}; /* append 0/1 flag for read write */