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
33 public:
34 friend class ActionTriggerWeeklySchedule;
35
37 struct {
38 uint8_t weeklySchedule : 1;
39 uint8_t locked : 1;
40 uint8_t reserved : 6;
41 } flags;
42 uint8_t rawValue = 0;
43 };
44
45 static_assert(sizeof(ActionTriggerFlags) == sizeof(uint8_t),
46 "Flags size must be 1 byte");
47
49 virtual ~ActionTrigger();
50 ActionTrigger *getActionTrigger() override { return this; }
51
52 // Use below methods to attach button instance to ActionTrigger.
53 // It will automatically register to all supported button actions
54 // during onInit() call on action trigger instance.
55 void attach(Supla::Control::Button *);
56 void attach(Supla::Control::Button &);
57
58 // Makes AT channel related to other channel, so Supla Cloud will not
59 // list AT as a separate channel, but it will be extending i.e. Relay
60 // channel.
61 void setRelatedChannel(Element *);
62 void setRelatedChannel(Channel *);
63 void setRelatedChannel(Element &);
64 void setRelatedChannel(Channel &);
65
66 void handleAction(int event, int action) override;
67 void activateAction(int action) override;
68 Supla::Channel *getChannel() override;
69 const Supla::Channel *getChannel() const override;
70 void onInit() override;
71 void onRegistered(Supla::Protocol::SuplaSrpc *suplaSrpc = nullptr) override;
72 void onLoadConfig(SuplaDeviceClass *) override;
73 void onLoadState() override;
74 void onSaveState() override;
75 void iterateAlways() override;
77 TSD_SuplaChannelNewValue *newValue) override;
78 void fillChannelConfig(void *channelConfig,
79 int *size,
80 uint8_t configType) override;
81 void purgeConfig() override;
82
83 void rebuildForAttachedButton();
84 void disableATCapability(uint32_t capToDisable);
85 void enableStateStorage();
86
87 static int actionTriggerCapToButtonEvent(uint32_t actionCap);
88 static int actionTriggerCapToActionId(uint32_t actionCap);
89 static int getActionTriggerCap(int action);
90
91 bool isAnyActionEnabledOnServer() const;
92
93 void setAlwaysUseOnClick1();
94
95 // Controls whether a locked AT button may execute local UNLOCK or
96 // TOGGLE_LOCK actions. The default keeps the historic full lock behavior.
97 ActionTrigger &setLocalUnlockAllowed(bool allowed = true);
98 bool isLocalUnlockAllowed() const;
99
100 // Explicit opt-in for a shared configuration button. This does not make
101 // every alwaysEnabled action bypass an AT lock; only the two built-in
102 // configuration actions are retained by Button when this is enabled.
103 ActionTrigger &setKeepConfigButtonTriggerAlwaysAvailable(
104 bool keep = true);
105 bool keepsConfigButtonTriggerAlwaysAvailable() const;
106
107 void enable();
108 void disable();
109
110 bool isWeeklyScheduleSupported() const;
111 ActionTrigger &setWeeklyScheduleAvailable(bool available = true);
112 bool setWeeklyScheduleController(
113 WeeklyScheduleController *controller,
114 WeeklyScheduleConfigHandler *configHandler = nullptr,
115 WeeklyScheduleProgramSource *programSource = nullptr);
116
117 protected:
118 ApplyConfigResult applyChannelConfig(TSD_ChannelConfig *result,
119 bool local) override;
120 bool shouldProcessChannelFunctionFromConfig() const override;
121 virtual void fillDefaultWeeklySchedule(
123 virtual bool isWeeklyScheduleProgramModeSupported(uint8_t mode) const;
124 bool ensureNativeWeeklyScheduleController();
125 void updateWeeklyScheduleCapabilities();
126 void scheduleStateSave(uint32_t delayMsMax = 5000,
127 uint32_t delayMsMin = 2000);
128 void applyButtonMode(uint8_t mode);
129 void applyManualButtonMode(uint8_t mode);
130 void addActionToButtonAndDisableIt(int event, int action);
131 void parseActiveActionsFromServer();
132 bool hasDirectionalPair() const;
133 bool requiresClickMode() const;
134 static void synchronizeDirectionalButtonModes();
135
136 Supla::Control::Button *attachedButton = nullptr;
137 Supla::ActionHandlerClient *localHandlerForEnabledAt = nullptr;
138 Supla::ActionHandlerClient *localHandlerForDisabledAt = nullptr;
139 Supla::ActionHandler *localHandlerClient = nullptr;
140 uint32_t activeActionsFromServer = 0;
141 // Comparison cache only: PublishAllDisableAll overwrites the effective mask
142 // above. Keep state storage and its existing format unchanged.
143 uint32_t lastReceivedActiveActions = 0;
144 uint32_t disablesLocalOperation = 0;
145 uint32_t disabledCapabilities = 0;
146
147 Supla::AtChannel channel;
148
149 uint16_t localHandlerAction = 0;
150 ActionHandlingType actionHandlingType = ActionHandlingType_RelayOnSuplaServer;
151 // Runtime-only flags; state storage uses a separate representation.
154 : storageEnabled(false),
155 alwaysUseOnClick1(false),
156 enabled(true),
157 localHandlerSwitchConfigured(false),
158 channelConfigReceived(false),
159 weeklyScheduleAvailable(false),
160 localUnlockAllowed(false),
161 keepConfigButtonTriggerAlwaysAvailable(false) {}
162
163 uint8_t storageEnabled : 1;
164 uint8_t alwaysUseOnClick1 : 1;
165 uint8_t enabled : 1;
166 uint8_t localHandlerSwitchConfigured : 1;
167 uint8_t channelConfigReceived : 1;
168 // Native weekly schedule remains opt-in.
169 uint8_t weeklyScheduleAvailable : 1;
170 uint8_t localUnlockAllowed : 1;
171 uint8_t keepConfigButtonTriggerAlwaysAvailable : 1;
172 };
173
174 static_assert(sizeof(RuntimeFlags) == sizeof(uint8_t),
175 "ActionTrigger runtime flags must fit in uint8_t");
176
177 RuntimeFlags runtimeFlags;
178 WeeklyScheduleComponents weeklyScheduleComponents;
179};
180
181} // namespace Control
182} // namespace Supla
183
184#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:37
Definition action_trigger.h:32
void onSaveState() override
Method called periodically during SuplaDevice iteration.
Definition action_trigger.cpp:894
int32_t handleNewValueFromServer(TSD_SuplaChannelNewValue *newValue) override
Handles "new value" request from server.
Definition action_trigger.cpp:1183
void iterateAlways() override
Method called on each SuplaDevice iteration.
Definition action_trigger.cpp:1176
void purgeConfig() override
Removes all configration data related to the element from Storage::Config.
Definition action_trigger.cpp:1237
void onRegistered(Supla::Protocol::SuplaSrpc *suplaSrpc=nullptr) override
Method called each time when device successfully registers to Supla server.
Definition action_trigger.cpp:372
void onInit() override
Third method called on element in SuplaDevice.begin()
Definition action_trigger.cpp:639
void onLoadState() override
Second method called on element in SuplaDevice.begin().
Definition action_trigger.cpp:983
void onLoadConfig(SuplaDeviceClass *) override
First method called on element in SuplaDevice.begin().
Definition action_trigger.cpp:923
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
Base class for all elements of SuplaDevice.
Definition element.h:30
Definition supla_srpc.h:47
Definition action_trigger.h:152
Definition proto.h:3276
Definition proto.h:3176
Definition proto.h:1243