Commit 93324592 by “liusq”

新增mqtt控制相机重合关闭

parent 6d4d10ba
......@@ -1073,7 +1073,7 @@ void CameraHandle::sdkDevSpvMn(const char *spvMn){
qInfo() << "sdkDevSpvMn 28181->修改失败"<<res;
}
}
void CameraHandle::deviceReboot(bool isCloseHandle){
int CameraHandle::deviceReboot(bool isCloseHandle){
int nRet=0;
XSDK_CFG::OPMachine cfg;
cfg.Action.SetValue("Reboot");
......@@ -1082,16 +1082,36 @@ void CameraHandle::deviceReboot(bool isCloseHandle){
nRet = XSDK_DevSetSysConfig(hDevice, JK_OPMachine, pCfg, strlen(pCfg), 1, 3000, EXCMD_SYSMANAGER_REQ);
if(nRet<0){
qInfo() << sSn<<"重启相机失败"<<nRet;
return ;
return 0 ;
}
if(isCloseHandle){
return ;
return 0;
}
QString ip=QString::fromUtf8(loginParam->sDevId);
MainWindow::sp_this->clearOfflineCameraHandle(ip,loginParam->nDevPort);
return nRet;
}
int CameraHandle::deviceShutdown()
{
int nRet = 0;
XSDK_CFG::OPMachine cfg;
cfg.Action.SetValue("Shutdown");
const char* pCfg = cfg.ToString();
nRet = XSDK_DevSetSysConfig(hDevice, JK_OPMachine, pCfg, strlen(pCfg), 1, 3000, EXCMD_SYSMANAGER_REQ);
if(nRet<0){
qInfo() << sSn<<"设备关机失败"<<nRet;
return 0;
}
QString ip=QString::fromUtf8(loginParam->sDevId);
MainWindow::sp_this->clearOfflineCameraHandle(ip,loginParam->nDevPort);
return nRet;
}
bool CameraHandle::polygonsOverlap( ParkingSpaceInfo &poly1, ParkingSpaceInfo &poly2) {
QPolygonF realPolygon;
......
......@@ -100,7 +100,9 @@ public:
void updateSdkDevSpvMn(vides_data::responseGb28181 *gb28181);
//重启设备
void deviceReboot(bool isCloseHandle );
int deviceReboot(bool isCloseHandle );
//设备关机
int deviceShutdown();
//获取固件版本
void findFirmwareVersion(QString &firmwareVersion);
......
#include "MqttSubscriber.h"
#include "mainwindow.h"
#include "CameraHandle.h"
MqttSubscriber* MqttSubscriber::instance = nullptr;
MqttSubscriber* MqttSubscriber::getInstance(vides_data::MqttConfig& config, QObject* parent) {
......@@ -101,22 +103,59 @@ int MqttSubscriber::messageArrived(char* topicName, int topicLen, MQTTAsync_mess
QString topic(topicName);
QString payload = QString::fromUtf8(reinterpret_cast<const char*>(m->payload), m->payloadlen);
qInfo() << "Message arrived";
qInfo() << "Topic:" << topic;
qInfo() << "Payload:" << payload;
qInfo()<<"topicLen"<<topicLen;
vides_data::responseMqttData response;
QJsonDocument jsonDoc = QJsonDocument::fromJson(payload.toUtf8());
if (!jsonDoc.isNull() && jsonDoc.isObject()) {
QJsonObject jsonObj = jsonDoc.object();
response.msg_type = jsonObj["msg_type"].toInt();
response.sn = jsonObj["sn"].toString();
response.uniq = jsonObj["uniq"].toString();
} else {
qInfo() << "Failed to parse JSON payload";
}
int res=-2;
//1开 2关 3 重启
CameraHandle*cameraHandle= MainWindow::sp_this->findHandle(response.sn);
if(cameraHandle==nullptr){
qInfo() << "不存在该相机";
return 1;
}
if(response.msg_type==2){
res=cameraHandle->deviceShutdown();
}
if(response.msg_type==3){
res= cameraHandle->deviceReboot(false);
}
vides_data::requestMqttData request;
qInfo() << "res"<<res;
request.code =res>=0?0:0x01;
request.uniq = response.uniq;
request.sn=response.sn ;
sendSubscriptionConfirmation(request);
MQTTAsync_freeMessage(&m);
MQTTAsync_free(topicName);
return 1;
}
void MqttSubscriber::sendSubscriptionConfirmation(const std::string& messageId) {
std::string confirmationTopic = "confirmation/subscription";
std::string confirmationMessage = "Subscription confirmed with message ID: " + messageId;
void MqttSubscriber::sendSubscriptionConfirmation(const vides_data::requestMqttData& response) {
QString responseTopic = "/thingshub/" + response.sn + "/device/post";
QByteArray bResponseTopic = responseTopic.toUtf8();
char* cResponseTopic = bResponseTopic.data();
qInfo() << "sendSubscriptionConfirmation"<<cResponseTopic;
// 将 struct 转换成 JSON 格式
QJsonObject json;
json["code"] = response.code;
json["uniq"] = response.uniq;
QJsonDocument jsonDoc(json);
QByteArray payload = jsonDoc.toJson(QJsonDocument::Compact);
MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
pubmsg.payload = const_cast<char*>(confirmationMessage.c_str());
pubmsg.payloadlen = confirmationMessage.length();
pubmsg.payload = const_cast<char*>(payload.data());
pubmsg.payloadlen = payload.size();
pubmsg.qos = config.qos;
pubmsg.retained = 0;
......@@ -130,7 +169,7 @@ void MqttSubscriber::sendSubscriptionConfirmation(const std::string& messageId)
opts.context = this;
int rc;
if ((rc = MQTTAsync_sendMessage(client, confirmationTopic.c_str(), &pubmsg, &opts)) != MQTTASYNC_SUCCESS) {
if ((rc = MQTTAsync_sendMessage(client, cResponseTopic, &pubmsg, &opts)) != MQTTASYNC_SUCCESS) {
qInfo() << "发送消息失败,返回编码" << rc;
}
}
......
......@@ -34,7 +34,7 @@ private:
void onPublishSuccess(MQTTAsync_successData* response);
void onPublishFailure(MQTTAsync_failureData* response);
void sendSubscriptionConfirmation(const std::string& messageId);
void sendSubscriptionConfirmation(const vides_data::requestMqttData& response);
static MqttSubscriber* instance;
};
......
......@@ -294,14 +294,17 @@ struct responseConfig {
Camera camera;
MqttConfig mqttConfig;
};
struct Devices{
QString id;
QString state;
};
struct responseMqttData{
uint8_t msg_type;
std::list<Devices>devices;
QString sn;
QString uniq;
};
struct requestMqttData{
QString sn;
int code;
QString uniq;
};
inline bool isVirtualMachine()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment