-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.cpp
111 lines (98 loc) · 2.8 KB
/
controller.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
#include "controller.h"
#include "aliseconstants.h"
#include <QDebug>
#include <QTimer>
#include <iostream>
Controller::Controller(QObject *parent) noexcept : QObject(parent)
{
}
bool Controller::launchOnPort(int port)
{
if (isIncorrectType())
{
qCritical() << "Controller has incorrect type or no type was given";
return false;
}
m_runner->runServer(port);
#if defined(AVTUK_STM)
m_deviceBroker->getTime();
#else
m_timeSynchronizer->systemTime();
#endif
return true;
}
void Controller::shutdown()
{
QMetaObject::invokeMethod(m_runner, &ZeroRunner::stopServer, Qt::DirectConnection);
}
void Controller::syncTime(const timespec &)
{
}
Controller *Controller::withBroker(Broker *broker)
{
m_deviceBroker = broker;
return this;
}
Controller *Controller::withTimeSynchonizer(TimeSyncronizer *tm)
{
m_timeSynchronizer = tm;
return this;
}
Controller *Controller::ofType(Controller::ContrTypes type)
{
if (!c_controllerMap.contains(type))
m_type = Controller::ContrTypes::IS_INCORRECT_TYPE;
else
m_type = type;
m_runner = new ZeroRunner(c_controllerMap[type], this);
switch (type)
{
case IS_ADMINJA:
adminjaSetup();
break;
case IS_BOOTER:
booterSetup();
break;
case IS_CORE:
coreSetup();
break;
default:
qCritical() << "Incorrect controller type";
break;
}
return this;
}
void Controller::adminjaSetup()
{
connect(m_runner, &ZeroRunner::timeReceived, m_timeSynchronizer, &TimeSyncronizer::printAndSetSystemTime);
#if defined(AVTUK_STM)
connect(m_runner, &ZeroRunner::timeReceived, m_deviceBroker, &Broker::setTime);
connect(m_runner, &ZeroRunner::timeRequest, m_deviceBroker, &Broker::getTime);
#else
connect(m_runner, &ZeroRunner::timeRequest, m_timeSynchronizer, //
[this] { m_deviceBroker->updateTime(m_timeSynchronizer->systemTime()); });
#endif
connect(m_deviceBroker, &Broker::receivedTime, m_runner, &ZeroRunner::publishTime, Qt::DirectConnection);
connect(m_timeSynchronizer, &TimeSyncronizer::ntpStatusChanged, this, [&](bool status) {
m_runner->publishNtpStatus(status);
if (!status)
return;
});
}
void Controller::booterSetup()
{
m_pingTimer = new QTimer(this);
m_pingTimer->setInterval(Alise::AliseConstants::HealthQueryPeriod());
// publish data to zeroMQ channel
connect(m_pingTimer, &QTimer::timeout, m_runner, &ZeroRunner::publishHealthQueryCallback);
m_pingTimer->start();
connect(m_runner, &ZeroRunner::healthReceived, m_deviceBroker, &Broker::healthReceived);
}
void Controller::coreSetup()
{
connect(m_deviceBroker, &Broker::receivedBlock, m_runner, &ZeroRunner::publishBlock);
}
bool Controller::isIncorrectType()
{
return (m_type == IS_INCORRECT_TYPE);
}