From ca39f81cfd6e2f89513e9c2b9597abac632a15e7 Mon Sep 17 00:00:00 2001 From: naheedsa <97088153+naheedsa@users.noreply.github.com> Date: Mon, 4 Mar 2024 15:28:00 +0300 Subject: [PATCH] Migrate Weather watcher (#2018) --- drivers/weather/weatherwatcher.cpp | 104 ++++++++++++++--------------- drivers/weather/weatherwatcher.h | 22 ++++-- 2 files changed, 67 insertions(+), 59 deletions(-) diff --git a/drivers/weather/weatherwatcher.cpp b/drivers/weather/weatherwatcher.cpp index 11e1f25ce6..cf090fa9ae 100644 --- a/drivers/weather/weatherwatcher.cpp +++ b/drivers/weather/weatherwatcher.cpp @@ -53,7 +53,7 @@ const char *WeatherWatcher::getDefaultName() bool WeatherWatcher::Connect() { - if (watchFileT[0].text == nullptr || watchFileT[0].text[0] == '\0') + if (watchFileTP[0].getText() == nullptr || watchFileTP[0].getText()[0] == '\0') { LOG_ERROR("Watch file must be specified first in options."); return false; @@ -79,7 +79,7 @@ bool WeatherWatcher::createPropertiesFromMap() double minOK = 0, maxOK = 0, percWarn = 15; for (auto const &x : weatherMap) { - if (x.first == keywordT[0].text) + if (x.first == keywordTP[RAIN].getText()) { minOK = 0; maxOK = 0; @@ -91,7 +91,7 @@ bool WeatherWatcher::createPropertiesFromMap() addParameter("WEATHER_RAIN_HOUR", "Rain (mm)", minOK, maxOK, percWarn); setCriticalParameter("WEATHER_RAIN_HOUR"); } - else if (x.first == keywordT[1].text) + else if (x.first == keywordTP[TEMP].getText()) { minOK = -10; maxOK = 30; @@ -103,7 +103,7 @@ bool WeatherWatcher::createPropertiesFromMap() addParameter("WEATHER_TEMPERATURE", "Temperature (C)", minOK, maxOK, percWarn); setCriticalParameter("WEATHER_TEMPERATURE"); } - else if (x.first == keywordT[2].text) + else if (x.first == keywordTP[WIND].getText()) { minOK = 0; maxOK = 20; @@ -115,7 +115,7 @@ bool WeatherWatcher::createPropertiesFromMap() addParameter("WEATHER_WIND_SPEED", "Wind (kph)", minOK, maxOK, percWarn); setCriticalParameter("WEATHER_WIND_SPEED"); } - else if (x.first == keywordT[3].text) + else if (x.first == keywordTP[GUST].getText()) { minOK = 0; maxOK = 20; @@ -126,7 +126,7 @@ bool WeatherWatcher::createPropertiesFromMap() addParameter("WEATHER_WIND_GUST", "Gust (kph)", minOK, maxOK, percWarn); } - else if (x.first == keywordT[4].text) + else if (x.first == keywordTP[CLOUDS].getText()) { minOK = 0; maxOK = 20; @@ -138,7 +138,7 @@ bool WeatherWatcher::createPropertiesFromMap() addParameter("WEATHER_CLOUDS", "Clouds (%)", minOK, maxOK, percWarn); setCriticalParameter("WEATHER_CLOUDS"); } - else if (x.first == keywordT[5].text) + else if (x.first == keywordTP[HUMIDITY].getText()) { minOK = 0; maxOK = 100; @@ -150,7 +150,7 @@ bool WeatherWatcher::createPropertiesFromMap() addParameter("WEATHER_HUMIDITY", "Humidity (%)", minOK, maxOK, percWarn); setCriticalParameter("WEATHER_HUMIDITY"); } - else if (x.first == keywordT[6].text) + else if (x.first == keywordTP[PRESSURE].getText()) { minOK = 983; maxOK = 1043; @@ -162,7 +162,7 @@ bool WeatherWatcher::createPropertiesFromMap() addParameter("WEATHER_PRESSURE", "Pressure (hPa)", minOK, maxOK, percWarn); setCriticalParameter("WEATHER_PRESSURE"); } - else if (x.first == keywordT[7].text) + else if (x.first == keywordTP[FORECAST].getText()) { minOK = 0; maxOK = 0; @@ -185,23 +185,23 @@ bool WeatherWatcher::initProperties() { INDI::Weather::initProperties(); - IUFillText(&keywordT[0], "RAIN", "Rain", "precip"); - IUFillText(&keywordT[1], "TEMP", "Temperature", "temperature"); - IUFillText(&keywordT[2], "WIND", "Wind", "wind"); - IUFillText(&keywordT[3], "GUST", "Gust", "gust"); - IUFillText(&keywordT[4], "CLOUDS", "Clouds", "clouds"); - IUFillText(&keywordT[5], "HUMIDITY", "Humidity", "humidity"); - IUFillText(&keywordT[6], "PRESSURE", "Pressure", "pressure"); - IUFillText(&keywordT[7], "FORECAST", "Forecast", "forecast"); - IUFillTextVector(&keywordTP, keywordT, 8, getDeviceName(), "KEYWORD", "Keywords", OPTIONS_TAB, IP_RW, + keywordTP[RAIN].fill("RAIN", "Rain", "precip"); + keywordTP[TEMP].fill("TEMP", "Temperature", "temperature"); + keywordTP[WIND].fill("WIND", "Wind", "wind"); + keywordTP[GUST].fill( "GUST", "Gust", "gust"); + keywordTP[CLOUDS].fill( "CLOUDS", "Clouds", "clouds"); + keywordTP[HUMIDITY].fill( "HUMIDITY", "Humidity", "humidity"); + keywordTP[PRESSURE].fill("PRESSURE", "Pressure", "pressure"); + keywordTP[FORECAST].fill( "FORECAST", "Forecast", "forecast"); + keywordTP.fill(getDeviceName(), "KEYWORD", "Keywords", OPTIONS_TAB, IP_RW, 60, IPS_IDLE); - IUFillText(&watchFileT[0], "URL", "File", nullptr); - IUFillTextVector(&watchFileTP, watchFileT, 1, getDeviceName(), "WATCH_SOURCE", "Source", OPTIONS_TAB, IP_RW, + watchFileTP[0].fill("URL", "File", nullptr); + watchFileTP.fill(getDeviceName(), "WATCH_SOURCE", "Source", OPTIONS_TAB, IP_RW, 60, IPS_IDLE); - IUFillText(&separatorT[0], "SEPARATOR", "Separator", "="); - IUFillTextVector(&separatorTP, separatorT, 1, getDeviceName(), "SEPARATOR_KEYWORD", "Separator", OPTIONS_TAB, IP_RW, + separatorTP[0].fill("SEPARATOR", "Separator", "="); + separatorTP.fill(getDeviceName(), "SEPARATOR_KEYWORD", "Separator", OPTIONS_TAB, IP_RW, 60, IPS_IDLE); addDebugControl(); @@ -213,13 +213,13 @@ void WeatherWatcher::ISGetProperties(const char *dev) { INDI::Weather::ISGetProperties(dev); - defineProperty(&watchFileTP); + defineProperty(watchFileTP); loadConfig(true, "WATCH_SOURCE"); - defineProperty(&keywordTP); + defineProperty(keywordTP); loadConfig(true, "KEYWORD"); - defineProperty(&separatorTP); + defineProperty(separatorTP); loadConfig(true, "SEPARATOR_KEYWORD"); } @@ -228,26 +228,26 @@ bool WeatherWatcher::ISNewText(const char *dev, const char *name, char *texts[], { if (dev != nullptr && strcmp(dev, getDeviceName()) == 0) { - if (!strcmp(watchFileTP.name, name)) + if (watchFileTP.isNameMatch(name)) { - IUUpdateText(&watchFileTP, texts, names, n); - watchFileTP.s = IPS_OK; - IDSetText(&watchFileTP, nullptr); + watchFileTP.update(texts, names, n); + watchFileTP.setState(IPS_OK); + watchFileTP.apply(); return true; } - if (!strcmp(keywordTP.name, name)) + if (keywordTP.isNameMatch(name)) { - IUUpdateText(&keywordTP, texts, names, n); - keywordTP.s = IPS_OK; - IDSetText(&keywordTP, nullptr); + keywordTP.update(texts, names, n); + keywordTP.setState(IPS_OK); + keywordTP.apply(); return true; } - if (!strcmp(separatorTP.name, name)) + if (separatorTP.isNameMatch(name)) { - IUUpdateText(&separatorTP, texts, names, n); - separatorTP.s = IPS_OK; - IDSetText(&separatorTP, nullptr); + separatorTP.update(texts, names, n); + separatorTP.setState(IPS_OK); + separatorTP.apply(); return true; } } @@ -263,35 +263,35 @@ IPState WeatherWatcher::updateWeather() for (auto const &x : weatherMap) { - if (x.first == keywordT[0].text) + if (x.first == keywordTP[RAIN].getText()) { setParameterValue("WEATHER_RAIN_HOUR", std::strtod(x.second.c_str(), nullptr)); } - else if (x.first == keywordT[1].text) + else if (x.first == keywordTP[TEMP].getText()) { setParameterValue("WEATHER_TEMPERATURE", std::strtod(x.second.c_str(), nullptr)); } - else if (x.first == keywordT[2].text) + else if (x.first == keywordTP[WIND].getText()) { setParameterValue("WEATHER_WIND_SPEED", std::strtod(x.second.c_str(), nullptr)); } - else if (x.first == keywordT[3].text) + else if (x.first == keywordTP[GUST].getText()) { setParameterValue("WEATHER_WIND_GUST", std::strtod(x.second.c_str(), nullptr)); } - else if (x.first == keywordT[4].text) + else if (x.first == keywordTP[CLOUDS].getText()) { setParameterValue("WEATHER_CLOUDS", std::strtod(x.second.c_str(), nullptr)); } - else if (x.first == keywordT[5].text) + else if (x.first == keywordTP[HUMIDITY].getText()) { setParameterValue("WEATHER_HUMIDITY", std::strtod(x.second.c_str(), nullptr)); } - else if (x.first == keywordT[6].text) + else if (x.first == keywordTP[PRESSURE].getText()) { setParameterValue("WEATHER_PRESSURE", std::strtod(x.second.c_str(), nullptr)); } - else if (x.first == keywordT[7].text) + else if (x.first == keywordTP[FORECAST].getText()) { setParameterValue("WEATHER_FORECAST", std::strtod(x.second.c_str(), nullptr)); } @@ -309,10 +309,10 @@ bool WeatherWatcher::readWatchFile() AutoCNumeric locale; - if (std::string(watchFileT[0].text).find("http") == 0) - snprintf(requestURL, MAXRBUF, "%s", watchFileT[0].text); + if (std::string(watchFileTP[0].getText()).find("http") == 0) + snprintf(requestURL, MAXRBUF, "%s", watchFileTP[0].getText()); else - snprintf(requestURL, MAXRBUF, "file://%s", watchFileT[0].text); + snprintf(requestURL, MAXRBUF, "file://%s", watchFileTP[0].getText()); curl = curl_easy_init(); @@ -338,9 +338,9 @@ bool WeatherWatcher::saveConfigItems(FILE *fp) { INDI::Weather::saveConfigItems(fp); - IUSaveConfigText(fp, &watchFileTP); - IUSaveConfigText(fp, &keywordTP); - IUSaveConfigText(fp, &separatorTP); + watchFileTP.save(fp); + keywordTP.save(fp); + separatorTP.apply(); return true; } @@ -352,7 +352,7 @@ std::map WeatherWatcher::createMap(std::string const & std::string key, val; std::istringstream iss(s); - while(std::getline(std::getline(iss, key, separatorT[0].text[0]) >> std::ws, val)) + while(std::getline(std::getline(iss, key, separatorTP[0].getText()[0]) >> std::ws, val)) m[key] = val; return m; diff --git a/drivers/weather/weatherwatcher.h b/drivers/weather/weatherwatcher.h index 1e3daae8fc..a07f158d3d 100644 --- a/drivers/weather/weatherwatcher.h +++ b/drivers/weather/weatherwatcher.h @@ -46,19 +46,27 @@ class WeatherWatcher : public INDI::Weather virtual IPState updateWeather() override; virtual bool saveConfigItems(FILE *fp) override; - IText keywordT[8] {}; - ITextVectorProperty keywordTP; - - IText separatorT[1] {}; - ITextVectorProperty separatorTP; + INDI::PropertyText keywordTP {8}; + enum + { + RAIN, + TEMP, + WIND, + GUST, + CLOUDS, + HUMIDITY, + PRESSURE, + FORECAST + }; + + INDI::PropertyText separatorTP {1}; private: std::map createMap(std::string const &s); bool readWatchFile(); bool createPropertiesFromMap(); - IText watchFileT[1] {}; - ITextVectorProperty watchFileTP; + INDI::PropertyText watchFileTP {1}; bool initialParse { false }; std::string readBuffer;