|
| 1 | +#include "OpenWeatherMap.h" |
| 2 | + |
| 3 | +String AirliftOpenWeatherMap::buildUrlCurrent(String appId, String location) { |
| 4 | + String units = OWM_METRIC ? "metric" : "imperial"; |
| 5 | + return "http://api.openweathermap.org/data/2.5/weather?q=" + location + "&appid=" + appId + "&units=" + units + "&lang=" + String(OWM_LANGUAGE); |
| 6 | +} |
| 7 | + |
| 8 | +String AirliftOpenWeatherMap::buildUrlForecast(String appId, String location) { |
| 9 | + String units = OWM_METRIC ? "metric" : "imperial"; |
| 10 | + return "http://api.openweathermap.org/data/2.5/forecast?q=" + location + "&cnt=6&appid=" + appId + "&units=" + units + "&lang=" + String(OWM_LANGUAGE); |
| 11 | +} |
| 12 | + |
| 13 | +String AirliftOpenWeatherMap::getMeteoconIcon(String icon) { |
| 14 | + // clear sky |
| 15 | + // 01d |
| 16 | + if (icon == "01d") { |
| 17 | + return "B"; |
| 18 | + } |
| 19 | + // 01n |
| 20 | + if (icon == "01n") { |
| 21 | + return "C"; |
| 22 | + } |
| 23 | + // few clouds |
| 24 | + // 02d |
| 25 | + if (icon == "02d") { |
| 26 | + return "H"; |
| 27 | + } |
| 28 | + // 02n |
| 29 | + if (icon == "02n") { |
| 30 | + return "4"; |
| 31 | + } |
| 32 | + // scattered clouds |
| 33 | + // 03d |
| 34 | + if (icon == "03d") { |
| 35 | + return "N"; |
| 36 | + } |
| 37 | + // 03n |
| 38 | + if (icon == "03n") { |
| 39 | + return "5"; |
| 40 | + } |
| 41 | + // broken clouds |
| 42 | + // 04d |
| 43 | + if (icon == "04d") { |
| 44 | + return "Y"; |
| 45 | + } |
| 46 | + // 04n |
| 47 | + if (icon == "04n") { |
| 48 | + return "%"; |
| 49 | + } |
| 50 | + // shower rain |
| 51 | + // 09d |
| 52 | + if (icon == "09d") { |
| 53 | + return "R"; |
| 54 | + } |
| 55 | + // 09n |
| 56 | + if (icon == "09n") { |
| 57 | + return "8"; |
| 58 | + } |
| 59 | + // rain |
| 60 | + // 10d |
| 61 | + if (icon == "10d") { |
| 62 | + return "Q"; |
| 63 | + } |
| 64 | + // 10n |
| 65 | + if (icon == "10n") { |
| 66 | + return "7"; |
| 67 | + } |
| 68 | + // thunderstorm |
| 69 | + // 11d |
| 70 | + if (icon == "11d") { |
| 71 | + return "P"; |
| 72 | + } |
| 73 | + // 11n |
| 74 | + if (icon == "11n") { |
| 75 | + return "6"; |
| 76 | + } |
| 77 | + // snow |
| 78 | + // 13d |
| 79 | + if (icon == "13d") { |
| 80 | + return "W"; |
| 81 | + } |
| 82 | + // 13n |
| 83 | + if (icon == "13n") { |
| 84 | + return "#"; |
| 85 | + } |
| 86 | + // mist |
| 87 | + // 50d |
| 88 | + if (icon == "50d") { |
| 89 | + return "M"; |
| 90 | + } |
| 91 | + // 50n |
| 92 | + if (icon == "50n") { |
| 93 | + return "M"; |
| 94 | + } |
| 95 | + // Nothing matched: N/A |
| 96 | + return ")"; |
| 97 | + |
| 98 | +} |
| 99 | + |
| 100 | +bool AirliftOpenWeatherMap::updateCurrent(OpenWeatherMapCurrentData &data, String json) |
| 101 | +{ |
| 102 | + Serial->println("updateCurrent()"); |
| 103 | + DynamicJsonDocument doc(2000); |
| 104 | + //StaticJsonDocument<2000> doc; |
| 105 | + |
| 106 | + DeserializationError error = deserializeJson(doc, json); |
| 107 | + if (error) { |
| 108 | + Serial->println(String("deserializeJson() failed: ") + (const char *)error.c_str()); |
| 109 | + Serial->println(json); |
| 110 | + setError(String("deserializeJson() failed: ") + error.c_str()); |
| 111 | + return false; |
| 112 | + } |
| 113 | + |
| 114 | + int code = (int) doc["cod"]; |
| 115 | + if(code != 200) |
| 116 | + { |
| 117 | + Serial->println(String("OpenWeatherMap error: ") + (const char *)doc["message"]); |
| 118 | + setError(String("OpenWeatherMap error: ") + (const char *)doc["message"]); |
| 119 | + return false; |
| 120 | + } |
| 121 | + |
| 122 | + data.lat = (float) doc["coord"]["lat"]; |
| 123 | + data.lon = (float) doc["coord"]["lon"]; |
| 124 | + |
| 125 | + data.main = (const char*) doc["weather"][0]["main"]; |
| 126 | + data.description = (const char*) doc["weather"][0]["description"]; |
| 127 | + data.icon = (const char*) doc["weather"][0]["icon"]; |
| 128 | + |
| 129 | + data.cityName = (const char*) doc["name"]; |
| 130 | + data.visibility = (uint16_t) doc["visibility"]; |
| 131 | + data.timezone = (time_t) doc["timezone"]; |
| 132 | + |
| 133 | + data.country = (const char*) doc["sys"]["country"]; |
| 134 | + data.observationTime = (time_t) doc["dt"]; |
| 135 | + data.sunrise = (time_t) doc["sys"]["sunrise"]; |
| 136 | + data.sunset = (time_t) doc["sys"]["sunset"]; |
| 137 | + |
| 138 | + data.temp = (float) doc["main"]["temp"]; |
| 139 | + data.pressure = (uint16_t) doc["main"]["pressure"]; |
| 140 | + data.humidity = (uint8_t) doc["main"]["humidity"]; |
| 141 | + data.tempMin = (float) doc["main"]["temp_min"]; |
| 142 | + data.tempMax = (float) doc["main"]["temp_max"]; |
| 143 | + |
| 144 | + data.windSpeed = (float) doc["wind"]["speed"]; |
| 145 | + data.windDeg = (float) doc["wind"]["deg"]; |
| 146 | + return true; |
| 147 | +} |
| 148 | + |
| 149 | +bool AirliftOpenWeatherMap::updateForecast(OpenWeatherMapForecastData &data, String json, int day) |
| 150 | +{ |
| 151 | + Serial->println("updateForecast()"); |
| 152 | + DynamicJsonDocument doc(5000); |
| 153 | + //StaticJsonDocument<5000> doc; |
| 154 | + |
| 155 | + DeserializationError error = deserializeJson(doc, json); |
| 156 | + if (error) { |
| 157 | + Serial->println(String("deserializeJson() failed: ") + (const char *)error.c_str()); |
| 158 | + Serial->println(json); |
| 159 | + setError(String("deserializeJson() failed: ") + error.c_str()); |
| 160 | + return false; |
| 161 | + } |
| 162 | + |
| 163 | + int code = (int) doc["cod"]; |
| 164 | + if(code != 200) |
| 165 | + { |
| 166 | + Serial->println(String("OpenWeatherMap error: ") + (const char *)doc["message"]); |
| 167 | + setError(String("OpenWeatherMap error: ") + (const char *)doc["message"]); |
| 168 | + return false; |
| 169 | + } |
| 170 | + |
| 171 | + data.observationTime = (time_t) doc["list"][day]["dt"]; |
| 172 | + |
| 173 | + data.temp = (float) doc["list"][day]["main"]["temp"]; |
| 174 | + data.pressure = (uint16_t) doc["list"][day]["main"]["pressure"]; |
| 175 | + data.humidity = (uint8_t) doc["list"][day]["main"]["humidity"]; |
| 176 | + data.tempMin = (float) doc["list"][day]["main"]["temp_min"]; |
| 177 | + data.tempMax = (float) doc["list"][day]["main"]["temp_max"]; |
| 178 | + |
| 179 | + data.main = (const char*) doc["list"][day]["weather"][0]["main"]; |
| 180 | + data.description = (const char*) doc["list"][day]["weather"][0]["description"]; |
| 181 | + data.icon = (const char*) doc["list"][day]["weather"][0]["icon"]; |
| 182 | + return true; |
| 183 | +} |
0 commit comments