EmlaLockSafe
Configuration.h
Go to the documentation of this file.
1
5#pragma once
6
7#include "..\UsedInterrupts.h"
8
9#include <Arduino.h>
10#include <SPIFFS.h>
11#include <ViewBase.h>
12#include <algorithm>
13#include <stdlib.h>
14#include <time.h>
15
16namespace configuration {
21public:
28 friend class Configuration;
29
30 public:
34 uint32_t startTime;
35
36 public:
40 uint32_t endTime;
41
42 public:
47
48 public:
53
54 public:
59
60 public:
65
66 public:
71 : startTime(0)
72 , endTime(86400)
73 , restrictUnlockTimes(false)
77
78 public:
82 bool checkTime() const {
83 // compute the number of seconds of this day
84 time_t currentTime = time(NULL);
85 tm tmBuf;
86 localtime_r(&currentTime, &tmBuf);
87 uint32_t currentSeconds = tmBuf.tm_hour * 3600 + tmBuf.tm_min * 60 + tmBuf.tm_sec;
88
89 if (startTime == endTime) {
90 return true;
91 }
92 else if (endTime > startTime) {
93 return (currentSeconds >= startTime) && (currentSeconds <= endTime);
94 }
95 else {
96 // Midnight overflow
97 return (currentSeconds >= endTime) && (currentSeconds <= startTime);
98 }
99 }
100
101 protected:
107 void readFromFile(File& file) {
108 String start = file.readStringUntil('\n');
109 String end = file.readStringUntil('\n');
110 String restrictions = file.readStringUntil('\n');
111 start.trim();
112 end.trim();
113 restrictions.end();
114 if (start.isEmpty() || end.isEmpty() || restrictions.isEmpty()) {
115 startTime = 0;
116 endTime = 86400;
117 restrictUnlockTimes = false;
121 }
122 else {
123 startTime = strtoul(start.c_str(), NULL, 0);
124 endTime = strtoul(end.c_str(), NULL, 0);
125
126 unsigned long l = strtoul(restrictions.c_str(), NULL, 0);
127 restrictUnlockTimes = l & (1 << 0);
128 restrictHygieneOpeningTimes = l & (1 << 1);
129 restrictEmergencyKeyTimes = l & (1 << 2);
130 restrictConfigurationServer = l & (1 << 3);
131 }
132 }
133
134 protected:
140 void writeToFile(File& file) {
141 file.println(startTime);
142 file.println(endTime);
145 }
146 };
147
148#pragma region Singelton
149protected:
153 inline static Configuration** getInstance() {
154 static Configuration* instance = nullptr;
155 return &instance;
156 }
157
158public:
162 inline static Configuration& getSingleton() {
163 return **getInstance();
164 }
165
166public:
170 inline static Configuration& begin() {
171 if (!*getInstance()) {
172 *getInstance() = new Configuration();
173 }
174 return **getInstance();
175 }
176#pragma endregion
177
178#pragma region WiFi Settings
179protected:
183 String ssid;
184
185protected:
189 String pwd;
190#pragma endregion
191
192#pragma region Emlalock settings
193protected:
197 String userId;
198
199protected:
203 String apiKey;
204
205protected:
210
211protected:
218#pragma endregion
219
220#pragma region Miscellaneous Settings
221protected:
226 String timezone;
227
228protected:
233
234protected:
238 unsigned long backlightTimeOut;
239
240protected:
246
247protected:
252#pragma endregion
253
254public:
257 }
258
259#pragma region Getter
260#pragma region WiFi Settings
261public:
265 const String& getSsid() const {
266 return ssid;
267 }
268
269public:
273 const String& getPwd() const {
274 return pwd;
275 }
276#pragma endregion
277
278#pragma region Emlalock settings
279public:
283 const String& getUserId() const {
284 return userId;
285 }
286
287public:
291 const String& getApiKey() const {
292 return apiKey;
293 }
294
295public:
299 const String& getEmergencyKey() const {
300 return emergencyKey;
301 }
302
303public:
309 const bool& getDisableFailedSession() const {
311 }
312
313#pragma endregion
314
315#pragma region Miscellaneous Settings
316public:
321 const String& getTimezone() const {
322 return timezone;
323 }
324
325public:
329 const String& getTimezoneName() const {
330 return timezoneName;
331 }
332
333public:
337 const unsigned long& getBacklightTimeOut() const {
338 return backlightTimeOut;
339 }
340
341public:
348 }
349
350public:
355 return timeRestrictions;
356 }
357#pragma endregion
358#pragma endregion
359
360#pragma region Setter
361public:
368 void setWifiSettings(const String& ssid, const String& pwd) {
369 this->ssid = ssid;
370 this->pwd = pwd;
372 }
373
390 const String& apiKey,
391 const bool& disableFailedSession,
392 const String& timezoneName,
393 const String& timezone,
394 const long& backlightTimeOut,
397 this->userId = userId;
398 this->apiKey = apiKey;
399 this->disableFailedSession = disableFailedSession;
400
401 this->timezoneName = timezoneName;
402 this->timezone = timezone;
403 setenv("TZ", timezone.c_str(), 1);
404 tzset();
405
406 this->backlightTimeOut = backlightTimeOut;
407 lcd::ViewBase::setBacklightTimeout(backlightTimeOut * 1000);
408
409 this->autoLockHygieneOpeningTimeout = autoLockHygieneOpeningTimeout;
410 this->timeRestrictions = timeRestrictions;
411 this->timeRestrictions.startTime = std::min(this->timeRestrictions.startTime, (uint32_t)86400);
412 this->timeRestrictions.endTime = std::min(this->timeRestrictions.endTime, (uint32_t)86400);
413
415 }
416
420 const String& generateNewEmergencyKey() {
421 std::srand((unsigned int)micros());
422 for (int i = 0; i < 6; i++) {
424 }
425
427
428 return emergencyKey;
429 }
430#pragma endregion
431
432public:
437 ssid = "";
438 pwd = "";
439 userId = "";
440 apiKey = "";
441 emergencyKey = "AAAAAA";
442 disableFailedSession = false;
443 timezone = "CET-1CEST,M3.5.0,M10.5.0/3";
444 timezoneName = "Europe/Berlin";
445 backlightTimeOut = 15;
449 }
450
451protected:
456 // load everything from the SPIFFS
458 File file = SPIFFS.open("/configuration.txt", "r");
459 if (!file) {
460 Serial.println("Loading configuration.txt failed");
461 return;
462 }
463
464 ssid = file.readStringUntil('\n');
465 ssid.trim();
466 pwd = file.readStringUntil('\n');
467 pwd.trim();
468 userId = file.readStringUntil('\n');
469 userId.trim();
470 apiKey = file.readStringUntil('\n');
471 apiKey.trim();
472 emergencyKey = file.readStringUntil('\n');
473 emergencyKey.trim();
474 if (emergencyKey.length() != 6) {
475 emergencyKey = "AAAAAA";
476 }
477 disableFailedSession = strtol(file.readStringUntil('\n').c_str(), NULL, 0) == 1;
478 timezone = file.readStringUntil('\n');
479 timezone.trim();
480 timezoneName = file.readStringUntil('\n');
481 timezoneName.trim();
482
483 backlightTimeOut = strtoul(file.readStringUntil('\n').c_str(), NULL, 0);
484 autoLockHygieneOpeningTimeout = strtol(file.readStringUntil('\n').c_str(), NULL, 0) == 1;
485
487
488 file.close();
489 file = SPIFFS.open("/configuration.txt", "r");
490 Serial.printf("configuration.txt: \n\"%s\"\n", file.readString().c_str());
491 });
492 }
493
494protected:
499 // load everything from the SPIFFS
501 File file = SPIFFS.open("/configuration.txt", "w");
502 if (!file) {
503 Serial.println("Loading configuration.txt failed");
504 return;
505 }
506
507 file.println(ssid);
508 file.println(pwd);
509 file.println(userId);
510 file.println(apiKey);
511 file.println(emergencyKey);
512 file.println((disableFailedSession) ? "1" : "0");
513 file.println(timezone);
514 file.println(timezoneName);
515 file.println(backlightTimeOut);
516 file.println((autoLockHygieneOpeningTimeout) ? "1" : "0");
517
519 });
520 }
521
522protected:
526 static char getRandomChar() {
527 int x = 36;
528 while (x > 35) {
529 // according to example of
530 // https://en.cppreference.com/w/cpp/numeric/random/rand we need this to
531 // have no bias....
532 x = std::rand() / ((RAND_MAX + 1u) / 35);
533 }
534
535 if (x < 26) {
536 return (char)x + 'A';
537 }
538 return (char)x - 26 + '0';
539 }
540};
541} // namespace configuration
static void executeWithoutInterrupts(std::function< void(void)> f)
Execute the passed function without interrupts.
Definition: UsedInterrupts.h:58
Container to restrict the time when the safe can be opened.
Definition: Configuration.h:27
bool restrictUnlockTimes
If set, the safe can only be opened during the specified time using the normal unlock function.
Definition: Configuration.h:46
bool restrictEmergencyKeyTimes
If set, the time when the emergency key is accepted is restricted.
Definition: Configuration.h:58
bool restrictHygieneOpeningTimes
If set, the time for hygiene openings is restricted.
Definition: Configuration.h:52
void writeToFile(File &file)
Writes all configuration items of this class to the file at the current stream position.
Definition: Configuration.h:140
uint32_t startTime
The number of seconds after midnight after which the safe can be opened.
Definition: Configuration.h:34
bool restrictConfigurationServer
If set, the time when the configuration server can be started is restricted.
Definition: Configuration.h:64
TimeRestrictions()
Construct a new Time Restrictions object.
Definition: Configuration.h:70
uint32_t endTime
The number of seconds after midnight until which the safe can be opened.
Definition: Configuration.h:40
void readFromFile(File &file)
Reads the content of this class from the configuration file stream.
Definition: Configuration.h:107
bool checkTime() const
check if the current time is valid for opening the safe
Definition: Configuration.h:82
Static class accessing the configuration.
Definition: Configuration.h:20
String timezoneName
The name to the timezone-string (e.g. "Europe/Berlin")
Definition: Configuration.h:232
static char getRandomChar()
Get a random character (A-Z, 0-9)
Definition: Configuration.h:526
const bool & getAutoLockHygieneOpeningTimeout() const
return if the safe should automatically lock after the time for hygiene opening is over....
Definition: Configuration.h:346
const String & getTimezone() const
get the timezone-string (e.g. "CET-1CEST,M3.5.0,M10.5.0/3") see also https://github....
Definition: Configuration.h:321
void setConfigurationSettings(const String &userId, const String &apiKey, const bool &disableFailedSession, const String &timezoneName, const String &timezone, const long &backlightTimeOut, const bool &autoLockHygieneOpeningTimeout, const TimeRestrictions &timeRestrictions)
Set the Configuration Settings.
Definition: Configuration.h:389
const bool & getDisableFailedSession() const
get disable support of failed session. Note: If selected, the safe will be locked until the last know...
Definition: Configuration.h:309
const unsigned long & getBacklightTimeOut() const
get the timeout of display backlight in seconds
Definition: Configuration.h:337
bool disableFailedSession
Disable support of failed session. Note: If selected, the safe will be locked until the last known en...
Definition: Configuration.h:217
const String & getUserId() const
get the User ID extracted from the webpage settings > API
Definition: Configuration.h:283
String ssid
The SSID(name) of your wifi.
Definition: Configuration.h:183
void restoreFactoryDefaults()
Reset all values to the default values and write configuration.
Definition: Configuration.h:436
static Configuration ** getInstance()
Function providing the instance for the singleton.
Definition: Configuration.h:153
TimeRestrictions timeRestrictions
Container to restrict the time when the safe can be opened.
Definition: Configuration.h:251
const String & generateNewEmergencyKey()
Generates a new key and stores it to the file system.
Definition: Configuration.h:420
String timezone
The timezone-string (e.g. "CET-1CEST,M3.5.0,M10.5.0/3") see also https://github.com/nayarsystems/posi...
Definition: Configuration.h:226
String apiKey
The API Key extracted from the webpage settings > API.
Definition: Configuration.h:203
const TimeRestrictions & getTimeRestrictions() const
get the timeout of display backlight in seconds
Definition: Configuration.h:354
String userId
The User ID extracted from the webpage settings > API.
Definition: Configuration.h:197
void readConfiguration()
Read all configuration data from file.
Definition: Configuration.h:455
unsigned long backlightTimeOut
Timeout of display backlight in seconds.
Definition: Configuration.h:238
const String & getSsid() const
get the SSID(name) of your wifi
Definition: Configuration.h:265
const String & getTimezoneName() const
get the name to the timezone-string (e.g. "Europe/Berlin")
Definition: Configuration.h:329
const String & getPwd() const
get the password of your wifi.
Definition: Configuration.h:273
static Configuration & getSingleton()
Get the Singleton object.
Definition: Configuration.h:162
const String & getEmergencyKey() const
get the emergency key of the safe
Definition: Configuration.h:299
void setWifiSettings(const String &ssid, const String &pwd)
Set the Wifi Settings.
Definition: Configuration.h:368
const String & getApiKey() const
get the API Key extracted from the webpage settings > API
Definition: Configuration.h:291
String emergencyKey
The emergency key.
Definition: Configuration.h:209
Configuration()
Definition: Configuration.h:255
bool autoLockHygieneOpeningTimeout
Automatically lock after the time for hygiene opening is over. If not set, the safe stays unlocked un...
Definition: Configuration.h:245
static Configuration & begin()
initialize the configuration object and load the configuration file
Definition: Configuration.h:170
String pwd
The password of your wifi.
Definition: Configuration.h:189
void writeConfiguration()
writes all configuration values to the SPIFFS
Definition: Configuration.h:498
Definition: Configuration.h:16