EmlaLockSafe
LockState.h
Go to the documentation of this file.
1
5#pragma once
6
7#include "Tools.h"
8#include "UsedInterrupts.h"
9
10#include <SPIFFS.h>
11#include <mutex>
12#include <time.h>
13
23class LockState {
24#pragma region Enums
25public:
29 using enumBaseType = uint8_t;
30
31public:
35 enum class Mode : enumBaseType { emlalock = 0, manual = 1 };
36
37public:
41 enum class DisplayTimePassed : enumBaseType { no = 0, yes = 1 };
42
43public:
47 enum class DisplayTimeLeft : enumBaseType { no = 0, yes = 1, temperature = 2, timeWithPenalty = 3, timeWithRandomPenalty = 4 };
48#pragma endregion
49
50protected:
54 std::mutex mtx;
55
56#pragma region Members stored on Flash
57protected:
62
63protected:
68
69protected:
74
75protected:
79 time_t startDate;
80
81protected:
85 time_t endDate;
86
87protected:
92
93protected:
98
99#pragma endregion
100
101protected:
107
108protected:
114
115protected:
120
121#pragma region singleton
122protected:
127 : mode(Mode::emlalock)
130 , startDate(0)
131 , endDate(0)
133 , cachedEndDate(0)
134 , cleaningEndDate(0)
135 , lastUpdateTime(0) {
136 loadData();
137 }
138
139protected:
144 static LockState lockState;
145 return lockState;
146 }
147#pragma endregion
148
149#pragma region Flash Access Functions
150protected:
154 void loadData() {
155 Serial.println("Loading lockState");
156 constexpr auto numberOfBytes = sizeof(Mode) + sizeof(DisplayTimePassed) + sizeof(DisplayTimeLeft) + 2 * sizeof(time_t) +
157 sizeof(uint32_t);
158 char buf[numberOfBytes];
159 bool readOk;
160
161 // load the data from the file system if available
162 UsedInterrupts::executeWithoutInterrupts([this, &readOk, &buf]() {
163 File file = SPIFFS.open("/lockSt.bin", "r");
164 if (!file) {
165 Serial.println("Loading lockState failed");
166 return;
167 }
168
169 // read everything to a buffer
170 readOk = file.readBytes(buf, numberOfBytes) == numberOfBytes;
171 if (readOk) {
172 temperatureString = file.readString();
173 }
174
175 file.close();
176 });
177
178 if (readOk) {
179 // copy the data from the buffer to the actual variables
180 char* src = buf;
181 memcpy(&mode, src, sizeof(Mode));
182 src += sizeof(Mode);
183
184 memcpy(&displayTimePassed, src, sizeof(DisplayTimePassed));
185 src += sizeof(DisplayTimePassed);
186
187 memcpy(&displayTimeLeft, src, sizeof(DisplayTimeLeft));
188 src += sizeof(DisplayTimeLeft);
189
190 memcpy(&startDate, src, sizeof(time_t));
191 src += sizeof(time_t);
192
193 memcpy(&endDate, src, sizeof(time_t));
194 src += sizeof(time_t);
195
196 memcpy(&numberOfFailedSessions, src, sizeof(uint32_t));
197 src += sizeof(uint32_t);
198 }
199 }
200
201protected:
205 void saveData() {
206 Serial.println("Saving lockState");
208 File file = SPIFFS.open("/lockSt.bin", "w");
209 if (!file) {
210 return;
211 }
212
213 file.write((uint8_t*)&mode, sizeof(DisplayTimePassed));
214 file.write((uint8_t*)&displayTimePassed, sizeof(DisplayTimePassed));
215 file.write((uint8_t*)&displayTimeLeft, sizeof(DisplayTimeLeft));
216 file.write((uint8_t*)&startDate, sizeof(time_t));
217 file.write((uint8_t*)&endDate, sizeof(time_t));
218 file.write((uint8_t*)&numberOfFailedSessions, sizeof(uint32_t));
219 file.write((uint8_t*)temperatureString.c_str(), temperatureString.length());
220 file.close();
221 });
222
223 Serial.println("Saved lockState");
224 }
225#pragma endregion
226
227#pragma region getter / setter
228#pragma region mode
229public:
233 static const Mode& getMode() {
234 std::unique_lock<std::mutex> lock(getSingleton().mtx);
235 return getSingleton().mode;
236 }
237
238public:
242 static void setMode(const Mode& mode) {
243 std::unique_lock<std::mutex> lock(getSingleton().mtx);
244 if (getSingleton().mode != mode) {
247 }
248 }
249#pragma endregion
250
251#pragma region displayTimePassed
252public:
257 std::unique_lock<std::mutex> lock(getSingleton().mtx);
259 }
260
261public:
266 std::unique_lock<std::mutex> lock(getSingleton().mtx);
270 }
271 }
272#pragma endregion
273
274#pragma region displayTimeLeft
275public:
280 std::unique_lock<std::mutex> lock(getSingleton().mtx);
282 }
283
284public:
289 std::unique_lock<std::mutex> lock(getSingleton().mtx);
293 }
294 }
295#pragma endregion
296
297#pragma region startDate
298public:
302 static const time_t& getStartDate() {
303 std::unique_lock<std::mutex> lock(getSingleton().mtx);
304 return getSingleton().startDate;
305 }
306
307public:
311 static void setStartDate(const time_t& startDate) {
312 std::unique_lock<std::mutex> lock(getSingleton().mtx);
316 }
317 }
318#pragma endregion
319
320#pragma region endDate
321public:
325 static const time_t& getEndDate() {
326 std::unique_lock<std::mutex> lock(getSingleton().mtx);
327 return getSingleton().endDate;
328 }
329
330public:
334 static void setEndDate(const time_t& endDate) {
335 std::unique_lock<std::mutex> lock(getSingleton().mtx);
336 if (getSingleton().endDate != endDate) {
339 }
340 }
341#pragma endregion
342
343#pragma region numberOfFailedSessions
344public:
348 static const uint32_t& getNumberOfFailedSessions() {
349 std::unique_lock<std::mutex> lock(getSingleton().mtx);
351 }
352
353public:
358 std::unique_lock<std::mutex> lock(getSingleton().mtx);
362 }
363 }
364#pragma endregion
365
366#pragma region temperatureString
367public:
371 static const String& getTemperatureString() {
372 std::unique_lock<std::mutex> lock(getSingleton().mtx);
374 }
375
376public:
380 static void setTemperatureString(const String& temperatureString) {
381 std::unique_lock<std::mutex> lock(getSingleton().mtx);
385 }
386 }
387#pragma endregion
388
389#pragma region cachedEndDate
390public:
394 static const time_t& getCachedEndDate() {
395 std::unique_lock<std::mutex> lock(getSingleton().mtx);
397 }
398
399public:
403 static void setCachedEndDate(const time_t& cachedEndDate) {
404 std::unique_lock<std::mutex> lock(getSingleton().mtx);
407 }
408 }
409#pragma endregion
410
411#pragma region cleaningEndDate
412public:
416 static const time_t& getCleaningEndDate() {
417 std::unique_lock<std::mutex> lock(getSingleton().mtx);
419 }
420
421public:
425 static void setCleaningEndDate(const time_t& cleaningEndDate) {
426 std::unique_lock<std::mutex> lock(getSingleton().mtx);
429 }
430 }
431#pragma endregion
432
433#pragma region lastUpdateTime
434public:
438 static const time_t& getLastUpdateTime() {
439 std::unique_lock<std::mutex> lock(getSingleton().mtx);
441 }
442
443public:
447 static void setLastUpdateTime(const time_t& lastUpdateTime) {
448 std::unique_lock<std::mutex> lock(getSingleton().mtx);
451 }
452 }
453#pragma endregion
454
455#pragma endregion
456};
Singleton class managing the current lock state. The class also automatically saves the lock state to...
Definition: LockState.h:23
static void setStartDate(const time_t &startDate)
Set the Start Date.
Definition: LockState.h:311
static const time_t & getEndDate()
Get the End Date.
Definition: LockState.h:325
uint8_t enumBaseType
Enum basetype used for saving to the flash.
Definition: LockState.h:29
DisplayTimeLeft
States for the selection how the time left should be displayed.
Definition: LockState.h:47
Mode
Current mode of the safe.
Definition: LockState.h:35
void loadData()
Loads the current state of the object to the flash.
Definition: LockState.h:154
static const Mode & getMode()
Get the mode.
Definition: LockState.h:233
static const uint32_t & getNumberOfFailedSessions()
Get the number of failed sessions.
Definition: LockState.h:348
static void setLastUpdateTime(const time_t &lastUpdateTime)
Set the time of the last update over the emlalock api.
Definition: LockState.h:447
time_t cachedEndDate
chache for the end date of the session which is currently configured. This value won't be saved to th...
Definition: LockState.h:106
static const DisplayTimePassed & getDisplayTimePassed()
Get how the time passed should be displayed.
Definition: LockState.h:256
static void setCachedEndDate(const time_t &cachedEndDate)
Set the Cached End Date.
Definition: LockState.h:403
void saveData()
Saves the current state of the object to the flash.
Definition: LockState.h:205
static void setEndDate(const time_t &endDate)
Set the End Date.
Definition: LockState.h:334
static void setTemperatureString(const String &temperatureString)
Set the temperature string.
Definition: LockState.h:380
static const time_t & getCachedEndDate()
Get the Cached End Date.
Definition: LockState.h:394
String temperatureString
temperature string
Definition: LockState.h:91
static const String & getTemperatureString()
Get the temperature string.
Definition: LockState.h:371
time_t startDate
start date of the current session
Definition: LockState.h:79
uint32_t numberOfFailedSessions
The number of failed session.
Definition: LockState.h:97
std::mutex mtx
mutex used to synchronize the data access
Definition: LockState.h:54
static const time_t & getLastUpdateTime()
Get the time of the last update over the emlalock api.
Definition: LockState.h:438
static void setNumberOfFailedSessions(const uint32_t &numberOfFailedSessions)
Set the number of failed sessions.
Definition: LockState.h:357
static void setMode(const Mode &mode)
Set the mode.
Definition: LockState.h:242
static const time_t & getCleaningEndDate()
Get the End Date of the current cleaning opening (or 0)
Definition: LockState.h:416
DisplayTimePassed displayTimePassed
how the passed time should be displayed
Definition: LockState.h:67
static LockState & getSingleton()
Get the singleton instance.
Definition: LockState.h:143
static void setDisplayTimePassed(const DisplayTimePassed &displayTimePassed)
Set how the time passed should be displayed.
Definition: LockState.h:265
time_t endDate
end date of the current session
Definition: LockState.h:85
DisplayTimeLeft displayTimeLeft
how the passed time should be displayed
Definition: LockState.h:73
time_t cleaningEndDate
end date of the current cleaning opening or 0 if not opened for cleaning. This value won't be saved t...
Definition: LockState.h:113
static void setDisplayTimeLeft(const DisplayTimeLeft &displayTimeLeft)
Set how the time left should be displayed.
Definition: LockState.h:288
static void setCleaningEndDate(const time_t &cleaningEndDate)
Set the End Date of the current cleaning opening.
Definition: LockState.h:425
time_t lastUpdateTime
time of the last update
Definition: LockState.h:119
static const time_t & getStartDate()
Get the Start Date.
Definition: LockState.h:302
Mode mode
The mode.
Definition: LockState.h:61
LockState()
Constructs the lock state by trying to reading the state from flash.
Definition: LockState.h:126
static const DisplayTimeLeft & getDisplayTimeLeft()
Get how the time left should be displayed.
Definition: LockState.h:279
DisplayTimePassed
States for the selection how the passed time should be displayed.
Definition: LockState.h:41
static void executeWithoutInterrupts(std::function< void(void)> f)
Execute the passed function without interrupts.
Definition: UsedInterrupts.h:58
Definition: EmlaLockApi.h:21