Vergil Cola
/
MQTTGatewayK64
Fork of my MQTTGateway
jsmn/WeatherInfo.cpp@0:f1d3878b8dd9, 2017-04-08 (annotated)
- Committer:
- vpcola
- Date:
- Sat Apr 08 14:45:51 2017 +0000
- Revision:
- 0:f1d3878b8dd9
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vpcola | 0:f1d3878b8dd9 | 1 | #include "WeatherInfo.h" |
vpcola | 0:f1d3878b8dd9 | 2 | #include <string.h> |
vpcola | 0:f1d3878b8dd9 | 3 | #include <stdlib.h> |
vpcola | 0:f1d3878b8dd9 | 4 | |
vpcola | 0:f1d3878b8dd9 | 5 | static int jsoneq(const char * json, jsmntok_t * tok, const char * s) |
vpcola | 0:f1d3878b8dd9 | 6 | { |
vpcola | 0:f1d3878b8dd9 | 7 | if (tok->type == JSMN_STRING && (int) strlen(s) == tok->end - tok->start && |
vpcola | 0:f1d3878b8dd9 | 8 | strncmp(json + tok->start, s, tok->end - tok->start) == 0) { |
vpcola | 0:f1d3878b8dd9 | 9 | return 0; |
vpcola | 0:f1d3878b8dd9 | 10 | } |
vpcola | 0:f1d3878b8dd9 | 11 | return -1; |
vpcola | 0:f1d3878b8dd9 | 12 | } |
vpcola | 0:f1d3878b8dd9 | 13 | |
vpcola | 0:f1d3878b8dd9 | 14 | int parseweatherinfo(const char * weather, WeatherInfo & winfo) |
vpcola | 0:f1d3878b8dd9 | 15 | { |
vpcola | 0:f1d3878b8dd9 | 16 | // Parse weather information |
vpcola | 0:f1d3878b8dd9 | 17 | jsmn_parser p; |
vpcola | 0:f1d3878b8dd9 | 18 | jsmntok_t t[128]; |
vpcola | 0:f1d3878b8dd9 | 19 | |
vpcola | 0:f1d3878b8dd9 | 20 | jsmn_init(&p); |
vpcola | 0:f1d3878b8dd9 | 21 | int r = jsmn_parse(&p, weather, strlen(weather), t, sizeof(t)/sizeof(t[0])); |
vpcola | 0:f1d3878b8dd9 | 22 | // Top level must be an object |
vpcola | 0:f1d3878b8dd9 | 23 | if ((r > 0) && (t[0].type == JSMN_OBJECT)) |
vpcola | 0:f1d3878b8dd9 | 24 | { |
vpcola | 0:f1d3878b8dd9 | 25 | /* Loop over all keys of the root object */ |
vpcola | 0:f1d3878b8dd9 | 26 | for (int i = 1; i < r; i++) |
vpcola | 0:f1d3878b8dd9 | 27 | { |
vpcola | 0:f1d3878b8dd9 | 28 | if (jsoneq(weather, &t[i], "description") == 0) |
vpcola | 0:f1d3878b8dd9 | 29 | { |
vpcola | 0:f1d3878b8dd9 | 30 | winfo.weather = std::string((const char *)(weather + t[i+1].start), (size_t) t[i+1].end - t[i+1].start); |
vpcola | 0:f1d3878b8dd9 | 31 | i++; |
vpcola | 0:f1d3878b8dd9 | 32 | } |
vpcola | 0:f1d3878b8dd9 | 33 | else if (jsoneq(weather, &t[i], "icon") == 0) |
vpcola | 0:f1d3878b8dd9 | 34 | { |
vpcola | 0:f1d3878b8dd9 | 35 | winfo.icon = std::string((const char *)(weather + t[i+1].start), (size_t) t[i+1].end - t[i+1].start); |
vpcola | 0:f1d3878b8dd9 | 36 | i++; |
vpcola | 0:f1d3878b8dd9 | 37 | } |
vpcola | 0:f1d3878b8dd9 | 38 | else if (jsoneq(weather, &t[i], "temp") == 0) |
vpcola | 0:f1d3878b8dd9 | 39 | { |
vpcola | 0:f1d3878b8dd9 | 40 | std::string tempstr((const char *)(weather + t[i+1].start), (size_t) t[i+1].end - t[i+1].start); |
vpcola | 0:f1d3878b8dd9 | 41 | winfo.temperature = strtod(tempstr.c_str(), NULL); |
vpcola | 0:f1d3878b8dd9 | 42 | i++; |
vpcola | 0:f1d3878b8dd9 | 43 | } |
vpcola | 0:f1d3878b8dd9 | 44 | else if (jsoneq(weather, &t[i], "humidity") == 0) |
vpcola | 0:f1d3878b8dd9 | 45 | { |
vpcola | 0:f1d3878b8dd9 | 46 | std::string humidstr((const char *)(weather + t[i+1].start), (size_t) t[i+1].end - t[i+1].start); |
vpcola | 0:f1d3878b8dd9 | 47 | winfo.humidity = strtod(humidstr.c_str(), NULL); |
vpcola | 0:f1d3878b8dd9 | 48 | i++; |
vpcola | 0:f1d3878b8dd9 | 49 | } |
vpcola | 0:f1d3878b8dd9 | 50 | else if (jsoneq(weather, &t[i], "pressure") == 0) |
vpcola | 0:f1d3878b8dd9 | 51 | { |
vpcola | 0:f1d3878b8dd9 | 52 | std::string pressurestr((const char *)(weather + t[i+1].start), (size_t) t[i+1].end - t[i+1].start); |
vpcola | 0:f1d3878b8dd9 | 53 | winfo.pressure = strtod(pressurestr.c_str(), NULL); |
vpcola | 0:f1d3878b8dd9 | 54 | i++; |
vpcola | 0:f1d3878b8dd9 | 55 | }else |
vpcola | 0:f1d3878b8dd9 | 56 | { |
vpcola | 0:f1d3878b8dd9 | 57 | // discard the rest of the tokens |
vpcola | 0:f1d3878b8dd9 | 58 | } |
vpcola | 0:f1d3878b8dd9 | 59 | } |
vpcola | 0:f1d3878b8dd9 | 60 | return 0; |
vpcola | 0:f1d3878b8dd9 | 61 | }else |
vpcola | 0:f1d3878b8dd9 | 62 | return -1; |
vpcola | 0:f1d3878b8dd9 | 63 | } |