//#pragma execution_character_set("utf-8") #include "wsserver.h" WSServer::WSServer(QObject *parent, quint16 port) : QObject(parent) { EsmworkPara.uRrPowerOn=1; EsmworkPara.uRrWorkMode=0; EsmworkPara.dScanAzCentDeg=0; EsmworkPara.dScanAzWidthDeg=5; EsmworkPara.dScanElCentDeg= 0; EsmworkPara.uLineNumPerFrame = 20; EsmworkPara.fAntScanVel = 1; EsmworkPara.fFreqScanVel = 100; EsmworkPara.fTecBeginFreqMhz = 500; EsmworkPara.fTecEndFreqMhz = 10000; m_pWebSocketServer = new QWebSocketServer(QStringLiteral("ZC Server"), QWebSocketServer::NonSecureMode, this); if (m_pWebSocketServer->listen(QHostAddress::Any, port)) { qDebug() << "ZC 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 objout; QStringList Keys = objin.keys(); for (auto k : Keys) { QJsonValue v = objin.value(k); if (k == "url") { objout.insert(k, v); } else if (k == "InstanceID") { objout.insert(k, v); } else if (k == "data") { if (v.isObject()) { QJsonObject Props = v.toObject(); QStringList Propkeys = Props.keys(); QJsonObject Dataout; for (auto propkey : Propkeys) { if (propkey.compare("InstanceID")==0) { objout.insert("InstanceID", Props.value("InstanceID")); } else if (propkey.compare("PlatformID")==0) { objout.insert("PlatformID", Props.value("PlatformID")); } else { Dataout.insert(propkey, Props.value(propkey)); } } if (!Dataout.isEmpty()) objout.insert("data", Dataout); } } // 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 << "{"; 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() << "]"; } } return objout; //qDebug() << QString(QJsonDocument(*objout).toJson()); } void WSServer::processMessage(QString message) { // 以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()) { QWebSocket *pSender = qobject_cast(sender()); // 解析message QJsonObject IncomingObj = IncomingJson.object(); QJsonObject msgobj = this->processJsonObj(IncomingObj); QStringList props = msgobj.keys(); QString url = msgobj.value("url").toString(); qDebug() << "url:" << url; // 判断请求 if (url == "getInit") { getInit(pSender,msgobj); } else if (url == "getPlatformTable") { getPlatformTable(pSender,msgobj); } else if (url == "getGlobalMap") { getGlobalMap(pSender,msgobj); } else { // 其他接口都需要带平台ID if (msgobj.contains("PlatformID")==false || msgobj.value("PlatformID") == QJsonValue()) { // 要是没有平台ID,返回空数据 QJsonObject *ret = new QJsonObject( { {"url",url}, {"data",""} }); returnMessage(pSender, ret); } // 态势接口 else if (url == "getLocalMap" || url == "getRemoteMap" || url == "getCombineMap") { } // ESM接口 else if (url == "getESMInstances") { getESMInstances(pSender,msgobj); } else if (url == "setESMParams") { setESMParams(pSender,msgobj); } else if (url == "getESMParams") { getESMParams(pSender,msgobj); } else if (url == "getESMStatus") { getESMStatus(pSender,msgobj); } else if (url == "getESMData") { getESMData(pSender,msgobj); } else if (url == "getNavParams") { getNavParams(pSender,msgobj); } else if (url == "setFreePost") { setFreePost(pSender,msgobj); } else if (url == "getLeadPost") { getLeadPost(pSender,msgobj); } else if (url == "getTaskPlan") { getTaskPlan(pSender,msgobj); } } } } } void WSServer::socketDisconnected() { QWebSocket *pClient = qobject_cast(sender()); if (pClient) { m_clients.removeAll(pClient); pClient->deleteLater(); } } void WSServer::returnMessage(QWebSocket *pSender, QJsonObject *jsonObj) { // qDebug() << "return message:"; // qDebug() << QString(QJsonDocument(*jsonObj).toJson()); pSender->sendTextMessage(QString(QJsonDocument(*jsonObj).toJson())); delete jsonObj; } // --------------------初始化状态接口--------------------------------- 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", 8015}, {"platformName", "侦察机_1"} }, { {"platformID", 8016}, {"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::getESMInstances(QWebSocket *pSender, QJsonObject obj) { qDebug() << "网页请求ESM传感器实例"; // 假接口 QJsonArray *Instances = new QJsonArray(); QJsonObject *SingleInstance; if (true) { SingleInstance = new QJsonObject(); SingleInstance->insert("instanceID", 62); SingleInstance->insert("instanceName", "信号侦察_1"); Instances->append(*SingleInstance); delete SingleInstance; } QJsonObject *ret = new QJsonObject({ {"url", obj.value("url")}, {"PlatformID", obj.value("PlatformID")}, {"data", *Instances} }); returnMessage(pSender, ret); delete Instances; } void WSServer::setESMParams(QWebSocket *pSender, QJsonObject obj) { qDebug() << "网页修改ESM参数"; // 假接口 int PlatformID = obj.value("PlatformID").toInt(); int InstanceID = obj.value("InstanceID").toInt(); QJsonObject data = obj.value("data").toObject(); if (data.contains("uRrPoweron")) { EsmworkPara.uRrPowerOn = data.value("uRrPoweron").toBool()?1:0; } if (data.contains("uRrWorkMode")) { EsmworkPara.uRrWorkMode = data.value("uRrWorkMode").toInt(); } if (data.contains("uLineNumPerFrame")) { EsmworkPara.uLineNumPerFrame = data.value("uLineNumPerFrame").toInt(); } if (data.contains("dScanAzCentDeg")) { EsmworkPara.dScanAzCentDeg = data.value("dScanAzCentDeg").toDouble(); } if (data.contains("dScanAzWidthDeg")) { EsmworkPara.dScanAzWidthDeg = data.value("dScanAzWidthDeg").toDouble(); } if (data.contains("dScanElCentDeg")) { EsmworkPara.dScanElCentDeg = data.value("dScanElCentDeg").toDouble(); } if (data.contains("fAntScanVel")) { EsmworkPara.fAntScanVel = data.value("fAntScanVel").toDouble(); } if (data.contains("fFreqScanVel")) { EsmworkPara.fFreqScanVel = data.value("fFreqScanVel").toDouble(); } if (data.contains("fFreqMode")) { int mode = data.value("fFreqMode").toInt(); if (mode == 1) { EsmworkPara.fTecBeginFreqMhz = 500; EsmworkPara.fTecEndFreqMhz = 2000; } else if (mode == 2) { EsmworkPara.fTecBeginFreqMhz = 2000; EsmworkPara.fTecEndFreqMhz = 5000; } else if (mode == 3) { EsmworkPara.fTecBeginFreqMhz = 5000; EsmworkPara.fTecEndFreqMhz = 18000; } } QJsonObject *ret = new QJsonObject({ {"url", obj.value("url")}, {"PlatformID", obj.value("PlatformID")}, {"data", 1} }); returnMessage(pSender, ret); } void WSServer::getESMParams(QWebSocket *pSender, QJsonObject obj) { qDebug() << "网页请求ESM参数"; // 假接口 int InstanceID = obj.value("InstanceID").toInt(); if (true) { QJsonObject ESMProps[] { //fBeamAzWidth { {"key","方位波束宽度"}, {"value", 2} }, //fBeamElWidth { {"key","俯仰波束宽度"}, {"value", 2} }, //fTecMinFreqMhz { {"key","侦收最小频率"}, {"value", 2e3} }, //fTecMaxFreqMhz { {"key","侦收最大频率"}, {"value", 18e3} }, //fInstantPageWidMhz { {"key","瞬时侦察工作频带带宽"}, {"value", 500} }, //ulChanNum { {"key","信道个数"}, {"value", 64} }, //fTecMinPW { {"key","侦收最小脉冲宽度"}, {"value", 0.5} }, //fTecMaxPW { {"key","侦收最大脉冲宽度"}, {"value", 10} }, //fTecPriMean { {"key","侦收平均重复周期"}, {"value", 40} }, //ulTecTrNumMean { {"key","侦收平均脉冲个数"}, {"value", 5} }, //fMinSnrdB { {"key","最小信噪比"}, {"value", 0.5} }, //fSysRcvLoss { {"key","接收内部系统损耗"}, {"value", 5} }, //fFreqAccuracyReq { {"key","频率测量精度要求"}, {"value", 100} }, //uRrPowerOn { {"key","工作状态"}, {"value",(int)EsmworkPara.uRrPowerOn} }, //uRrWorkMode { {"key","工作模式"}, {"value",(int)EsmworkPara.uRrWorkMode} }, //dScanAzCentDeg { {"key","侦察方位中心"}, {"value",EsmworkPara.dScanAzCentDeg} }, //dScanAzWidthDeg { {"key","侦察方位范围"}, {"value",EsmworkPara.dScanAzWidthDeg} }, //dScanElCentDeg { {"key","侦察俯仰中心"}, {"value",EsmworkPara.dScanElCentDeg} }, //uLineNumPerFrame { {"key","景幅行数"}, {"value",(int)EsmworkPara.uLineNumPerFrame} }, //fAntScanVel { {"key","天线扫描速度"}, {"value",EsmworkPara.fAntScanVel} }, //fFreqScanVel { {"key","频率扫描速度"}, {"value",EsmworkPara.fFreqScanVel} }, //fTecBeginFreqMhz { {"key","侦收起始频率"}, {"value",EsmworkPara.fTecBeginFreqMhz} }, //fTecEndFreqMhz { {"key","侦收终止频率"}, {"value",EsmworkPara.fTecEndFreqMhz} } }; QJsonArray ESMInitParams, ESMCtrlParams; for (int i =0;i<13;i++) { ESMInitParams.append(ESMProps[i]); } for (int i =13;i<23;i++) { ESMCtrlParams.append(ESMProps[i]); } QJsonObject ESMParams { {"InitParams", ESMInitParams}, {"CtrlParams", ESMCtrlParams} }; QJsonObject *ret = new QJsonObject({ {"url", obj.value("url")}, {"PlatformID", obj.value("PlatformID")}, {"InstanceID", InstanceID}, {"data", ESMParams} }); returnMessage(pSender, ret); } } void WSServer::getESMStatus(QWebSocket *pSender, QJsonObject obj) { qDebug() << "网页请求初始化状态"; // 假接口 QJsonArray ESMStatus; QJsonObject ESMState[]{ { {"key","扫描行号"}, {"value",0} }, { {"key","扫描行状态"}, {"value",0} }, { {"key","侦察频率中心"}, {"value",0} }, { {"key","侦察频率带宽"}, {"value",0} }, { {"key","天线系波束方位"}, {"value",0} }, { {"key","天线系波束俯仰"}, {"value",0} }, { {"key","天线系波束方位"}, {"value",0} }, { {"key","天线系波束方位"}, {"value",0} } }; for (int i = 0; i < 8; i++) { ESMStatus.append(ESMState[i]); } // 真实接口 int PlatformID = obj.value("PlatformID").toInt(); int InstanceID = obj.value("InstanceID").toInt(); // 构造返回对象 QJsonObject *ret = new QJsonObject( { {"url", obj.value("url")}, {"PlatformID", obj.value("PlatformID")}, {"data", ESMStatus} }); returnMessage(pSender, ret); } void WSServer::getESMData(QWebSocket *pSender, QJsonObject obj) { } // --------------------------态势显控接口--------------------------------- 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 *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)} }; delete alies; delete enemys; // 真实接口 // 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::getNavParams(QWebSocket *pSender, QJsonObject obj) { int InstanceID = obj.value("InstanceID").toInt(); QJsonArray *NavInitParams; QJsonArray *NavCtrlParams; QJsonObject *NavPropTemp; NavInitParams = new QJsonArray(); NavCtrlParams = new QJsonArray(); NavPropTemp = new QJsonObject(); NavPropTemp->insert("key","precision"); NavPropTemp->insert("value",QJsonValue(5)); NavInitParams->append(*NavPropTemp); delete NavPropTemp; qDebug() << "aaa" << QString(QJsonDocument(*NavCtrlParams).toJson()); NavPropTemp = new QJsonObject(); NavPropTemp->insert("key","longitude"); NavPropTemp->insert("value",QJsonValue(120)); NavCtrlParams->append(*NavPropTemp); delete NavPropTemp; NavPropTemp = new QJsonObject(); NavPropTemp->insert("key","latitude"); NavPropTemp->insert("value",QJsonValue(30)); NavCtrlParams->append(*NavPropTemp); delete NavPropTemp; NavPropTemp = new QJsonObject(); NavPropTemp->insert("key","altitude"); NavPropTemp->insert("value",QJsonValue(8014.8)); NavCtrlParams->append(*NavPropTemp); delete NavPropTemp; NavPropTemp = new QJsonObject(); NavPropTemp->insert("key","azimush"); NavPropTemp->insert("value",QJsonValue(10)); NavCtrlParams->append(*NavPropTemp); delete NavPropTemp; NavPropTemp = new QJsonObject(); NavPropTemp->insert("key","elevation"); NavPropTemp->insert("value",QJsonValue(-3)); NavCtrlParams->append(*NavPropTemp); delete NavPropTemp; QJsonObject *NAVParams = new QJsonObject(); NAVParams->insert("InitParams", *NavInitParams); NAVParams->insert("CtrlParams", *NavCtrlParams); QJsonObject *ret = new QJsonObject({ {"url", obj.value("url")}, {"PlatformID", obj.value("PlatformID")}, {"data", *NAVParams} }); returnMessage(pSender, ret); delete NavInitParams; delete NavCtrlParams; delete NAVParams; } void WSServer::setFreePost(QWebSocket *pSender, QJsonObject obj) { }