-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainApplication.cpp
190 lines (163 loc) · 4.59 KB
/
MainApplication.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
//
//
//
#define DEBUG_UPDATER
#include "Constants.h"
#include "StatusHandler.h"
#include "MainApplication.h"
void tick()
{
//every second we check time only in automatic mode
if (Status.mode() == StatusHandler::kAutomatic)
{
int time = HTTPServer.timeClient().getEpochTime();
//we get time from midnight
time = time % 86400;
/*Serial.println("time");
Serial.println(time);*/
int lowerBoundTime = 0;
int higherBoundTime = 1;
int lowerBoundValue = 0;
int higherBoundValue = 1;
int newValue = -1;
if (time < Configuration._timeStart)
{
//off before light
newValue = 0;
}
else if (time < Configuration._timeStart + Configuration._startDuration)
{
//sunrise
lowerBoundTime = Configuration._timeStart;
lowerBoundValue = 0;
higherBoundTime = Configuration._timeStart + Configuration._startDuration;
higherBoundValue = 100;
}
else if (time < Configuration._timeEnd)
{
//lighted
newValue = 100;
}
else if (time < Configuration._timeEnd + Configuration._endDuration)
{
//sunset
lowerBoundTime = Configuration._timeEnd;
lowerBoundValue = 100;
higherBoundTime = Configuration._timeEnd + Configuration._endDuration;
higherBoundValue = 0;
}
else
{
//off after light
newValue = 0;
}
if (newValue == -1)
{
//newValue hasn't been set
float implication = ((float)time - lowerBoundTime) / (higherBoundTime - lowerBoundTime);
newValue = (int)(implication * higherBoundValue + (1.0 - implication) * lowerBoundValue);
}
/*Serial.println("new Value");
Serial.println(newValue);*/
Status.currentValue(newValue);
}
}
//gets called when WiFiManager enters configuration mode
void configModeCallback(WiFiManager *myWiFiManager) {
Serial.println("Entered config mode");
Serial.println(WiFi.softAPIP());
//if you used auto generated SSID, print it
Serial.println(myWiFiManager->getConfigPortalSSID());
}
MainApplication::MainApplication()
{
Serial.begin(115200);
}
MainApplication::~MainApplication()
{
}
void MainApplication::setup(void)
{
delay(500);
pinMode(Constants::LedPinNumber, OUTPUT);
//_ticker.attach(0.2, tick);
if (!SPIFFS.begin())
{
Serial.println("failed to initialize SPIFFS");
}
//we look if a file named lighter.ino.bin is on the spiffs
//if yes we update the rom and delete the file
String updateFilename = "/lighter.ino.bin";
Serial.println("Checking update file " + updateFilename);
if (SPIFFS.exists(updateFilename))
{
Serial.println("update file found");
File updateFile = SPIFFS.open(updateFilename, "r");
Serial.println("running update");
runUpdate(updateFile, updateFile.size(), U_FLASH);
Serial.println("update finished");
updateFile.close();
Serial.println("removing update file");
SPIFFS.remove(updateFilename);
Serial.println("restarting");
ESP.reset();
}
Configuration.setup();
Status.setup();
_wifiManager.setDebugOutput(true);
//reset settings - for testing
//_wifiManager.resetSettings();
//set callback that gets called when connecting to previous WiFi fails, and enters Access Point mode
_wifiManager.setAPCallback(configModeCallback);
//fetches ssid and pass and tries to connect
//if it does not connect it starts an access point with the specified name
//here "AutoConnectAP"
//and goes into a blocking loop awaiting configuration
if (!_wifiManager.autoConnect()) {
Serial.println("failed to connect and hit timeout");
//reset and try again, or maybe put it to deep sleep
ESP.reset();
delay(1000);
}
//if you get here you have connected to the WiFi
Serial.println("connected.");
HTTPServer.setup();
analogWrite(Constants::LedPinNumber, 255);
//Configuration.restoreDefault();
//install timer
_ticker.attach(1, tick);
}
void MainApplication::handle(void)
{
HTTPServer.handle();
}
/**
* write Update to flash
* @param in Stream&
* @param size uint32_t
* @param md5 String
* @return true if Update ok
*/
bool MainApplication::runUpdate(Stream& in, uint32_t size, int command)
{
StreamString error;
if(!Update.begin(size, command)) {
Update.printError(error);
error.trim(); // remove line ending
Serial.printf("Update.begin failed! (%s)\n", error.c_str());
return false;
}
if(Update.writeStream(in) != size) {
Update.printError(error);
error.trim(); // remove line ending
Serial.printf("Update.writeStream failed! (%s)\n", error.c_str());
return false;
}
if(!Update.end()) {
Update.printError(error);
error.trim(); // remove line ending
Serial.printf("Update.end failed! (%s)\n", error.c_str());
return false;
}
return true;
}