Instrument Neutral Distributed Interface INDI 2.2.5
weathersimulator.cpp
Go to the documentation of this file.
1/*******************************************************************************
2 Copyright(c) 2018 Jasem Mutlaq. All rights reserved.
3
4 INDI Weather Simulator
5
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2 of the License, or (at your option)
9 any later version.
10
11 This program is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 more details.
15
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
20
21 The full GNU General Public License is included in this distribution in the
22 file called LICENSE.
23*******************************************************************************/
24
25#include "weathersimulator.h"
26
27#include <memory>
28#include <cstring>
29
30// We declare an auto pointer to WeatherSimulator.
31std::unique_ptr<WeatherSimulator> weatherSimulator(new WeatherSimulator());
32
39
41{
42 return (const char *)"Weather Simulator";
43}
44
46{
47 return true;
48}
49
51{
52 return true;
53}
54
56{
58
59 ControlWeatherNP[CONTROL_WEATHER].fill("Weather", "Weather", "%.f", 0, 1, 1, 0);
60 ControlWeatherNP[CONTROL_TEMPERATURE].fill("Temperature", "Temperature", "%.2f", -50, 70, 10, 15);
61 ControlWeatherNP[CONTROL_HUMIDITY].fill( "Humidity", "Humidity", "%.f", 0, 100, 5, 0);
62 ControlWeatherNP[CONTROL_WIND].fill("Wind", "Wind", "%.2f", 0, 100, 5, 0);
63 ControlWeatherNP[CONTROL_GUST].fill("Gust", "Gust", "%.2f", 0, 50, 5, 0);
64 ControlWeatherNP[CONTROL_RAIN].fill( "Precip", "Precip", "%.f", 0, 100, 10, 0);
65 ControlWeatherNP.fill(getDeviceName(), "WEATHER_CONTROL", "Control", MAIN_CONTROL_TAB,
66 IP_RW, 0, IPS_IDLE);
67
68 addParameter("WEATHER_FORECAST", "Weather", 0, 0, 0);
69 addParameter("WEATHER_TEMPERATURE", "Temperature (C)", -10, 30, 15);
70 addParameter("WEATHER_HUMIDITY", "Humidity (%)", 0, 100, 15);
71 addParameter("WEATHER_WIND_SPEED", "Wind (kph)", 0, 20, 15);
72 addParameter("WEATHER_WIND_GUST", "Gust (kph)", 0, 20, 15);
73 addParameter("WEATHER_RAIN_HOUR", "Precip (mm)", 0, 0, 0);
74
75 setCriticalParameter("WEATHER_FORECAST");
76 setCriticalParameter("WEATHER_TEMPERATURE");
77 setCriticalParameter("WEATHER_WIND_SPEED");
78 setCriticalParameter("WEATHER_RAIN_HOUR");
79
81
82 return true;
83}
84
86{
88
89 if (isConnected())
90 defineProperty(ControlWeatherNP);
91 else
92 deleteProperty(ControlWeatherNP);
93
94 return true;
95}
96
98{
99 setParameterValue("WEATHER_FORECAST", ControlWeatherNP[CONTROL_WEATHER].getValue());
100 setParameterValue("WEATHER_TEMPERATURE", ControlWeatherNP[CONTROL_TEMPERATURE].getValue());
101 setParameterValue("WEATHER_HUMIDITY", ControlWeatherNP[CONTROL_HUMIDITY].getValue());
102 setParameterValue("WEATHER_WIND_SPEED", ControlWeatherNP[CONTROL_WIND].getValue());
103 setParameterValue("WEATHER_WIND_GUST", ControlWeatherNP[CONTROL_GUST].getValue());
104 setParameterValue("WEATHER_RAIN_HOUR", ControlWeatherNP[CONTROL_RAIN].getValue());
105
106 return IPS_OK;
107}
108
109bool WeatherSimulator::ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n)
110{
111 if (dev != nullptr && strcmp(dev, getDeviceName()) == 0)
112 {
113 if (ControlWeatherNP.isNameMatch(name))
114 {
115 ControlWeatherNP.update(values, names, n);
116 ControlWeatherNP.setState(IPS_OK);
117 ControlWeatherNP.apply();
118 LOG_INFO("Values are updated and should be active on the next weather update.");
119 return true;
120 }
121 }
122
123 return INDI::Weather::ISNewNumber(dev, name, values, names, n);
124}
125
127{
129
130 ControlWeatherNP.save(fp);
131
132 return true;
133}
bool isConnected() const
void setVersion(uint16_t vMajor, uint16_t vMinor)
Set driver version information to be defined in DRIVER_INFO property as vMajor.vMinor.
virtual bool deleteProperty(const char *propertyName)
Delete a property and unregister it. It will also be deleted from all clients.
void defineProperty(INumberVectorProperty *property)
void addDebugControl()
Add Debug control to the driver.
void setState(IPState state)
void apply(const char *format,...) const ATTRIBUTE_FORMAT_PRINTF(2
void save(FILE *f) const
save Save property to configuration file.
bool isNameMatch(const char *otherName) const
bool update(const double values[], const char *const names[], int n)
void fill(const char *device, const char *name, const char *label, const char *group, IPerm permission, double timeout, IPState state)
void addParameter(std::string name, std::string label, double numMinOk, double numMaxOk, double percWarning, bool flipWarning=false)
Add a physical weather measurable parameter to the weather driver.
bool setCriticalParameter(std::string name)
setCriticalParameter Set parameter that is considered critical to the operation of the observatory....
void setParameterValue(std::string name, double value)
setParameterValue Update weather parameter value
virtual bool updateProperties() override
updateProperties is called whenever there is a change in the CONNECTION status of the driver....
virtual bool saveConfigItems(FILE *fp) override
saveConfigItems Save specific properties in the provide config file handler. Child class usually over...
virtual bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n) override
Process the client newNumber command.
void setWeatherConnection(const uint8_t &value)
setWeatherConnection Set Weather connection mode. Child class should call this in the constructor bef...
virtual bool initProperties() override
Initialize 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...
const char * getDefaultName() override
virtual bool updateProperties() override
updateProperties is called whenever there is a change in the CONNECTION status of the driver....
bool Disconnect() override
Disconnect from device.
bool Connect() override
Connect to the device. INDI::DefaultDevice implementation connects to appropriate connection interfac...
virtual IPState updateWeather() override
updateWeather Update weather conditions from device or service. The function should not change the st...
virtual bool initProperties() override
Initialize properties initial state and value. The child class must implement this function.
virtual bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n) override
Process the client newNumber command.
const char * MAIN_CONTROL_TAB
MAIN_CONTROL_TAB Where all the primary controls for the device are located.
@ IP_RW
Definition indiapi.h:184
IPState
Property state.
Definition indiapi.h:158
@ IPS_IDLE
Definition indiapi.h:159
@ IPS_OK
Definition indiapi.h:160
#define LOG_INFO(txt)
Definition indilogger.h:74
#define getDeviceName
std::unique_ptr< WeatherSimulator > weatherSimulator(new WeatherSimulator())