-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cpp
75 lines (57 loc) · 1.97 KB
/
Program.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
#include "Program.h"
#include <any>
namespace MicroController
{
Program::Program() {
Menager = new OperationMenager();
Serial.begin(AppSettings::SerialBitRate);
Provider = ConnectionFactory::Create(AppSettings::DefaultProvider, Menager);
Oled = new OledDisplay();
}
void Program::Start()
{
Serial.println("Program Started");
// Setup light pins
for (int i = 0; i < AppSettings::LightPins.size(); i++) {
pinMode(AppSettings::LightPins[i], OUTPUT);
}
pinMode(AppSettings::MotionDetectionPin, INPUT);
Oled->Setup();
Oled->Print(Name + " v" + to_string(Version).substr(0, 3), 0, 0);
Provider->OnMessage->addListener(L"Provider_OnMessage", [&] (std::string message) {Provider_OnMessage(message);});
Provider->Setup();
Provider->Connect();
auto operation = new Operation([&] () {
pinMode(2, OUTPUT);
digitalWrite(2, HIGH);
return "";
}, [&] (std::string message) {});
auto operationOptions = new OperationOptions();
operationOptions->Delay = 20000;
operation->SetOptions(operationOptions);
Menager->AddOperation(operation);
}
void Program::Loop()
{
Provider->Loop();
}
void Program::Restart() {
Provider->OnMessage->removeListener(L"Provider_OnMessage");
Provider = ConnectionFactory::Create(AppSettings::DefaultProvider, Menager);
Start();
}
void Program::Provider_OnMessage(const std::string &message)
{
Oled->Print(message, 2);
if (message == "Connected to Socket") {
auto operation = new Operation([&] () {
auto rest = new Rest();
return rest->Get("/api/dashboard/time");
}, [&] (std::string message) {
Oled->Print(message, 3);
});
Menager->AddOperation(operation);
}
Serial.println(message.c_str());
}
}