Instrument Neutral Distributed Interface INDI 2.2.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#include <inditimer.h>
28
29#include <memory>
30#include <cstring>
31#include <unistd.h>
32
33std::unique_ptr<Dome> dome(new Dome());
34
35/**************************************************************************************
36** Client is asking us to establish connection to the device
37***************************************************************************************/
39{
40 LOG_INFO("Dome connected successfully!");
41 return true;
42}
43
44/**************************************************************************************
45** Client is asking us to terminate connection to the device
46***************************************************************************************/
48{
49 LOG_INFO("Dome disconnected successfully!");
50 return true;
51}
52
53/**************************************************************************************
54** INDI is asking us for our default device name
55***************************************************************************************/
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)
76 else
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)
98 else
99 LOG_INFO("Rain Alert Detected! Dome is already closed.");
100 }
101
102 if (newRainState != IPS_ALERT && oldRainState == IPS_ALERT)
103 {
104 LOG_INFO("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 function 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 {
142 mShutterSwitch[0].setState(ISS_OFF);
143 mShutterSwitch[1].setState(ISS_ON);
144
145 mShutterSwitch.setState(IPS_OK);
146 mShutterSwitch.apply("Shutter is closed.");
147 });
148}
149
150/********************************************************************************************
151** Open shutter
152*********************************************************************************************/
154{
155 if (mRainLight[0].getState() == IPS_ALERT)
156 {
157 mShutterSwitch.setState(IPS_ALERT);
158 mShutterSwitch[0].setState(ISS_OFF);
159 mShutterSwitch[1].setState(ISS_ON);
160 mShutterSwitch.apply("It is raining, cannot open Shutter.");
161 return;
162 }
163
164 mShutterSwitch.setState(IPS_BUSY);
165 mShutterSwitch.apply("Shutter is opening...");
166
167 INDI::Timer::singleShot(5000 /* ms */, [this]()
168 {
169 mShutterSwitch[0].setState(ISS_ON);
170 mShutterSwitch[1].setState(ISS_OFF);
171
172 mShutterSwitch.setState(IPS_OK);
173 mShutterSwitch.apply("Shutter is open.");
174 });
175}
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
Initialize 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:153
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
const char * getDeviceName() const
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()
Initialize 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)
static void singleShot(int msec, const std::function< void()> &callback)
This static function calls a the given function after a given time interval.
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:149
@ ISS_ON
Definition indiapi.h:150
@ IP_RW
Definition indiapi.h:184
IPState
Property state.
Definition indiapi.h:158
@ IPS_BUSY
Definition indiapi.h:161
@ IPS_ALERT
Definition indiapi.h:162
@ IPS_IDLE
Definition indiapi.h:159
@ IPS_OK
Definition indiapi.h:160
@ ISR_1OFMANY
Definition indiapi.h:171
#define LOG_INFO(txt)
Definition indilogger.h:74