123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718 |
- #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<QWebSocket *>(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<QWebSocket *>(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<QWebSocket *>(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<double, 2U> S;
- coder::array<double, 2U> 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<double, 2U> 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<creal_T, 2U> 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<double, 2U> S;
- coder::array<double, 2U> S1;
- coder::array<double, 2U> SS;
- coder::array<double, 2U> S_disturb;
- coder::array<double, 2U> 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<double, 2U> 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);
- }
- }
|