Commit 746b99d593fda79672990b377371df81e8c00ca6

Authored by Imanol-Mikel Barba Sabariego
1 parent 08c7da6d

--no commit message

Project/applications/smartcities/Makefile
... ... @@ -10,7 +10,7 @@
10 10 SDK_ROOT = ../../
11 11 PROJECT_OUT = smartcities
12 12  
13   -USER_SRC = main.c httpClient.c
  13 +USER_SRC = main.c httpClient.c callbacks.c
14 14 USER_INCDIR = include/
15 15  
16 16 # if you need to add build Defines options add to USER_DEFS define
... ...
Project/applications/smartcities/include/globals.h
1 1 #ifndef GLOBALS_H
2 2 #define GLOBALS_H
3 3  
4   -#define SERVER_IP "10.0.60.241"
  4 +#define SERVER_IP "10.0.60.241"
  5 +#define MODULE_ID_LENGTH 6
5 6  
6 7 #endif
7 8 \ No newline at end of file
... ...
Project/applications/smartcities/include/json.h
... ... @@ -17,7 +17,7 @@
17 17 char* prepare_json_statement(char** data, uint32_t nObservations);
18 18 char* prepare_json_register_statement(char* module_ID, uint8_t sensor_type);
19 19 char* prepare_observation(char* observation, uint32_t length);
20   -uint8_t send_observation(char* statement, uint32_t length, char* ID);
  20 +uint8_t send_observation(char* statement, uint32_t length, char* provider_ID, char* sensor_ID);
21 21 uint32_t find_next_index(char* string, uint32_t length, char delimiter);
22 22 char* join_strings(char* str1, char* str2, uint32_t len1, uint32_t len2, bool)
23 23  
... ...
Project/applications/smartcities/include/module.h
1 1 #ifndef MODULE_H
2 2 #define MODULE_H
3 3  
4   -#define SERIAL_NUMBER "123456"
  4 +typedef struct module_info {
  5 + char* ID;
  6 + char* geoloc;
  7 +} module_info;
  8 +
  9 +module_info module = {"123456", "41.39479 2.148768"} /* append 0/1 flag for read write */
5 10  
6 11 #endif
7 12 \ No newline at end of file
... ...
Project/applications/smartcities/include/sensors.h
1 1 #ifndef SENSORS_H
2 2 #define SENSORS_H
3 3  
  4 +typedef struct sensor_info {
  5 + uint8_t ID;
  6 + char* description;
  7 + char* type;
  8 + char* unit;
  9 +} sensor_info;
4 10  
5   -#endif
  11 +sensor_info humidity_sensor = {ID = 0x77, "Humidity sensor", "humidity", "%"} /* append 0/1 flag for read write */
  12 +
  13 +
  14 +#endif
6 15 \ No newline at end of file
... ...
Project/applications/smartcities/json.c
... ... @@ -14,48 +14,48 @@ char* prepare_json_observation_statement(char** data, uint32_t nObservations)
14 14 length += observation_length;
15 15 }
16 16 length--; //REMOVE LAST ','
17   - json_statement = join_strings(str_aux,"]}",length,2,JOIN_NO_FREE);
  17 + json_statement = join_strings(str_aux,"]}\0",length,3,JOIN_NO_FREE);
18 18 chHeapFree(str_aux);
19 19 return json_statement;
20 20 }
21 21  
22   -char* prepare_json_register_statement(char* module_ID, uint8_t sensor_type)
  22 +char* prepare_json_register_statement(module_info module, sensor_info sensor)
