-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathAppContext.cpp
More file actions
111 lines (102 loc) · 3.31 KB
/
AppContext.cpp
File metadata and controls
111 lines (102 loc) · 3.31 KB
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
#include "AppContext.h"
#include "rapidjson/stringbuffer.h"
#include <memory>
namespace ctx
{
void AppContext::setup()
{
fs::FileSystem::getInstance().loadPartitions();
mpWifiContext = std::make_shared<WifiContext>();
try
{
mModel = fs::ConfigReader().readConfiguration();
}
catch(const std::exception& e)
{
mModel = fs::ConfigReader().readFailsafeConfiguration();
connectWireless();
mpWebServer = std::make_unique<web::WebServer>(shared_from_this(), mModel.mWebCredentials);
throw std::runtime_error("Configuration invalid! Login via browser" + std::string(e.what()));
}
if (!mModel.hasWifiCredentials())
{
mpCaptiveServer = std::make_unique<wifi::CaptiveServer>(shared_from_this());
return;
}
mpMQTTConnection = std::make_shared<mqtt::MQTTConnection>(mModel.mMQTTServerConfig, mModel.mMQTTGroups);
connectWireless();
mpMQTTConnection->registerConnectionStatusCallback([&](auto cb) {
if (cb == mqtt::MQTTConnectionStatus::CONNECTED)
{
mpMQTTConnection->bindScenes();
mAppStateNotifier.broadcast(ContextState::Ready);
}
});
mpWebServer = std::make_unique<web::WebServer>(shared_from_this(), mModel.mWebCredentials);
mAppStateNotifier.broadcast(ContextState::Ready);
}
void AppContext::reload()
{
mAppStateNotifier.broadcast(ContextState::Reload);
try
{
mModel = fs::ConfigReader().readConfiguration();
}
catch(const std::exception& e)
{
// Reboot to failsafe
ESP.restart();
}
mpMQTTConnection = std::make_shared<mqtt::MQTTConnection>(mModel.mMQTTServerConfig, mModel.mMQTTGroups);
mpMQTTConnection->registerConnectionStatusCallback([&](auto cb) {
if (cb == mqtt::MQTTConnectionStatus::CONNECTED)
{
mpMQTTConnection->bindScenes();
mAppStateNotifier.broadcast(ContextState::Ready);
}
Serial.println("Connected");
});
mpMQTTConnection->connect();
}
void AppContext::setFirstLaunch(const WifiCredentials credentials,
const std::string login, const std::string username)
{
fs::ConfigReader().setFirstLaunch(credentials, login, username);
ESP.restart();
}
Dispatcher<ctx::ContextState>::CBID AppContext::registerStateCallback(AppStateCB callback)
{
return mAppStateNotifier.addCB(callback);
}
void AppContext::deleteStateCallback(Dispatcher<ctx::ContextState>::CBID callback)
{
mAppStateNotifier.delCB(callback);
}
void AppContext::connectWireless()
{
auto& wifi = mModel.mWifiCredentials;
mpWifiContext->connect(wifi.mSSID, wifi.mPassword, wifi.mHostname);
using namespace std::placeholders;
mpWifiContext->registerCallback(std::bind(&AppContext::connectionStateChanged, this, _1));
}
std::vector<MQTTVariants> &AppContext::getMQTTGroups()
{
return mModel.mMQTTGroups;
}
void AppContext::connectionStateChanged(ctx::WifiConnectionState state)
{
if (state.wifiState == ctx::WifiAssociationState::CONNECTED)
{
if (mModel.mTimeZone != "")
{
mNTPSync = std::make_shared<ntp::NTPSync>(mModel.mTimeZone);
mNTPSync->syncTime();
}
if (mpMQTTConnection && !mModel.mMQTTServerConfig.addr.empty())
{
mpMQTTConnection->connect();
}
mpWebServer->startServer();
}
}
} // namespace ctx