Instrument Neutral Distributed Interface INDI  2.0.2
dome.cpp
Go to the documentation of this file.
1 /*
2  INDI Developers Manual
3  Tutorial #5 - Snooping
4 
5  Dome
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 
26 #include "dome.h"
27 #include <inditimer.h>
28 
29 #include <memory>
30 #include <cstring>
31 #include <unistd.h>
32 
33 std::unique_ptr<Dome> dome(new Dome());
34 
35 /**************************************************************************************
36 ** Client is asking us to establish connection to the device
37 ***************************************************************************************/
39 {
40  IDMessage(getDeviceName(), "Dome connected successfully!");
41  return true;
42 }
43 
44 /**************************************************************************************
45 ** Client is asking us to terminate connection to the device
46 ***************************************************************************************/
48 {
49  IDMessage(getDeviceName(), "Dome disconnected successfully!");
50  return true;
51 }
52 
53 /**************************************************************************************
54 ** INDI is asking us for our default device name
55 ***************************************************************************************/
56 const char *Dome::getDefaultName()
57 {
58  return "Dome";
59 }
60 
61 /**************************************************************************************
62 ** INDI is asking us to init our properties.
63 ***************************************************************************************/
65 {
66  // Must init parent properties first!
68 
69  mShutterSwitch[0].fill("Open", "", ISS_ON);
70  mShutterSwitch[1].fill("Close", "", ISS_OFF);
71  mShutterSwitch.fill(getDeviceName(), "Shutter Door", "", MAIN_CONTROL_TAB, IP_RW, ISR_1OFMANY, 0, IPS_IDLE);
72  mShutterSwitch.onUpdate([this]()
73  {
74  if (mShutterSwitch[0].getState() == ISS_ON)
75  openShutter();
76  else
77  closeShutter();
78 
79  });
80  // We init here the property we wish to "snoop" from the target device
81  mRainLight[0].fill("Status", "", IPS_IDLE);
82 
83  // wait for "Rain Detector" driver
84  watchDevice("Rain Detector", [this](INDI::BaseDevice device)
85  {
86  // wait for "Rain Alert" property available
87  device.watchProperty("Rain Alert", [this](INDI::PropertyLight rain)
88  {
89  mRainLight = rain; // we have real rainLight property, override mRainLight
90  static IPState oldRainState = rain[0].getState();
91 
92  IPState newRainState = rain[0].getState();
93  if (newRainState == IPS_ALERT)
94  {
95  // If dome is open, then close it */
96  if (mShutterSwitch[0].getState() == ISS_ON)
97  closeShutter();
98  else
99  IDMessage(getDeviceName(), "Rain Alert Detected! Dome is already closed.");
100  }
101 
102  if (newRainState != IPS_ALERT && oldRainState == IPS_ALERT)
103  {
104  IDMessage(getDeviceName(), "Rain threat passed. Opening the dome is now safe.");
105  }
106 
107  oldRainState = newRainState;
108  }, BaseDevice::WATCH_NEW_OR_UPDATE);
109  });
110 
111  return true;
112 }
113 
114 /********************************************************************************************
115 ** INDI is asking us to update the properties because there is a change in CONNECTION status
116 ** This fucntion is called whenever the device is connected or disconnected.
117 *********************************************************************************************/
119 {
120  // Call parent update properties first
122 
123  if (isConnected())
124  defineProperty(mShutterSwitch);
125  else
126  // We're disconnected
127  deleteProperty(mShutterSwitch);
128 
129  return true;
130 }
131 
132 /********************************************************************************************
133 ** Close shutter
134 *********************************************************************************************/
136 {
137  mShutterSwitch.setState(IPS_BUSY);
138  mShutterSwitch.apply("Shutter is closing...");
139 
140  INDI::Timer::singleShot(5000 /* ms */, [this](){
141  mShutterSwitch[0].setState(ISS_OFF);
142  mShutterSwitch[1].setState(ISS_ON);
143 
144  mShutterSwitch.setState(IPS_OK);
145  mShutterSwitch.apply("Shutter is closed.");
146  });
147 }
148 
149 /********************************************************************************************
150 ** Open shutter
151 *********************************************************************************************/
153 {
154  if (mRainLight[0].getState() == IPS_ALERT)
155  {
156  mShutterSwitch.setState(IPS_ALERT);
157  mShutterSwitch[0].setState(ISS_OFF);
158  mShutterSwitch[1].setState(ISS_ON);
159  mShutterSwitch.apply("It is raining, cannot open Shutter.");
160  return;
161  }
162 
163  mShutterSwitch.setState(IPS_BUSY);
164  mShutterSwitch.apply("Shutter is opening...");
165 
166  INDI::Timer::singleShot(5000 /* ms */, [this](){
167  mShutterSwitch[0].setState(ISS_ON);
168  mShutterSwitch[1].setState(ISS_OFF);
169 
170  mShutterSwitch.setState(IPS_OK);
171  mShutterSwitch.apply("Shutter is open.");
172  });
173 }
hid_device * device
Definition: dome.h:33
bool Connect() override
Connect to the device. INDI::DefaultDevice implementation connects to appropriate connection interfac...
Definition: dome.cpp:38
bool initProperties() override
Initilize properties initial state and value. The child class must implement this function.
Definition: dome.cpp:64
bool updateProperties() override
updateProperties is called whenever there is a change in the CONNECTION status of the driver....
Definition: dome.cpp:118
void openShutter()
Definition: dome.cpp:152
bool Disconnect() override
Disconnect from device.
Definition: dome.cpp:47
const char * getDefaultName() override
Definition: dome.cpp:56
void closeShutter()
Definition: dome.cpp:135
Class to provide basic INDI device functionality.
Definition: basedevice.h:52
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....
void watchDevice(const char *deviceName, const std::function< void(INDI::BaseDevice)> &callback)
Add a device to the watch list.
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
IPState getState() const
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)
static void singleShot(int msec, const std::function< void()> &callback)
This static function calls a the given function after a given time interval.
Definition: inditimer.cpp:146
const char * MAIN_CONTROL_TAB
MAIN_CONTROL_TAB Where all the primary controls for the device are located.
std::unique_ptr< Dome > dome(new Dome())
Construct a dome device that the user may operate to open or close the dome shutter door....
@ ISS_OFF
Definition: indiapi.h:151
@ ISS_ON
Definition: indiapi.h:152
@ IP_RW
Definition: indiapi.h:186
IPState
Property state.
Definition: indiapi.h:160
@ IPS_BUSY
Definition: indiapi.h:163
@ 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