Instrument Neutral Distributed Interface INDI  2.0.2
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.
31 std::unique_ptr<WeatherSimulator> weatherSimulator(new WeatherSimulator());
32 
34 {
35  setVersion(1, 0);
36 
38 }
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  IUFillNumber(&ControlWeatherN[CONTROL_WEATHER], "Weather", "Weather", "%.f", 0, 1, 1, 0);
60  IUFillNumber(&ControlWeatherN[CONTROL_TEMPERATURE], "Temperature", "Temperature", "%.2f", -50, 70, 10, 15);
61  IUFillNumber(&ControlWeatherN[CONTROL_WIND], "Wind", "Wind", "%.2f", 0, 100, 5, 0);
62  IUFillNumber(&ControlWeatherN[CONTROL_GUST], "Gust", "Gust", "%.2f", 0, 50, 5, 0);
63  IUFillNumber(&ControlWeatherN[CONTROL_RAIN], "Precip", "Precip", "%.f", 0, 100, 10, 0);
64  IUFillNumberVector(&ControlWeatherNP, ControlWeatherN, 5, getDeviceName(), "WEATHER_CONTROL", "Control", MAIN_CONTROL_TAB,
65  IP_RW, 0, IPS_IDLE);
66 
67  addParameter("WEATHER_FORECAST", "Weather", 0, 0, 15);
68  addParameter("WEATHER_TEMPERATURE", "Temperature (C)", -10, 30, 15);
69  addParameter("WEATHER_WIND_SPEED", "Wind (kph)", 0, 20, 15);
70  addParameter("WEATHER_WIND_GUST", "Gust (kph)", 0, 20, 15);
71  addParameter("WEATHER_RAIN_HOUR", "Precip (mm)", 0, 0, 15);
72 
73  setCriticalParameter("WEATHER_FORECAST");
74  setCriticalParameter("WEATHER_TEMPERATURE");
75  setCriticalParameter("WEATHER_WIND_SPEED");
76  setCriticalParameter("WEATHER_RAIN_HOUR");
77 
79 
80  return true;
81 }
82 
84 {
86 
87  if (isConnected())
88  defineProperty(&ControlWeatherNP);
89  else
90  deleteProperty(ControlWeatherNP.name);
91 
92  return true;
93 }
94 
96 {
97  setParameterValue("WEATHER_FORECAST", ControlWeatherN[CONTROL_WEATHER].value);
98  setParameterValue("WEATHER_TEMPERATURE", ControlWeatherN[CONTROL_TEMPERATURE].value);
99  setParameterValue("WEATHER_WIND_SPEED", ControlWeatherN[CONTROL_WIND].value);
100  setParameterValue("WEATHER_WIND_GUST", ControlWeatherN[CONTROL_GUST].value);
101  setParameterValue("WEATHER_RAIN_HOUR", ControlWeatherN[CONTROL_RAIN].value);
102 
103  return IPS_OK;
104 }
105 
106 bool WeatherSimulator::ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n)
107 {
108  if (dev != nullptr && strcmp(dev, getDeviceName()) == 0)
109  {
110  if (!strcmp(name, ControlWeatherNP.name))
111  {
112  IUUpdateNumber(&ControlWeatherNP, values, names, n);
113  ControlWeatherNP.s = IPS_OK;
114  IDSetNumber(&ControlWeatherNP, nullptr);
115  LOG_INFO("Values are updated and should be active on the next weather update.");
116  return true;
117  }
118  }
119 
120  return INDI::Weather::ISNewNumber(dev, name, values, names, n);
121 }
122 
124 {
126 
127  IUSaveConfigNumber(fp, &ControlWeatherNP);
128 
129  return true;
130 }
bool isConnected() const
Definition: basedevice.cpp:520
const char * getDeviceName() const
Definition: basedevice.cpp:821
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 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 updateProperties() override
updateProperties is called whenever there is a change in the CONNECTION status of the driver....
Definition: indiweather.cpp:85
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
Initilize properties initial state and value. The child class must implement this function.
Definition: indiweather.cpp:41
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
Initilize 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:186
IPState
Property state.
Definition: indiapi.h:160
@ IPS_IDLE
Definition: indiapi.h:161
@ IPS_OK
Definition: indiapi.h:162
void IUFillNumberVector(INumberVectorProperty *nvp, INumber *np, int nnp, const char *dev, const char *name, const char *label, const char *group, IPerm p, double timeout, IPState s)
Assign attributes for a number vector property. The vector's auxiliary elements will be set to NULL.
Definition: indidevapi.c:272
void IUSaveConfigNumber(FILE *fp, const INumberVectorProperty *nvp)
Add a number vector property value to the configuration file.
Definition: indidevapi.c:15
void IUFillNumber(INumber *np, const char *name, const char *label, const char *format, double min, double max, double step, double value)
Assign attributes for a number property. The number's auxiliary elements will be set to NULL.
Definition: indidevapi.c:180
void IDSetNumber(const INumberVectorProperty *nvp, const char *fmt,...)
Definition: indidriver.c:1211
int IUUpdateNumber(INumberVectorProperty *nvp, double values[], char *names[], int n)
Update all numbers in a number vector property.
Definition: indidriver.c:1362
#define LOG_INFO(txt)
Definition: indilogger.h:74
char name[MAXINDINAME]
Definition: indiapi.h:323
std::unique_ptr< WeatherSimulator > weatherSimulator(new WeatherSimulator())