-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessagegenerator.cpp
More file actions
283 lines (240 loc) · 9.12 KB
/
messagegenerator.cpp
File metadata and controls
283 lines (240 loc) · 9.12 KB
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
283
#include "messagegenerator.h"
#include <memory>
#include <QDebug>
#include <QByteArray>
#include <QJsonObject>
#include <QJsonDocument>
#include <QHostAddress>
namespace
{
const static QString SETUP = "SETUP";
}
MessageGenerator::MessageGenerator(QObject *parent) :
QObject(parent),
m_client(std::make_unique<TcpClient>("localhost", 8095))
{
setProperty("ApiVersion", 2);
QObject::connect(m_client.get(), &TcpClient::connected, this, [this]()
{ m_isConnected = true;
emit connectionChanged();
sendCurrentVersion();
});
QObject::connect(m_client.get(), &TcpClient::disconnected, this, [this]()
{ m_isConnected = false;
emit connectionChanged();
});
QObject::connect(m_client.get(), &TcpClient::messageReceived, this, &MessageGenerator::onMessageReceived);
}
void MessageGenerator::onMessageReceived(const QString& message)
{
QString formattedMessage = QString("[%1][%2:%3]: %4").arg(QDateTime::currentDateTime().time().toString()).arg(m_client->host()).arg(m_client->port()).arg(message);
msgReceived(formattedMessage);
if (m_ignoreMessage)
{
qDebug() << "message will be ignored.";
return;
}
emit rawMsgReceived(message);
QJsonDocument jsonDoc = QJsonDocument::fromJson(message.toUtf8());
QJsonObject jsonHeader = jsonDoc.object().value("header").toObject();
QJsonObject jsonPayload = jsonDoc.object().value("payload").toObject();
auto msgName = jsonHeader.value("messageName").toString();
auto msgType = jsonHeader.value("messageType").toString();
auto msgId = jsonHeader.value("messageId").toInt();
if (msgType == CONTROL)
{
auto object = jsonPayload.value("object").toString();
if (msgName == CHANGE_OBJECT_VALUE_REQ)
{
sendControlMessage(CHANGE_OBJECT_VALUE_RESP, msgId);
}
else if (msgName == MOVE_OBJECT_REQ)
{
sendControlMessage(MOVE_OBJECT_RESP, msgId);
}
else if (msgName == ACTION_OBJECT_TRIGGER_REQ)
{
sendControlMessage(ACTION_OBJECT_TRIGGER_RESP, msgId);
}
else if (msgName == CHANGE_METER_COUNTER_VALUE_REQ)
{
sendControlMessage(CHANGE_METER_COUNTER_VALUE_RESP, msgId);
}
else
{
qDebug() << "undefinded message name: " << msgName;
}
}
}
void MessageGenerator::updateErrorCode(int errorCode)
{
m_errorCode = errorCode;
}
void MessageGenerator::updateMsgStatus(bool status)
{
m_messageStatus = status;
}
void MessageGenerator::updateErrorDsc(const QString& description)
{
m_errorDsc = description;
}
void MessageGenerator::updateVideoAction(int videoAction)
{
m_videoAction = videoAction;
}
void MessageGenerator::connectToHost(const QString& ip, const QString& port)
{
QHostAddress host(ip);
if (host.setAddress(ip))
m_client->setIpAddr(host.toString());
else
qDebug() << "Wrong IP addr: " << ip;
bool isParseOk;
quint32 hostPort = port.toInt(&isParseOk);
if (isParseOk)
m_client->setPort(hostPort);
else
qDebug() << "Wrong port: " << port;
m_client->connect();
}
void MessageGenerator::disconnectFromHost()
{
m_client->disconnect();
}
void MessageGenerator::sendControlMessage(const QString& msgName, MessageId msgId)
{
m_client->sendMessage(createMessage(createHeader(msgName, CONTROL, msgId), createControlRespPayload()));
}
void MessageGenerator::sendMeterCounterValue(const QString& val, const QString& unit, bool isLateral)
{
static const QString METER_COUNTER_STATUS_IND = "METER_COUNTER_STATUS_IND";
m_client->sendMessage(createMessage(createHeader(METER_COUNTER_STATUS_IND, MONITORING, MESSAGE_ID),
createMeterCounterStatusIndPayload(val, unit, isLateral)));
}
static QJsonObject createInclinationPayload(QString value, const QString& unit)
{
return QJsonObject::fromVariantMap({{"value", value.replace(",", ".").toDouble()},
{"unit", unit}});
}
void MessageGenerator::sendInclinationValue(const QString &value, const QString &unit)
{
m_client->sendMessage(createMessage(createHeader("INCLINATION_VALUE_STATUS_IND", MONITORING, MESSAGE_ID),
createInclinationPayload(value, unit)));
}
void MessageGenerator::sendPerformVideoAction()
{
static const QString PERFORM_VIDEO_ACTION_REQ = "PERFORM_VIDEO_ACTION_REQ";
m_client->sendMessage(createMessage(createHeader(PERFORM_VIDEO_ACTION_REQ, VIDEO, MESSAGE_ID),
createPerformVideoActionRespPayload()));
}
void MessageGenerator::sendStartVideoStreaming(const QString& port)
{
static const QString START_VIDEO_STREAMING = "START_VIDEO_STREAMING_REQ";
m_client->sendMessage(createMessage(createHeader(START_VIDEO_STREAMING, VIDEO, MESSAGE_ID),
createStartVideoStreamingPayload(port.toInt())));
}
void MessageGenerator::sendObjectValue(const QString& obj, const QString& val)
{
static const QString OBJECT_STATUS_IND = "OBJECT_STATUS_IND";
m_client->sendMessage(createMessage(createHeader(OBJECT_STATUS_IND, MONITORING, MESSAGE_ID),
createObjectStatusIndPayload(obj, val)));
}
void MessageGenerator::sendMonitoringMessage(const QString& messageName, const QVariant& payload)
{
m_client->sendMessage(createMessage(createHeader(messageName, MONITORING, MESSAGE_ID),
QJsonObject::fromVariantMap(payload.toMap())));
}
void MessageGenerator::sendCreateFreeText(const QString& text,
int x,
int y,
int visibleTime,
const QString& textColor,
const QString& backColor)
{
QVariantMap payload{{
{"text", text},
{"x", x},
{"y", y},
{"visible", visibleTime},
{"textColor", textColor},
{"backColor", backColor}
}};
m_client->sendMessage(createMessage(createHeader("CREATE_FREE_TEXT", "OSD", MESSAGE_ID), QJsonObject::fromVariantMap(payload)));
}
void MessageGenerator::sendSettingInfoReq(const QString& setting)
{
m_client->sendMessage(createMessage(createHeader("SETTING_INFO_REQ", "SETTING", MESSAGE_ID), QJsonObject{{{"setting", setting}}}));
}
void MessageGenerator::sendSetupMessage(const QString& msg, const QVariant& payload)
{
m_client->sendMessage(createMessage(createHeader(msg, "SETUP", MESSAGE_ID), QJsonObject::fromVariantMap(payload.toMap())));
}
QByteArray MessageGenerator::createMessage(const QJsonObject& header, const QJsonObject& payload)
{
QJsonObject msg;
msg.insert("header", header);
msg.insert("payload", payload);
QJsonDocument doc(msg);
return doc.toJson(QJsonDocument::Compact);
}
QJsonObject MessageGenerator::createHeader(const QString& msgName, const QString& msgType, MessageId messageId)
{
QJsonObject header;
header.insert("messageId", QJsonValue::fromVariant(messageId));
header.insert("messageType", QJsonValue::fromVariant(msgType));
header.insert("messageName", QJsonValue::fromVariant(msgName));
return header;
}
QJsonObject MessageGenerator::createMeterCounterStatusIndPayload(QString val, const QString& unit, bool isLateral)
{
QJsonObject payload;
payload.insert("value", val.replace(",", ".").toDouble());
payload.insert("unit", unit);
payload.insert("isLateral", isLateral);
return payload;
}
QJsonObject MessageGenerator::createStartVideoStreamingPayload(int port) const
{
return QJsonObject{
{"port", port}
};
}
QJsonObject MessageGenerator::createObjectStatusIndPayload(const QString& obj, const QString& val)
{
QJsonObject payload;
payload.insert("value", QJsonValue::fromVariant(getValueForObject(obj.simplified(), val)));
payload.insert("object", QJsonValue::fromVariant(obj.simplified()));
return payload;
}
QVariant MessageGenerator::getValueForObject(const QString& obj, const QString& val)
{
if (obj.contains("spinbox", Qt::CaseInsensitive) ||
obj.contains("levelIndicator", Qt::CaseInsensitive))
return val.toInt();
else if (obj.contains("switch", Qt::CaseInsensitive))
return (val == "true" ? true : false);
return val;
}
QJsonObject MessageGenerator::createControlRespPayload()
{
QJsonObject payload;
payload.insert("status", QJsonValue::fromVariant(m_messageStatus));
payload.insert("errorCode", QJsonValue::fromVariant(m_errorCode));
payload.insert("error", QJsonValue::fromVariant(m_errorDsc));
return payload;
}
QJsonObject MessageGenerator::createPerformVideoActionRespPayload()
{
return QJsonObject{
{"videoAction", m_videoAction}
};
}
void MessageGenerator::sendCurrentVersion()
{
m_client->sendMessage(createMessage(createHeader("CHOOSE_API_VERSION", SETUP, MESSAGE_ID),
QJsonObject::fromVariantMap({{"value", property("ApiVersion")}})));
}
void MessageGenerator::sendMessage(const QString& msg)
{
m_client->sendMessage(msg.toLatin1());
}