supla-device
Loading...
Searching...
No Matches
action_trigger.h
1// SPDX-FileCopyrightText: AC SOFTWARE SP. Z O.O.
2// SPDX-License-Identifier: GPL-2.0-or-later
3
4#ifndef SRC_SUPLA_CONTROL_ACTION_TRIGGER_H_
5#define SRC_SUPLA_CONTROL_ACTION_TRIGGER_H_
6
7#include <stdint.h>
8
9#include <supla/action_handler.h>
10#include <supla/actions.h>
11#include <supla/at_channel.h>
12#include <supla/control/weekly_schedule_component.h>
13#include <supla/element_with_channel_actions.h>
14
15namespace Supla {
16
17namespace Protocol {
18class SuplaSrpc;
19}
20
21enum ActionHandlingType : uint8_t {
22 ActionHandlingType_RelayOnSuplaServer = 0,
23 ActionHandlingType_PublishAllDisableNone = 1,
24 ActionHandlingType_PublishAllDisableAll = 2
25};
26
27namespace Control {
28
29class Button;
30class ActionTriggerWeeklySchedule;
31
32
34 public:
35 friend class ActionTriggerWeeklySchedule;
36
38 struct {
39 uint8_t weeklySchedule : 1;
40 uint8_t locked : 1;
41 uint8_t reserved : 6;
42 } flags;
43 uint8_t rawValue = 0;
44 };
45
46 static_assert(sizeof(ActionTriggerFlags) == sizeof(uint8_t),
47 "Flags size must be 1 byte");
48
50 virtual ~ActionTrigger();
51 ActionTrigger *getActionTrigger() override { return this; }
52
53 // Use below methods to attach button instance to ActionTrigger.
54 // It will automatically register to all supported button actions
55 // during onInit() call on action trigger instance.
56 // One AT per Button. An occupied target is rejected, keeping this binding.
57 // Passing nullptr releases this AT's binding.
58 void attach(Supla::Control::Button *);
59 void attach(Supla::Control::Button &);
60
61 // Makes AT channel related to other channel, so Supla Cloud will not
62 // list AT as a separate channel, but it will be extending i.e. Relay
63 // channel.
64 void setRelatedChannel(Element *);
65 void setRelatedChannel(Channel *);
66 void setRelatedChannel(Element &);
67 void setRelatedChannel(Channel &);
68
69 void handleAction(int event, int action) override;
70 void activateAction(int action) override;
71 Supla::Channel *getChannel() override;
72 const Supla::Channel *getChannel() const override;
73 void onInit() override;
74 void onRegistered(Supla::Protocol::SuplaSrpc *suplaSrpc = nullptr) override;
75 bool iterateConnected() override;
77 void onLoadConfig(SuplaDeviceClass *) override;
78 void onLoadState() override;
79 void onSaveState() override;
80 void iterateAlways() override;
81 bool isEffectivelyLocked() const;
82 bool isTemporaryUnlockActive() const;
84 TSD_SuplaChannelNewValue *newValue) override;
85 void fillChannelConfig(void *channelConfig,
86 int *size,
87 uint8_t configType) override;
88 void purgeConfig() override;
89
90 void rebuildForAttachedButton();
91 void disableATCapability(uint32_t capToDisable);
92 void enableStateStorage();
93
94 static int actionTriggerCapToButtonEvent(uint32_t actionCap);
95 static int actionTriggerCapToActionId(uint32_t actionCap);
96 static int getActionTriggerCap(int action);
97
98 bool isAnyActionEnabledOnServer() const;
99
100 void setAlwaysUseOnClick1();
101
102 // Controls whether a locked AT button may execute local UNLOCK or
103 // TOGGLE_LOCK actions. The default keeps the historic full lock behavior.
104 ActionTrigger &setLocalUnlockAllowed(bool allowed = true);
105 bool isLocalUnlockAllowed() const;
106
107 // Explicit opt-in for a shared configuration button. This does not make
108 // every alwaysEnabled action bypass an AT lock; only the two built-in
109 // configuration actions are retained by Button when this is enabled.
110 ActionTrigger &setKeepConfigButtonTriggerAlwaysAvailable(
111 bool keep = true);
112 bool keepsConfigButtonTriggerAlwaysAvailable() const;
113
114 void enable();
115 void disable();
116
117 // Button modes: lock, temporary unlock and weekly schedule. By default they
118 // are unsupported for a standalone AT, and attaching a Button enables them
119 // permanently (attach() may be called any time before onInit(); a later
120 // detach keeps them). An explicit call overrides the default regardless of
121 // attach() order. When unsupported, mode commands and lock actions are
122 // rejected and the current/restored mode is dropped (state storage format
123 // is unchanged).
124 ActionTrigger &setButtonModeSupported(bool supported = true);
125 bool isButtonModeSupported() const;
126 // Temporary unlock (mode 6) follows button mode support by default and can
127 // be disabled separately. It can't be enabled without button modes. When
128 // disabled, commands 6/7 and weekly schedules with mode 6 are rejected, and
129 // a current/restored manual mode 6 becomes LOCKED.
130 ActionTrigger &setTemporaryUnlockSupported(bool supported = true);
131 bool isTemporaryUnlockSupported() const;
132
133 bool isWeeklyScheduleSupported() const;
134 ActionTrigger &setWeeklyScheduleAvailable(bool available = true);
135 bool setWeeklyScheduleController(
136 WeeklyScheduleController *controller,
137 WeeklyScheduleConfigHandler *configHandler = nullptr,
138 WeeklyScheduleProgramSource *programSource = nullptr);
139
140 protected:
141 ApplyConfigResult applyChannelConfig(TSD_ChannelConfig *result,
142 bool local) override;
143 bool shouldProcessChannelFunctionFromConfig() const override;
144 virtual void fillDefaultWeeklySchedule(
146 virtual bool isWeeklyScheduleProgramModeSupported(uint8_t mode) const;
147 bool ensureNativeWeeklyScheduleController();
148 void updateWeeklyScheduleCapabilities();
149 void updateButtonModeCapabilities();
150 void scheduleStateSave(uint32_t delayMsMax = 5000,
151 uint32_t delayMsMin = 2000);
152 void applyButtonMode(uint8_t mode);
153 void applyManualButtonMode(uint8_t mode);
154 void addActionToButtonAndDisableIt(int event, int action);
155 void parseActiveActionsFromServer();
156 bool hasDirectionalPair() const;
157 bool requiresClickMode() const;
158 static void synchronizeDirectionalButtonModes();
159
160 bool startTemporaryUnlock(uint32_t seconds, int32_t senderId = 0);
161 void updateTemporaryUnlock();
162 void updateUnlockSensor();
163 void syncEffectiveButtonLock();
164
165#pragma pack(push, 1)
167 uint8_t version = 1;
168 uint32_t function = SUPLA_CHANNELFNC_ACTIONTRIGGER;
169 uint32_t duration = 0;
170 uint8_t edge = SUPLA_AT_SENSOR_EDGE_NONE;
171 uint8_t sensor = 255;
172 // Disambiguates mode 6 from the legacy all-actions state sentinel.
173 uint32_t escapedActions = 0;
174 };
175#pragma pack(pop)
178 uint32_t startMs = 0;
179 uint32_t durationMs = 0;
180 uint16_t weeklyDurationS = 0;
181 bool baselineKnown = false;
182 bool baseline = false;
183 bool persistenceUncertain = false;
184 bool manualModeTransition = false;
185 // Weekly switch passes through mode 0; keep the timer until it settles.
186 bool keepTimerOnModeChange = false;
187 bool hasStoredActiveActions = false;
188 // Reported to the server with the remaining time (0 for local starts).
189 int32_t senderId = 0;
190 };
191 bool ensureTemporaryUnlockState();
192 bool persistTemporaryUnlockConfig(const TemporaryUnlockConfig &config,
193 bool localChange = false,
194 const uint32_t *activeActions = nullptr);
195 bool prepareTemporaryUnlockState();
196 TemporaryUnlockConfig currentTemporaryUnlockConfig() const;
197 TemporaryUnlockState *temporaryUnlock = nullptr;
198 // Remaining temporary unlock time is reported as a timer extended value.
199 // Returns true and the remaining time if the timer is active.
200 // Requires timerAccessMutex.
201 bool getTemporaryUnlockRemainingMs(uint32_t *remainingMs) const;
202 void sendTimerValue(uint32_t remainingMs,
203 uint8_t *targetValue,
204 int32_t senderId);
205
206 Supla::Control::Button *attachedButton = nullptr;
207 Supla::ActionHandlerClient *localHandlerForEnabledAt = nullptr;
208 Supla::ActionHandlerClient *localHandlerForDisabledAt = nullptr;
209 Supla::ActionHandler *localHandlerClient = nullptr;
210 uint32_t activeActionsFromServer = 0;
211 // Comparison cache only: PublishAllDisableAll overwrites the effective mask
212 // above. Keep state storage and its existing format unchanged.
213 uint32_t lastReceivedActiveActions = 0;
214 uint32_t disablesLocalOperation = 0;
215 uint32_t disabledCapabilities = 0;
216
217 Supla::AtChannel channel;
218
219 uint16_t localHandlerAction = 0;
220 ActionHandlingType actionHandlingType = ActionHandlingType_RelayOnSuplaServer;
221 // Runtime-only flags; state storage uses a separate representation.
224 : storageEnabled(false),
225 alwaysUseOnClick1(false),
226 enabled(true),
227 localHandlerSwitchConfigured(false),
228 channelConfigReceived(false),
229 weeklyScheduleAvailable(false),
230 localUnlockAllowed(false),
231 keepConfigButtonTriggerAlwaysAvailable(false),
232 buttonModeExplicit(false),
233 buttonModeEnabled(false),
234 temporaryUnlockDisabled(false),
235 timerReportForced(false),
236 timerReportPending(false),
237 timerReportedActive(false) {}
238
239 uint16_t storageEnabled : 1;
240 uint16_t alwaysUseOnClick1 : 1;
241 uint16_t enabled : 1;
242 uint16_t localHandlerSwitchConfigured : 1;
243 uint16_t channelConfigReceived : 1;
244 // Native weekly schedule remains opt-in.
245 uint16_t weeklyScheduleAvailable : 1;
246 uint16_t localUnlockAllowed : 1;
247 uint16_t keepConfigButtonTriggerAlwaysAvailable : 1;
248 // Button mode support: set by setButtonModeSupported() (explicit), or
249 // enabled by attaching a Button.
250 uint16_t buttonModeExplicit : 1;
251 uint16_t buttonModeEnabled : 1;
252 uint16_t temporaryUnlockDisabled : 1;
253 // Report timer state after registration, also when it is not running,
254 // so a timer stored by the server before a restart is cleared.
255 uint16_t timerReportForced : 1;
256 // Set on every timer (re)start, so a restart with the same end time
257 // (e.g. different sender) is reported too.
258 uint16_t timerReportPending : 1;
259 // Server was told that the timer is running. Kept outside of
260 // temporaryUnlock, because that state may be released while the server
261 // still shows a running timer.
262 uint16_t timerReportedActive : 1;
263 };
264
265 static_assert(sizeof(RuntimeFlags) == sizeof(uint16_t),
266 "ActionTrigger runtime flags must fit in uint16_t");
267
268 RuntimeFlags runtimeFlags;
269 WeeklyScheduleComponents weeklyScheduleComponents;
270};
271
272} // namespace Control
273} // namespace Supla
274
275#endif // SRC_SUPLA_CONTROL_ACTION_TRIGGER_H_
Definition SuplaDevice.h:167
Definition local_action.h:59
Definition action_handler.h:8
Definition at_channel.h:12
Definition channel.h:26
Definition action_trigger.cpp:66
Definition action_trigger.h:33
void onSaveState() override
Method called periodically during SuplaDevice iteration.
Definition action_trigger.cpp:1149
int32_t handleNewValueFromServer(TSD_SuplaChannelNewValue *newValue) override
Handles "new value" request from server.
Definition action_trigger.cpp:1559
void iterateAlways() override
Method called on each SuplaDevice iteration.
Definition action_trigger.cpp:1547
bool iterateConnected() override
Method called on each SuplaDevice iteration when device is connected and registered to Supla server o...
Definition action_trigger.cpp:514
void purgeConfig() override
Removes all configration data related to the element from Storage::Config.
Definition action_trigger.cpp:1687
void onRegistered(Supla::Protocol::SuplaSrpc *suplaSrpc=nullptr) override
Method called each time when device successfully registers to Supla server.
Definition action_trigger.cpp:499
void onInit() override
Third method called on element in SuplaDevice.begin()
Definition action_trigger.cpp:891
void onLoadState() override
Second method called on element in SuplaDevice.begin().
Definition action_trigger.cpp:1274
void onLoadConfig(SuplaDeviceClass *) override
First method called on element in SuplaDevice.begin().
Definition action_trigger.cpp:1188
Definition button.h:21
Definition weekly_schedule_component.h:130
Definition weekly_schedule_component.h:114
Definition weekly_schedule_component.h:55
Definition weekly_schedule_component.h:34
Definition element_with_channel_actions.h:56
bool iterateConnected() override
Method called on each SuplaDevice iteration when device is connected and registered to Supla server o...
Definition element_with_channel_actions.cpp:511
Base class for all elements of SuplaDevice.
Definition element.h:30
Definition supla_srpc.h:47
Definition action_trigger.h:222
Definition proto.h:3336
Definition proto.h:3206
Definition proto.h:1264