3 Commits

Author SHA1 Message Date
Nic Limper
77cbf92281 bugfix getlocation 2023-08-13 01:48:32 +02:00
Moritz Wirger
c4022e45f9 Fix weather icons exception (#114) 2023-08-13 01:00:47 +02:00
atc1441
a13c220565 Fixed countdown via negative count value 2023-08-12 21:43:37 +02:00
3 changed files with 39 additions and 29 deletions

View File

@@ -23,8 +23,8 @@ void drawString(TFT_eSprite &spr, String content, int16_t posx, int16_t posy, St
void initSprite(TFT_eSprite &spr, int w, int h, imgParam &imageParams);
void drawDate(String &filename, tagRecord *&taginfo, imgParam &imageParams);
void drawNumber(String &filename, int32_t count, int32_t thresholdred, tagRecord *&taginfo, imgParam &imageParams);
void drawWeather(String &filename, JsonObject &cfgobj, tagRecord *&taginfo, imgParam &imageParams);
void drawForecast(String &filename, JsonObject &cfgobj, tagRecord *&taginfo, imgParam &imageParams);
void drawWeather(String &filename, JsonObject &cfgobj, const tagRecord *taginfo, imgParam &imageParams);
void drawForecast(String &filename, JsonObject &cfgobj, const tagRecord *taginfo, imgParam &imageParams);
int getImgURL(String &filename, String URL, time_t fetched, imgParam &imageParams, String MAC);
bool getRssFeed(String &filename, String URL, String title, tagRecord *&taginfo, imgParam &imageParams);
bool getCalFeed(String &filename, String URL, String title, tagRecord *&taginfo, imgParam &imageParams);

View File

@@ -69,7 +69,7 @@ static void printLargestFreeBlock() {
/// @param timeout Request timeout
/// @param redirects Redirects handling
/// @return True on success, false on error (httpCode != 200 || deserialization error)
static bool httpGetJson(String &url, JsonDocument &json, const uint16_t timeout) //, const followRedirects_t redirects = followRedirects_t::HTTPC_DISABLE_FOLLOW_REDIRECTS)
static bool httpGetJson(String &url, JsonDocument &json, const uint16_t timeout, JsonDocument *filter = nullptr) //, const followRedirects_t redirects = followRedirects_t::HTTPC_DISABLE_FOLLOW_REDIRECTS)
{
HTTPClient http;
http.begin(url);
@@ -82,7 +82,12 @@ static bool httpGetJson(String &url, JsonDocument &json, const uint16_t timeout)
return false;
}
DeserializationError error = deserializeJson(json, http.getString());
DeserializationError error;
if (filter) {
error = deserializeJson(json, http.getString(), DeserializationOption::Filter(*filter));
} else {
error = deserializeJson(json, http.getString());
}
http.end();
if (error) {
Serial.println(error.c_str());

View File

@@ -544,6 +544,8 @@ void drawDate(String &filename, tagRecord *&taginfo, imgParam &imageParams) {
}
void drawNumber(String &filename, int32_t count, int32_t thresholdred, tagRecord *&taginfo, imgParam &imageParams) {
int32_t countTemp = count;
count = abs(count);
if (taginfo->hwType == SOLUM_SEG_UK) {
imageParams.symbols = 0x00;
if (count > 19999) {
@@ -570,7 +572,7 @@ void drawNumber(String &filename, int32_t count, int32_t thresholdred, tagRecord
initSprite(spr, imageParams.width, imageParams.height, imageParams);
spr.setTextDatum(MC_DATUM);
if (count > thresholdred) {
if (countTemp > thresholdred) {
spr.setTextColor(TFT_RED, TFT_WHITE);
} else {
spr.setTextColor(TFT_BLACK, TFT_WHITE);
@@ -591,28 +593,28 @@ void drawNumber(String &filename, int32_t count, int32_t thresholdred, tagRecord
/// @param id Icon identifier/index
/// @param isNight Use night icons (true) or not (false)
/// @return String reference to icon
const String &getWeatherIcon(const uint8_t id, const bool isNight = false) {
static const String weatherIcons[] = {"\uf00d", "\uf00c", "\uf002", "\uf013", "\uf013", "\uf014", "", "", "\uf014", "", "",
"\uf01a", "", "\uf01a", "", "\uf01a", "\uf017", "\uf017", "", "", "",
"\uf019", "", "\uf019", "", "\uf019", "\uf015", "\uf015", "", "", "",
"\uf01b", "", "\uf01b", "", "\uf01b", "", "\uf076", "", "", "\uf01a",
"\uf01a", "\uf01a", "", "", "\uf064", "\uf064", "", "", "", "",
"", "", "", "", "\uf01e", "\uf01d", "", "", "\uf01e"};
const String getWeatherIcon(const uint8_t id, const bool isNight = false) {
const String weatherIcons[] = {"\uf00d", "\uf00c", "\uf002", "\uf013", "\uf013", "\uf014", "", "", "\uf014", "", "",
"\uf01a", "", "\uf01a", "", "\uf01a", "\uf017", "\uf017", "", "", "",
"\uf019", "", "\uf019", "", "\uf019", "\uf015", "\uf015", "", "", "",
"\uf01b", "", "\uf01b", "", "\uf01b", "", "\uf076", "", "", "\uf01a",
"\uf01a", "\uf01a", "", "", "\uf064", "\uf064", "", "", "", "",
"", "", "", "", "\uf01e", "\uf01d", "", "", "\uf01e"};
if (isNight && id <= 3) {
static const String nightIcons[] = {"\uf02e", "\uf083", "\uf086"};
const String nightIcons[] = {"\uf02e", "\uf083", "\uf086"};
return nightIcons[id];
}
return weatherIcons[id];
}
void drawWeather(String &filename, JsonObject &cfgobj, tagRecord *&taginfo, imgParam &imageParams) {
void drawWeather(String &filename, JsonObject &cfgobj, const tagRecord *taginfo, imgParam &imageParams) {
wsLog("get weather");
getLocation(cfgobj);
String lat = cfgobj["#lat"];
String lon = cfgobj["#lon"];
String tz = cfgobj["#tz"];
const String lat = cfgobj["#lat"];
const String lon = cfgobj["#lon"];
const String tz = cfgobj["#tz"];
StaticJsonDocument<1000> doc;
const bool success = util::httpGetJson("https://api.open-meteo.com/v1/forecast?latitude=" + lat + "&longitude=" + lon + "&current_weather=true&windspeed_unit=ms&timezone=" + tz, doc, 5000);
@@ -624,7 +626,7 @@ void drawWeather(String &filename, JsonObject &cfgobj, tagRecord *&taginfo, imgP
const double temperature = currentWeather["temperature"].as<double>();
const int windspeed = currentWeather["windspeed"].as<int>();
const int winddirection = currentWeather["winddirection"].as<int>();
const uint8_t isday = currentWeather["is_day"].as<int>();
const bool isNight = currentWeather["is_day"].as<int>() == 0;
uint8_t weathercode = currentWeather["weathercode"].as<int>();
if (weathercode > 40) weathercode -= 40;
const int beaufort = windSpeedToBeaufort(windspeed);
@@ -632,12 +634,12 @@ void drawWeather(String &filename, JsonObject &cfgobj, tagRecord *&taginfo, imgP
doc.clear();
if (taginfo->hwType == SOLUM_SEG_UK) {
static const String weatherText[] = {"sun", "sun", "sun", "CLDY", "CLDY", "FOG", "", "", "FOG", "", "",
"DRZL", "", "DRZL", "", "DRZL", "ice", "ice", "", "", "",
"rain", "", "rain", "", "rain", "ice", "ice", "", "", "",
"SNOW", "", "SNOW", "", "SNOW", "", "SNOW", "", "", "rain",
"rain", "rain", "", "", "SNOW", "SNOW", "", "", "", "",
"", "", "", "", "STRM", "HAIL", "", "", "HAIL"};
const String weatherText[] = {"sun", "sun", "sun", "CLDY", "CLDY", "FOG", "", "", "FOG", "", "",
"DRZL", "", "DRZL", "", "DRZL", "ice", "ice", "", "", "",
"rain", "", "rain", "", "rain", "ice", "ice", "", "", "",
"SNOW", "", "SNOW", "", "SNOW", "", "SNOW", "", "", "rain",
"rain", "rain", "", "", "SNOW", "SNOW", "", "", "", "",
"", "", "", "", "STRM", "HAIL", "", "", "HAIL"};
if (temperature < -9.9) {
sprintf(imageParams.segments, "%3d^%2d%-4.4s", static_cast<int>(temperature), beaufort, weatherText[weathercode].c_str());
imageParams.symbols = 0x00;
@@ -668,7 +670,7 @@ void drawWeather(String &filename, JsonObject &cfgobj, tagRecord *&taginfo, imgP
? TFT_RED
: TFT_BLACK;
const auto &icon = doc["icon"];
drawString(spr, getWeatherIcon(weathercode, isday == 0), icon[0], icon[1], "/fonts/weathericons.ttf", icon[3], iconcolor, icon[2]);
drawString(spr, getWeatherIcon(weathercode, isNight), icon[0], icon[1], "/fonts/weathericons.ttf", icon[3], iconcolor, icon[2]);
const auto &dir = doc["dir"];
drawString(spr, windDirectionIcon(winddirection), dir[0], dir[1], "/fonts/weathericons.ttf", TC_DATUM, TFT_BLACK, dir[2]);
if (weathercode > 10) {
@@ -680,9 +682,7 @@ void drawWeather(String &filename, JsonObject &cfgobj, tagRecord *&taginfo, imgP
spr.deleteSprite();
}
void drawForecast(String &filename, JsonObject &cfgobj, tagRecord *&taginfo, imgParam &imageParams) {
TFT_eSprite spr = TFT_eSprite(&tft);
void drawForecast(String &filename, JsonObject &cfgobj, const tagRecord *taginfo, imgParam &imageParams) {
wsLog("get weather");
getLocation(cfgobj);
@@ -696,6 +696,7 @@ void drawForecast(String &filename, JsonObject &cfgobj, tagRecord *&taginfo, img
return;
}
TFT_eSprite spr = TFT_eSprite(&tft);
tft.setTextWrap(false, false);
StaticJsonDocument<512> loc;
@@ -1197,8 +1198,12 @@ void getLocation(JsonObject &cfgobj) {
if (util::isEmptyOrNull(lat) || util::isEmptyOrNull(lon)) {
wsLog("get location");
StaticJsonDocument<80> filter;
filter["results"][0]["latitude"] = true;
filter["results"][0]["longitude"] = true;
filter["results"][0]["timezone"] = true;
StaticJsonDocument<1000> doc;
if (util::httpGetJson("https://geocoding-api.open-meteo.com/v1/search?name=" + urlEncode(cfgobj["location"]) + "&count=1", doc, 5000)) {
if (util::httpGetJson("https://geocoding-api.open-meteo.com/v1/search?name=" + urlEncode(cfgobj["location"]) + "&count=1", doc, 5000, &filter)) {
cfgobj["#lat"] = doc["results"][0]["latitude"].as<String>();
cfgobj["#lon"] = doc["results"][0]["longitude"].as<String>();
cfgobj["#tz"] = doc["results"][0]["timezone"].as<String>();