supla-device
Loading...
Searching...
No Matches
weathersender.h
1// SPDX-FileCopyrightText: malarz
2// SPDX-License-Identifier: GPL-2.0-or-later
3
4#ifndef SRC_SUPLA_PROTOCOL_WEATHERSENDER_H_
5#define SRC_SUPLA_PROTOCOL_WEATHERSENDER_H_
6
7#include <supla/log_wrapper.h>
8#include <supla/network/network.h>
9#include <supla/sensor/general_purpose_channel_base.h>
10
11#define MAXSENSORS 12
12namespace Supla {
13
14enum SenorType : uint8_t {
15 PM1,
16 PM2_5,
17 PM4,
18 PM10,
19 TEMP,
20 HUMI,
21 PRESS,
22 LIGHT,
23 WIND,
24 RAIN,
25 CO2,
26};
27
28namespace Protocol {
29
31 public:
32 explicit WeatherSender(Supla::Network* _network) {
33 network = _network;
34 lastSendTime = millis() - 100 * 1000;
35 }
36
37 void addSensor(Supla::SenorType type, Supla::LocalAction* sensor,
38 int option = 0) {
39 SUPLA_LOG_DEBUG("weathersender: added sensor [%d]", type);
40 sensors[type] = sensor;
41 options[type] = option;
42 }
43
44 double getSensorValue(int type) {
45 double value = NAN;
46
47 if (sensors[type] == nullptr) {
48 return value;
49 }
50
51 switch (type) {
52 case Supla::SenorType::PM1:
53 value = ((Supla::Sensor::GeneralPurposeChannelBase*)(sensors[type]))
54 ->getCalculatedValue();
55 SUPLA_LOG_DEBUG("weathersender: pm1.0 = %.2f", value);
56 break;
57 case Supla::SenorType::PM2_5:
58 value = ((Supla::Sensor::GeneralPurposeChannelBase*)(sensors[type]))
59 ->getCalculatedValue();
60 SUPLA_LOG_DEBUG("weathersender: pm2.5 = %.2f", value);
61 break;
62 case Supla::SenorType::PM4:
63 value = ((Supla::Sensor::GeneralPurposeChannelBase*)(sensors[type]))
64 ->getCalculatedValue();
65 SUPLA_LOG_DEBUG("weathersender: pm4 = %.2f", value);
66 break;
67 case Supla::SenorType::PM10:
68 value = ((Supla::Sensor::GeneralPurposeChannelBase*)(sensors[type]))
69 ->getCalculatedValue();
70 SUPLA_LOG_DEBUG("weathersender: pm10 = %.2f", value);
71 break;
72 case Supla::SenorType::TEMP:
73 value = ((Supla::Channel*)(sensors[type]))
74 ->getLastTemperature();
75 SUPLA_LOG_DEBUG("weathersender: temperature = %.2f", value);
76 break;
77 case Supla::SenorType::HUMI:
78 value = ((Supla::Channel*)(sensors[type]))
79 ->getValueDoubleSecond();
80 SUPLA_LOG_DEBUG("weathersender: humidity = %.2f", value);
81 break;
82 case Supla::SenorType::PRESS:
83 value = ((Supla::Channel*)(sensors[type]))
84 ->getValueDouble();
85 SUPLA_LOG_DEBUG("weathersender: press = %.2f", value);
86 break;
87 case Supla::SenorType::LIGHT:
88 value = ((Supla::Sensor::GeneralPurposeChannelBase*)(sensors[type]))
89 ->getCalculatedValue();
90 SUPLA_LOG_DEBUG("weathersender: light = %.2f", value);
91 break;
92 case Supla::SenorType::WIND:
93 value = ((Supla::Channel*)(sensors[type]))
94 ->getValueDouble();
95 SUPLA_LOG_DEBUG("weathersender: wind = %.2f", value);
96 break;
97 case Supla::SenorType::RAIN:
98 value = ((Supla::Channel*)(sensors[type]))
99 ->getValueDouble();
100 SUPLA_LOG_DEBUG("weathersender: rain = %.2f", value);
101 break;
102 case Supla::SenorType::CO2:
103 value = ((Supla::Sensor::GeneralPurposeChannelBase*)(sensors[type]))
104 ->getCalculatedValue();
105 SUPLA_LOG_DEBUG("weathersender: co2 = %.2f", value);
106 break;
107 }
108
109 return value;
110 }
111
112 virtual bool sendData() = 0;
113
114 void iterateAlways() override {
115 if (millis() - lastSendTime > refreshTime * 1000) {
116 if (!Supla::Network::IsReady()) {
117 lastSendTime += 10000;
118 } else {
119 if (sendData())
120 lastSendTime = millis();
121 else
122 lastSendTime += 10000;
123 }
124 }
125 }
126
127 protected:
128 int refreshTime = 180;
129 uint32_t lastSendTime = 0;
130 Supla::LocalAction* sensors[MAXSENSORS] = {};
131 int options[MAXSENSORS] = {};
132 Supla::Network* network = nullptr;
133};
134
135} // namespace Protocol
136} // namespace Supla
137
138#endif // SRC_SUPLA_PROTOCOL_WEATHERSENDER_H_
Definition channel.h:26
Base class for all elements of SuplaDevice.
Definition element.h:27
Definition local_action.h:42
Definition network.h:21
Definition weathersender.h:30
void iterateAlways() override
Method called on each SuplaDevice iteration.
Definition weathersender.h:114
Base class for General Purpose Measurement (GPM/KPOP) and General Purpose Meter (GPM/KLOP)
Definition general_purpose_channel_base.h:22