EmlaLockSafe
EmergencyEnterKeyView.h
Go to the documentation of this file.
1
5#pragma once
6
7#include "../LockState.h"
8#include "../configuration/Configuration.h"
9#include "ViewStore.h"
10
11#include <RotaryEncoder.h>
12#include <ViewBase.h>
13
14namespace views {
18class EmergencyEnterKeyView : public lcd::ViewBase {
19protected:
23 RotaryEncoder* encoder;
24
25protected:
29 char enteredKey[7];
30
31protected:
36
37public:
43 EmergencyEnterKeyView(LiquidCrystal_PCF8574* display, RotaryEncoder* encoder)
44 : lcd::ViewBase(display, "EmergencyEnterKeyView")
45 , encoder(encoder) {}
46
47public:
52
53public:
57 EmergencyEnterKeyView(EmergencyEnterKeyView&& other) noexcept = delete;
58
59protected:
63 virtual void activate() {
64 strcpy(enteredKey, "AAAAAA");
65 editIndex = 0;
66
67 display->clear();
68 display->setCursor(0, 0);
69 display->print("Enter emergency key:");
70 tick(true);
71 }
72
73public:
79 virtual void tick(const bool& forceRedraw) {
80 auto direction = encoder->getDirection();
81 bool redraw = forceRedraw;
82
83 if (direction == RotaryEncoder::Direction::CLOCKWISE) {
84 if (enteredKey[editIndex] == 'Z') {
85 enteredKey[editIndex] = '0';
86 }
87 else if (enteredKey[editIndex] == '9') {
88 enteredKey[editIndex] = 'A';
89 }
90 else {
92 }
93 redraw = true;
94 }
95 else if (direction == RotaryEncoder::Direction::COUNTERCLOCKWISE) {
96 if (enteredKey[editIndex] == 'A') {
97 enteredKey[editIndex] = '9';
98 }
99 else if (enteredKey[editIndex] == '0') {
100 enteredKey[editIndex] = 'Z';
101 }
102 else {
104 }
105 redraw = true;
106 }
107
108 if (encoder->getNewClick()) {
109 editIndex++;
110 if (editIndex == 6) {
111 editIndex = 0;
112 display->setCursor(0, 1);
113
114 if (strcmp(enteredKey, configuration::Configuration::getSingleton().getEmergencyKey().c_str()) == 0) {
115 const auto& timeRestictions = configuration::Configuration::getSingleton().getTimeRestrictions();
116 if (!timeRestictions.restrictEmergencyKeyTimes || timeRestictions.checkTime()) {
117 // Correct key was entered...
118 LockState::setEndDate(0); // set to unlock
119 LockState::setLastUpdateTime(time(NULL) + 10);
121 return;
122 }
123 else {
125 return;
126 }
127 }
128 else {
129 display->print("Invalid Key");
130 }
131 }
132 redraw = true;
133 }
134
135 if (redraw) {
136 for (int i = 0; i < 6; i++) {
137 display->setCursor(2 * i + 3, 3);
138 display->print(' ');
139 display->print(enteredKey[i]);
140 }
141 display->print(' ');
142
143 display->setCursor(editIndex * 2 + 3, 3);
144 display->print('>');
145 display->setCursor(editIndex * 2 + 5, 3);
146 display->print('<');
147 }
148 }
149};
150} // namespace views
static void setLastUpdateTime(const time_t &lastUpdateTime)
Set the time of the last update over the emlalock api.
Definition: LockState.h:447
static void setEndDate(const time_t &endDate)
Set the End Date.
Definition: LockState.h:334
const TimeRestrictions & getTimeRestrictions() const
get the timeout of display backlight in seconds
Definition: Configuration.h:354
static Configuration & getSingleton()
Get the Singleton object.
Definition: Configuration.h:162
View which allows to enter a key to unlock without WiFi.
Definition: EmergencyEnterKeyView.h:18
char enteredKey[7]
Cache of the entered key.
Definition: EmergencyEnterKeyView.h:29
EmergencyEnterKeyView(EmergencyEnterKeyView &&other) noexcept=delete
Move constructor.
EmergencyEnterKeyView(const EmergencyEnterKeyView &other)=delete
Copy constructor - not available.
virtual void tick(const bool &forceRedraw)
called during the loop function
Definition: EmergencyEnterKeyView.h:79
RotaryEncoder * encoder
pointer to the encoder instance
Definition: EmergencyEnterKeyView.h:23
virtual void activate()
called as soon as the view becomes active
Definition: EmergencyEnterKeyView.h:63
EmergencyEnterKeyView(LiquidCrystal_PCF8574 *display, RotaryEncoder *encoder)
Construct the view object.
Definition: EmergencyEnterKeyView.h:43
int editIndex
Position of the key cache which is currently edited.
Definition: EmergencyEnterKeyView.h:35
static bool activateView(const ViewId &id)
activates the view described by the id
Definition: ViewStore.h:98
@ UnlockSafeView
Definition: ViewStore.h:38
@ TimeRestrictedView
Definition: ViewStore.h:36
views::ConfigurationServerView configurationServerView & display
Definition: main.cpp:57
Definition: ConfigurationServerView.h:10