23 23 {
24   - //id, desc, type, unit
25   -
26 24 unsigned int i, length, observation_length;
27 25 char *json_statement, *str_aux, *str_aux2;
28   - length = 17;
29   - str_aux = join_strings("{\"observations\":[","",length,0,JOIN_NO_FREE);
30   - for(i = 0; i < nObservations; i++)
31   - {
32   - str_aux2 = prepare_observation(data[i],strlen(data[i]));
33   - observation_length = strlen(str_aux2);
34   - str_aux = join_strings(str_aux,str_aux2,length,observation_length,JOIN_FREE_MEM);
35   - length += observation_length;
36   - }
37   - length--;
38   - json_statement = join_strings(str_aux,"]}",length,2,JOIN_NO_FREE);
  26 + length = 23;
  27 + char* ID[MODULE_ID_LENGTH+3];
  28 + strcpy(ID,module.ID);
  29 + sprintf(ID+MODULE_ID_LENGTH,"%d",sensor.ID);
  30 + str_aux = join_strings("{\"sensors\":[{\"sensor\":\"",ID,length,MODULE_ID_LENGTH+2,JOIN_NO_FREE);
  31 + length += MODULE_ID_LENGTH+2;
  32 + str_aux2 = join_strings(str_aux,"\",\"description\":\"",length,17,JOIN_NO_FREE);
39 33 chHeapFree(str_aux);
40   - return json_statement;
41   -
42   - char *json_data, *str_aux, *str_aux2;
43   - int value_length = find_next_index(observation,length,',');
44   - int timestamp_length = length - (value_length + 1);
45   - char *value = (char*) chHeapAlloc(NULL,value_length+1);
46   - char *timestamp = (char*) chHeapAlloc(NULL,timestamp_length+1);
47   - strncpy(value,observation,value_length);
48   - strncpy(timestamp,observation+(value_length+1),timestamp_length);
49   - value[value_length] = '\0';
50   - timestamp[timestamp_length] = '\0';
51   - str_aux = join_strings("{\"value\":\"",value,10,value_length,JOIN_NO_FREE);
52   - str_aux2 = join_strings(str_aux,"\",\"timestamp\":\"",10+value_length,15,JOIN_NO_FREE);
53   - str_aux2 = join_strings(str_aux2,timestamp,25+value_length,timestamp_length,JOIN_FREE_MEM);
54   - json_data = join_strings(str_aux2,"\"},",25+value_length+timestamp_length,3,JOIN_NO_FREE);
  34 + length += 17;
  35 + str_aux = join_strings(str_aux2,sensor.description,length,strlen(sensor.description),JOIN_NO_FREE);
  36 + chHeapFree(str_aux2);
  37 + length += strlen(sensor.description);
  38 + str_aux2 = join_strings(str_aux,"\",\"type\":\"",length,10,JOIN_NO_FREE);
55 39 chHeapFree(str_aux);
  40 + length += 10;
  41 + str_aux = join_strings(str_aux2,sensor.type,length,strlen(sensors.type),JOIN_NO_FREE);
56 42 chHeapFree(str_aux2);
57   - chHeapFree(value);
58   - return json_data;
  43 + length += strlen(sensors.type);
  44 + str_aux2 = join_strings(str_aux,"\",\"unit\":\"",length,10,JOIN_NO_FREE);
  45 + chHeapFree(str_aux);
  46 + length += 10;
  47 + str_aux = join_strings(str_aux2,sensor.unit,length,strlen(sensor.unit),JOIN_NO_FREE);
  48 + chHeapFree(str_aux2);
  49 + length += strlen(sensor.unit);
  50 + str_aux2 = join_strings(str_aux,"\",\"location\":\"",length,14,JOIN_NO_FREE);
  51 + chHeapFree(str_aux);
  52 + length += 14;
  53 + str_aux = join_strings(str_aux2,module.geoloc,length,strlen(module.geoloc),JOIN_NO_FREE);
  54 + chHeapFree(str_aux2);
  55 + length += strlen(module.geoloc);
  56 + json_statement = join_strings(str_aux,"\"}]}\0",length,5,JOIN_NO_FREE);
  57 + chHeapFree(str_aux);
  58 + return json_statement;
59 59 }
60 60  
61 61 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
161 161 free(str2);
162 162 }
163 163 return str;
164   -}
165 164 \ No newline at end of file
  165 +}
  166 +
  167 +//check if exists
  168 +//subscribe
166 169 \ No newline at end of file
... ...