supla-device
Loading...
Searching...
No Matches
weathersender.h
1/*
2 * Copyright (C) malarz
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19#ifndef SRC_SUPLA_PROTOCOL_WEATHERSENDER_H_
20#define SRC_SUPLA_PROTOCOL_WEATHERSENDER_H_
21
22#include <supla/log_wrapper.h>
23#include <supla/network/network.h>
24#include <supla/sensor/general_purpose_channel_base.h>
25
26#define MAXSENSORS 12
27namespace Supla {
28
29enum SenorType : uint8_t {
30 PM1,
31 PM2_5,
32 PM4,
33 PM10,
34 TEMP,
35 HUMI,
36 PRESS,
37 LIGHT,
38 WIND,
39 RAIN,
40 CO2,
41};
42
43namespace Protocol {
44
45class WeatherSender : public Supla::Element {
46 public:
47 explicit WeatherSender(Supla::Network* _network) {
48 network = _network;
49 lastSendTime = millis() - 100 * 1000;
50 }
51
52 void addSensor(Supla::SenorType type, Supla::LocalAction* sensor,
53 int option = 0) {
54 SUPLA_LOG_DEBUG("weathersender: added sensor [%d]", type);
55 sensors[type] = sensor;
56 options[type] = option;
57 }
58
59 double getSensorValue(int type) {
60 double value = NAN;
61
62 if (sensors[type] == nullptr) {
63 return value;
64 }
65
66 switch (type) {
67 case Supla::SenorType::PM1:
68 value = ((Supla::Sensor::GeneralPurposeChannelBase*)(sensors[type]))
69 ->getCalculatedValue();
70 SUPLA_LOG_DEBUG("weathersender: pm1.0 = %.2f", value);
71 break;
72 case Supla::SenorType::PM2_5:
73 value = ((Supla::Sensor::GeneralPurposeChannelBase*)(sensors[type]))
74 ->getCalculatedValue();
75 SUPLA_LOG_DEBUG("weathersender: pm2.5 = %.2f", value);
76 break;
77 case Supla::SenorType::PM4:
78 value = ((Supla::Sensor::GeneralPurposeChannelBase*)(sensors[type]))
79 ->getCalculatedValue();
80 SUPLA_LOG_DEBUG("weathersender: pm4 = %.2f", value);
81 break;
82 case Supla::SenorType::PM10:
83 value = ((Supla::Sensor::GeneralPurposeChannelBase*)(sensors[type]))
84 ->getCalculatedValue();
85 SUPLA_LOG_DEBUG("weathersender: pm10 = %.2f", value);
86 break;
87 case Supla::SenorType::TEMP:
88 value = ((Supla::Channel*)(sensors[type]))
89 ->getLastTemperature();
90 SUPLA_LOG_DEBUG("weathersender: temperature = %.2f", value);
91 break;
92 case Supla::SenorType::HUMI:
93 value = ((Supla::Channel*)(sensors[type]))
94 ->getValueDoubleSecond();
95 SUPLA_LOG_DEBUG("weathersender: humidity = %.2f", value);
96 break;
97 case Supla::SenorType::PRESS:
98 value = ((Supla::Channel*)(sensors[type]))
99 ->getValueDouble();
100 SUPLA_LOG_DEBUG("weathersender: press = %.2f", value);
101 break;
102 case Supla::SenorType::LIGHT:
103 value = ((Supla::Sensor::GeneralPurposeChannelBase*)(sensors[type]))
104 ->getCalculatedValue();
105 SUPLA_LOG_DEBUG("weathersender: light = %.2f", value);
106 break;
107 case Supla::SenorType::WIND:
108 value = ((Supla::Channel*)(sensors[type]))
109 ->getValueDouble();
110 SUPLA_LOG_DEBUG("weathersender: wind = %.2f", value);
111 break;
112 case Supla::SenorType::RAIN:
113 value = ((Supla::Channel*)(sensors[type]))
114 ->getValueDouble();
115 SUPLA_LOG_DEBUG("weathersender: rain = %.2f", value);
116 break;
117 case Supla::SenorType::CO2:
118 value = ((Supla::Sensor::GeneralPurposeChannelBase*)(sensors[type]))
119 ->getCalculatedValue();
120 SUPLA_LOG_DEBUG("weathersender: co2 = %.2f", value);
121 break;
122 }
123
124 return value;
125 }
126
127 virtual bool sendData() = 0;
128
129 void iterateAlways() override {
130 if (millis() - lastSendTime > refreshTime * 1000) {
131 if (!Supla::Network::IsReady()) {
132 lastSendTime += 10000;
133 } else {
134 if (sendData())
135 lastSendTime = millis();
136 else
137 lastSendTime += 10000;
138 }
139 }
140 }
141
142 protected:
143 int refreshTime = 180;
144 uint32_t lastSendTime = 0;
145 Supla::LocalAction* sensors[MAXSENSORS] = {};
146 int options[MAXSENSORS] = {};
147 Supla::Network* network = nullptr;
148};
149
150} // namespace Protocol
151} // namespace Supla
152
153#endif // SRC_SUPLA_PROTOCOL_WEATHERSENDER_H_
Definition channel.h:33
Base class for all elements of SuplaDevice.
Definition element.h:37
Definition local_action.h:52
Definition network.h:36
void iterateAlways() override
Method called on each SuplaDevice iteration.
Definition weathersender.h:129
Base class for General Purpose Measurement (GPM/KPOP) and General Purpose Meter (GPM/KLOP)
Definition general_purpose_channel_base.h:37