Instrument Neutral Distributed Interface INDI 2.2.5
safetymonitor_client.cpp
Go to the documentation of this file.
1/*******************************************************************************
2 Copyright(c) 2025 Jasem Mutlaq. All rights reserved.
3
4 INDI Safety Monitor Client
5
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2 of the License, or (at your option)
9 any later version.
10
11 This program is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 more details.
15
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
20
21 The full GNU General Public License is included in this distribution in the
22 file called LICENSE.
23*******************************************************************************/
24
26#include "indilogger.h"
27#include "indidevapi.h"
28
29SafetyMonitorClient::SafetyMonitorClient(const std::string &deviceName, std::function<void()> statusCallback)
30 : m_DeviceName(deviceName), m_DeviceOnline(false), m_HasSafetyStatus(false), m_SafetyStatusLP(1),
31 m_StatusCallback(statusCallback)
32{
33}
34
38
39bool SafetyMonitorClient::connectToServer(const std::string &host, int port)
40{
41 setServer(host.c_str(), port);
42
43 if (!connectServer())
44 {
45 LOGF_ERROR("Failed to connect to server %s:%d for device %s",
46 host.c_str(), port, m_DeviceName.c_str());
47 return false;
48 }
49
50 // Watch for the specific device
51 watchDevice(m_DeviceName.c_str());
52
53 LOGF_INFO("Safety Monitor Client: Connecting to %s@%s:%d",
54 m_DeviceName.c_str(), host.c_str(), port);
55
56 return true;
57}
58
60{
61 if (!m_HasSafetyStatus)
62 return IPS_IDLE;
63
64 return m_SafetyStatusLP.getState();
65}
66
68{
69 if (m_DeviceName == dp.getDeviceName())
70 {
71 m_DeviceOnline = true;
72 LOGF_INFO("Safety Monitor Client: Device %s is online", m_DeviceName.c_str());
73 }
74}
75
77{
78 if (m_DeviceName == dp.getDeviceName())
79 {
80 m_DeviceOnline = false;
81 m_HasSafetyStatus = false;
82 LOGF_WARN("Safety Monitor Client: Device %s went offline", m_DeviceName.c_str());
83 }
84}
85
87{
88 if (property.getDeviceName() == m_DeviceName && property.isNameMatch("SAFETY_STATUS"))
89 {
90 auto lp = property.getLight();
91 if (lp)
92 {
93 m_SafetyStatusLP = property;
94 m_HasSafetyStatus = true;
95 LOGF_INFO("Safety Monitor Client: Received SAFETY_STATUS from %s, state: %s",
96 m_DeviceName.c_str(), pstateStr(m_SafetyStatusLP.getState()));
97
98 // Notify parent driver of status change
99 if (m_StatusCallback)
100 m_StatusCallback();
101 }
102 }
103}
104
106{
107 if (property.getDeviceName() == m_DeviceName && property.isNameMatch("SAFETY_STATUS"))
108 {
109 m_SafetyStatusLP = property;
110 LOGF_INFO("Safety Monitor Client: Updated safety status from %s, state: %s",
111 m_DeviceName.c_str(), pstateStr(m_SafetyStatusLP.getState()));
112
113 // Notify parent driver of status change
114 if (m_StatusCallback)
115 m_StatusCallback();
116 }
117}
118
120{
121 LOGF_INFO("Safety Monitor Client: Connected to server for %s", m_DeviceName.c_str());
122}
123
125{
126 LOGF_WARN("Safety Monitor Client: Disconnected from server for %s (exit code: %d)",
127 m_DeviceName.c_str(), exitCode);
128 m_DeviceOnline = false;
129 m_HasSafetyStatus = false;
130}
void setServer(const char *hostname, unsigned int port)
Set the server host name and port.
void watchDevice(const char *deviceName)
Add a device to the watch list.
bool connectServer() override
Connect to INDI server.
Class to provide basic INDI device functionality.
Definition basedevice.h:52
const char * getDeviceName() const
Provides generic container for INDI properties.
const char * getDeviceName() const
bool isNameMatch(const char *otherName) const
void newProperty(INDI::Property property) override
Emmited when a new property is created for an INDI driver.
bool connectToServer(const std::string &host, int port)
Connect to an INDI server.
void newDevice(INDI::BaseDevice dp) override
Emmited when a new device is created from INDI server.
IPState getSafetyStatus() const
Get the current safety status.
void updateProperty(INDI::Property property) override
Emmited when a new property value arrives from INDI server.
void serverConnected() override
Emmited when the server is connected.
void removeDevice(INDI::BaseDevice dp) override
Emmited when a device is deleted from INDI server.
void serverDisconnected(int exitCode) override
Emmited when the server gets disconnected.
SafetyMonitorClient(const std::string &deviceName, std::function< void()> statusCallback)
IPState
Property state.
Definition indiapi.h:158
@ IPS_IDLE
Definition indiapi.h:159
const char * pstateStr(IPState s)
Definition indidevapi.c:845
Interface to the reference INDI C API device implementation on the Device Driver side.
#define LOGF_INFO(fmt,...)
Definition indilogger.h:82
#define LOGF_WARN(fmt,...)
Definition indilogger.h:81
#define LOGF_ERROR(fmt,...)
Definition indilogger.h:80