-
Notifications
You must be signed in to change notification settings - Fork 3
/
MiddlewareStatusListener.cpp
47 lines (42 loc) · 1.31 KB
/
MiddlewareStatusListener.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
#include "MiddlewareStatusListener.h"
void MiddlewareStatusListener::AddStatusCallback(std::function<void(MiddlewareStatusUpdate)> callback)
{
_statusCallbacks.push_back(callback);
}
MiddlewareStatusUpdate MiddlewareStatusListener::lastStatus()
{
return _data;
}
void MiddlewareStatusListener::OnMessageReceived(WeArtMessage* message)
{
try {
bool toUpdate = false;
if (message->getID() == MiddlewareStatusMessage::ID) {
MiddlewareStatusMessage* mwStatus = dynamic_cast<MiddlewareStatusMessage*>(message);
if (mwStatus != nullptr) {
MiddlewareStatusData newStatus = mwStatus->data();
_data.timestamp = mwStatus->timestamp();
_data.status = newStatus.status;
_data.version = newStatus.version;
_data.statusCode = newStatus.statusCode;
_data.errorDesc = newStatus.errorDesc;
_data.actuationsEnabled = newStatus.actuationsEnabled;
toUpdate = true;
}
}
else if (message->getID() == DevicesStatusMessage::ID) {
DevicesStatusMessage* devicesStatus = dynamic_cast<DevicesStatusMessage*>(message);
if (devicesStatus != nullptr) {
_data.timestamp = devicesStatus->timestamp();
_data.devices = devicesStatus->devices();
toUpdate = true;
}
}
if (toUpdate) {
for (auto callback : _statusCallbacks) {
callback(_data);
}
}
}
catch (const std::bad_cast&) {}
}