Skip to content

Commit

Permalink
Migrate Weather watcher (#2018)
Browse files Browse the repository at this point in the history
  • Loading branch information
naheedsa authored Mar 4, 2024
1 parent 7062907 commit ca39f81
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 59 deletions.
104 changes: 52 additions & 52 deletions drivers/weather/weatherwatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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();
Expand All @@ -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");

}
Expand All @@ -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;
}
}
Expand All @@ -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));
}
Expand All @@ -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();

Expand All @@ -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;
}
Expand All @@ -352,7 +352,7 @@ std::map<std::string, std::string> 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;
Expand Down
22 changes: 15 additions & 7 deletions drivers/weather/weatherwatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string, std::string> createMap(std::string const &s);
bool readWatchFile();
bool createPropertiesFromMap();

IText watchFileT[1] {};
ITextVectorProperty watchFileTP;
INDI::PropertyText watchFileTP {1};

bool initialParse { false };
std::string readBuffer;
Expand Down

0 comments on commit ca39f81

Please sign in to comment.