-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage_manager.cpp
179 lines (159 loc) · 5.3 KB
/
message_manager.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
#include "message_manager.h"
#include "connector.h"
#include <QJsonObject>
#include <QJsonDocument>
#include <QFile>
#include <QMessageBox>
#include <QTimer>
#ifndef RESULT_FUNC
#define RESULT_FUNC(VAL) ((VAL==1)?"OK":"Error")
#endif
MessageManager *MessageManager::getInstance()
{
static MessageManager instance;
return &instance;
}
void MessageManager::sendFirstRequest()
{
QJsonObject obj;
obj.insert("Msg", "Calibration");
QJsonDocument json_document;
json_document.setObject(obj);
QByteArray byte_array = json_document.toJson(QJsonDocument::Compact);
QString json_str(byte_array);
m_connector->sendSocketMessage(byte_array);
}
void MessageManager::open()
{
m_connector->open();
}
void MessageManager::setConnectCallback(std::function<void (bool)> callback)
{
m_connector->setConnectCallback(callback);
}
void MessageManager::setBadAckFromServerCallback(std::function<void (const QString &)> callback)
{
this->m_ack = callback;
}
void MessageManager::setRecordCallback(std::function<void (const std::string &)> start, std::function<void ()> stop)
{
this->m_start = start;
this->m_stop = stop;
}
void MessageManager::parseJsonData(const QString &message)
{
// qDebug() << "message : " << message;
auto data = message.simplified().trimmed();
QJsonParseError jsonerror;
auto doc = QJsonDocument::fromJson(data.toLatin1(), &jsonerror);
auto json = doc.object();
if(jsonerror.error == QJsonParseError::NoError) {
if(json.contains("functionSw") && json.contains("cfgValue")) {
m_json = json;
QFile file("./config.json");
if(file.open(QIODevice::WriteOnly)) {
file.write(message.toLatin1());
}
file.close();
if(m_json["functionSw"].toObject()["rawDataSw"].toInt() == 0) {
QMessageBox::information(nullptr,"info","Raw data Switch is OFF");
}
else if(m_json["functionSw"].toObject()["rawDataSw"].toInt() == 1) {
qDebug() << "Raw data Switch is ON";
}
}
else if(json.contains("Msg")) {
auto ack = QString("%1 -> %2").arg(json["Msg"].toString()).arg(RESULT_FUNC(json["res"].toInt()));
if(!json["res"].toInt()) {
this->m_ack(ack);
}
qDebug() << ack;
}
}
else {
qDebug() << "json parse error : " << message;
}
}
bool MessageManager::checkJsonData(const QString &message)
{
auto data = message.simplified().trimmed();
QJsonParseError jsonerror;
auto doc = QJsonDocument::fromJson(data.toLatin1(), &jsonerror);
if(jsonerror.error == QJsonParseError::NoError) {
m_json = doc.object();
// 合法性检查
if(!m_json.contains("functionSw")) return false;
if(!m_json.contains("cfgValue")) return false;
if(!m_json["functionSw"].toObject().contains("dynamicSw")) return false;
if(!m_json["functionSw"].toObject().contains("rawDataSw")) return false;
m_backup = m_json;
return true;
}
return false;
}
void MessageManager::runCommand()
{
closeDynamicPower();
m_count = 0;
auto json = m_json["cfgValue"].toObject();
for(auto &key : json.keys()) {
QJsonObject obj;
obj.insert("Msg", "Calibration");
QJsonObject node;
node.insert(key, json[key]);
obj.insert("cfgValue", node);
QJsonDocument json_document;
json_document.setObject(obj);
QByteArray byte_array = json_document.toJson(QJsonDocument::Compact);
QString json_str(byte_array);
m_connector->sendSocketMessage(byte_array);
}
}
void MessageManager::startToCollect(const QString &name)
{
m_start(name.toStdString());
QJsonObject obj;
obj.insert("Msg", "Calibration");
QJsonObject node;
QString key = QString("rawDataSw");
node.insert(key, 1);
obj.insert("functionSw", node);
QJsonDocument json_document;
json_document.setObject(obj);
QByteArray byte_array = json_document.toJson(QJsonDocument::Compact);
QString json_str(byte_array);
m_connector->sendSocketMessage(byte_array);
m_timer->start();
}
void MessageManager::closeDynamicPower()
{
QJsonObject obj;
obj.insert("Msg", "Calibration");
QJsonObject node;
QString key = QString("dynamicSw");
node.insert(key, 0);
obj.insert("functionSw", node);
QJsonDocument json_document;
json_document.setObject(obj);
QByteArray byte_array = json_document.toJson(QJsonDocument::Compact);
QString json_str(byte_array);
m_connector->sendSocketMessage(byte_array);
}
void MessageManager::setTimerCount(int timerCount)
{
this->m_timerCount = timerCount * 1000;
m_timer->setInterval(this->m_timerCount);
m_timer->setSingleShot(true);
}
MessageManager::MessageManager(QObject *parent) : QObject(parent), m_connector(new Connector(this)), m_timer(new QTimer(this)), m_count(0)
{
m_connector->setJsonParserCallback(std::bind(&MessageManager::parseJsonData, this, std::placeholders::_1));
m_connector->setSendCallback(std::bind(&MessageManager::sendFirstRequest, this));
QObject::connect(m_timer, &QTimer::timeout, this, [&]() {
this->m_stop();
QMessageBox::information(nullptr,"info","Pcap has been saved to local path");
});
}
MessageManager::~MessageManager()
{
}