Instrument Neutral Distributed Interface INDI 2.2.5
ups.h
Go to the documentation of this file.
1/*******************************************************************************
2 Copyright(c) 2025 Jérémie Klein. All rights reserved.
3
4 INDI UPS Driver using NUT (Network UPS Tools)
5 This driver monitors a UPS through NUT and exposes its status through INDI's weather interface.
6 Battery level and power status are mapped to weather parameters for compatibility.
7
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2 of the License, or (at your option)
11 any later version.
12
13 This program is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 more details.
17
18 You should have received a copy of the GNU Library General Public License
19 along with this library; see the file COPYING.LIB. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA.
22
23 The full GNU General Public License is included in this distribution in the
24 file called LICENSE.
25*******************************************************************************/
26
27#pragma once
28
29#include "defaultdevice.h"
31#include <string>
32#include <functional>
33#include <map>
34
35#ifdef _USE_SYSTEM_JSONLIB
36#include <nlohmann/json.hpp>
37#else
38#include <indijson.hpp>
39#endif
40
42{
43 public:
44 UPS();
45 virtual ~UPS() = default;
46
47 virtual bool initProperties() override;
48 const char *getDefaultName() override;
49 void ISGetProperties(const char *dev) override;
50 virtual bool updateProperties() override;
51 virtual bool ISNewText(const char *dev, const char *name, char *texts[], char *names[], int n) override;
52 virtual bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n) override;
53 virtual bool saveConfigItems(FILE *fp) override;
54 virtual bool loadConfig(bool silent = false, const char *property = nullptr) override;
55
56 protected:
57 bool Connect() override;
58 bool Disconnect() override;
59
60 // Timer callback for periodic updates
61 void TimerHit() override;
62
63 private:
64 // Handshake will be called by the Connection::TCP class
65 bool Handshake();
66
67 // Update UPS status
68 void updateUPSStatus();
69
70 // NUT helper methods
71 bool queryUPSStatus();
72 bool parseUPSResponse(const std::string& response);
73 std::string makeNUTRequest(const std::string& command);
74
75 // UPS name property
76 INDI::PropertyText UPSNameTP {1}; // UPS name in NUT
77
78 // Connection settings property
79 INDI::PropertyNumber ConnectionSettingsNP {2}; // Retries, retry delay
80
81 // Update period
82 INDI::PropertyNumber UpdatePeriodNP {1}; // Update period in seconds
83
84 // UPS parameters (battery charge, voltage, etc.)
85 INDI::PropertyNumber UPSParametersNP {3};
86 enum {
87 UPS_BATTERY_CHARGE,
88 UPS_BATTERY_VOLTAGE,
89 UPS_INPUT_VOLTAGE
90 };
91
92 // Safety status property
93 INDI::PropertyLight SafetyStatusLP {1};
94
95 // Battery charge thresholds
96 INDI::PropertyNumber BatteryThresholdsNP {2};
97 enum {
98 BATTERY_WARNING_THRESHOLD,
99 BATTERY_CRITICAL_THRESHOLD
100 };
101
102 // UPS status parameters mapping
103 std::map<std::string, std::string> upsParameters;
104
105 // TCP Connection
106 Connection::TCP *tcpConnection {nullptr};
107
108 // State variables
109 bool LastParseSuccess {false};
110 int PortFD {-1};
111 int m_TimerID {-1};
112};
The TCP class manages connection with devices over the network via TCP/IP. Upon successful connection...
Class to provide extended functionality for devices in addition to the functionality provided by INDI...
Definition ups.h:42
bool Disconnect() override
Disconnect from device.
Definition ups.cpp:177
virtual ~UPS()=default
void TimerHit() override
Callback function to be called once SetTimer duration elapses.
Definition ups.cpp:212
virtual bool saveConfigItems(FILE *fp) override
saveConfigItems Save specific properties in the provide config file handler. Child class usually over...
Definition ups.cpp:579
bool Connect() override
Connect to the device. INDI::DefaultDevice implementation connects to appropriate connection interfac...
Definition ups.cpp:166
virtual bool updateProperties() override
updateProperties is called whenever there is a change in the CONNECTION status of the driver....
Definition ups.cpp:189
virtual bool ISNewText(const char *dev, const char *name, char *texts[], char *names[], int n) override
Process the client newSwitch command.
Definition ups.cpp:516
virtual bool loadConfig(bool silent=false, const char *property=nullptr) override
Load the last saved configuration file.
Definition ups.cpp:591
void ISGetProperties(const char *dev) override
define the driver's properties to the client. Usually, only a minimum set of properties are defined t...
Definition ups.cpp:100
virtual bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n) override
Process the client newNumber command.
Definition ups.cpp:533
virtual bool initProperties() override
Initialize properties initial state and value. The child class must implement this function.
Definition ups.cpp:62
const char * getDefaultName() override
Definition ups.cpp:57
UPS()
Definition ups.cpp:41