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 &setPreloadStateOnSoftReset(bool enabled = true);
71 virtual Relay &keepTurnOnDuration(bool keep = true); // DEPREACATED
72
73 [[deprecated("Use IoPin::writeActive/writeInactive instead")]]
74 virtual uint8_t pinOnValue();
75 [[deprecated("Use IoPin::writeActive/writeInactive instead")]]
76 virtual uint8_t pinOffValue();
77 virtual void turnOn(_supla_int_t duration = 0);
78 virtual void turnOff(_supla_int_t duration = 0);
79 virtual bool isOn();
80 virtual void toggle(_supla_int_t duration = 0);
81
82 void attach(Supla::Control::Button *);
83
84 void handleAction(int event, int action) override;
85
86 void onLoadConfig(SuplaDeviceClass *sdc) override;
87 void purgeConfig() override;
88 void onInit() override;
89 void onLoadState() override;
90 void onSaveState() override;
91 void iterateAlways() override;
92 bool iterateConnected() override;
93 int32_t handleNewValueFromServer(TSD_SuplaChannelNewValue *newValue) override;
94 void onRegistered(Supla::Protocol::SuplaSrpc *suplaSrpc) override;
95 Supla::ApplyConfigResult applyChannelConfig(TSD_ChannelConfig *result,
96 bool local = false) override;
97 void fillChannelConfig(void *channelConfig,
98 int *size,
99 uint8_t configType) override;
107 bool getRemainingCountdownTimerSec(uint32_t *remainingSec) const override;
108
109 // Method is used by external integrations to prepare TSD_SuplaChannelNewValue
110 // value for specific channel type (i.e. to prefill durationMS field when
111 // required)
113
114 unsigned _supla_int_t getStoredTurnOnDurationMs();
115 void setStoredTurnOnDurationMs(uint32_t durationMs);
116
117 bool isStaircaseFunction(uint32_t functionToCheck = 0) const;
118 bool isImpulseFunction(uint32_t functionToCheck = 0) const;
119 void disableCountdownTimerFunction();
120 void enableCountdownTimerFunction();
121 bool isCountdownTimerFunctionEnabled() const;
122 void setMinimumAllowedDurationMs(uint32_t durationMs);
123
124 static void setRelayStorageSaveDelay(int delayMs);
125
126 bool isDefaultRelatedMeterChannelSet() const;
127 uint32_t getCurrentValueFromMeter() const;
128 void setDefaultRelatedMeterChannelNo(int channelNo);
129 void setTurnOffWhenEmptyAggregator(bool turnOff);
130
136 void setOvercurrentThreshold(uint32_t value);
137
143 uint32_t getOvercurrentThreshold() const { return overcurrentThreshold; }
144
150 void setOvercurrentMaxAllowed(uint32_t value);
151
157 uint32_t getOvercurrentMaxAllowed() const { return overcurrentMaxAllowed; }
158
164 void setDefaultStaircaseDurationMs(uint16_t durationMs) {
165 defaultStaircaseDurationMs = durationMs;
166 }
167
173 void setDefaultImpulseDurationMs(uint16_t durationMs) {
174 defaultImpulseDurationMs = durationMs;
175 }
176
177 bool setAndSaveFunction(uint32_t channelFunction) override;
178
184 void setRestartTimerOnToggle(bool restart);
185
191 bool isRestartTimerOnToggle() const;
192
193 bool isFullyInitialized() const;
194
195 void enableCyclicMode(uint32_t turnOnTimeMs, uint32_t turnOffTimeMs);
196 void disableCyclicMode();
197 bool isCyclicMode() const;
198
199 protected:
200 Relay(Supla::Io::IoPin outputPin,
201 _supla_int_t functions,
202 Supla::Channel &externalChannel,
203 ElementMode mode);
204
206 Supla::Control::Button *button = nullptr;
207 ButtonListElement *next = nullptr;
208 };
209
210 void applyDuration(int durationMs, bool turnOn);
211
212 virtual void setNewChannelValue(bool value);
213
214 void saveConfig() const;
215 void loadRelayConfigOnly();
216 void purgeRelayConfigOnly();
217 void updateTimerValue();
218 void emitCountdownTimerActionIfNeeded();
219 void updateRelayHvacAggregator();
220 uint32_t durationMs = 0;
221 uint32_t storedTurnOnDurationMs = 0;
222 uint32_t durationTimestamp = 0;
223 uint32_t turnOffDurationForCycle = 0;
224 uint16_t defaultStaircaseDurationMs = 10000;
225 uint16_t defaultImpulseDurationMs = 500;
226
227 uint32_t overcurrentThreshold = 0; // 0.01 A
228 uint32_t overcurrentMaxAllowed = 0; // 0.01 A
229 uint32_t overcurrentActiveTimestamp = 0;
230 uint32_t overcurrentCheckTimestamp = 0;
231
232 uint32_t timerUpdateTimestamp = 0;
233 uint32_t lastCountdownTimerRemainingSec = UINT32_MAX;
234 uint32_t postponeCommTimestamp = 0;
235
236 ButtonListElement *buttonList = nullptr;
237
238 uint16_t minimumAllowedDurationMs = 0;
239 int16_t defaultRelatedMeterChannelNo = -1;
240
241 bool keepTurnOnDurationMs = false;
242 bool turnOffWhenEmptyAggregator = true;
243 bool initDone = false;
244 bool restartTimerOnToggle = false;
245 bool skipInitialStateSetting = false;
246 bool preloadStateOnSoftReset = false;
247
248 int8_t stateOnInit = STATE_ON_INIT_OFF;
249 Supla::Io::IoPin outputPin;
250
251 static int16_t relayStorageSaveDelay;
252};
253
254}; // namespace Control
255}; // namespace Supla
256
257#endif // SRC_SUPLA_CONTROL_RELAY_H_
Definition SuplaDevice.h:177
Definition channel.h:33
Definition button.h:34
Definition relay.h:51
bool getRemainingCountdownTimerSec(uint32_t *remainingSec) const override
Returns remaining countdown timer time in seconds for an active relay countdown timer.
Definition relay.cpp:418
void setRestartTimerOnToggle(bool restart)
Set restart timer on toggle for staircase and impulse functions.
Definition relay.cpp:1124
uint32_t getOvercurrentMaxAllowed() const
Get overcurrent max level allowed.
Definition relay.h:157
void setOvercurrentThreshold(uint32_t value)
Set overcurrent threshold.
Definition relay.cpp:1073
void onRegistered(Supla::Protocol::SuplaSrpc *suplaSrpc) override
Method called each time when device successfully registers to Supla server.
Definition relay.cpp:146
uint32_t getOvercurrentThreshold() const
Get overcurrent threshold.
Definition relay.h:143
void onLoadConfig(SuplaDeviceClass *sdc) override
First method called on element in SuplaDevice.begin().
Definition relay.cpp:105
void setDefaultImpulseDurationMs(uint16_t durationMs)
Set default duration for impulse functions (controlling gate, door etc.).
Definition relay.h:173
void iterateAlways() override
Method called on each SuplaDevice iteration.
Definition relay.cpp:359
bool iterateConnected() override
Method called on each SuplaDevice iteration when device is connected and registered to Supla server o...
Definition relay.cpp:451
void onInit() override
Third method called on element in SuplaDevice.begin().
Definition relay.cpp:271
void setOvercurrentMaxAllowed(uint32_t value)
Set overcurrent max level allowed.
Definition relay.cpp:1069
bool isRestartTimerOnToggle() const
Get restart timer on toggle for staircase and impulse functions.
Definition relay.cpp:1128
void setDefaultStaircaseDurationMs(uint16_t durationMs)
Set default duration for staircase timer function.
Definition relay.h:164
void purgeConfig() override
Removes all configration data related to the element from Storage::Config.
Definition relay.cpp:1110
void onLoadState() override
Second method called on element in SuplaDevice.begin().
Definition relay.cpp:691
void onSaveState() override
Method called periodically during SuplaDevice iteration.
Definition relay.cpp:656
int32_t handleNewValueFromServer(TSD_SuplaChannelNewValue *newValue) override
Handles "new value" request from server.
Definition relay.cpp:466
void fillSuplaChannelNewValue(TSD_SuplaChannelNewValue *value) override
Fills TSD_SuplaChannelNewValue based on current state.
Definition relay.cpp:520
Definition io.h:36
Definition supla_srpc.h:61
Definition relay.h:205
Definition io_pin.h:28
Definition proto.h:3411
Definition proto.h:1226