supla-device
Loading...
Searching...
No Matches
weekly_schedule_component.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_WEEKLY_SCHEDULE_COMPONENT_H_
5#define SRC_SUPLA_CONTROL_WEEKLY_SCHEDULE_COMPONENT_H_
6
7#include <stdint.h>
8#include <supla-common/proto.h>
9#include <supla/apply_config_result.h>
10
11#include "weekly_schedule_common.h"
12
13namespace Supla {
14namespace Control {
15
16class WeeklyScheduleConfigHandler;
17
18enum class WeeklyScheduleClockState : uint8_t {
19 Ready,
20 Waiting,
21 TimedOut,
22};
23
25 WeeklyScheduleClockState state = WeeklyScheduleClockState::TimedOut;
26 enum DayOfWeek dayOfWeek = DayOfWeek_Sunday;
27 uint8_t hour = 0;
28 uint8_t quarter = 0;
29 uint16_t secondOfQuarter = 0;
30 // Local civil day number, used to distinguish recurring weekly occurrences.
31 int32_t dayNumber = 0;
32};
33
35 public:
36 virtual ~WeeklyScheduleProgramSource() = default;
37 virtual bool resolveProgram(const WeeklyScheduleTimeSnapshot &time,
38 bool alt,
40 int *programId) = 0;
41 virtual bool resolveProgramTiming(const WeeklyScheduleTimeSnapshot &time,
42 bool alt,
43 int programId,
44 int32_t *occurrence,
45 uint32_t *elapsedSeconds) {
46 (void)(time);
47 (void)(alt);
48 (void)(programId);
49 (void)(occurrence);
50 (void)(elapsedSeconds);
51 return false;
52 }
53};
54
56 public:
57 virtual ~WeeklyScheduleController() = default;
58 virtual bool canActivate() const = 0;
59 virtual bool isActive() const = 0;
60 virtual bool switchToWeeklySchedule() = 0;
61 virtual void switchToManualMode() = 0;
62 virtual void restoreWeeklyScheduleMode(bool enabled) = 0;
63 virtual bool processWeeklySchedule() = 0;
64 virtual bool isManualActionAllowed(bool turnOn) const {
65 (void)(turnOn);
66 return true;
67 }
68 virtual bool isExternallyManaged() const {
69 return false;
70 }
71 virtual void onManualAction() {}
72
73 protected:
74 WeeklyScheduleClockState getClockState() const;
75 WeeklyScheduleClockState getClockState(bool startupDelay) const;
76 WeeklyScheduleTimeSnapshot getWeeklyScheduleTimeSnapshot(
77 bool startupDelay) const;
78 bool processCurrentProgram(bool startupDelay);
79 bool updateCurrentProgramId(int programId);
80 void resetCurrentProgramId();
81 // The source is not owned and has to remain valid while registered.
82 void setWeeklyScheduleProgramSource(WeeklyScheduleProgramSource *source);
83
84 virtual void onWeeklyScheduleClockState(WeeklyScheduleClockState state) {
85 (void)(state);
86 }
87 virtual bool shouldUseAltWeeklySchedule() const {
88 return false;
89 }
90 virtual bool requiresReadyClock() const { return false; }
91 bool resolveProgramTiming(const WeeklyScheduleTimeSnapshot &time,
92 int programId,
93 int32_t *occurrence,
94 uint32_t *elapsedSeconds);
95 virtual bool applyProgramAt(const WeeklyScheduleTimeSnapshot &time,
96 const TWeeklyScheduleProgram &program,
97 int programId,
98 bool programChanged);
99 bool resolveWeeklyScheduleProgram(
100 const WeeklyScheduleTimeSnapshot &time,
101 TWeeklyScheduleProgram *program,
102 int *programId);
103 virtual bool applyResolvedWeeklyScheduleProgram(
104 const TWeeklyScheduleProgram &program,
105 int programId,
106 bool programChanged);
107
108 private:
109 friend class WeeklyScheduleComponents;
110 WeeklyScheduleProgramSource *programSource_ = nullptr;
111 int currentProgramId_ = -1;
112};
113
115 public:
116 virtual ~WeeklyScheduleConfigHandler() = default;
117 virtual void onLoadConfig() = 0;
118 virtual bool supportsConfigType(uint8_t configType) const = 0;
119 virtual Supla::ApplyConfigResult applyChannelConfig(
120 TSD_ChannelConfig *config, bool local) = 0;
121 virtual void fillChannelConfig(void *config,
122 int *size,
123 uint8_t configType) = 0;
124 virtual void purgeConfig() = 0;
125};
126
128 public:
129 WeeklyScheduleComponents() = default;
133 delete;
134
135 // Ownership of the controller is transferred only when true is returned.
136 // The config handler and program source are not owned.
137 bool set(WeeklyScheduleController *controller,
138 WeeklyScheduleConfigHandler *configHandler = nullptr,
139 WeeklyScheduleProgramSource *programSource = nullptr);
140 void loadConfig();
141 bool isAssigned() const;
142 bool isStarted() const;
143 WeeklyScheduleController *getController() const;
144 WeeklyScheduleConfigHandler *getConfigHandler() const;
145 WeeklyScheduleProgramSource *getProgramSource() const;
146
147 private:
148 struct State;
149 State *state_ = nullptr;
150};
151
153 public:
154 bool canActivate() const override;
155 bool isActive() const override;
156 bool switchToWeeklySchedule() override;
157 void switchToManualMode() override;
158 void restoreWeeklyScheduleMode(bool enabled) override;
159 bool processWeeklySchedule() override;
160 bool isExternallyManaged() const override {
161 return true;
162 }
163
164 private:
165 bool active_ = false;
166};
167
168} // namespace Control
169} // namespace Supla
170
171#endif // SRC_SUPLA_CONTROL_WEEKLY_SCHEDULE_COMPONENT_H_
Definition weekly_schedule_component.h:152
Definition weekly_schedule_component.h:127
Definition weekly_schedule_component.h:114
Definition weekly_schedule_component.h:55
Definition weekly_schedule_component.h:34
Definition weekly_schedule_component.cpp:12
Definition weekly_schedule_component.h:24
Definition proto.h:3176
Definition proto.h:3253