-
Notifications
You must be signed in to change notification settings - Fork 2
/
launchframe.cpp
282 lines (217 loc) · 8.78 KB
/
launchframe.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#include "launchframe.h"
#include "datathread.h"
#include <QMessageBox>
#include <QJsonParseError>
#include "mainwindow.h"
static int setupSteps = 0;
QApplication* gApp = nullptr;
LaunchFrame::LaunchFrame(QWidget *parent) :
QFrame(parent)
{
this->setObjectName("launchWnd");
this->setStyleSheet("LaunchFrame#launchWnd{ border-image: url(:/res/img/grass.jpg);}");
_rediscli = new RedisClient(this);
_rediscli->connectToServer(REDISSERVER, REDISPORT);
_loading = new QLabel(this);
_loading->setText("Loading ...");
}
LaunchFrame::~LaunchFrame()
{
}
void LaunchFrame::showEvent(QShowEvent* e) {
QFrame::showEvent(e);
}
void LaunchFrame::resizeEvent(QResizeEvent* e) {
QFrame::resizeEvent(e);
QRect rect = this->geometry();
_loading->setGeometry(10, 10, rect.width() - 10, 50);
}
void LaunchFrame::switchMainFrame() {
static MainWindow *w = nullptr;
if(nullptr == w) {
w = new MainWindow();
}
w->setMinimumSize(800, 600);
// w->setMaximumSize(1024, 768);
w->showFullScreen();
gApp->connect( gApp, SIGNAL( lastWindowClosed() ), w, SLOT( quit() ) );
}
void LaunchFrame::connected() {
qDebug() << "redis connect to local server OK";
_rediscli->sendCommand(Command::Auth(REDISAUTH));
}
void LaunchFrame::disconnected() {
usleep(2 * 1000 * 1000);
_rediscli->connectToServer(REDISSERVER, REDISPORT); //重连
}
bool LaunchFrame::loadTables() {
QFile tab(QString("%1/tables.conf").arg(WORKDIR));
if( tab.exists() == false) {
qDebug() << "load tables conf failed: file is not exists";
return false;
}
if(!tab.open(QIODevice::ReadWrite))
{
qDebug() << "load tables conf failed: file opened failed";
return false;
}
QString data = tab.readAll();
QJsonParseError json_error;
QJsonDocument jsonDoc(QJsonDocument::fromJson(QByteArray::fromPercentEncoding(data.toLocal8Bit()), &json_error));
if(json_error.error != QJsonParseError::NoError)
{
//QMessageBox::warning(nullptr, "错误", "无法加载口味设置配置文件,配置文件格式不合法");
qDebug() << "load local data failed, json error: " << json_error.errorString();
return false;
} else {
gTables = jsonDoc.toVariant().toList();
this->_rediscli->sendCommand(Command::GET("echo"));
return true;
}
}
bool LaunchFrame::loadFlavors() {
QFile flavors(QString("%1/flavors.conf").arg(WORKDIR));
if( flavors.exists() == false) {
qDebug() << "load flavors conf failed: file is not exists";
return false;
}
if(!flavors.open(QIODevice::ReadWrite))
{
qDebug() << "load flavors conf failed: file opened failed";
return false;
}
QString data = flavors.readAll();
QJsonParseError json_error;
QJsonDocument jsonDoc(QJsonDocument::fromJson(QByteArray::fromPercentEncoding(data.toLocal8Bit()), &json_error));
if(json_error.error != QJsonParseError::NoError)
{
//QMessageBox::warning(nullptr, "错误", "无法加载口味设置配置文件,配置文件格式不合法");
qDebug() << "load local data failed, json error: " << json_error.errorString();
return false;
} else {
gFlavors = jsonDoc.toVariant().toMap();
this->_rediscli->sendCommand(Command::GET("echo"));
return true;
}
}
void LaunchFrame::onReply(const QString& cmd, Reply value) {
//
setupSteps++;
qDebug() << "LaunchFrame::onReply step: " << setupSteps << ", cmd: " << cmd << ", value: " << value.value().toString();
QString key = cmd.toLower().trimmed();
if(key.startsWith("auth")) {
if(value.value().toString().trimmed() != "OK") {
setupSteps --;
this->_loading->setText("AMP: Connect to Server Failed");
return;
}
} else if(key.startsWith(QString("hgetall res:info:%1").arg(gRID))) {
QList<QVariant> item = value.value().toList();
if(item.length() == 0) {
QMessageBox::warning(nullptr, "错误", "当前店家没有设置安装数据");
return;
}
for(int i = 0; i < item.length(); i++) {
if( item[i].toString() == "rid") {
gRestaurant.insert("rid", item[i + 1].toInt());
gRestaurant.insert("id", item[i + 1].toInt());
} else if(item[i].toString() == "account") {
gRestaurant.insert("account", item[i + 1].toString());
} else if(item[i].toString() == "name") {
gRestaurant.insert("name", item[i + 1].toString());
} else if(item[i].toString() == "uuid") {
gRestaurant.insert("uuid", item[i + 1].toString());
} else if(item[i].toString() == "token") {
gRestaurant.insert("token", item[i + 1].toString());
} else if(item[i].toString() == "taxRate") {
gRestaurant.insert("taxRate", item[i + 1].toFloat());
} else if(item[i].toString() == "address") {
gRestaurant.insert("address", item[i + 1].toString());
} else if(item[i].toString() == "phoneNumbers") {
gRestaurant.insert("phoneNumbers", item[i + 1].toString());
} else if(item[i].toString() == "cloudIP") {
gRestaurant.insert("cloudIP", item[i + 1].toString());
}
}
} else if(key.startsWith(QString("hgetall res:printer:config:rid:%1").arg(gRID))) {
_kitchPrinter.clear();
_cashierPrinter = "";
QList<QVariant> item = value.value().toList();
for(int i = 0; i < item.length() - 1; i++) {
if( item[i + 1].toString() == "cashier") {
_cashierPrinter = item[i].toString();
} else if( item[i + 1].toString().startsWith("kitchen-")) {
_kitchPrinter.push_back( item[i].toString());
}
}
if("" == _cashierPrinter) {
QMessageBox::warning(nullptr, "错误", "前台打印机设置无效");
}
} else if(key.startsWith(QString("hget res:conf:%1 flavors").arg(gRID))) {
QString flavors = value.value().toString();
if("" == flavors) {
setupSteps--;
}
else {
QJsonParseError json_error;
QJsonDocument jsonDoc(QJsonDocument::fromJson(QByteArray::fromPercentEncoding(flavors.toLocal8Bit()), &json_error));
if(json_error.error != QJsonParseError::NoError)
{
//QMessageBox::warning(nullptr, "错误", "无法加载口味设置配置文件,配置文件格式不合法");
qDebug() << "json error: " << json_error.errorString();
gFlavors = QVariantMap();
} else {
gFlavors = jsonDoc.toVariant().toMap();
}
}
} else if(key.startsWith(QString("hget res:conf:%1 tables").arg(gRID))) {
QString tables = value.value().toString();
if("" == tables) {
setupSteps--;
}
else {
QJsonParseError json_error;
QJsonDocument jsonDoc(QJsonDocument::fromJson(QByteArray::fromPercentEncoding(tables.toLocal8Bit()), &json_error));
if(json_error.error != QJsonParseError::NoError)
{
//QMessageBox::warning(nullptr, "错误", "无法加载桌台设置配置文件,配置文件格式不合法");
qDebug() << "json error: " << json_error.errorString();
gTables = QList<QVariant>();
setupSteps--;
} else {
gTables = jsonDoc.toVariant().toList();
}
}
}
usleep(200 * 1000);
switch(setupSteps) {
case 1:
this->_loading->setText("AMP: Connect to Server OK, Waiting for loding shop's information...");
_rediscli->sendCommand(Command::HGETALL("res:info:" + QString::number(gRID) ));
break;
case 2:
this->_loading->setText("AMP: loading shop's info OK, Waiting for loding shop's printers...");
_rediscli->sendCommand(Command::HGETALL("printers:config:" + QString::number(gRID)));
break;
case 3:
if( false == this->loadTables()) {
this->_loading->setText("AMP: Connect to Server OK, Waiting for loding shop's tables info ...");
_rediscli->sendCommand(Command::HGET("res:conf:" + QString::number(gRID), "tables"));
}
break;
case 4:
if(false == this->loadFlavors()) {
this->_loading->setText("AMP: Connect to Server OK, Waiting for loding shop's flavors info ...");
_rediscli->sendCommand(Command::HGET("res:conf:" + QString::number(gRID), "flavors"));
break;
}
break;
case 5:
{
this->hide();
this->switchMainFrame();
break;
}
}
this->_loading->update();
}