Home Alert System
Dependencies: PWM_Tone_Library DHT
M2XStreamClient/LocationParseFunctions.h@3:78f223d34f36, 2019-03-05 (annotated)
- Committer:
- ethaderu
- Date:
- Tue Mar 05 02:34:44 2019 +0000
- Revision:
- 3:78f223d34f36
Publish 1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ethaderu | 3:78f223d34f36 | 1 | #ifndef LocationParseFunctions_h |
ethaderu | 3:78f223d34f36 | 2 | #define LocationParseFunctions_h |
ethaderu | 3:78f223d34f36 | 3 | |
ethaderu | 3:78f223d34f36 | 4 | // Data structures and functions used to parse locations |
ethaderu | 3:78f223d34f36 | 5 | |
ethaderu | 3:78f223d34f36 | 6 | #define LOCATION_BUF_LEN 20 |
ethaderu | 3:78f223d34f36 | 7 | |
ethaderu | 3:78f223d34f36 | 8 | typedef struct { |
ethaderu | 3:78f223d34f36 | 9 | uint16_t state; |
ethaderu | 3:78f223d34f36 | 10 | char name_str[LOCATION_BUF_LEN + 1]; |
ethaderu | 3:78f223d34f36 | 11 | double latitude; |
ethaderu | 3:78f223d34f36 | 12 | double longitude; |
ethaderu | 3:78f223d34f36 | 13 | double elevation; |
ethaderu | 3:78f223d34f36 | 14 | char timestamp_str[LOCATION_BUF_LEN + 1]; |
ethaderu | 3:78f223d34f36 | 15 | int index; |
ethaderu | 3:78f223d34f36 | 16 | |
ethaderu | 3:78f223d34f36 | 17 | location_read_callback callback; |
ethaderu | 3:78f223d34f36 | 18 | void* context; |
ethaderu | 3:78f223d34f36 | 19 | } location_parsing_context_state; |
ethaderu | 3:78f223d34f36 | 20 | |
ethaderu | 3:78f223d34f36 | 21 | #define WAITING_NAME 0x1 |
ethaderu | 3:78f223d34f36 | 22 | #define WAITING_LATITUDE 0x2 |
ethaderu | 3:78f223d34f36 | 23 | #define WAITING_LONGITUDE 0x4 |
ethaderu | 3:78f223d34f36 | 24 | #define WAITING_ELEVATION 0x8 |
ethaderu | 3:78f223d34f36 | 25 | #define WAITING_TIMESTAMP 0x10 |
ethaderu | 3:78f223d34f36 | 26 | |
ethaderu | 3:78f223d34f36 | 27 | #define GOT_NAME 0x20 |
ethaderu | 3:78f223d34f36 | 28 | #define GOT_LATITUDE 0x40 |
ethaderu | 3:78f223d34f36 | 29 | #define GOT_LONGITUDE 0x80 |
ethaderu | 3:78f223d34f36 | 30 | #define GOT_ELEVATION 0x100 |
ethaderu | 3:78f223d34f36 | 31 | #define GOT_TIMESTAMP 0x200 |
ethaderu | 3:78f223d34f36 | 32 | |
ethaderu | 3:78f223d34f36 | 33 | #define GOT_LOCATION (GOT_NAME | GOT_LATITUDE | GOT_LONGITUDE | GOT_ELEVATION | GOT_TIMESTAMP) |
ethaderu | 3:78f223d34f36 | 34 | #define TEST_GOT_LOCATION(state_) (((state_) & GOT_LOCATION) == GOT_LOCATION) |
ethaderu | 3:78f223d34f36 | 35 | |
ethaderu | 3:78f223d34f36 | 36 | #define TEST_IS_NAME(state_) (((state_) & (WAITING_NAME | GOT_NAME)) == WAITING_NAME) |
ethaderu | 3:78f223d34f36 | 37 | #define TEST_IS_LATITUDE(state_) (((state_) & (WAITING_LATITUDE | GOT_LATITUDE)) \ |
ethaderu | 3:78f223d34f36 | 38 | == WAITING_LATITUDE) |
ethaderu | 3:78f223d34f36 | 39 | #define TEST_IS_LONGITUDE(state_) (((state_) & (WAITING_LONGITUDE | GOT_LONGITUDE)) \ |
ethaderu | 3:78f223d34f36 | 40 | == WAITING_LONGITUDE) |
ethaderu | 3:78f223d34f36 | 41 | #define TEST_IS_ELEVATION(state_) (((state_) & (WAITING_ELEVATION | GOT_ELEVATION)) \ |
ethaderu | 3:78f223d34f36 | 42 | == WAITING_ELEVATION) |
ethaderu | 3:78f223d34f36 | 43 | #define TEST_IS_TIMESTAMP(state_) (((state_) & (WAITING_TIMESTAMP | GOT_TIMESTAMP)) \ |
ethaderu | 3:78f223d34f36 | 44 | == WAITING_TIMESTAMP) |
ethaderu | 3:78f223d34f36 | 45 | |
ethaderu | 3:78f223d34f36 | 46 | static void on_location_key_found(jsonlite_callback_context* context, |
ethaderu | 3:78f223d34f36 | 47 | jsonlite_token* token) { |
ethaderu | 3:78f223d34f36 | 48 | location_parsing_context_state* state = |
ethaderu | 3:78f223d34f36 | 49 | (location_parsing_context_state*) context->client_state; |
ethaderu | 3:78f223d34f36 | 50 | if (strncmp((const char*) token->start, "waypoints", 9) == 0) { |
ethaderu | 3:78f223d34f36 | 51 | // only parses those locations in waypoints, skip the outer one |
ethaderu | 3:78f223d34f36 | 52 | state->state = 0; |
ethaderu | 3:78f223d34f36 | 53 | } else if (strncmp((const char*) token->start, "name", 4) == 0) { |
ethaderu | 3:78f223d34f36 | 54 | state->state |= WAITING_NAME; |
ethaderu | 3:78f223d34f36 | 55 | } else if (strncmp((const char*) token->start, "latitude", 8) == 0) { |
ethaderu | 3:78f223d34f36 | 56 | state->state |= WAITING_LATITUDE; |
ethaderu | 3:78f223d34f36 | 57 | } else if (strncmp((const char*) token->start, "longitude", 9) == 0) { |
ethaderu | 3:78f223d34f36 | 58 | state->state |= WAITING_LONGITUDE; |
ethaderu | 3:78f223d34f36 | 59 | } else if (strncmp((const char*) token->start, "elevation", 9) == 0) { |
ethaderu | 3:78f223d34f36 | 60 | state->state |= WAITING_ELEVATION; |
ethaderu | 3:78f223d34f36 | 61 | } else if (strncmp((const char*) token->start, "timestamp", 9) == 0) { |
ethaderu | 3:78f223d34f36 | 62 | state->state |= WAITING_TIMESTAMP; |
ethaderu | 3:78f223d34f36 | 63 | } |
ethaderu | 3:78f223d34f36 | 64 | } |
ethaderu | 3:78f223d34f36 | 65 | |
ethaderu | 3:78f223d34f36 | 66 | static void on_location_string_found(jsonlite_callback_context* context, |
ethaderu | 3:78f223d34f36 | 67 | jsonlite_token* token) { |
ethaderu | 3:78f223d34f36 | 68 | location_parsing_context_state* state = |
ethaderu | 3:78f223d34f36 | 69 | (location_parsing_context_state*) context->client_state; |
ethaderu | 3:78f223d34f36 | 70 | |
ethaderu | 3:78f223d34f36 | 71 | if (TEST_IS_NAME(state->state)) { |
ethaderu | 3:78f223d34f36 | 72 | strncpy(state->name_str, (const char*) token->start, |
ethaderu | 3:78f223d34f36 | 73 | MIN(token->end - token->start, LOCATION_BUF_LEN)); |
ethaderu | 3:78f223d34f36 | 74 | state->name_str[MIN(token->end - token->start, LOCATION_BUF_LEN)] = '\0'; |
ethaderu | 3:78f223d34f36 | 75 | state->state |= GOT_NAME; |
ethaderu | 3:78f223d34f36 | 76 | } else if (TEST_IS_LATITUDE(state->state)) { |
ethaderu | 3:78f223d34f36 | 77 | state->latitude = atof((const char*) token->start); |
ethaderu | 3:78f223d34f36 | 78 | state->state |= GOT_LATITUDE; |
ethaderu | 3:78f223d34f36 | 79 | } else if (TEST_IS_LONGITUDE(state->state)) { |
ethaderu | 3:78f223d34f36 | 80 | state->longitude = atof((const char*) token->start); |
ethaderu | 3:78f223d34f36 | 81 | state->state |= GOT_LONGITUDE; |
ethaderu | 3:78f223d34f36 | 82 | } else if (TEST_IS_ELEVATION(state->state)) { |
ethaderu | 3:78f223d34f36 | 83 | state->elevation = atof((const char*) token->start); |
ethaderu | 3:78f223d34f36 | 84 | state->state |= GOT_ELEVATION; |
ethaderu | 3:78f223d34f36 | 85 | } else if (TEST_IS_TIMESTAMP(state->state)) { |
ethaderu | 3:78f223d34f36 | 86 | strncpy(state->timestamp_str, (const char*) token->start, |
ethaderu | 3:78f223d34f36 | 87 | MIN(token->end - token->start, LOCATION_BUF_LEN)); |
ethaderu | 3:78f223d34f36 | 88 | state->timestamp_str[MIN(token->end - token->start, LOCATION_BUF_LEN)] = '\0'; |
ethaderu | 3:78f223d34f36 | 89 | state->state |= GOT_TIMESTAMP; |
ethaderu | 3:78f223d34f36 | 90 | } |
ethaderu | 3:78f223d34f36 | 91 | |
ethaderu | 3:78f223d34f36 | 92 | if (TEST_GOT_LOCATION(state->state)) { |
ethaderu | 3:78f223d34f36 | 93 | state->callback(state->name_str, state->latitude, state->longitude, |
ethaderu | 3:78f223d34f36 | 94 | state->elevation, state->timestamp_str, state->index++, |
ethaderu | 3:78f223d34f36 | 95 | state->context); |
ethaderu | 3:78f223d34f36 | 96 | state->state = 0; |
ethaderu | 3:78f223d34f36 | 97 | } |
ethaderu | 3:78f223d34f36 | 98 | } |
ethaderu | 3:78f223d34f36 | 99 | |
ethaderu | 3:78f223d34f36 | 100 | #endif /* LocationParseFunctions_h */ |