Instrument Neutral Distributed Interface INDI  1.9.5
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 
28 #include <memory>
29 #include <cstring>
30 #include <unistd.h>
31 
32 std::unique_ptr<Dome> dome(new Dome());
33 
34 /**************************************************************************************
35 ** Client is asking us to establish connection to the device
36 ***************************************************************************************/
38 {
39  IDMessage(getDeviceName(), "Dome connected successfully!");
40  return true;
41 }
42 
43 /**************************************************************************************
44 ** Client is asking us to terminate connection to the device
45 ***************************************************************************************/
47 {
48  IDMessage(getDeviceName(), "Dome disconnected successfully!");
49  return true;
50 }
51 
52 /**************************************************************************************
53 ** INDI is asking us for our default device name
54 ***************************************************************************************/
55 const char *Dome::getDefaultName()
56 {
57  return "Dome";
58 }
59 
60 /**************************************************************************************
61 ** INDI is asking us to init our properties.
62 ***************************************************************************************/
64 {
65  // Must init parent properties first!
67 
68  IUFillSwitch(&ShutterS[0], "Open", "", ISS_ON);
69  IUFillSwitch(&ShutterS[1], "Close", "", ISS_OFF);
70  IUFillSwitchVector(&ShutterSP, ShutterS, 2, getDeviceName(), "Shutter Door", "", MAIN_CONTROL_TAB, IP_RW,
71  ISR_1OFMANY, 0, IPS_IDLE);
72 
73  // We init here the property we wish to "snoop" from the target device
74  IUFillLight(&RainL[0], "Status", "", IPS_IDLE);
75  // Make sure to set the device name to "Rain Detector" since we are snooping on rain detector device.
76  IUFillLightVector(&RainLP, RainL, 1, "Rain Detector", "Rain Alert", "", MAIN_CONTROL_TAB, IPS_IDLE);
77 
78  return true;
79 }
80 
81 /********************************************************************************************
82 ** INDI is asking us to update the properties because there is a change in CONNECTION status
83 ** This fucntion is called whenever the device is connected or disconnected.
84 *********************************************************************************************/
86 {
87  // Call parent update properties first
89 
90  if (isConnected())
91  {
92  defineProperty(&ShutterSP);
93  /* Let's listen for Rain Alert property in the device Rain */
94  IDSnoopDevice("Rain Detector", "Rain Alert");
95  }
96  else
97  // We're disconnected
98  deleteProperty(ShutterSP.name);
99 
100  return true;
101 }
102 
103 /********************************************************************************************
104 ** Client is asking us to update a switch
105 *********************************************************************************************/
106 bool Dome::ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n)
107 {
108  if (dev != nullptr && strcmp(dev, getDeviceName()) == 0)
109  {
110  if (strcmp(name, ShutterSP.name) == 0)
111  {
112  IUUpdateSwitch(&ShutterSP, states, names, n);
113 
114  ShutterSP.s = IPS_BUSY;
115 
116  if (ShutterS[0].s == ISS_ON)
117  {
118  if (RainL[0].s == IPS_ALERT)
119  {
120  ShutterSP.s = IPS_ALERT;
121  ShutterS[0].s = ISS_OFF;
122  ShutterS[1].s = ISS_ON;
123  IDSetSwitch(&ShutterSP, "It is raining, cannot open Shutter.");
124  return true;
125  }
126 
127  IDSetSwitch(&ShutterSP, "Shutter is opening.");
128  }
129  else
130  IDSetSwitch(&ShutterSP, "Shutter is closing.");
131 
132  sleep(5);
133 
134  ShutterSP.s = IPS_OK;
135 
136  if (ShutterS[0].s == ISS_ON)
137  IDSetSwitch(&ShutterSP, "Shutter is open.");
138  else
139  IDSetSwitch(&ShutterSP, "Shutter is closed.");
140 
141  return true;
142  }
143  }
144 
145  return INDI::DefaultDevice::ISNewSwitch(dev, name, states, names, n);
146 }
147 
148 /********************************************************************************************
149 ** We received snooped property update from rain detector device
150 *********************************************************************************************/
152 {
153  IPState old_state = RainL[0].s;
154 
155  /* If the "Rain Alert" property gets updated in the Rain device, we will receive a notification. We need to process the new values of Rain Alert and update the local version
156  of the property.*/
157  if (IUSnoopLight(root, &RainLP) == 0)
158  {
159  // If the dome is connected and rain is Alert */
160  if (RainL[0].s == IPS_ALERT)
161  {
162  // If dome is open, then close it */
163  if (ShutterS[0].s == ISS_ON)
164  closeShutter();
165  else
166  IDMessage(getDeviceName(), "Rain Alert Detected! Dome is already closed.");
167  }
168  else if (old_state == IPS_ALERT && RainL[0].s != IPS_ALERT)
169  IDMessage(getDeviceName(), "Rain threat passed. Opening the dome is now safe.");
170 
171  return true;
172  }
173 
174  return false;
175 }
176 
177 /********************************************************************************************
178 ** Close shutter
179 *********************************************************************************************/
180 void Dome::closeShutter()
181 {
182  ShutterSP.s = IPS_BUSY;
183 
184  IDSetSwitch(&ShutterSP, "Rain Alert! Shutter is closing...");
185 
186  sleep(5);
187 
188  ShutterS[0].s = ISS_OFF;
189  ShutterS[1].s = ISS_ON;
190 
191  ShutterSP.s = IPS_OK;
192 
193  IDSetSwitch(&ShutterSP, "Shutter is closed.");
194 }
Dome
Definition: dome.h:30
dome.h
Construct a dome device that the user may operate to open or close the dome shutter door....
Dome::updateProperties
bool updateProperties() override
updateProperties is called whenever there is a change in the CONNECTION status of the driver....
Definition: dome.cpp:85
Dome::initProperties
bool initProperties() override
Initilize properties initial state and value. The child class must implement this function.
Definition: dome.cpp:63
IPState
IPState
Property state.
Definition: indiapi.h:158
IPS_OK
@ IPS_OK
Definition: indiapi.h:161
ISS_OFF
@ ISS_OFF
Definition: indiapi.h:150
IPS_ALERT
@ IPS_ALERT
Definition: indiapi.h:163
IUSnoopLight
int IUSnoopLight(XMLEle *root, ILightVectorProperty *lvp)
Update a snooped light vector property from the given XML root element.
Definition: indidriver.c:612
IDSnoopDevice
void IDSnoopDevice(const char *snooped_device, const char *snooped_property)
Function a Driver calls to snoop on another Device. Snooped messages will then arrive via ISSnoopDevi...
Definition: indidriver.c:137
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
Dome::ISNewSwitch
bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n) override
Process the client newSwitch command.
Definition: dome.cpp:106
INDI::BaseDevice::getDeviceName
const char * getDeviceName() const
Definition: basedevice.cpp:799
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
Dome::Disconnect
bool Disconnect() override
Disconnect from device.
Definition: dome.cpp:46
INDI::DefaultDevice::initProperties
virtual bool initProperties()
Initilize properties initial state and value. The child class must implement this function.
Definition: defaultdevice.cpp:917
Dome::getDefaultName
const char * getDefaultName() override
Definition: dome.cpp:55
Dome::Connect
bool Connect() override
Connect to the device. INDI::DefaultDevice implementation connects to appropriate connection interfac...
Definition: dome.cpp:37
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
IPS_BUSY
@ IPS_BUSY
Definition: indiapi.h:162
ISR_1OFMANY
@ ISR_1OFMANY
Definition: indiapi.h:172
IPS_IDLE
@ IPS_IDLE
Definition: indiapi.h:160
dome
std::unique_ptr< Dome > dome(new Dome())
xml_ele_
Definition: lilxml.c:105
IDMessage
void IDMessage(const char *dev, const char *msg,...) ATTRIBUTE_FORMAT_PRINTF(2
Function Drivers call to send log messages to Clients.
IUUpdateSwitch
int IUUpdateSwitch(ISwitchVectorProperty *svp, ISState *states, char *names[], int n)
Update all switches in a switch vector property.
Definition: indidriver.c:171
INDI::BaseDevice::isConnected
bool isConnected() const
Definition: basedevice.cpp:518
Dome::ISSnoopDevice
bool ISSnoopDevice(XMLEle *root) override
Process a snoop event from INDI server. This function is called when a snooped property is updated in...
Definition: dome.cpp:151
name
const char * name
Definition: indiserver.c:116
_ISwitchVectorProperty::s
IPState s
Definition: indiapi.h:382
IP_RW
@ IP_RW
Definition: indiapi.h:185
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
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