#pragma execution_character_set("utf-8") #include "wsserver.h" WSServer::WSServer(QObject *parent, quint16 port) : QObject(parent) { m_pWebSocketServer = new QWebSocketServer(QStringLiteral("Mock Server"), QWebSocketServer::NonSecureMode, this); if (m_pWebSocketServer->listen(QHostAddress::Any, port)) { qDebug() << "GR Server listening on port" << port; connect(m_pWebSocketServer, &QWebSocketServer::newConnection, this, &WSServer::onNewConnection); } } WSServer::~WSServer() { m_pWebSocketServer->close(); qDeleteAll(m_clients.begin(), m_clients.end()); } void WSServer::onNewConnection() { QWebSocket *pSocket = m_pWebSocketServer->nextPendingConnection(); connect(pSocket, &QWebSocket::textMessageReceived, this, &WSServer::processMessage); connect(pSocket, &QWebSocket::disconnected, this, &WSServer::socketDisconnected); m_clients << pSocket; } QJsonObject WSServer::processJsonObj(QJsonObject objin) { QJsonObject ret; QStringList Keys = objin.keys(); for (auto k : Keys) { QJsonValue v = objin.value(k); if (k == "url") { ret.insert(k, v); } else if (k == "InstanceID") { ret.insert(k, v); } else if (k == "data") { if (v.isObject()) { QJsonObject Props = v.toObject(); QStringList Propkeys = Props.keys(); if (Propkeys.contains("InstanceID")) { ret.insert("InstanceID", Props.take("InstanceID")); } if (Propkeys.contains("PlatformID")) { ret.insert("PlatformID", Props.take("PlatformID")); } ret.insert("data", Props); } } // if (v.isArray()) // { // for (auto i : v.toArray()) // { // if (i.isString()) // { // objout->insert(i.toString(),QJsonValue()); // } // } // } // else if (v.isObject()) // { // QJsonObject Props = v.toObject(); // QStringList Propkeys = Props.keys(); // for (auto propkey: Propkeys) // { // objout->insert(propkey,Props.value(k)); // } // } // 不想打印 // if (v.isObject()) // { // processJsonObj(v.toObject(),objout); // } // 打印调试信息 if (v.isDouble()) { qDebug() << k << ": " << v.toDouble(); } else if (v.isString()) { qDebug() << k << ": " << v.toString(); } else if (v.isObject()) { qDebug() << k << "{"; // processJsonObj(v.toObject(),objout); QJsonObject objout = processJsonObj(v.toObject()); qDebug() << "}"; } else if (v.isArray()) { QJsonArray qarray = v.toArray(); qDebug() << k << "["; for (auto i : qarray) { if (i.isString()) { qDebug() << i.toString(); } } qDebug() << "]"; } } // qDebug() << QString(QJsonDocument(*objout).toJson()); return ret; } void WSServer::processMessage(QString message) { // 获取客户端 QWebSocket *pSender = qobject_cast(sender()); // 以Json格式解码message QByteArray *ByteMessage = new QByteArray(); ByteMessage->append(message); QJsonParseError *error = new QJsonParseError(); QJsonDocument IncomingJson = QJsonDocument::fromJson(*ByteMessage, error); if (error->error == QJsonParseError::NoError) { if (IncomingJson.isObject()) { // 解析message QJsonObject IncomingObj = IncomingJson.object(); QJsonObject msgobj = this->processJsonObj(IncomingObj); QStringList props = msgobj.keys(); QString url = msgobj.value("url").toString(); // qDebug() << QString(QJsonDocument(msgobj).toJson()); qDebug() << "url:" << url; // 判断请求 if (url == "getInit") { getInit(pSender, msgobj); } else if (url == "getPlatformTable") { getPlatformTable(pSender, msgobj); } else if (url == "getGlobalMap" || url == "getLocalMap" || url == "getRemoteMap" || url == "getCombineMap") { getGlobalMap(pSender, msgobj); } else if (url == "getJammingSig") { getJammingSig(pSender, msgobj); } else { // 其他接口都需要带平台ID if (msgobj.contains("PlatformID") == false) { QJsonObject *ret = new QJsonObject(); ret->insert("data", QJsonValue()); // 要是没有平台ID,返回空数据 msgobj.insert("data", QJsonValue()); if (msgobj.contains("url") == true) { ret->insert("url", msgobj.value("url")); } returnMessage(pSender, ret); } else if (url == "getRadarInstances") { getRadarInstances(pSender, msgobj); } else if (url == "getRadarParams" && msgobj.contains("InstanceID")) { getRadarParams(pSender, msgobj); } else if (url == "getThaadParams" && msgobj.contains("InstanceID")) { getRadarParams(pSender, msgobj); } else if (url == "getJammingInstances") { getJammingInstances(pSender, msgobj); } else if (url == "getJammingParams" && msgobj.contains("InstanceID")) { getJammingParams(pSender, msgobj); } else if (url == "setJammingStyle" && msgobj.contains("InstanceID")) { setJammingStyle(pSender, msgobj); } else if (url == "getNavParams") { // getNavParams(msgobj); } else if (url == "setFreePost") { // setFreePost(msgobj); } else if (url == "getLeadPost") { getLeadPost(pSender, msgobj); } else if (url == "getTaskPlan") { getTaskPlan(pSender, msgobj); } } // qDebug() << "return message:"; // qDebug() << QString(QJsonDocument(*msgobj).toJson()); // QWebSocket *pSender = qobject_cast(sender()); // pSender->sendTextMessage(QString(QJsonDocument(*msgobj).toJson())); // for (QWebSocket *pClient : qAsConst(m_clients)) { // QJsonDocument *msgdoc = new QJsonDocument(*msgobj); // if (pClient == pSender) //don't echo message back to sender // { // QJsonValue msg = new QJsonValue(message); // pClient->sendTextMessage(QString(msgdoc->toJson())); // } // qDebug() << QString(msgdoc->toJson()); } } } void WSServer::socketDisconnected() { QWebSocket *pClient = qobject_cast(sender()); if (pClient) { m_clients.removeAll(pClient); pClient->deleteLater(); } } void WSServer::returnMessage(QWebSocket *pSender, QJsonObject *obj) { // qDebug() << "return message:"; // qDebug() << QString(QJsonDocument(*obj).toJson()); pSender->sendTextMessage(QString(QJsonDocument(*obj).toJson())); delete obj; } // --------------------初始化状态接口--------------------------------- void WSServer::getInit(QWebSocket *pSender, QJsonObject obj) { qDebug() << "网页请求初始化状态"; // 假接口 QJsonObject InitState{ {"InitState", 100}}; // 真实接口 // QJsonObject InitState // { // {"InitState",100*(int)this->zcman->common_inter->InitState} // }; QJsonObject *ret = new QJsonObject( {{"url", obj.value("url")}, {"data", InitState}}); returnMessage(pSender, ret); } // --------------------平台表接口---------------------------------------------- void WSServer::getPlatformTable(QWebSocket *pSender, QJsonObject obj) { qDebug() << "网页请求平台信息"; QJsonArray Platforms; // 假接口 QJsonObject SinglePlatform[]{ {{"platformID", 8013}, {"platformName", "干扰机_1"}}, {{"platformID", 8014}, {"platformName", "干扰机_2"}}, }; for (auto i : SinglePlatform) { Platforms.append(i); } // 真实接口 // for (auto i : this->grman->grtaskplats) // { // QJsonObject SinglePlatform // { // {"platformID", (int)i->PlatID}, // {"platformName", QString::fromLocal8Bit(i->PlatName.c_str())} // }; // Platforms.append(SinglePlatform); // } // 构造返回对象 QJsonObject *ret = new QJsonObject( {{"url", obj.value("url")}, {"data", Platforms}}); returnMessage(pSender, ret); } // --------------------------任务管理显控接口------------------------------ void WSServer::getLeadPost(QWebSocket *pSender, QJsonObject obj) { qDebug() << "网页请求指挥电文"; // 假接口 QJsonObject CurrentLeadPost1{ {"time", "20:20:01"}, {"GroupID", "0001"}, {"LeaderID", "8015"}, {"Abstract", "开始侦察"}}; QJsonObject CurrentLeadPost2{ {"time", "20:20:30"}, {"GroupID", "0001"}, {"LeaderID", "8015"}, {"Abstract", "发现辐射源"}}; QJsonObject CurrentLeadPost3{ {"time", "20:20:40"}, {"GroupID", "0001"}, {"LeaderID", "8015"}, {"Abstract", "请求干扰"}}; QJsonObject CurrentLeadPost4{ {"time", "20:20:01"}, {"GroupID", "0003"}, {"LeaderID", "8014"}, {"Abstract", "开始干扰"}}; QJsonArray LeadPost = { CurrentLeadPost1, CurrentLeadPost2, CurrentLeadPost3, CurrentLeadPost4}; // 真实接口 // 待定 // 构造返回对象 QJsonObject *ret = new QJsonObject( {{"url", obj.value("url")}, {"PlatformID", obj.value("PlatformID")}, {"data", LeadPost}}); returnMessage(pSender, ret); } void WSServer::getTaskPlan(QWebSocket *pSender, QJsonObject obj) { qDebug() << "网页请求任务计划"; // 假接口 QJsonObject CurrentAirLine{ {"TargetArea", 3}, {"Longitude", 123}, {"Longitude", 27}, {"Radius", 300000}}; QJsonObject CurrentTaskPlan{ {"TargetArea", 2}, {"ThrowGas", 2}, {"ThrowBT", 2}}; QJsonObject TaskPlan{ {"Airline", CurrentAirLine}, {"TaskPlan", CurrentTaskPlan}}; // 真实接口 待定 // 构造返回对象 QJsonObject *ret = new QJsonObject( {{"url", obj.value("url")}, {"PlatformID", obj.value("PlatformID")}, {"data", TaskPlan}}); returnMessage(pSender, ret); } // --------------------------态势显控接口--------------------------------- void WSServer::getLocalMap(QWebSocket *pSender, QJsonObject obj) { qDebug() << "网页请求本地态势"; // 来源1:导航位置信息 // 来源2:ESM数据 } void WSServer::getRemoteMap(QWebSocket *pSender, QJsonObject obj) { qDebug() << "网页请求远端态势"; // 来源:保存下来的平台参数情报和航迹情报 } void WSServer::getCombineMap(QWebSocket *pSender, QJsonObject obj) { qDebug() << "网页请求融合态势"; // 来源1:导航位置信息 // 来源2:ESM数据 // 来源3:保存下来的平台参数情报和航迹情报 } void WSServer::getGlobalMap(QWebSocket *pSender, QJsonObject obj) { qDebug() << "网页请求全局态势"; // 来源:支撑平台的平台位置信息 // 假接口 QJsonArray allyNames = { "导弹发射架_1", "机场_1", "预警机_1", "运侦8_1"}; QJsonArray allyPos[] = { {117.525, 25.826, 100}, {119.271, 26.26, 100}, {120.288, 25.5734, 100}, {117.525, 25.826, 100}}; QJsonArray enemyNames = { "80xxA电子侦察船_1", "宙斯盾舰"}; QJsonArray enemyPos[] = { {120.446, 24.8902, 100}, {123.528, 25.2951, 100}}; QJsonArray ally; QJsonArray enemy; for (int i = 0; i < allyNames.count(); i++) { QJsonObject temp{ {"name", allyNames.at(i)}, {"value", allyPos[i]}}; ally.append(temp); } for (int i = 0; i < enemyNames.count(); i++) { QJsonObject temp{ {"name", enemyNames.at(i)}, {"value", enemyPos[i]}}; enemy.append(temp); } // 真实接口 // QJsonArray ally; // QJsonArray enemy; // for (auto i : this->zcman->common_inter->PlatformMovement) // { // QJsonArray currentPos = { // i.second.PlatLon, // i.second.PlatLat, // 100 // }; // QJsonObject currentPlat // { // {"name", QString::fromLocal8Bit(i.second.PlatName.c_str())}, // {"value", currentPos} // }; // // 判断敌我 // if (i.second.PlatIdentification == 2) // { // ally.append(currentPlat); // } // else if (i.second.PlatIdentification == 1) // { // enemy.append(currentPlat); // } // } // 构造返回对象 QJsonObject GlobalMap{ {"friend", QJsonValue(ally)}, {"enemy", QJsonValue(enemy)}}; QJsonObject *ret = new QJsonObject({{"url", obj.value("url")}, {"data", GlobalMap}}); returnMessage(pSender, ret); } void WSServer::getRadarInstances(QWebSocket *pSender, QJsonObject obj) { int nowPlatform = obj.value("PlatformID").toInt(); QJsonArray RadarInstances; // 假接口 if (1) { QJsonObject tempRadar{ {"instanceID", 20}, {"instanceName", "雷达_1"}}; RadarInstances.append(tempRadar); } // 真实接口:待开发 // 构造返回对象 QJsonObject *ret = new QJsonObject({{"url", obj.value("url")}, {"PlatformID", obj.value("PlatformID")}, {"data", RadarInstances}}); returnMessage(pSender, ret); } void WSServer::getRadarParams(QWebSocket *pSender, QJsonObject obj) { int nowInstance = obj.value("InstanceID").toString().toInt(); QJsonObject *ret = new QJsonObject({{"url", obj.value("url")}, {"PlatformID", obj.value("PlatformID")}, {"InstanceID", obj.value("InstanceID")}, {"data", QJsonValue()}}); returnMessage(pSender, ret); } void WSServer::getJammingInstances(QWebSocket *pSender, QJsonObject obj) { QJsonArray ECMInstances; // 假接口 if (1) { QJsonObject tempECM{ {"instanceID", 20}, {"instanceName", "ECM_1"}}; ECMInstances.append(tempECM); } // 真实接口:待开发 // 构造返回对象 QJsonObject *ret = new QJsonObject({{"url", obj.value("url")}, {"PlatformID", obj.value("PlatformID")}, {"data", ECMInstances}}); returnMessage(pSender, ret); } void WSServer::getJammingParams(QWebSocket *pSender, QJsonObject obj) { QJsonArray nowParams = {}; QJsonObject *ret = new QJsonObject(); ret->insert("url", obj.value("url")); ret->insert("PlatformID", obj.value("PlatformID")); ret->insert("InstanceID", obj.value("InstanceID")); ret->insert("data", QJsonValue(nowParams)); returnMessage(pSender, ret); } void WSServer::setJammingStyle(QWebSocket *pSender, QJsonObject obj) { // PlatformID和InstanceID是字符串 int nowPlatform = obj.value("PlatformID").toString().toInt(); int nowInstance = obj.value("InstanceID").toString().toInt(); QJsonObject *ret = new QJsonObject({{"url", obj.value("url")}, {"PlatformID", obj.value("PlatformID")}, {"InstanceID", obj.value("InstanceID")}, {"data", QJsonValue()}}); returnMessage(pSender, ret); } void WSServer::getJammingSig(QWebSocket *pSender, QJsonObject obj) { QJsonObject dat = obj.value("data").toObject(); if (dat.contains("order") == false) { QJsonObject *ret = new QJsonObject({{"order", 0}}); returnMessage(pSender, ret); } else { int order = dat.value("order").toInt(); QJsonObject *ret = new QJsonObject({{"order", order}}); if (order == 1) { int lines = 0; double N = 10000; double B_n = 2e6; double T_s = 100e-6; double m_fe = 1; double f_0 = 10e6; double A = 1; double f_s = 100e6; coder::array S; coder::array S0; FMjamming_H(N, B_n, f_s, T_s, m_fe, A, f_0, S, S0); ret->insert("size", S.size(1)); QJsonArray data; for (int i = 0; i < S.size(1); i++) { data.append(S[i]); } ret->insert("data", data); } else if (order == 2) { int lines = 0; double N = 10000; double B_n = 2e6; double m_A = 1; double f_0 = 10e6; double f_s = 100e6; coder::array S; AMJamming_H(N, f_s, B_n, f_0, m_A, S); ret->insert("size", S.size(1)); QJsonArray data; for (int i = 0; i < S.size(1); i++) { data.append(S[i]); } ret->insert("data", data); } else if (order == 3) { int lines = 0; double N = 1024; double B_n = 2e6; double A = 1; double f_0 = 10e6; double f_s = 100e6; coder::array S; jamming_H(N, f_s, A, B_n, f_0, S); double sig[1024]; QJsonArray data; for (int i = 0; i < S.size(1); i++) { sig[i] = S[i].re; data.append(sig[i]); } ret->insert("size", 1024); ret->insert("data", data); } else if (order == 4) { int lines = 0; double fd = 200 / 3e2; double N = 1; double ts = 1 / 1e7; double Tr = 5e-4; double t_r = 2 / 1e4; double tau = 30e-6; double fo = 1e6; double k = 1e12 / 6; double A = 1; double dlt_t = 25e-6; double R = 30e3; double fs = 1e7; coder::array S; coder::array S1; coder::array SS; coder::array S_disturb; coder::array S3; false_target_distance(fd, N, ts, Tr, t_r, tau, fo, k, A, dlt_t, R, fs, S, S1, SS, S_disturb, S3); ret->insert("size", S3.size(1)); QJsonArray data; for (int i = 0; i < S3.size(1); i++) { data.append(S3[i]); } ret->insert("data", data); } else if (order == 5) { int lines = 0; double fd = 4; double N = 1; double ts = 1e-7; double Tr = 5e-4; double t_r = 20e-5; double tau = 30e-6; double fo = 1e6; double k = 5e6 / 30e-6; double V = 600; coder::array S; false_speed(fd, fo, ts, tau, Tr, k, t_r, V, N, S); ret->insert("size", S.size(1)); QJsonArray data; for (int i = 0; i < S.size(1); i++) { data.append(S[i]); } ret->insert("data", data); } else if (order == 6) { int lines = 0; double fd = 0.2; double N = 1; double dlt = 25e-6; double S[10500]; S_jam(fd, dlt, N, S); ret->insert("size", 10500); QJsonArray data; for (int i = 0; i < 10500; i++) { data.append(S[i]); } ret->insert("data", data); } returnMessage(pSender, ret); } }