Instrument Neutral Distributed Interface INDI  2.0.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  mRainLight[0].fill("Status", "", IPS_IDLE);
60  mRainLight.fill(getDeviceName(), "Rain Alert", "", MAIN_CONTROL_TAB, IPS_IDLE);
61 
62  mRainSwitch[0].fill("On", "");
63  mRainSwitch[1].fill("Off", "");
64  mRainSwitch.fill(getDeviceName(), "Control Rain", "", MAIN_CONTROL_TAB, IP_RW, ISR_1OFMANY, 0, IPS_IDLE);
65  mRainSwitch.onUpdate([this]()
66  {
67  if (mRainSwitch[0].getState() == ISS_ON)
68  {
69  mRainLight[0].setState(IPS_ALERT);
70  mRainLight.setState(IPS_ALERT);
71  mRainLight.apply("Alert! Alert! Rain detected!");
72  }
73  else
74  {
75  mRainLight[0].setState(IPS_IDLE);
76  mRainLight.setState(IPS_OK);
77  mRainLight.apply("Rain threat passed. The skies are clear.");
78  }
79  mRainSwitch.setState(IPS_OK);
80  mRainSwitch.apply();
81  });
82  return true;
83 }
84 
85 /********************************************************************************************
86 ** INDI is asking us to update the properties because there is a change in CONNECTION status
87 ** This fucntion is called whenever the device is connected or disconnected.
88 *********************************************************************************************/
90 {
91  // Call parent update properties first
93 
94  if (isConnected())
95  {
96  defineProperty(mRainLight);
97  defineProperty(mRainSwitch);
98  }
99  else
100  // We're disconnected
101  {
102  deleteProperty(mRainLight);
103  deleteProperty(mRainSwitch);
104  }
105 
106  return true;
107 }
bool isConnected() const
Definition: basedevice.cpp:520
const char * getDeviceName() const
Definition: basedevice.cpp:821
virtual bool updateProperties()
updateProperties is called whenever there is a change in the CONNECTION status of the driver....
virtual bool deleteProperty(const char *propertyName)
Delete a property and unregister it. It will also be deleted from all clients.
void defineProperty(INumberVectorProperty *property)
virtual bool initProperties()
Initilize properties initial state and value. The child class must implement this function.
void setState(IPState state)
void apply(const char *format,...) const ATTRIBUTE_FORMAT_PRINTF(2
void fill(const char *device, const char *name, const char *label, const char *group, IPState state)
void fill(const char *device, const char *name, const char *label, const char *group, IPerm permission, ISRule rule, double timeout, IPState state)
void onUpdate(const std::function< void()> &callback)
bool Disconnect() override
Disconnect from device.
bool initProperties() override
Initilize properties initial state and value. The child class must implement this function.
bool updateProperties() override
updateProperties is called whenever there is a change in the CONNECTION status of the driver....
const char * getDefaultName() override
bool Connect() override
Connect to the device. INDI::DefaultDevice implementation connects to appropriate connection interfac...
const char * MAIN_CONTROL_TAB
MAIN_CONTROL_TAB Where all the primary controls for the device are located.
@ ISS_ON
Definition: indiapi.h:152
@ IP_RW
Definition: indiapi.h:186
@ IPS_ALERT
Definition: indiapi.h:164
@ IPS_IDLE
Definition: indiapi.h:161
@ IPS_OK
Definition: indiapi.h:162
@ ISR_1OFMANY
Definition: indiapi.h:173
void IDMessage(const char *dev, const char *fmt,...)
Definition: indidriver.c:960
std::unique_ptr< RainDetector > rainDetector(new RainDetector())
Construct a rain detector device that the user may operate to raise a rain alert. This rain light pro...