-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib-main.cpp
207 lines (175 loc) · 6.21 KB
/
lib-main.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include "lib-main.h"
namespace Xn {
AppThread main_thread;
LibMain lib(nullptr);
///////////////////////////////////////////////////////////////////////////////
LibMain::LibMain(QObject* parent) : QObject(parent) {
xn.loglevel = LogLevel::Debug;
QObject::connect(&xn, SIGNAL(onError(QString)), this, SLOT(xnOnError(QString)));
QObject::connect(&xn, SIGNAL(onLog(QString, Xn::LogLevel)), this,
SLOT(xnOnLog(QString, Xn::LogLevel)));
QObject::connect(&xn, SIGNAL(onConnect()), this, SLOT(xnOnConnect()));
QObject::connect(&xn, SIGNAL(onDisconnect()), this, SLOT(xnOnDisconnect()));
QObject::connect(&xn, SIGNAL(onLocoStolen(Xn::LocoAddr)), this, SLOT(xnOnLocoStolen(Xn::LocoAddr)));
QObject::connect(&xn, SIGNAL(onTrkStatusChanged(Xn::TrkStatus)), this,
SLOT(xnOnTrkStatusChanged(Xn::TrkStatus)));
this->config_filename = _DEFAULT_CONFIG_FILENAME;
s.load(this->config_filename);
this->xnSetConfig();
this->guiInit();
log("Library loaded.", LogLevel::Info);
}
LibMain::~LibMain() {
try {
if (xn.connected())
xn.disconnect();
if (this->config_filename != "")
this->s.save(this->config_filename);
} catch (...) {
// No exceptions in destructor!
}
}
///////////////////////////////////////////////////////////////////////////////
void LibMain::log(const QString &msg, LogLevel loglevel) {
events.call(events.onLog, loglevel, msg);
}
///////////////////////////////////////////////////////////////////////////////
// Xn events:
void LibMain::xnOnLog(QString message, LogLevel loglevel) {
if (this->opening && (message == "Not responded to command: LI Get Address" ||
message == "Not responded to command: Get Command station version"))
loglevel = LogLevel::Warning;
this->events.call(this->events.onLog, loglevel, message);
}
void LibMain::xnOnError(QString error) {
// Xn error is considered fatal -> close device
log("XN error: " + error, LogLevel::Error);
if (xn.connected())
xn.disconnect();
}
void LibMain::xnOnConnect() {
lib.guiOnOpen();
this->opening = true;
this->getLIVersion();
}
void LibMain::xnOnDisconnect() {
this->opening = false;
this->guiOnClose();
this->events.call(this->events.afterClose);
}
void LibMain::xnOnLocoStolen(LocoAddr addr) {
this->events.call(this->events.onLocoStolen, addr);
}
void LibMain::xnOnTrkStatusChanged(TrkStatus trkStatus) {
this->events.call(this->events.onTrkStatusChanged, trkStatus);
if (this->opening) {
this->opening = false;
this->events.call(this->events.afterOpen);
}
}
void LibMain::getLIVersion() {
try {
xn.getLIVersion(
[this](void *s, unsigned hw, unsigned sw) { xnGotLIVersion(s, hw, sw); },
std::make_unique<Cb>([this](void *s, void *d) { xnOnLIVersionError(s, d); })
);
} catch (const QStrException &e) {
log("Get LI Version: " + e.str(), LogLevel::Error);
events.call(events.onOpenError, "Get LI Version: " + e.str());
xn.disconnect();
}
}
void LibMain::xnGotLIVersion(void *, unsigned hw, unsigned sw) {
QString version = "HW:" + QString::number(hw) + ", SW: " + QString::number(sw);
this->li_ver_hw = hw;
this->li_ver_sw = sw;
form.ui.l_li_version->setText(version);
form.ui.l_info_datetime->setText(QTime::currentTime().toString("hh:mm:ss"));
try {
xn.getLIAddress(
[this](void *s, unsigned addr) { xnGotLIAddress(s, addr); },
std::make_unique<Cb>([this](void *s, void *d) { xnOnLIAddrError(s, d); })
);
} catch (const QStrException &e) {
log("Get LI Address: " + e.str(), LogLevel::Error);
events.call(events.onOpenError, "Get LI Address: " + e.str());
xn.disconnect();
}
}
void LibMain::xnOnLIVersionError(void *, void *) {
log("Get LI Version: no response!", LogLevel::Error);
events.call(events.onOpenError, "Get LI Version: no response!");
xn.disconnect();
}
void LibMain::xnGotLIAddress(void *, unsigned addr) {
form.ui.sb_li_addr->setValue(addr);
form.ui.l_info_datetime->setText(QTime::currentTime().toString("hh:mm:ss"));
this->getCSVersion();
}
void LibMain::xnOnLIAddrError(void *, void *) {
log("Unable to get LI address, ignoring!", LogLevel::Warning);
form.ui.l_info_datetime->setText(QTime::currentTime().toString("hh:mm:ss"));
this->getCSVersion();
}
void LibMain::getCSVersion() {
try {
xn.getCommandStationVersion(
[this](void *s, unsigned major, unsigned minor, uint8_t id) {
xnGotCSVersion(s, major, minor, id);
},
std::make_unique<Cb>([this](void *s, void *d) { xnOnCsVersionError(s, d); })
);
} catch (const QStrException &e) {
log("Get CS Version: " + e.str(), LogLevel::Error);
events.call(events.onOpenError, "Get CS Version: " + e.str());
xn.disconnect();
}
}
void LibMain::xnGotCSVersion(void *, unsigned major, unsigned minor, uint8_t id) {
QString version = QString::number(major) + "." + QString::number(minor);
form.ui.l_cs_version->setText(version);
form.ui.l_cs_id->setText(QString::number(id));
form.ui.l_info_datetime->setText(QTime::currentTime().toString("hh:mm:ss"));
this->getCSStatus();
}
void LibMain::xnOnCsVersionError(void *, void *) {
log("Command station version not received, ignoring!", LogLevel::Warning);
form.ui.l_cs_version->setText("Nelze zjistit");
form.ui.l_cs_id->setText("Nelze zjistit");
form.ui.l_info_datetime->setText(QTime::currentTime().toString("hh:mm:ss"));
this->getCSStatus();
}
void LibMain::getCSStatus() {
try {
xn.getCommandStationStatus(
nullptr,
std::make_unique<Cb>([this](void *s, void *d) { xnOnCSStatusError(s, d); })
);
} catch (const QStrException &e) {
log("Get CS Status: " + e.str(), LogLevel::Error);
events.call(events.onOpenError, "Get CS Status: " + e.str());
xn.disconnect();
}
}
void LibMain::xnOnCSStatusError(void *, void *) {
log("Get CS Status: no response!", LogLevel::Error);
events.call(events.onOpenError, "Get CS Status: no response!");
xn.disconnect();
}
void LibMain::xnSetConfig() {
try {
XNConfig config;
bool ok;
config.outInterval = s["XN"]["outIntervalMs"].toUInt(&ok);
if (!ok) {
log("Unable to load xnConfig: 'outIntervalMs' is not a number!", LogLevel::Error);
return;
}
xn.setConfig(config);
} catch (const QStrException& e) {
log("Unable to load xnConfig: "+e.str(), LogLevel::Error);
} catch (...) {
log("Unable to load xnConfig: cannot set config (unknown exception)!", LogLevel::Error);
}
}
} // namespace Xn