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 "../actions.h"
29#include "../channel_element.h"
30#include "../io.h"
31#include "../local_action.h"
32#include "../storage/storage.h"
33
34#define STATE_ON_INIT_RESTORED_OFF -3
35#define STATE_ON_INIT_RESTORED_ON -2
36#define STATE_ON_INIT_RESTORE -1
37#define STATE_ON_INIT_OFF 0
38#define STATE_ON_INIT_ON 1
39
40#define RELAY_FLAGS_ON (1 << 0)
41#define RELAY_FLAGS_STAIRCASE (1 << 1)
42#define RELAY_FLAGS_IMPULSE_FUNCTION (1 << 2) // i.e. gate, door, gateway
43
44
45namespace Supla {
46namespace Control {
47class Button;
48
49class Relay : public ChannelElement, public ActionHandler {
50 public:
51 explicit Relay(Supla::Io *io, int pin,
52 bool highIsOn = true,
53 _supla_int_t functions = (0xFF ^
54 SUPLA_BIT_FUNC_CONTROLLINGTHEROLLERSHUTTER));
55 explicit Relay(int pin,
56 bool highIsOn = true,
57 _supla_int_t functions = (0xFF ^
58 SUPLA_BIT_FUNC_CONTROLLINGTHEROLLERSHUTTER));
59
60 virtual ~Relay();
61
62 virtual Relay &setDefaultStateOn();
63 virtual Relay &setDefaultStateOff();
64 virtual Relay &setDefaultStateRestore();
65 virtual Relay &keepTurnOnDuration(bool keep = true); // DEPREACATED
66
67 virtual uint8_t pinOnValue();
68 virtual uint8_t pinOffValue();
69 virtual void turnOn(_supla_int_t duration = 0);
70 virtual void turnOff(_supla_int_t duration = 0);
71 virtual bool isOn();
72 virtual void toggle(_supla_int_t duration = 0);
73
74 void attach(Supla::Control::Button *);
75
76 void handleAction(int event, int action) override;
77
78 void onLoadConfig(SuplaDeviceClass *sdc) override;
79 void onInit() override;
80 void onLoadState() override;
81 void onSaveState() override;
82 void iterateAlways() override;
83 bool iterateConnected() override;
84 int32_t handleNewValueFromServer(TSD_SuplaChannelNewValue *newValue) override;
85 void onRegistered(Supla::Protocol::SuplaSrpc *suplaSrpc) override;
86 uint8_t applyChannelConfig(TSD_ChannelConfig *result,
87 bool local = false) override;
88
89 // Method is used by external integrations to prepare TSD_SuplaChannelNewValue
90 // value for specific channel type (i.e. to prefill durationMS field when
91 // required)
93
94 unsigned _supla_int_t getStoredTurnOnDurationMs();
95
96 bool isStaircaseFunction() const;
97 bool isImpulseFunction() const;
98 void disableCountdownTimerFunction();
99 void enableCountdownTimerFunction();
100 bool isCountdownTimerFunctionEnabled() const;
101 void setMinimumAllowedDurationMs(uint32_t durationMs);
102 void fillChannelConfig(void *channelConfig, int *size) override;
103
104 static void setRelayStorageSaveDelay(int delayMs);
105
106 void setDefaultRelatedMeterChannelNo(int channelNo);
107 void setTurnOffWhenEmptyAggregator(bool turnOff);
108
109 protected:
111 Supla::Control::Button *button = nullptr;
112 ButtonListElement *next = nullptr;
113 };
114
115 void setChannelFunction(_supla_int_t newFunction);
116 void updateTimerValue();
117 void updateRelayHvacAggregator();
118 int32_t channelFunction = 0;
119 uint32_t durationMs = 0;
120 uint32_t storedTurnOnDurationMs = 0;
121 uint32_t durationTimestamp = 0;
122 uint32_t overcurrentThreshold = 0;
123
124 uint32_t timerUpdateTimestamp = 0;
125
126 Supla::Io *io = nullptr;
127 ButtonListElement *buttonList = nullptr;
128
129 uint16_t minimumAllowedDurationMs = 0;
130 int16_t pin = -1;
131
132 bool highIsOn = true;
133 bool keepTurnOnDurationMs = false;
134 bool lastStateOnTimerUpdate = false;
135 bool turnOffWhenEmptyAggregator = true;
136
137 int8_t stateOnInit = STATE_ON_INIT_OFF;
138
139 int16_t defaultRelatedMeterChannelNo = -1;
140
141 static int16_t relayStorageSaveDelay;
142};
143
144}; // namespace Control
145}; // namespace Supla
146
147#endif // SRC_SUPLA_CONTROL_RELAY_H_
Definition SuplaDevice.h:93
Definition button.h:30
void onRegistered(Supla::Protocol::SuplaSrpc *suplaSrpc) override
Method called each time when device successfully registers to Supla server.
Definition relay.cpp:77
void onLoadConfig(SuplaDeviceClass *sdc) override
First method called on element in SuplaDevice.begin().
Definition relay.cpp:69
void iterateAlways() override
Method called on each SuplaDevice iteration.
Definition relay.cpp:200
bool iterateConnected() override
Method called on each SuplaDevice iteration when device is connected and registered to Supla server o...
Definition relay.cpp:206
void onInit() override
Third method called on element in SuplaDevice.begin()
Definition relay.cpp:147
void onLoadState() override
Second method called on element in SuplaDevice.begin().
Definition relay.cpp:396
void onSaveState() override
Method called periodically during SuplaDevice iteration.
Definition relay.cpp:364
int32_t handleNewValueFromServer(TSD_SuplaChannelNewValue *newValue) override
Handles "new value" request from server.
Definition relay.cpp:216
void fillSuplaChannelNewValue(TSD_SuplaChannelNewValue *value) override
Fills TSD_SuplaChannelNewValue based on current state.
Definition relay.cpp:263
Definition io.h:33
Definition supla_srpc.h:55
Definition relay.h:110
Definition proto.h:2912
Definition proto.h:1167