28 #include <curl/curl.h>
34 static std::unique_ptr<WeatherWatcher> weatherWatcher(
new WeatherWatcher());
36 static size_t write_data(
void *contents,
size_t size,
size_t nmemb,
void *userp)
38 ((std::string *)userp)->append((
char *)contents, size * nmemb);
51 return "Weather Watcher";
56 if (watchFileT[0].text ==
nullptr || watchFileT[0].text[0] ==
'\0')
58 LOG_ERROR(
"Watch file must be specified first in options.");
62 return createPropertiesFromMap();
70 bool WeatherWatcher::createPropertiesFromMap()
76 if (readWatchFile() ==
false)
79 double minOK = 0, maxOK = 0, percWarn = 15;
80 for (
auto const &x : weatherMap)
91 addParameter(
"WEATHER_RAIN_HOUR",
"Rain (mm)", minOK, maxOK, percWarn);
94 else if (x.first ==
keywordT[1].text)
103 addParameter(
"WEATHER_TEMPERATURE",
"Temperature (C)", minOK, maxOK, percWarn);
106 else if (x.first ==
keywordT[2].text)
115 addParameter(
"WEATHER_WIND_SPEED",
"Wind (kph)", minOK, maxOK, percWarn);
118 else if (x.first ==
keywordT[3].text)
127 addParameter(
"WEATHER_WIND_GUST",
"Gust (kph)", minOK, maxOK, percWarn);
129 else if (x.first ==
keywordT[4].text)
138 addParameter(
"WEATHER_CLOUDS",
"Clouds (%)", minOK, maxOK, percWarn);
141 else if (x.first ==
keywordT[5].text)
150 addParameter(
"WEATHER_HUMIDITY",
"Humidity (%)", minOK, maxOK, percWarn);
153 else if (x.first ==
keywordT[6].text)
162 addParameter(
"WEATHER_PRESSURE",
"Pressure (hPa)", minOK, maxOK, percWarn);
165 else if (x.first ==
keywordT[7].text)
174 addParameter(
"WEATHER_FORECAST",
"Weather", minOK, maxOK, percWarn);
199 IUFillText(&watchFileT[0],
"URL",
"File",
nullptr);
231 if (!strcmp(watchFileTP.
name, name))
261 if (readWatchFile() ==
false)
264 for (
auto const &x : weatherMap)
270 else if (x.first ==
keywordT[1].text)
272 setParameterValue(
"WEATHER_TEMPERATURE", std::strtod(x.second.c_str(),
nullptr));
274 else if (x.first ==
keywordT[2].text)
278 else if (x.first ==
keywordT[3].text)
282 else if (x.first ==
keywordT[4].text)
286 else if (x.first ==
keywordT[5].text)
290 else if (x.first ==
keywordT[6].text)
294 else if (x.first ==
keywordT[7].text)
303 bool WeatherWatcher::readWatchFile()
312 if (std::string(watchFileT[0].text).find(
"http") == 0)
313 snprintf(requestURL,
MAXRBUF,
"%s", watchFileT[0].text);
315 snprintf(requestURL,
MAXRBUF,
"file://%s", watchFileT[0].text);
317 curl = curl_easy_init();
321 curl_easy_setopt(curl, CURLOPT_URL, requestURL);
322 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
323 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
324 res = curl_easy_perform(curl);
328 weatherMap = createMap(readBuffer);
331 curl_easy_cleanup(curl);
348 std::map<std::string, std::string> WeatherWatcher::createMap(std::string
const &s)
350 std::map<std::string, std::string> m;
352 std::string key, val;
353 std::istringstream iss(s);
355 while(std::getline(std::getline(iss, key,
separatorT[0].text[0]) >> std::ws, val))
const char * getDeviceName() const
virtual void ISGetProperties(const char *dev)
define the driver's properties to the client. Usually, only a minimum set of properties are defined t...
void setVersion(uint16_t vMajor, uint16_t vMinor)
Set driver version information to be defined in DRIVER_INFO property as vMajor.vMinor.
virtual bool loadConfig(bool silent=false, const char *property=nullptr)
Load the last saved configuration file.
void defineProperty(INumberVectorProperty *property)
void addDebugControl()
Add Debug control to the driver.
void setParameterValue(std::string name, double value)
setParameterValue Update weather parameter value
bool setCriticalParameter(std::string param)
setCriticalParameter Set parameter that is considered critical to the operation of the observatory....
void addParameter(std::string name, std::string label, double numMinOk, double numMaxOk, double percWarning)
addParameter Add a physical weather measurable parameter to the weather driver. The weather value has...
virtual bool ISNewText(const char *dev, const char *name, char *texts[], char *names[], int n) override
Process the client newSwitch command.
virtual bool saveConfigItems(FILE *fp) override
saveConfigItems Save specific properties in the provide config file handler. Child class usually over...
void setWeatherConnection(const uint8_t &value)
setWeatherConnection Set Weather connection mode. Child class should call this in the constructor bef...
virtual bool initProperties() override
Initilize properties initial state and value. The child class must implement this function.
virtual void ISGetProperties(const char *dev) override
define the driver's properties to the client. Usually, only a minimum set of properties are defined t...
virtual IPState updateWeather() override
updateWeather Update weather conditions from device or service. The function should not change the st...
ITextVectorProperty separatorTP
ITextVectorProperty keywordTP
bool Disconnect() override
Disconnect from device.
const char * getDefaultName() override
virtual bool initProperties() override
Initilize properties initial state and value. The child class must implement this function.
virtual bool saveConfigItems(FILE *fp) override
saveConfigItems Save specific properties in the provide config file handler. Child class usually over...
bool Connect() override
Connect to the device. INDI::DefaultDevice implementation connects to appropriate connection interfac...
virtual bool ISNewText(const char *dev, const char *name, char *texts[], char *names[], int n) override
Process the client newSwitch command.
const char * OPTIONS_TAB
OPTIONS_TAB Where all the driver's options are located. Those may include auxiliary controls,...
void IUFillTextVector(ITextVectorProperty *tvp, IText *tp, int ntp, const char *dev, const char *name, const char *label, const char *group, IPerm p, double timeout, IPState s)
Assign attributes for a text vector property. The vector's auxiliary elements will be set to NULL.
void IUFillText(IText *tp, const char *name, const char *label, const char *initialText)
Assign attributes for a text property. The text's auxiliary elements will be set to NULL.
void IUSaveConfigText(FILE *fp, const ITextVectorProperty *tvp)
Add a text vector property value to the configuration file.
int IUUpdateText(ITextVectorProperty *tvp, char *texts[], char *names[], int n)
Update all text members in a text vector property.
int IUGetConfigNumber(const char *dev, const char *property, const char *member, double *value)
IUGetConfigNumber Opens configuration file and reads single number property.
void IDSetText(const ITextVectorProperty *tvp, const char *fmt,...)
#define LOG_ERROR(txt)
Shorter logging macros. In order to use these macros, the function (or method) "getDeviceName()" must...