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
30#define STATE_ON_INIT_RESTORED_OFF -3
31#define STATE_ON_INIT_RESTORED_ON -2
32#define STATE_ON_INIT_RESTORE -1
33#define STATE_ON_INIT_OFF 0
34#define STATE_ON_INIT_ON 1
35
36#define RELAY_FLAGS_ON (1 << 0)
37#define RELAY_FLAGS_STAIRCASE (1 << 1)
38#define RELAY_FLAGS_IMPULSE_FUNCTION (1 << 2) // i.e. gate, door, gateway
39#define RELAY_FLAGS_OVERCURRENT (1 << 3)
40
41
42namespace Supla {
43namespace Io {
44class Base;
45}
46
47namespace Control {
48class Button;
49
50class Relay : public ChannelElement, public ActionHandler {
51 public:
52 explicit Relay(Supla::Io::Base *io, int pin,
53 bool highIsOn = true,
54 _supla_int_t functions = (0xFF ^
55 SUPLA_BIT_FUNC_CONTROLLINGTHEROLLERSHUTTER));
56 explicit Relay(int pin,
57 bool highIsOn = true,
58 _supla_int_t functions = (0xFF ^
59 SUPLA_BIT_FUNC_CONTROLLINGTHEROLLERSHUTTER));
60
61 virtual ~Relay();
62
63 virtual Relay &setDefaultStateOn();
64 virtual Relay &setDefaultStateOff();
65 virtual Relay &setDefaultStateRestore();
66 virtual Relay &keepTurnOnDuration(bool keep = true); // DEPREACATED
67
68 virtual uint8_t pinOnValue();
69 virtual uint8_t pinOffValue();
70 virtual void turnOn(_supla_int_t duration = 0);
71 virtual void turnOff(_supla_int_t duration = 0);
72 virtual bool isOn();
73 virtual void toggle(_supla_int_t duration = 0);
74
75 void attach(Supla::Control::Button *);
76
77 void handleAction(int event, int action) override;
78
79 void onLoadConfig(SuplaDeviceClass *sdc) override;
80 void onInit() override;
81 void onLoadState() override;
82 void onSaveState() override;
83 void iterateAlways() override;
84 bool iterateConnected() override;
85 int32_t handleNewValueFromServer(TSD_SuplaChannelNewValue *newValue) override;
86 void onRegistered(Supla::Protocol::SuplaSrpc *suplaSrpc) override;
87 Supla::ApplyConfigResult applyChannelConfig(TSD_ChannelConfig *result,
88 bool local = false) override;
89
90 // Method is used by external integrations to prepare TSD_SuplaChannelNewValue
91 // value for specific channel type (i.e. to prefill durationMS field when
92 // required)
94
95 unsigned _supla_int_t getStoredTurnOnDurationMs();
96 void setStoredTurnOnDurationMs(uint32_t durationMs);
97
98 bool isStaircaseFunction(uint32_t functionToCheck = 0) const;
99 bool isImpulseFunction(uint32_t functionToCheck = 0) const;
100 void disableCountdownTimerFunction();
101 void enableCountdownTimerFunction();
102 bool isCountdownTimerFunctionEnabled() const;
103 void setMinimumAllowedDurationMs(uint32_t durationMs);
104 void fillChannelConfig(void *channelConfig,
105 int *size,
106 uint8_t configType) override;
107
108 static void setRelayStorageSaveDelay(int delayMs);
109
110 bool isDefaultRelatedMeterChannelSet() const;
111 uint32_t getCurrentValueFromMeter() const;
112 void setDefaultRelatedMeterChannelNo(int channelNo);
113 void setTurnOffWhenEmptyAggregator(bool turnOff);
114
120 void setOvercurrentThreshold(uint32_t value);
121
127 uint32_t getOvercurrentThreshold() const { return overcurrentThreshold; }
128
134 void setOvercurrentMaxAllowed(uint32_t value);
135
141 uint32_t getOvercurrentMaxAllowed() const { return overcurrentMaxAllowed; }
142
148 void setDefaultStaircaseDurationMs(uint16_t durationMs) {
149 defaultStaircaseDurationMs = durationMs;
150 }
151
157 void setDefaultImpulseDurationMs(uint16_t durationMs) {
158 defaultImpulseDurationMs = durationMs;
159 }
160
161 bool setAndSaveFunction(uint32_t channelFunction) override;
162
163 protected:
165 Supla::Control::Button *button = nullptr;
166 ButtonListElement *next = nullptr;
167 };
168
169 void saveConfig() const;
170 void updateTimerValue();
171 void updateRelayHvacAggregator();
172 uint32_t durationMs = 0;
173 uint32_t storedTurnOnDurationMs = 0;
174 uint32_t durationTimestamp = 0;
175 uint16_t defaultStaircaseDurationMs = 10000;
176 uint16_t defaultImpulseDurationMs = 500;
177
178 uint32_t overcurrentThreshold = 0; // 0.01 A
179 uint32_t overcurrentMaxAllowed = 0; // 0.01 A
180 uint32_t overcurrentActiveTimestamp = 0;
181 uint32_t overcurrentCheckTimestamp = 0;
182
183 uint32_t timerUpdateTimestamp = 0;
184
185 Supla::Io::Base *io = nullptr;
186 ButtonListElement *buttonList = nullptr;
187
188 uint16_t minimumAllowedDurationMs = 0;
189 int16_t pin = -1;
190
191 bool highIsOn = true;
192 bool keepTurnOnDurationMs = false;
193 bool lastStateOnTimerUpdate = false;
194 bool turnOffWhenEmptyAggregator = true;
195 bool initDone = false;
196
197 int8_t stateOnInit = STATE_ON_INIT_OFF;
198
199 int16_t defaultRelatedMeterChannelNo = -1;
200
201 static int16_t relayStorageSaveDelay;
202};
203
204}; // namespace Control
205}; // namespace Supla
206
207#endif // SRC_SUPLA_CONTROL_RELAY_H_
Definition SuplaDevice.h:97
Definition button.h:34
uint32_t getOvercurrentMaxAllowed() const
Get overcurrent max level allowed.
Definition relay.h:141
void setOvercurrentThreshold(uint32_t value)
Set overcurrent threshold.
Definition relay.cpp:903
void onRegistered(Supla::Protocol::SuplaSrpc *suplaSrpc) override
Method called each time when device successfully registers to Supla server.
Definition relay.cpp:107
uint32_t getOvercurrentThreshold() const
Get overcurrent threshold.
Definition relay.h:127
void onLoadConfig(SuplaDeviceClass *sdc) override
First method called on element in SuplaDevice.begin().
Definition relay.cpp:76
void setDefaultImpulseDurationMs(uint16_t durationMs)
Set default duration for impulse functions (controlling gate, door etc.)
Definition relay.h:157
void iterateAlways() override
Method called on each SuplaDevice iteration.
Definition relay.cpp:289
bool iterateConnected() override
Method called on each SuplaDevice iteration when device is connected and registered to Supla server o...
Definition relay.cpp:340
void onInit() override
Third method called on element in SuplaDevice.begin()
Definition relay.cpp:232
void setOvercurrentMaxAllowed(uint32_t value)
Set overcurrent max level allowed.
Definition relay.cpp:899
void setDefaultStaircaseDurationMs(uint16_t durationMs)
Set default duration for staircase timer function.
Definition relay.h:148
void onLoadState() override
Second method called on element in SuplaDevice.begin().
Definition relay.cpp:534
void onSaveState() override
Method called periodically during SuplaDevice iteration.
Definition relay.cpp:499
int32_t handleNewValueFromServer(TSD_SuplaChannelNewValue *newValue) override
Handles "new value" request from server.
Definition relay.cpp:350
void fillSuplaChannelNewValue(TSD_SuplaChannelNewValue *value) override
Fills TSD_SuplaChannelNewValue based on current state.
Definition relay.cpp:397
Definition io.h:35
Definition supla_srpc.h:55
Definition relay.h:164
Definition proto.h:3041
Definition proto.h:1179