123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724 |
- //#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() << "Mock 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::getInit(QWebSocket *pSender, QJsonObject obj)
- {
- qDebug() << "Build with Slot";
- // 假接口,指针写法
- QJsonObject *InitState = new QJsonObject();
- InitState->insert("InitState", 100);
- QJsonObject *ret = new QJsonObject();
- ret->insert("url", obj.value("url"));
- ret->insert("data", *InitState);
- delete InitState;
- // 真实接口,局部变量
- // QJsonObject InitState
- // {
- // {"InitState",100*(int)this->grman->common_inter->InitState}
- // };
- // QJsonObject *ret = new QJsonObject(
- // {
- // {"url", obj.value("url")},
- // {"data", InitState}
- // });
- returnMessage(pSender, ret);
- }
- void WSServer::getLeadPost(QWebSocket *pSender, QJsonObject obj)
- {
- QJsonArray *LeadPost = new QJsonArray();
- QJsonObject *CurrentLeadPost;
- CurrentLeadPost = new QJsonObject();
- CurrentLeadPost->insert("time", "20:20:01");
- CurrentLeadPost->insert("GroupID", "编队1");
- CurrentLeadPost->insert("LeaderID", "1001");
- CurrentLeadPost->insert("Abstract", "发现雷达");
- LeadPost->append(*CurrentLeadPost);
- delete CurrentLeadPost;
- CurrentLeadPost = new QJsonObject();
- CurrentLeadPost->insert("time", "20:20:30");
- CurrentLeadPost->insert("GroupID", "编队1");
- CurrentLeadPost->insert("LeaderID", "1002");
- CurrentLeadPost->insert("Abstract", "请求施加干扰");
- LeadPost->append(*CurrentLeadPost);
- delete CurrentLeadPost;
- CurrentLeadPost = new QJsonObject();
- CurrentLeadPost->insert("time", "20:20:40");
- CurrentLeadPost->insert("GroupID", "编队1");
- CurrentLeadPost->insert("LeaderID", "1002");
- CurrentLeadPost->insert("Abstract", "干扰样式生成");
- LeadPost->append(*CurrentLeadPost);
- delete CurrentLeadPost;
- CurrentLeadPost = new QJsonObject();
- CurrentLeadPost->insert("time", "20:21:00");
- CurrentLeadPost->insert("GroupID", "编队1");
- CurrentLeadPost->insert("LeaderID", "1003");
- CurrentLeadPost->insert("Abstract", "干扰信号发射");
- LeadPost->append(*CurrentLeadPost);
- delete CurrentLeadPost;
- QJsonObject *ret = new QJsonObject();
- ret->insert("url", obj.value("url"));
- ret->insert("PlatformID", obj.value("PlatformID"));
- ret->insert("data", *LeadPost);
- delete LeadPost;
- returnMessage(pSender, ret);
- }
- void WSServer::getTaskPlan(QWebSocket *pSender, QJsonObject obj)
- {
- QJsonObject *TaskPlan = new QJsonObject();
- QJsonObject *CurrentAirLine;
- QJsonObject *CurrentTaskPlan;
- CurrentAirLine = new QJsonObject;
- CurrentAirLine->insert("TargetArea", 3);
- CurrentAirLine->insert("Longitude", 123);
- CurrentAirLine->insert("Longitude", 27);
- CurrentAirLine->insert("Radius", 300000);
- CurrentTaskPlan = new QJsonObject;
- CurrentTaskPlan->insert("TargetArea", 2);
- CurrentTaskPlan->insert("ThrowGas", 2);
- CurrentTaskPlan->insert("ThrowBT", 2);
- TaskPlan->insert("Airline", *CurrentAirLine);
- TaskPlan->insert("TaskPlan", *CurrentTaskPlan);
- QJsonObject *ret = new QJsonObject();
- ret->insert("url", obj.value("url"));
- ret->insert("PlatformID", obj.value("PlatformID"));
- ret->insert("data", *TaskPlan);
- delete TaskPlan;
- delete CurrentAirLine;
- delete CurrentTaskPlan;
- returnMessage(pSender, ret);
- }
- void WSServer::getGlobalMap(QWebSocket *pSender, QJsonObject obj)
- {
- QJsonArray *alies = new QJsonArray();
- QJsonArray *enemys = new QJsonArray();
- QJsonArray *temparray;
- QJsonObject *tempobj;
- {
- temparray = new QJsonArray();
- tempobj = new QJsonObject();
- tempobj->insert("name", "80xxA电子侦察船_1");
- temparray->append(120.446), temparray->append(24.8902), temparray->append(100);
- tempobj->insert("value", *temparray);
- // alies->insert("80xxA电子侦察船_1"), QJsonValue(*temparray));
- alies->append(*tempobj);
- delete temparray;
- delete tempobj;
- temparray = new QJsonArray();
- tempobj = new QJsonObject();
- tempobj->insert("name", "导弹发射架_1");
- temparray->append(117.525), temparray->append(25.826), temparray->append(100);
- tempobj->insert("value", *temparray);
- alies->append(*tempobj);
- delete temparray;
- delete tempobj;
- temparray = new QJsonArray();
- tempobj = new QJsonObject();
- tempobj->insert("name", "机场_1");
- temparray->append(119.271), temparray->append(26.26), temparray->append(100);
- tempobj->insert("value", *temparray);
- alies->append(*tempobj);
- delete temparray;
- delete tempobj;
- temparray = new QJsonArray();
- tempobj = new QJsonObject();
- tempobj->insert("name", "预警机_1");
- temparray->append(120.288), temparray->append(25.5734), temparray->append(100);
- tempobj->insert("value", *temparray);
- alies->append(*tempobj);
- delete temparray;
- delete tempobj;
- temparray = new QJsonArray();
- tempobj = new QJsonObject();
- tempobj->insert("name", "运侦8_1");
- temparray->append(117.525), temparray->append(25.826), temparray->append(100);
- tempobj->insert("value", *temparray);
- alies->append(*tempobj);
- delete temparray;
- delete tempobj;
- }
- {
- temparray = new QJsonArray();
- tempobj = new QJsonObject();
- tempobj->insert("name", "宙斯盾舰");
- temparray->append(123.528), temparray->append(25.2951), temparray->append(100);
- tempobj->insert("value",*temparray);
- enemys->append(*tempobj);
- delete temparray;
- delete tempobj;
- }
- QJsonObject GlobalMap
- {
- {"friend", QJsonValue(*alies)},
- {"enemy", QJsonValue(*enemys)}
- };
- QJsonObject *ret = new QJsonObject(
- {
- {"url", obj.value("url")},
- {"data", GlobalMap}
- });
- returnMessage(pSender, ret);
- delete alies;
- delete enemys;
- }
- void WSServer::getPlatformTable(QWebSocket *pSender, QJsonObject obj)
- {
- 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::getRadarInstances(QWebSocket *pSender, QJsonObject obj)
- {
- int nowPlatform = obj.value("PlatformID").toInt();
- QJsonArray RadarInstances;
- if (1)
- {
- QJsonObject tempRadar
- {
- {"instanceID",20},
- {"instanceName","雷达_1"},
- {"instanceState","开机"}
- };
- RadarInstances.append(tempRadar);
- }
- QJsonObject *ret = new QJsonObject();
- ret->insert("url", obj.value("url"));
- ret->insert("PlatformID", obj.value("PlatformID"));
- ret->insert("data",QJsonValue(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"},
- {"instanceState","开机"}
- };
- ECMInstances.append(tempECM);
- }
- QJsonObject *ret = new QJsonObject();
- ret->insert("url", obj.value("url"));
- ret->insert("PlatformID", obj.value("PlatformID"));
- ret->insert("data",QJsonValue(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);
- }
- }
- void WSServer::returnMessage(QWebSocket *pSender, QJsonObject *obj)
- {
- qDebug() << "return message:";
- qDebug() << QString(QJsonDocument(*obj).toJson());
- pSender->sendTextMessage(QString(QJsonDocument(*obj).toJson()));
- delete obj;
- }
|