Instrument Neutral Distributed Interface INDI  1.9.2
raindetector.cpp
Go to the documentation of this file.
1 /*
2  INDI Developers Manual
3  Tutorial #5 - Snooping
4 
5  Rain Detector
6 
7  Refer to README, which contains instruction on how to build this driver, and use it
8  with an INDI-compatible client.
9 
10 */
11 
18 #include "raindetector.h"
19 
20 #include <memory>
21 #include <cstring>
22 
23 std::unique_ptr<RainDetector> rainDetector(new RainDetector());
24 
25 /**************************************************************************************
26 ** Client is asking us to establish connection to the device
27 ***************************************************************************************/
29 {
30  IDMessage(getDeviceName(), "Rain Detector connected successfully!");
31  return true;
32 }
33 
34 /**************************************************************************************
35 ** Client is asking us to terminate connection to the device
36 ***************************************************************************************/
38 {
39  IDMessage(getDeviceName(), "Rain Detector disconnected successfully!");
40  return true;
41 }
42 
43 /**************************************************************************************
44 ** INDI is asking us for our default device name
45 ***************************************************************************************/
47 {
48  return "Rain Detector";
49 }
50 
51 /**************************************************************************************
52 ** INDI is asking us to init our properties.
53 ***************************************************************************************/
55 {
56  // Must init parent properties first!
58 
59  IUFillLight(&RainL[0], "Status", "", IPS_IDLE);
60  IUFillLightVector(&RainLP, RainL, 1, getDeviceName(), "Rain Alert", "", MAIN_CONTROL_TAB, IPS_IDLE);
61 
62  IUFillSwitch(&RainS[0], "On", "", ISS_OFF);
63  IUFillSwitch(&RainS[1], "Off", "", ISS_OFF);
64  IUFillSwitchVector(&RainSP, RainS, 2, getDeviceName(), "Control Rain", "", MAIN_CONTROL_TAB, IP_RW, ISR_1OFMANY, 0,
65  IPS_IDLE);
66 
67  return true;
68 }
69 
70 /********************************************************************************************
71 ** INDI is asking us to update the properties because there is a change in CONNECTION status
72 ** This fucntion is called whenever the device is connected or disconnected.
73 *********************************************************************************************/
75 {
76  // Call parent update properties first
78 
79  if (isConnected())
80  {
81  defineProperty(&RainLP);
82  defineProperty(&RainSP);
83  }
84  else
85  // We're disconnected
86  {
87  deleteProperty(RainLP.name);
88  deleteProperty(RainSP.name);
89  }
90 
91  return true;
92 }
93 
94 /********************************************************************************************
95 ** Client is asking us to update a switch
96 *********************************************************************************************/
97 bool RainDetector::ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n)
98 {
99  if (dev != nullptr && strcmp(dev, getDeviceName()) == 0)
100  {
101  if (strcmp(name, RainSP.name) == 0)
102  {
103  IUUpdateSwitch(&RainSP, states, names, n);
104 
105  if (RainS[0].s == ISS_ON)
106  {
107  RainL[0].s = IPS_ALERT;
108  RainLP.s = IPS_ALERT;
109  IDSetLight(&RainLP, "Alert! Alert! Rain detected!");
110  }
111  else
112  {
113  RainL[0].s = IPS_IDLE;
114  RainLP.s = IPS_OK;
115  IDSetLight(&RainLP, "Rain threat passed. The skies are clear.");
116  }
117 
118  RainSP.s = IPS_OK;
119  IDSetSwitch(&RainSP, nullptr);
120  return true;
121  }
122  }
123 
124  return INDI::DefaultDevice::ISNewSwitch(dev, name, states, names, n);
125 }
RainDetector
Definition: raindetector.h:25
IPS_OK
@ IPS_OK
Definition: indiapi.h:161
ISS_OFF
@ ISS_OFF
Definition: indiapi.h:150
IPS_ALERT
@ IPS_ALERT
Definition: indiapi.h:163
INDI::DefaultDevice::defineProperty
void defineProperty(INumberVectorProperty *property)
Definition: defaultdevice.cpp:997
MAIN_CONTROL_TAB
const char * MAIN_CONTROL_TAB
MAIN_CONTROL_TAB Where all the primary controls for the device are located.
Definition: defaultdevice.cpp:34
RainDetector::initProperties
bool initProperties() override
Initilize properties initial state and value. The child class must implement this function.
Definition: raindetector.cpp:54
INDI::BaseDevice::getDeviceName
const char * getDeviceName() const
Definition: basedevice.cpp:799
_ILightVectorProperty::s
IPState s
Definition: indiapi.h:426
RainDetector::Disconnect
bool Disconnect() override
Disconnect from device.
Definition: raindetector.cpp:37
IUFillLight
void IUFillLight(ILight *lp, const char *name, const char *label, IPState s)
Assign attributes for a light property. The light's auxiliary elements will be set to NULL.
Definition: indidriver.c:334
raindetector.h
Construct a rain detector device that the user may operate to raise a rain alert. This rain light pro...
INDI::DefaultDevice::initProperties
virtual bool initProperties()
Initilize properties initial state and value. The child class must implement this function.
Definition: defaultdevice.cpp:917
_ILightVectorProperty::name
char name[MAXINDINAME]
Definition: indiapi.h:420
IDSetLight
void void void void void void void IDSetLight(const ILightVectorProperty *l, const char *msg,...) ATTRIBUTE_FORMAT_PRINTF(2
Tell client to update an existing light vector property.
IUFillSwitchVector
void IUFillSwitchVector(ISwitchVectorProperty *svp, ISwitch *sp, int nsp, const char *dev, const char *name, const char *label, const char *group, IPerm p, ISRule r, double timeout, IPState s)
Assign attributes for a switch vector property. The vector's auxiliary elements will be set to NULL.
Definition: indidriver.c:412
ISR_1OFMANY
@ ISR_1OFMANY
Definition: indiapi.h:172
IPS_IDLE
@ IPS_IDLE
Definition: indiapi.h:160
IDMessage
void IDMessage(const char *dev, const char *msg,...) ATTRIBUTE_FORMAT_PRINTF(2
Function Drivers call to send log messages to Clients.
RainDetector::getDefaultName
const char * getDefaultName() override
Definition: raindetector.cpp:46
IUUpdateSwitch
int IUUpdateSwitch(ISwitchVectorProperty *svp, ISState *states, char *names[], int n)
Update all switches in a switch vector property.
Definition: indidriver.c:171
rainDetector
std::unique_ptr< RainDetector > rainDetector(new RainDetector())
INDI::BaseDevice::isConnected
bool isConnected() const
Definition: basedevice.cpp:518
_ISwitchVectorProperty::s
IPState s
Definition: indiapi.h:382
RainDetector::Connect
bool Connect() override
Connect to the device. INDI::DefaultDevice implementation connects to appropriate connection interfac...
Definition: raindetector.cpp:28
IP_RW
@ IP_RW
Definition: indiapi.h:185
RainDetector::ISNewSwitch
bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n) override
Process the client newSwitch command.
Definition: raindetector.cpp:97
ISState
ISState
Switch state.
Definition: indiapi.h:148
INDI::DefaultDevice::ISNewSwitch
virtual bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n)
Process the client newSwitch command.
Definition: defaultdevice.cpp:409
INDI::DefaultDevice::deleteProperty
virtual bool deleteProperty(const char *propertyName)
Delete a property and unregister it. It will also be deleted from all clients.
Definition: defaultdevice.cpp:965
IUFillLightVector
void IUFillLightVector(ILightVectorProperty *lvp, ILight *lp, int nlp, const char *dev, const char *name, const char *label, const char *group, IPState s)
Assign attributes for a light vector property. The vector's auxiliary elements will be set to NULL.
Definition: indidriver.c:435
IDSetSwitch
void void void void void IDSetSwitch(const ISwitchVectorProperty *s, const char *msg,...) ATTRIBUTE_FORMAT_PRINTF(2
Tell client to update an existing switch vector property.
IUFillSwitch
void IUFillSwitch(ISwitch *sp, const char *name, const char *label, ISState s)
Assign attributes for a switch property. The switch's auxiliary elements will be set to NULL.
Definition: indidriver.c:320
RainDetector::updateProperties
bool updateProperties() override
updateProperties is called whenever there is a change in the CONNECTION status of the driver....
Definition: raindetector.cpp:74
INDI::DefaultDevice::updateProperties
virtual bool updateProperties()
updateProperties is called whenever there is a change in the CONNECTION status of the driver....
Definition: defaultdevice.cpp:890
_ISwitchVectorProperty::name
char name[MAXINDINAME]
Definition: indiapi.h:370
ISS_ON
@ ISS_ON
Definition: indiapi.h:151