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 {
43class Io;
44
45namespace Control {
46class Button;
47
48class Relay : public ChannelElement, public ActionHandler {
49 public:
50 explicit Relay(Supla::Io *io, int pin,
51 bool highIsOn = true,
52 _supla_int_t functions = (0xFF ^
53 SUPLA_BIT_FUNC_CONTROLLINGTHEROLLERSHUTTER));
54 explicit Relay(int pin,
55 bool highIsOn = true,
56 _supla_int_t functions = (0xFF ^
57 SUPLA_BIT_FUNC_CONTROLLINGTHEROLLERSHUTTER));
58
59 virtual ~Relay();
60
61 virtual Relay &setDefaultStateOn();
62 virtual Relay &setDefaultStateOff();
63 virtual Relay &setDefaultStateRestore();
64 virtual Relay &keepTurnOnDuration(bool keep = true); // DEPREACATED
65
66 virtual uint8_t pinOnValue();
67 virtual uint8_t pinOffValue();
68 virtual void turnOn(_supla_int_t duration = 0);
69 virtual void turnOff(_supla_int_t duration = 0);
70 virtual bool isOn();
71 virtual void toggle(_supla_int_t duration = 0);
72
73 void attach(Supla::Control::Button *);
74
75 void handleAction(int event, int action) override;
76
77 void onLoadConfig(SuplaDeviceClass *sdc) override;
78 void onInit() override;
79 void onLoadState() override;
80 void onSaveState() override;
81 void iterateAlways() override;
82 bool iterateConnected() override;
83 int32_t handleNewValueFromServer(TSD_SuplaChannelNewValue *newValue) override;
84 void onRegistered(Supla::Protocol::SuplaSrpc *suplaSrpc) override;
85 Supla::ApplyConfigResult applyChannelConfig(TSD_ChannelConfig *result,
86 bool local = false) override;
87
88 // Method is used by external integrations to prepare TSD_SuplaChannelNewValue
89 // value for specific channel type (i.e. to prefill durationMS field when
90 // required)
92
93 unsigned _supla_int_t getStoredTurnOnDurationMs();
94
95 bool isStaircaseFunction() const;
96 bool isImpulseFunction() const;
97 void disableCountdownTimerFunction();
98 void enableCountdownTimerFunction();
99 bool isCountdownTimerFunctionEnabled() const;
100 void setMinimumAllowedDurationMs(uint32_t durationMs);
101 void fillChannelConfig(void *channelConfig,
102 int *size,
103 uint8_t configType) override;
104
105 static void setRelayStorageSaveDelay(int delayMs);
106
107 bool isDefaultRelatedMeterChannelSet() const;
108 uint32_t getCurrentValueFromMeter() const;
109 void setDefaultRelatedMeterChannelNo(int channelNo);
110 void setTurnOffWhenEmptyAggregator(bool turnOff);
111
117 void setOvercurrentThreshold(uint32_t value);
118
124 uint32_t getOvercurrentThreshold() const { return overcurrentThreshold; }
125
131 void setOvercurrentMaxAllowed(uint32_t value);
132
138 uint32_t getOvercurrentMaxAllowed() const { return overcurrentMaxAllowed; }
139
140 protected:
142 Supla::Control::Button *button = nullptr;
143 ButtonListElement *next = nullptr;
144 };
145
146 void saveConfig() const;
147 void setChannelFunction(_supla_int_t newFunction);
148 void updateTimerValue();
149 void updateRelayHvacAggregator();
150 int32_t channelFunction = 0;
151 uint32_t durationMs = 0;
152 uint32_t storedTurnOnDurationMs = 0;
153 uint32_t durationTimestamp = 0;
154
155 uint32_t overcurrentThreshold = 0; // 0.01 A
156 uint32_t overcurrentMaxAllowed = 0; // 0.01 A
157 uint32_t overcurrentActiveTimestamp = 0;
158 uint32_t overcurrentCheckTimestamp = 0;
159
160 uint32_t timerUpdateTimestamp = 0;
161
162 Supla::Io *io = nullptr;
163 ButtonListElement *buttonList = nullptr;
164
165 uint16_t minimumAllowedDurationMs = 0;
166 int16_t pin = -1;
167
168 bool highIsOn = true;
169 bool keepTurnOnDurationMs = false;
170 bool lastStateOnTimerUpdate = false;
171 bool turnOffWhenEmptyAggregator = true;
172
173 int8_t stateOnInit = STATE_ON_INIT_OFF;
174
175 int16_t defaultRelatedMeterChannelNo = -1;
176
177 static int16_t relayStorageSaveDelay;
178};
179
180}; // namespace Control
181}; // namespace Supla
182
183#endif // SRC_SUPLA_CONTROL_RELAY_H_
Definition SuplaDevice.h:93
Definition button.h:30
uint32_t getOvercurrentMaxAllowed() const
Get overcurrent max level allowed.
Definition relay.h:138
void setOvercurrentThreshold(uint32_t value)
Set overcurrent threshold.
Definition relay.cpp:831
void onRegistered(Supla::Protocol::SuplaSrpc *suplaSrpc) override
Method called each time when device successfully registers to Supla server.
Definition relay.cpp:100
uint32_t getOvercurrentThreshold() const
Get overcurrent threshold.
Definition relay.h:124
void onLoadConfig(SuplaDeviceClass *sdc) override
First method called on element in SuplaDevice.begin().
Definition relay.cpp:76
void iterateAlways() override
Method called on each SuplaDevice iteration.
Definition relay.cpp:264
bool iterateConnected() override
Method called on each SuplaDevice iteration when device is connected and registered to Supla server o...
Definition relay.cpp:315
void onInit() override
Third method called on element in SuplaDevice.begin()
Definition relay.cpp:211
void setOvercurrentMaxAllowed(uint32_t value)
Set overcurrent max level allowed.
Definition relay.cpp:827
void onLoadState() override
Second method called on element in SuplaDevice.begin().
Definition relay.cpp:509
void onSaveState() override
Method called periodically during SuplaDevice iteration.
Definition relay.cpp:474
int32_t handleNewValueFromServer(TSD_SuplaChannelNewValue *newValue) override
Handles "new value" request from server.
Definition relay.cpp:325
void fillSuplaChannelNewValue(TSD_SuplaChannelNewValue *value) override
Fills TSD_SuplaChannelNewValue based on current state.
Definition relay.cpp:372
Definition io.h:33
Definition supla_srpc.h:55
Definition relay.h:141
Definition proto.h:2987
Definition proto.h:1169