supla-device
Loading...
Searching...
No Matches
weekly_schedule_storage.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_STORAGE_H_
5#define SRC_SUPLA_CONTROL_WEEKLY_SCHEDULE_STORAGE_H_
6
7#include <supla-common/proto.h>
8
9#include "weekly_schedule_buffer.h"
10#include "weekly_schedule_cache_runtime.h"
11#include "weekly_schedule_common.h"
12#include "weekly_schedule_component.h"
13
14namespace Supla {
15class Element;
16
17namespace Control {
18
21 public:
22 void onLoadConfig() override;
23 Supla::ApplyConfigResult applyChannelConfig(TSD_ChannelConfig *config,
24 bool local) override;
25 void fillChannelConfig(void *config,
26 int *size,
27 uint8_t configType) override;
28 void purgeConfig() override;
29 bool supportsConfigType(uint8_t configType) const override;
30
31 protected:
32 virtual Supla::Element *getScheduleOwner() const = 0;
33 virtual const char *getDeviceLabel() const = 0;
34 virtual const char *getScheduleStorageTag(bool alt) const = 0;
35 virtual bool validateSchedule(
36 const TChannelConfig_WeeklySchedule *schedule, bool alt) const = 0;
37 virtual void fillDefaultSchedule(TChannelConfig_WeeklySchedule *schedule,
38 bool alt) = 0;
39 virtual bool supportsAltSchedule() const {
40 return false;
41 }
42 virtual bool hasAltScheduleStorage() const {
43 return false;
44 }
45 virtual bool hasPersistentDefaultSchedule(bool alt) const {
46 (void)(alt);
47 return false;
48 }
49 virtual void onNativeScheduleLoaded() {
50 }
51 virtual void onNativeScheduleLoadFailed(bool alt) {
52 (void)(alt);
53 }
54 virtual void onNativeScheduleApplied(bool alt, bool local, bool changed) {
55 (void)(alt);
56 (void)(local);
57 (void)(changed);
58 }
59 virtual void onNativeScheduleSaved(bool alt, bool notify) {
60 (void)(alt);
61 (void)(notify);
62 }
63 virtual void onNativeSchedulePurged() {
64 }
65
66 bool isConfigured() const;
67 bool isConfigured(bool alt) const;
68 bool isPersisted(bool alt) const;
69
70 TChannelConfig_WeeklySchedule *getSchedule(bool alt,
71 bool loadIfMissing = true);
72 const TChannelConfig_WeeklySchedule *getSchedule(
73 bool alt, bool loadIfMissing = true) const;
74 TChannelConfig_WeeklySchedule *ensureSchedule(bool alt);
75 bool ensureScheduleForUse(bool alt);
76 bool updateSchedule(bool alt,
77 const TChannelConfig_WeeklySchedule &schedule);
78 bool saveSchedule(bool alt, bool notify = false);
79 void clearSchedule(bool alt);
80 void clearSchedules();
81 void unloadSchedule(bool alt);
82
83 int calculateIndex(enum DayOfWeek dayOfWeek, int hour, int quarter) const;
84 int getProgramId(const TChannelConfig_WeeklySchedule *schedule,
85 int index) const;
86 bool setWeeklySchedule(TChannelConfig_WeeklySchedule *schedule,
87 int index,
88 int programId) const;
89 TWeeklyScheduleProgram getProgramById(
90 const TChannelConfig_WeeklySchedule *schedule, int programId) const;
91 TWeeklyScheduleProgram getProgramAt(
92 const TChannelConfig_WeeklySchedule *schedule, int quarterIndex) const;
93 int getCurrentQuarter() const;
94 bool resolveCurrentProgram(bool alt,
96 int *programId);
97 bool resolveCurrentProgram(bool alt,
99 int *programId) const;
100 bool resolveCurrentProgram(bool alt,
101 const WeeklyScheduleTimeSnapshot &time,
102 TWeeklyScheduleProgram *program,
103 int *programId);
104 bool resolveProgram(const WeeklyScheduleTimeSnapshot &time,
105 bool alt,
106 TWeeklyScheduleProgram *program,
107 int *programId) override;
108 bool resolveProgramTiming(const WeeklyScheduleTimeSnapshot &time,
109 bool alt, int programId, int32_t *occurrence,
110 uint32_t *elapsedSeconds) override;
111
112 void touchCache(bool active, uint32_t nowMs);
113 bool processCache(bool active, uint32_t nowMs);
114 void resetCache();
115
116 private:
117 bool loadSchedule(bool alt);
118 void eraseSchedule(bool alt);
119 bool configTypeToAlt(uint8_t configType, bool *alt) const;
120 const char *getScheduleLabel(bool alt) const;
121
122 WeeklyScheduleBuffer buffer_;
123 WeeklyScheduleCacheRuntime cacheRuntime_;
124 bool configured_[2] = {false, false};
125 bool persisted_[2] = {false, false};
126 bool loadAttempted_[2] = {false, false};
127};
128
132 public:
133 bool canActivate() const override;
134 bool isActive() const override;
135 bool switchToWeeklySchedule() override;
136 void switchToManualMode() override;
137 void restoreWeeklyScheduleMode(bool enabled) override;
138 bool processWeeklySchedule() override;
139 bool isWeeklyScheduleConfigured() const;
140
141 protected:
142 virtual void syncWeeklyScheduleMode(uint8_t mode) = 0;
143 virtual void scheduleWeeklyScheduleStateSave() = 0;
144 virtual bool applyWeeklyScheduleMode(uint8_t mode, bool programChanged);
145
146 void onNativeScheduleLoaded() override;
147 void onNativeScheduleLoadFailed(bool alt) override;
148 void onNativeScheduleApplied(bool alt,
149 bool local,
150 bool changed) override;
151 void onNativeScheduleSaved(bool alt, bool notify) override;
152 void onNativeSchedulePurged() override;
153 bool applyResolvedWeeklyScheduleProgram(
154 const TWeeklyScheduleProgram &program,
155 int programId,
156 bool programChanged) override;
157
158 private:
159 bool enabled_ = false;
160};
161
162} // namespace Control
163} // namespace Supla
164
165#endif // SRC_SUPLA_CONTROL_WEEKLY_SCHEDULE_STORAGE_H_
Definition weekly_schedule_storage.h:20
Definition weekly_schedule_storage.h:131
Definition weekly_schedule_buffer.h:14
Definition weekly_schedule_cache_runtime.h:12
Definition weekly_schedule_component.h:114
Definition weekly_schedule_component.h:55
Definition weekly_schedule_component.h:34
Base class for all elements of SuplaDevice.
Definition element.h:30
Definition weekly_schedule_component.h:24
Definition proto.h:3276
Definition proto.h:3176
Definition proto.h:3253