supla-device
Loading...
Searching...
No Matches
relay_roller_shutter_pair.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*/
9
10#ifndef SRC_SUPLA_CONTROL_RELAY_ROLLER_SHUTTER_PAIR_H_
11#define SRC_SUPLA_CONTROL_RELAY_ROLLER_SHUTTER_PAIR_H_
12
13#include <supla/channels/channel.h>
14#include <supla/control/relay.h>
15#include <supla/control/roller_shutter.h>
16#include <supla/io.h>
17
18namespace Supla {
19namespace Control {
20class ActionTrigger;
21class Button;
22
23class ManagedRelay : public Relay {
24 public:
25 ManagedRelay(Supla::Io::IoPin outputPin,
26 Channel &externalChannel,
27 _supla_int_t functions =
28 (0xFF ^ SUPLA_BIT_FUNC_CONTROLLINGTHEROLLERSHUTTER));
29
30 void setRuntimeActive(bool active);
31 void setStorageMode(bool active);
32 void setLogicalState(bool state);
33 void forceOff();
34 // Loads relay-specific config only. The shared channel function must already
35 // be loaded by the owner before this method is called.
36 void loadEngineConfigOnly();
37 void purgeEngineConfigOnly();
38 void setupButtonActions(Button *button);
39
40 void turnOn(_supla_int_t duration = 0) override;
41 void turnOff(_supla_int_t duration = 0) override;
42 bool isOn() override;
43 void toggle(_supla_int_t duration = 0) override;
44 void handleAction(int event, int action) override;
46 override;
47 void iterateAlways() override;
48 bool iterateConnected() override;
49 bool setAndSaveFunction(uint32_t channelFunction) override;
50
51 private:
52 bool runtimeActive = true;
53 bool storageMode = false;
54 bool logicalState = false;
55};
56
57class ManagedRollerShutter : public RollerShutter {
58 public:
59 ManagedRollerShutter(Supla::Io::IoPin pinUp,
60 Supla::Io::IoPin pinDown,
61 bool tiltFunctionsEnabled,
62 Channel &externalChannel);
63
64 void setRuntimeActive(bool active);
65 void forceStopAndSwitchOff();
66 void forcePublishValue();
67 bool markCalibrationLostAfterRelayMode();
68 // Loads roller-specific config only. The shared channel function must already
69 // be loaded by the owner before this method is called.
70 void loadEngineConfigOnly();
71 void purgeEngineConfigOnly();
72 void setupButtonActions(Button *button, bool upButton, bool asInternal);
73
75 override;
76 void handleAction(int event, int action) override;
77 void iterateAlways() override;
78 void onTimer() override;
79
80 void close() override;
81 void open() override;
82 void stop() override;
83 void moveUp() override;
84 void moveDown() override;
85 void setTargetPosition(int newPosition,
86 int newTilt = UNKNOWN_POSITION) override;
87
88 private:
89 bool runtimeActive = true;
90};
91
92class RelayRollerShutterPair : public ElementWithChannelActions {
93 public:
94 RelayRollerShutterPair(Supla::Io::IoPin output0,
95 Supla::Io::IoPin output1,
96 bool tiltFunctionsEnabled = true,
97 _supla_int_t relayFunctions =
98 (0xFF ^
99 SUPLA_BIT_FUNC_CONTROLLINGTHEROLLERSHUTTER));
100 RelayRollerShutterPair(Supla::Io::Base *io,
101 int output0,
102 int output1,
103 bool highIsOn = true,
104 bool tiltFunctionsEnabled = true,
105 _supla_int_t relayFunctions =
106 (0xFF ^
107 SUPLA_BIT_FUNC_CONTROLLINGTHEROLLERSHUTTER));
108 RelayRollerShutterPair(int output0,
109 int output1,
110 bool highIsOn = true,
111 bool tiltFunctionsEnabled = true,
112 _supla_int_t relayFunctions =
113 (0xFF ^
114 SUPLA_BIT_FUNC_CONTROLLINGTHEROLLERSHUTTER));
115 ~RelayRollerShutterPair() override;
116
117 void attach(Button *primaryButton, Button *secondaryButton,
118 bool asInternal = true);
119 void attach(Button *primaryButton,
120 Button *secondaryButton,
121 ActionTrigger *primaryActionTrigger,
122 ActionTrigger *secondaryActionTrigger,
123 bool asInternal = true);
124 void attach(Button *button, bool primaryOrUp, bool asInternal = true);
125 void attach(Button *button,
126 ActionTrigger *actionTrigger,
127 bool primaryOrUp,
128 bool asInternal = true);
129
130 Channel *getChannel() override;
131 const Channel *getChannel() const override;
132 Channel *getSecondaryChannel() override;
133 const Channel *getSecondaryChannel() const override;
134
135 bool isInRelayMode() const;
136 bool isInRollerShutterMode() const;
137 RelayRollerShutterPair &setDefaultStateOn();
138 RelayRollerShutterPair &setDefaultStateOff();
139 RelayRollerShutterPair &setDefaultStateRestore();
140 bool setDefaultFunctions(uint32_t primaryFunction,
141 uint32_t secondaryFunction);
142 RollerShutter *getRollerShutter();
143 const RollerShutter *getRollerShutter() const;
144
145 int32_t handleNewValueFromServer(TSD_SuplaChannelNewValue *value) override;
147 bool getRemainingCountdownTimerSec(uint32_t *remainingSec) const override;
149 bool local = false) override;
151 TSDS_SetChannelConfigResult *result) override;
152 void handleChannelConfigFinished() override;
153 int handleCalcfgFromServer(TSD_DeviceCalCfgRequest *request) override;
155 TSD_DeviceCalCfgRequest *request) const override;
156
157 void onLoadConfig(SuplaDeviceClass *sdc) override;
158 void purgeConfig() override;
159 void onLoadState() override;
160 void onSaveState() override;
161 void onInit() override;
162 void iterateAlways() override;
163 bool iterateConnected() override;
164 void onTimer() override;
165 void onFunctionChange(uint32_t currentFunction,
166 uint32_t newFunction) override;
167
168 ApplyConfigResult applyChannelConfig(TSD_ChannelConfig *config,
169 bool local = false) override;
170 void fillChannelConfig(void *channelConfig,
171 int *size,
172 uint8_t configType) override;
173
174 protected:
175 bool setAndSaveFunction(uint32_t channelFunction) override;
176
177 private:
178 static Supla::Io::IoPin makeOutputPin(Supla::Io::Base *io,
179 int pin,
180 bool highIsOn);
181 static _supla_int_t rollerFunctions(bool tiltFunctionsEnabled);
182 static _supla_int_t relayOnlyFunctions(_supla_int_t relayFunctions);
183 static _supla_int_t primaryFunctions(_supla_int_t relayFunctions,
184 bool tiltFunctionsEnabled);
185
186 struct ButtonListElement {
187 Button *button = nullptr;
188 ActionTrigger *actionTrigger = nullptr;
189 bool primaryOrUp = true;
190 bool asInternal = true;
191 ButtonListElement *next = nullptr;
192 };
193
194 bool isPrimaryRollerFunction() const;
195 bool isRollerFunction(uint32_t function) const;
196 bool isPrimaryChannel(int channelNumber) const;
197 bool isSecondaryChannel(int channelNumber) const;
198 void switchToRelayMode();
199 void switchToRollerMode();
200 void applyRuntimeMode();
201 ElementWithChannelActions *primaryActiveEngine();
202 const ElementWithChannelActions *primaryActiveEngine() const;
203 void rebuildButtonActions();
204 void rebuildButtonActionsThreadSafe();
205 void appendButton(Button *button,
206 ActionTrigger *actionTrigger,
207 bool primaryOrUp,
208 bool asInternal);
209 void setupButtonAction(ButtonListElement *buttonListElement);
210 void updateActionTriggerRelatedChannel(ButtonListElement *buttonListElement);
211
212 Channel primaryChannel;
213 Channel secondaryChannel;
214
215 ManagedRelay relay0;
216 ManagedRelay relay1;
217 ManagedRollerShutter rollerShutter;
218
219 bool rollerModeActive = false;
220 bool buttonActionsInitialized = false;
221 ButtonListElement *buttonList = nullptr;
222};
223
224} // namespace Control
225} // namespace Supla
226
227#endif // SRC_SUPLA_CONTROL_RELAY_ROLLER_SHUTTER_PAIR_H_
Definition SuplaDevice.h:177
Definition channel.h:33
Definition action_trigger.h:45
Definition button.h:34
Definition relay_roller_shutter_pair.h:23
bool iterateConnected() override
Method called on each SuplaDevice iteration when device is connected and registered to Supla server o...
Definition relay_roller_shutter_pair.cpp:133
int32_t handleNewValueFromServer(TSD_SuplaChannelNewValue *newValue) override
Handles "new value" request from server.
Definition relay_roller_shutter_pair.cpp:118
void iterateAlways() override
Method called on each SuplaDevice iteration.
Definition relay_roller_shutter_pair.cpp:126
Definition relay_roller_shutter_pair.h:57
void onTimer() override
Method called on timer interupt.
Definition relay_roller_shutter_pair.cpp:237
int32_t handleNewValueFromServer(TSD_SuplaChannelNewValue *newValue) override
Handles "new value" request from server.
Definition relay_roller_shutter_pair.cpp:215
void iterateAlways() override
Method called on each SuplaDevice iteration.
Definition relay_roller_shutter_pair.cpp:230
void handleChannelConfigFinished() override
Handles "channel config finished" message from server.
Definition relay_roller_shutter_pair.cpp:760
const Channel * getChannel() const override
Returns pointer to main channel.
Definition relay_roller_shutter_pair.cpp:431
uint32_t getCalcfgPendingTimeoutMs(TSD_DeviceCalCfgRequest *request) const override
Returns timeout in milliseconds for CALCFG requests that become pending.
Definition relay_roller_shutter_pair.cpp:785
void onLoadConfig(SuplaDeviceClass *sdc) override
First method called on element in SuplaDevice.begin().
Definition relay_roller_shutter_pair.cpp:801
void fillSuplaChannelNewValue(TSD_SuplaChannelNewValue *value) override
Fills TSD_SuplaChannelNewValue based on current state.
Definition relay_roller_shutter_pair.cpp:699
uint8_t handleChannelConfig(TSD_ChannelConfig *config, bool local=false) override
Handles "set channel config" request from server.
Definition relay_roller_shutter_pair.cpp:727
void onSaveState() override
Method called periodically during SuplaDevice iteration.
Definition relay_roller_shutter_pair.cpp:832
int32_t handleNewValueFromServer(TSD_SuplaChannelNewValue *value) override
Handles "new value" request from server.
Definition relay_roller_shutter_pair.cpp:678
void onLoadState() override
Second method called on element in SuplaDevice.begin().
Definition relay_roller_shutter_pair.cpp:817
void handleSetChannelConfigResult(TSDS_SetChannelConfigResult *result) override
Handles "set channel config" reply from server.
Definition relay_roller_shutter_pair.cpp:744
void onFunctionChange(uint32_t currentFunction, uint32_t newFunction) override
Called when channel function changes.
Definition relay_roller_shutter_pair.cpp:880
bool getRemainingCountdownTimerSec(uint32_t *remainingSec) const override
Returns active countdown timer remaining time in seconds.
Definition relay_roller_shutter_pair.cpp:716
bool iterateConnected() override
Method called on each SuplaDevice iteration when device is connected and registered to Supla server o...
Definition relay_roller_shutter_pair.cpp:864
const Channel * getSecondaryChannel() const override
Returns pointer to secondary channel.
Definition relay_roller_shutter_pair.cpp:439
void onInit() override
Third method called on element in SuplaDevice.begin().
Definition relay_roller_shutter_pair.cpp:842
void onTimer() override
Method called on timer interupt.
Definition relay_roller_shutter_pair.cpp:874
int handleCalcfgFromServer(TSD_DeviceCalCfgRequest *request) override
Handles CALCFG requests from server.
Definition relay_roller_shutter_pair.cpp:767
void purgeConfig() override
Removes all configration data related to the element from Storage::Config.
Definition relay_roller_shutter_pair.cpp:810
void iterateAlways() override
Method called on each SuplaDevice iteration.
Definition relay_roller_shutter_pair.cpp:854
Definition roller_shutter.h:36
RollerShutter(Supla::Io::Base *io, int pinUp, int pinDown, bool highIsOn=true, bool tiltFunctionsEnabled=false)
Constructor.
Definition roller_shutter.cpp:43
Element * next()
Returns next Element from the list.
Definition element.cpp:114
Definition io.h:36
Definition io_pin.h:28
Definition proto.h:3427
Definition proto.h:3411
Definition proto.h:2592
Definition proto.h:1226