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