Instrument Neutral Distributed Interface INDI 2.2.5
weatherflow.h
Go to the documentation of this file.
1/*******************************************************************************
2 Copyright(c) 2024 WeatherFlow Tempest Weather Driver
3
4 INDI WeatherFlow Tempest Weather Driver
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
25#pragma once
26
27#include "indiweather.h"
28#include "indipropertytext.h"
29#include "indipropertynumber.h"
30
31#include <memory>
32#include <string>
33#include <chrono>
34
35#ifdef _USE_SYSTEM_JSONLIB
36#include <nlohmann/json.hpp>
37#else
38#include <indijson.hpp>
39#endif
40
42
44{
45 public:
47 virtual ~WeatherFlow();
48
49 // Generic indi device entries
50 bool Connect() override;
51 bool Disconnect() override;
52 const char *getDefaultName() override;
53
54 virtual bool initProperties() override;
55 virtual void ISGetProperties(const char *dev) override;
56 virtual bool ISNewText(const char *dev, const char *name, char *texts[], char *names[], int n) override;
57 virtual bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n) override;
58
59 protected:
60 virtual IPState updateWeather() override;
61 virtual bool saveConfigItems(FILE *fp) override;
62 virtual bool updateLocation(double latitude, double longitude, double elevation) override;
63
64 private:
65 // Configuration properties
66 INDI::PropertyText wfAPIKeyTP{1};
67 INDI::PropertyText wfStationIDTP{1};
68 INDI::PropertyNumber wfSettingsNP{3};
69
70 // Settings indices
71 enum
72 {
73 WF_UPDATE_INTERVAL,
74 WF_CONNECTION_TIMEOUT,
75 WF_RETRY_ATTEMPTS
76 };
77
78 // WeatherFlow API data structures
79 struct WeatherFlowData
80 {
81 double air_temperature = 0.0;
82 double relative_humidity = 0.0;
83 double barometric_pressure = 0.0;
84 double wind_avg = 0.0;
85 double wind_gust = 0.0;
86 double wind_direction = 0.0;
87 double precip_accum_local_day = 0.0;
88 double precip_rate = 0.0;
89 double solar_radiation = 0.0;
90 double uv = 0.0;
91 std::chrono::system_clock::time_point timestamp;
92 };
93
94 // Private helper methods
95 bool fetchStationInfo();
96 bool fetchCurrentObservations();
97 bool parseStationResponse(const std::string &response);
98 bool parseObservationsResponse(const std::string &response);
99 bool makeAPIRequest(const std::string &endpoint, std::string &response);
100 bool retryRequest(const std::function<bool()> &request);
101
102 // State variables
103 std::string m_stationID;
104 std::string m_deviceID;
105 WeatherFlowData m_lastData;
106 std::chrono::system_clock::time_point m_lastUpdate;
107 bool m_isConnected = false;
108
109 // API configuration
110 // httplib::Client takes host and port separately from the request path.
111 const std::string API_HOST = "swd.weatherflow.com";
112 const std::string STATIONS_ENDPOINT = "/swd/rest/stations";
113 const std::string OBSERVATIONS_ENDPOINT = "/swd/rest/observations/station/";
114 const std::string DEVICE_OBSERVATIONS_ENDPOINT = "/swd/rest/observations/";
115
116 // Rate limiting
117 const int RATE_LIMIT_REQUESTS = 1000;
118 const int RATE_LIMIT_PERIOD = 3600; // 1 hour in seconds
119 std::chrono::system_clock::time_point m_lastRequestTime;
120 int m_requestCount = 0;
121};
virtual void ISGetProperties(const char *dev) override
define the driver's properties to the client. Usually, only a minimum set of properties are defined t...
virtual bool initProperties() override
Initialize properties initial state and value. The child class must implement this function.
virtual ~WeatherFlow()
virtual IPState updateWeather() override
updateWeather Update weather conditions from device or service. The function should not change the st...
bool Connect() override
Connect to the device. INDI::DefaultDevice implementation connects to appropriate connection interfac...
virtual bool ISNewText(const char *dev, const char *name, char *texts[], char *names[], int n) override
Process the client newSwitch command.
virtual bool saveConfigItems(FILE *fp) override
saveConfigItems Save specific properties in the provide config file handler. Child class usually over...
virtual bool updateLocation(double latitude, double longitude, double elevation) override
Update weather station location.
const char * getDefaultName() override
bool Disconnect() override
Disconnect from device.
virtual bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n) override
Process the client newNumber command.
a class to store JSON values
IPState
Property state.
Definition indiapi.h:158
basic_json<> json
default specialization