Commit 0af9274faee8680c86d3ca578be5e4067ed6a2ef

Authored by Imanol-Mikel Barba Sabariego
1 parent b13c6408

JSON finished

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 callbacks.c module.c sensors.c
  13 +USER_SRC = main.c httpClient.c callbacks.c module.c sensors.c json.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"
5   -#define MODULE_ID_LENGTH 6
  4 +#define SERVER_IP "10.0.60.241"
  5 +#define SERVER_HOSTNAME SERVER_IP
  6 +#define MODULE_ID_LENGTH 6
6 7  
7 8 #endif
8 9 \ No newline at end of file
... ...
Project/applications/smartcities/include/json.h
... ... @@ -8,6 +8,7 @@
8 8 #include "module.h"
9 9 #include <string.h>
10 10 #include <stdio.h>
  11 +#include "libwismart.h"
11 12  
12 13 #define JSON_POST_OK 0
13 14 #define JSON_COMM_ERROR 1
... ... @@ -19,7 +20,7 @@
19 20 char* prepare_json_statement(char** data, uint32_t nObservations);
20 21 char* prepare_json_register_statement(module mod, sensor sens);
21 22 char* prepare_observation(char* observation, uint32_t length);
22   -uint8_t send_observation(char* statement, uint32_t length, char* provider_ID, char* sensor_ID);
  23 +uint8_t send_json(char* statement, uint32_t length, char* provider_ID, char* sensor_ID);
23 24 uint32_t find_next_index(char* string, uint32_t length, char delimiter);
24 25 char* join_strings(char* str1, char* str2, uint32_t len1, uint32_t len2, uint8_t free_mem);
25 26  
... ...
Project/applications/smartcities/json.c
... ... @@ -21,7 +21,7 @@ char* prepare_json_observation_statement(char** data, uint32_t nObservations)
21 21  
22 22 char* prepare_json_register_statement(module mod, sensor sens)
23 23 {
24   - unsigned int i, length, observation_length;
  24 + unsigned int length;
25 25 char *json_statement, *str_aux, *str_aux2;
26 26 char ID[MODULE_ID_LENGTH+3];
27 27 length = 23;
... ... @@ -46,7 +46,7 @@ char* prepare_json_register_statement(module mod, sensor sens)
46 46 length += 10;
47 47 str_aux = join_strings(str_aux2,sens.unit,length,strlen(sens.unit),JOIN_NO_FREE);
48 48 chHeapFree(str_aux2);
49   - length += strlen(sensor.unit);
  49 + length += strlen(sens.unit);
50 50 str_aux2 = join_strings(str_aux,"\",\"location\":\"",length,14,JOIN_NO_FREE);
51 51 chHeapFree(str_aux);
52 52 length += 14;
... ... @@ -86,32 +86,33 @@ char* prepare_observation(char* observation, uint32_t length)
86 86 return json_data;
87 87 }
88 88  
89   -uint8_t send_observation(char* statement, uint32_t length, char* provider_ID, char* sensor_ID)
  89 +uint8_t send_json(char* statement, uint32_t length, char* provider_ID, char* sensor_ID)
90 90 {
91 91 int connectivity, response_code;
92 92 char* URL;
  93 + int server_hostname_length = strlen(SERVER_HOSTNAME);
93 94 connectivity = libwismart_IsConnected();
94   - if(connectivity != WISMART_CONNECT)
  95 + if(connectivity != WISMART_CONNECTED)
95 96 {
96 97 return JSON_COMM_ERROR;
97 98 }
98   - if(sensor_ID == NULL)
  99 + if(sensor_ID == NULL) //Register sensor
99 100 {
100   - URL = (char*) chHeapAlloc(NULL,17+SERVER_HOSTNAME_LENGTH+strlen(provider_ID));
  101 + URL = (char*) chHeapAlloc(NULL,17+server_hostname_length+strlen(provider_ID));
101 102 strcpy(URL,"http://");
102 103 strcpy(URL+7,SERVER_HOSTNAME);
103   - strcpy(URL+7+SERVER_HOSTNAME_LENGTH,"/catalog/");
104   - strcpy(URL+16+SERVER_HOSTNAME_LENGTH,provider_ID);
  104 + strcpy(URL+7+server_hostname_length,"/catalog/");
  105 + strcpy(URL+16+server_hostname_length,provider_ID);
105 106 }
106   - else
  107 + else //Post data
107 108 {
108   - URL = (char*) chHeapAlloc(NULL,18+SERVER_HOSTNAME_LENGTH+strlen(provider_ID)+strlen(sensor_ID));
  109 + URL = (char*) chHeapAlloc(NULL,18+server_hostname_length+strlen(provider_ID)+strlen(sensor_ID));
109 110 strcpy(URL,"http://");
110 111 strcpy(URL+7,SERVER_HOSTNAME);
111   - strcpy(URL+7+SERVER_HOSTNAME_LENGTH,"/data/");
112   - strcpy(URL+16+SERVER_HOSTNAME_LENGTH,provider_ID);
113   - strcpy(URL+16+SERVER_HOSTNAME_LENGTH+strlen(provider_ID),"/");
114   - strcpy(URL+17+SERVER_HOSTNAME_LENGTH+strlen(provider_ID),sensor_ID);
  112 + strcpy(URL+7+server_hostname_length,"/data/");
  113 + strcpy(URL+16+server_hostname_length,provider_ID);
  114 + strcpy(URL+16+server_hostname_length+strlen(provider_ID),"/");
  115 + strcpy(URL+17+server_hostname_length+strlen(provider_ID),sensor_ID);
115 116 }
116 117  
117 118 /* SELF-REMINDER
... ... @@ -121,8 +122,9 @@ uint8_t send_observation(char* statement, uint32_t length, char* provider_ID, ch
121 122 * --Imanol
122 123 */
123 124  
124   - response_code = http_post(URL,statement,URL_length,length);
125   - if(response code == 200)
  125 + //response_code = http_post(URL,statement,URL_length,length);
  126 + response_code = 200; //placeholder
  127 + if(response_code == 200)
126 128 {
127 129 return JSON_POST_OK;
128 130 }
... ...