Commit f7cd72e58980afb7f99db838362ed02cbd976faf
1 parent
f6f543a3
JSON code in progress
Showing
2 changed files
with
36 additions
and
10 deletions
Project/applications/smartcities/include/json.h
1 | #ifndef JSON_H | 1 | #ifndef JSON_H |
2 | #define JSON_H | 2 | #define JSON_H |
3 | 3 | ||
4 | +#include <string.h> | ||
5 | +#include <stdint.h> | ||
6 | + | ||
4 | #define JSON_POST_OK 0 | 7 | #define JSON_POST_OK 0 |
5 | #define JSON_COMM_ERROR 1 | 8 | #define JSON_COMM_ERROR 1 |
6 | #define JSON_OTHER_ERROR 2 | 9 | #define JSON_OTHER_ERROR 2 |
7 | 10 | ||
8 | -char* prepare_json_statement(char* data, uint32_t length); | 11 | +char* prepare_json_statement(char** data, uint32_t nObservations); |
9 | char* prepare_observation(char* observation, uint32_t length); | 12 | char* prepare_observation(char* observation, uint32_t length); |
10 | uint8_t send_json(char* statement, uint32_t length); | 13 | uint8_t send_json(char* statement, uint32_t length); |
11 | uint32_t find_next_index(char* string, uint32_t length, char delimiter); | 14 | uint32_t find_next_index(char* string, uint32_t length, char delimiter); |
12 | -char* join_strings(char* str1, char* str2, uint32_t len1, uint32_t len2) | 15 | +char* join_strings(char* str1, char* str2, uint32_t len1, uint32_t len2, bool) |
13 | 16 | ||
14 | #endif | 17 | #endif |
Project/applications/smartcities/json.c
1 | -char* prepare_json_statement(char* data, uint32_t length) | 1 | +#include "json.h" |
2 | + | ||
3 | +char* prepare_json_statement(char** data, uint32_t nObservations) | ||
2 | { | 4 | { |
3 | 5 | ||
4 | } | 6 | } |
5 | 7 | ||
6 | char* prepare_observation(char* observation, uint32_t length) | 8 | char* prepare_observation(char* observation, uint32_t length) |
7 | { | 9 | { |
8 | - | 10 | + char *json_data, *str_aux, *str_aux2; |
11 | + int value_length = find_next_index(observation,length,','); | ||
12 | + int timestamp_length = length - (value_length + 1); | ||
13 | + char *value = (char*) chHeapAlloc(NULL,value_length+1); | ||
14 | + char *timestamp = (char*) chHeapAlloc(NULL,timestamp_length+1); | ||
15 | + strncpy(value,observation,value_length); | ||
16 | + strncpy(timestamp,observation+(value_length+1),timestamp_length); | ||
17 | + value[value_length] = '\0'; | ||
18 | + timestamp[timestamp_length] = '\0'; | ||
19 | + str_aux = join_strings("{\"value\":\"",value,10,value_length,0); | ||
20 | + str_aux2 = join_strings(str_aux,"\",\"timestamp\":\"",10+value_length,15,0); | ||
21 | + str_aux2 = join_strings(str_aux2,timestamp,25+value_length,timestamp_length,1); | ||
22 | + json_data = join_strings(str_aux2,"\"}",25+value_length+timestamp_length,2,0); | ||
23 | + chHeapFree(str_aux); | ||
24 | + chHeapFree(str_aux2); | ||
25 | + chHeapFree(value); | ||
26 | + chHeapFree(timestamp); | ||
27 | + return json_data; | ||
9 | } | 28 | } |
10 | 29 | ||
11 | uint8_t send_json(char* statement, uint32_t length) | 30 | uint8_t send_json(char* statement, uint32_t length) |
@@ -31,14 +50,18 @@ uint32_t find_next_index(char* string, uint32_t length, char delimiter) | @@ -31,14 +50,18 @@ uint32_t find_next_index(char* string, uint32_t length, char delimiter) | ||
31 | return -1; | 50 | return -1; |
32 | } | 51 | } |
33 | 52 | ||
34 | -char* join_strings(char* str1, char* str2, uint32_t len1, uint32_t len2) | 53 | +char* join_strings(char* str1, char* str2, uint32_t len1, uint32_t len2, uint8_t free_mem) |
35 | { | 54 | { |
36 | char* str = (char*) chHeapAlloc(NULL,len1+len2+1); | 55 | char* str = (char*) chHeapAlloc(NULL,len1+len2+1); |
37 | - /*strcat(str,str1); | ||
38 | - strcat(str,str2); | ||
39 | - USAR STRNCAT | ||
40 | - */ | ||
41 | - | 56 | + strncpy(str,str1,len1); |
57 | + str[len1] = '\0'; | ||
58 | + strncat(str,str2,len2); | ||
59 | + if(free_mem) | ||
60 | + { | ||
61 | + free(str1); | ||
62 | + free(str2); | ||
63 | + } | ||
64 | + return str; | ||
42 | } | 65 | } |
43 | 66 | ||
44 | /* | 67 | /* |