supla-device
Loading...
Searching...
No Matches
relay.h
1/*
2 Copyright (C) AC SOFTWARE SP. Z O.O.
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 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12 You should have received a copy of the GNU General Public License
13 along with this program; if not, write to the Free Software
14 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15*/
16
17/* Relay class
18 * This class is used to control any type of relay that can be controlled
19 * by setting LOW or HIGH output on selected GPIO.
20 */
21
22#ifndef SRC_SUPLA_CONTROL_RELAY_H_
23#define SRC_SUPLA_CONTROL_RELAY_H_
24
25#include <stdint.h>
26
27#include "../action_handler.h"
28#include "../channel_element.h"
29#include "../io.h"
30
31#define STATE_ON_INIT_RESTORED_OFF -3
32#define STATE_ON_INIT_RESTORED_ON -2
33#define STATE_ON_INIT_RESTORE -1
34#define STATE_ON_INIT_OFF 0
35#define STATE_ON_INIT_ON 1
36
37#define RELAY_FLAGS_ON (1 << 0)
38#define RELAY_FLAGS_STAIRCASE (1 << 1)
39#define RELAY_FLAGS_IMPULSE_FUNCTION (1 << 2) // i.e. gate, door, gateway
40#define RELAY_FLAGS_OVERCURRENT (1 << 3)
41
42
43namespace Supla {
44namespace Io {
45struct IoPin;
46}
47
48namespace Control {
49class Button;
50
51class Relay : public ChannelElement, public ActionHandler {
52 public:
53 explicit Relay(Supla::Io::IoPin outputPin,
54 _supla_int_t functions =
55 (0xFF ^ SUPLA_BIT_FUNC_CONTROLLINGTHEROLLERSHUTTER));
56 explicit Relay(Supla::Io::Base *io, int pin,
57 bool highIsOn = true,
58 _supla_int_t functions = (0xFF ^
59 SUPLA_BIT_FUNC_CONTROLLINGTHEROLLERSHUTTER));
60 explicit Relay(int pin,
61 bool highIsOn = true,
62 _supla_int_t functions = (0xFF ^
63 SUPLA_BIT_FUNC_CONTROLLINGTHEROLLERSHUTTER));
64
65 virtual ~Relay();
66
67 virtual Relay &setDefaultStateOn();
68 virtual Relay &setDefaultStateOff();
69 virtual Relay &setDefaultStateRestore();
70 virtual Relay &keepTurnOnDuration(bool keep = true); // DEPREACATED
71
72 [[deprecated("Use IoPin::writeActive/writeInactive instead")]]
73 virtual uint8_t pinOnValue();
74 [[deprecated("Use IoPin::writeActive/writeInactive instead")]]
75 virtual uint8_t pinOffValue();
76 virtual void turnOn(_supla_int_t duration = 0);
77 virtual void turnOff(_supla_int_t duration = 0);
78 virtual bool isOn();
79 virtual void toggle(_supla_int_t duration = 0);
80
81 void attach(Supla::Control::Button *);
82
83 void handleAction(int event, int action) override;
84
85 void onLoadConfig(SuplaDeviceClass *sdc) override;
86 void purgeConfig() override;
87 void onInit() override;
88 void onLoadState() override;
89 void onSaveState() override;
90 void iterateAlways() override;
91 bool iterateConnected() override;
92 int32_t handleNewValueFromServer(TSD_SuplaChannelNewValue *newValue) override;
93 void onRegistered(Supla::Protocol::SuplaSrpc *suplaSrpc) override;
94 Supla::ApplyConfigResult applyChannelConfig(TSD_ChannelConfig *result,
95 bool local = false) override;
96 void fillChannelConfig(void *channelConfig,
97 int *size,
98 uint8_t configType) override;
99
100 // Method is used by external integrations to prepare TSD_SuplaChannelNewValue
101 // value for specific channel type (i.e. to prefill durationMS field when
102 // required)
104
105 unsigned _supla_int_t getStoredTurnOnDurationMs();
106 void setStoredTurnOnDurationMs(uint32_t durationMs);
107
108 bool isStaircaseFunction(uint32_t functionToCheck = 0) const;
109 bool isImpulseFunction(uint32_t functionToCheck = 0) const;
110 void disableCountdownTimerFunction();
111 void enableCountdownTimerFunction();
112 bool isCountdownTimerFunctionEnabled() const;
113 void setMinimumAllowedDurationMs(uint32_t durationMs);
114
115 static void setRelayStorageSaveDelay(int delayMs);
116
117 bool isDefaultRelatedMeterChannelSet() const;
118 uint32_t getCurrentValueFromMeter() const;
119 void setDefaultRelatedMeterChannelNo(int channelNo);
120 void setTurnOffWhenEmptyAggregator(bool turnOff);
121
127 void setOvercurrentThreshold(uint32_t value);
128
134 uint32_t getOvercurrentThreshold() const { return overcurrentThreshold; }
135
141 void setOvercurrentMaxAllowed(uint32_t value);
142
148 uint32_t getOvercurrentMaxAllowed() const { return overcurrentMaxAllowed; }
149
155 void setDefaultStaircaseDurationMs(uint16_t durationMs) {
156 defaultStaircaseDurationMs = durationMs;
157 }
158
164 void setDefaultImpulseDurationMs(uint16_t durationMs) {
165 defaultImpulseDurationMs = durationMs;
166 }
167
168 bool setAndSaveFunction(uint32_t channelFunction) override;
169
175 void setRestartTimerOnToggle(bool restart);
176
182 bool isRestartTimerOnToggle() const;
183
184 bool isFullyInitialized() const;
185
186 void enableCyclicMode(uint32_t turnOnTimeMs, uint32_t turnOffTimeMs);
187 void disableCyclicMode();
188 bool isCyclicMode() const;
189
190 protected:
192 Supla::Control::Button *button = nullptr;
193 ButtonListElement *next = nullptr;
194 };
195
196 void applyDuration(int durationMs, bool turnOn);
197
198 virtual void setNewChannelValue(bool value);
199
200 void saveConfig() const;
201 void updateTimerValue();
202 void updateRelayHvacAggregator();
203 uint32_t durationMs = 0;
204 uint32_t storedTurnOnDurationMs = 0;
205 uint32_t durationTimestamp = 0;
206 uint32_t turnOffDurationForCycle = 0;
207 uint16_t defaultStaircaseDurationMs = 10000;
208 uint16_t defaultImpulseDurationMs = 500;
209
210 uint32_t overcurrentThreshold = 0; // 0.01 A
211 uint32_t overcurrentMaxAllowed = 0; // 0.01 A
212 uint32_t overcurrentActiveTimestamp = 0;
213 uint32_t overcurrentCheckTimestamp = 0;
214
215 uint32_t timerUpdateTimestamp = 0;
216 uint32_t postponeCommTimestamp = 0;
217
218 ButtonListElement *buttonList = nullptr;
219
220 uint16_t minimumAllowedDurationMs = 0;
221 int16_t defaultRelatedMeterChannelNo = -1;
222
223 bool keepTurnOnDurationMs = false;
224 bool turnOffWhenEmptyAggregator = true;
225 bool initDone = false;
226 bool restartTimerOnToggle = false;
227 bool skipInitialStateSetting = false;
228
229 int8_t stateOnInit = STATE_ON_INIT_OFF;
230 Supla::Io::IoPin outputPin;
231
232 static int16_t relayStorageSaveDelay;
233};
234
235}; // namespace Control
236}; // namespace Supla
237
238#endif // SRC_SUPLA_CONTROL_RELAY_H_
Definition SuplaDevice.h:162
Definition button.h:34
void setRestartTimerOnToggle(bool restart)
Set restart timer on toggle for staircase and impulse functions.
Definition relay.cpp:1061
uint32_t getOvercurrentMaxAllowed() const
Get overcurrent max level allowed.
Definition relay.h:148
void setOvercurrentThreshold(uint32_t value)
Set overcurrent threshold.
Definition relay.cpp:1014
void onRegistered(Supla::Protocol::SuplaSrpc *suplaSrpc) override
Method called each time when device successfully registers to Supla server.
Definition relay.cpp:126
uint32_t getOvercurrentThreshold() const
Get overcurrent threshold.
Definition relay.h:134
void onLoadConfig(SuplaDeviceClass *sdc) override
First method called on element in SuplaDevice.begin().
Definition relay.cpp:92
void setDefaultImpulseDurationMs(uint16_t durationMs)
Set default duration for impulse functions (controlling gate, door etc.).
Definition relay.h:164
void iterateAlways() override
Method called on each SuplaDevice iteration.
Definition relay.cpp:339
bool iterateConnected() override
Method called on each SuplaDevice iteration when device is connected and registered to Supla server o...
Definition relay.cpp:397
void onInit() override
Third method called on element in SuplaDevice.begin().
Definition relay.cpp:251
void setOvercurrentMaxAllowed(uint32_t value)
Set overcurrent max level allowed.
Definition relay.cpp:1010
bool isRestartTimerOnToggle() const
Get restart timer on toggle for staircase and impulse functions.
Definition relay.cpp:1065
void setDefaultStaircaseDurationMs(uint16_t durationMs)
Set default duration for staircase timer function.
Definition relay.h:155
void purgeConfig() override
Removes all configration data related to the element from Storage::Config.
Definition relay.cpp:1051
void onLoadState() override
Second method called on element in SuplaDevice.begin().
Definition relay.cpp:637
void onSaveState() override
Method called periodically during SuplaDevice iteration.
Definition relay.cpp:602
int32_t handleNewValueFromServer(TSD_SuplaChannelNewValue *newValue) override
Handles "new value" request from server.
Definition relay.cpp:412
void fillSuplaChannelNewValue(TSD_SuplaChannelNewValue *value) override
Fills TSD_SuplaChannelNewValue based on current state.
Definition relay.cpp:466
Definition io.h:36
Definition supla_srpc.h:55
Definition relay.h:191
Definition io_pin.h:28
Definition proto.h:3074
Definition proto.h:1199