Commit 93324592 by “liusq”

新增mqtt控制相机重合关闭

parent 6d4d10ba
...@@ -23,20 +23,20 @@ CameraHandle::CameraHandle(QString &url, QString &httpUrl, QString &sSn, int &ch ...@@ -23,20 +23,20 @@ CameraHandle::CameraHandle(QString &url, QString &httpUrl, QString &sSn, int &ch
devConfig(devConfig) devConfig(devConfig)
{ {
connect(this, SIGNAL(afterDownloadFile(int,int,QString)), this, SLOT(pushRecordToCloud(int,int,QString)),Qt::QueuedConnection); connect(this, SIGNAL(afterDownloadFile(int,int,QString)), this, SLOT(pushRecordToCloud(int,int,QString)),Qt::QueuedConnection);
faceMapWorker.setX(0); faceMapWorker.setX(0);
faceMapWorker.setY(0); faceMapWorker.setY(0);
} }
CameraHandle::~CameraHandle() { CameraHandle::~CameraHandle() {
semaphore.acquire(); semaphore.acquire();
Common & instace= Common::getInstance(); Common & instace= Common::getInstance();
dev_snap_syn_timer->stop(); dev_snap_syn_timer->stop();
ScopeSemaphoreExit guard([this]() { ScopeSemaphoreExit guard([this]() {
semaphore.release(); // 释放信号量 semaphore.release(); // 释放信号量
}); });
...@@ -293,7 +293,7 @@ void CameraHandle::sdkRealTimeDevSnapSyn(int hDevice) { ...@@ -293,7 +293,7 @@ void CameraHandle::sdkRealTimeDevSnapSyn(int hDevice) {
auto taskSyn = [this, hDevice]() { auto taskSyn = [this, hDevice]() {
sdkDevSnapSyn(hDevice, this->channel); sdkDevSnapSyn(hDevice, this->channel);
}; };
if (threadPool->activeThreadCount() >= threadPool->maxThreadCount()) { if (threadPool->activeThreadCount() >= threadPool->maxThreadCount()) {
qInfo() << "任务积压,跳过本次执行"; qInfo() << "任务积压,跳过本次执行";
return; return;
...@@ -412,7 +412,7 @@ int CameraHandle::callbackFunction(XSDK_HANDLE hObject, QString &szString) { ...@@ -412,7 +412,7 @@ int CameraHandle::callbackFunction(XSDK_HANDLE hObject, QString &szString) {
} }
void CameraHandle::sdkDevSnapSyn(XSDK_HANDLE hDevice, int nChannel){ void CameraHandle::sdkDevSnapSyn(XSDK_HANDLE hDevice, int nChannel){
if(hDevice<=0){ if(hDevice<=0){
qInfo() << "相机断线"; qInfo() << "相机断线";
return; return;
...@@ -434,7 +434,7 @@ void CameraHandle::sdkDevSnapSyn(XSDK_HANDLE hDevice, int nChannel){ ...@@ -434,7 +434,7 @@ void CameraHandle::sdkDevSnapSyn(XSDK_HANDLE hDevice, int nChannel){
if (offlineCount >= 3) { // 判断是否连续3次返回0 if (offlineCount >= 3) { // 判断是否连续3次返回0
qInfo() << "设备离线"; qInfo() << "设备离线";
QString ip=QString::fromUtf8(loginParam->sDevId); QString ip=QString::fromUtf8(loginParam->sDevId);
bool is_ping=vides_data::pingAddress(ip); bool is_ping=vides_data::pingAddress(ip);
if(!is_ping){ if(!is_ping){
...@@ -1073,7 +1073,7 @@ void CameraHandle::sdkDevSpvMn(const char *spvMn){ ...@@ -1073,7 +1073,7 @@ void CameraHandle::sdkDevSpvMn(const char *spvMn){
qInfo() << "sdkDevSpvMn 28181->修改失败"<<res; qInfo() << "sdkDevSpvMn 28181->修改失败"<<res;
} }
} }
void CameraHandle::deviceReboot(bool isCloseHandle){ int CameraHandle::deviceReboot(bool isCloseHandle){
int nRet=0; int nRet=0;
XSDK_CFG::OPMachine cfg; XSDK_CFG::OPMachine cfg;
cfg.Action.SetValue("Reboot"); cfg.Action.SetValue("Reboot");
...@@ -1082,16 +1082,36 @@ void CameraHandle::deviceReboot(bool isCloseHandle){ ...@@ -1082,16 +1082,36 @@ void CameraHandle::deviceReboot(bool isCloseHandle){
nRet = XSDK_DevSetSysConfig(hDevice, JK_OPMachine, pCfg, strlen(pCfg), 1, 3000, EXCMD_SYSMANAGER_REQ); nRet = XSDK_DevSetSysConfig(hDevice, JK_OPMachine, pCfg, strlen(pCfg), 1, 3000, EXCMD_SYSMANAGER_REQ);
if(nRet<0){ if(nRet<0){
qInfo() << sSn<<"重启相机失败"<<nRet; qInfo() << sSn<<"重启相机失败"<<nRet;
return ; return 0 ;
} }
if(isCloseHandle){ if(isCloseHandle){
return ; return 0;
} }
QString ip=QString::fromUtf8(loginParam->sDevId); QString ip=QString::fromUtf8(loginParam->sDevId);
MainWindow::sp_this->clearOfflineCameraHandle(ip,loginParam->nDevPort); 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) { bool CameraHandle::polygonsOverlap( ParkingSpaceInfo &poly1, ParkingSpaceInfo &poly2) {
QPolygonF realPolygon; QPolygonF realPolygon;
......
...@@ -100,7 +100,9 @@ public: ...@@ -100,7 +100,9 @@ public:
void updateSdkDevSpvMn(vides_data::responseGb28181 *gb28181); void updateSdkDevSpvMn(vides_data::responseGb28181 *gb28181);
//重启设备 //重启设备
void deviceReboot(bool isCloseHandle ); int deviceReboot(bool isCloseHandle );
//设备关机
int deviceShutdown();
//获取固件版本 //获取固件版本
void findFirmwareVersion(QString &firmwareVersion); void findFirmwareVersion(QString &firmwareVersion);
......
#include "MqttSubscriber.h" #include "MqttSubscriber.h"
#include "mainwindow.h"
#include "CameraHandle.h"
MqttSubscriber* MqttSubscriber::instance = nullptr; MqttSubscriber* MqttSubscriber::instance = nullptr;
MqttSubscriber* MqttSubscriber::getInstance(vides_data::MqttConfig& config, QObject* parent) { MqttSubscriber* MqttSubscriber::getInstance(vides_data::MqttConfig& config, QObject* parent) {
...@@ -10,15 +12,15 @@ MqttSubscriber* MqttSubscriber::getInstance(vides_data::MqttConfig& config, QObj ...@@ -10,15 +12,15 @@ MqttSubscriber* MqttSubscriber::getInstance(vides_data::MqttConfig& config, QObj
MqttSubscriber::MqttSubscriber(vides_data::MqttConfig& config, QObject* parent) MqttSubscriber::MqttSubscriber(vides_data::MqttConfig& config, QObject* parent)
: QObject(parent), config(config) { : QObject(parent), config(config) {
QByteArray bAddress = config.address.toUtf8(); QByteArray bAddress = config.address.toUtf8();
char* cAddress=bAddress.data(); char* cAddress=bAddress.data();
QByteArray bClientId = config.clientId.toUtf8(); QByteArray bClientId = config.clientId.toUtf8();
char* cClientId=bClientId.data(); char* cClientId=bClientId.data();
MQTTAsync_create(&client,cAddress,cClientId, MQTTCLIENT_PERSISTENCE_NONE, nullptr); MQTTAsync_create(&client,cAddress,cClientId, MQTTCLIENT_PERSISTENCE_NONE, nullptr);
MQTTAsync_setCallbacks(client, this, [](void* context, char* cause) { MQTTAsync_setCallbacks(client, this, [](void* context, char* cause) {
static_cast<MqttSubscriber*>(context)->connectionLost(cause); static_cast<MqttSubscriber*>(context)->connectionLost(cause);
...@@ -36,14 +38,14 @@ void MqttSubscriber::start() { ...@@ -36,14 +38,14 @@ void MqttSubscriber::start() {
MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer; MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;
conn_opts.keepAliveInterval = 20; conn_opts.keepAliveInterval = 20;
conn_opts.cleansession = 1; conn_opts.cleansession = 1;
QByteArray bUsername = config.username.toUtf8(); QByteArray bUsername = config.username.toUtf8();
char* cUsername=bUsername.data(); char* cUsername=bUsername.data();
QByteArray bPassword = config.password.toUtf8(); QByteArray bPassword = config.password.toUtf8();
char* cPassword=bPassword.data(); char* cPassword=bPassword.data();
conn_opts.username = cUsername; conn_opts.username = cUsername;
conn_opts.password = cPassword; conn_opts.password = cPassword;
conn_opts.onSuccess = [](void* context, MQTTAsync_successData* response) { conn_opts.onSuccess = [](void* context, MQTTAsync_successData* response) {
...@@ -53,7 +55,7 @@ void MqttSubscriber::start() { ...@@ -53,7 +55,7 @@ void MqttSubscriber::start() {
static_cast<MqttSubscriber*>(context)->onConnectFailure(response); static_cast<MqttSubscriber*>(context)->onConnectFailure(response);
}; };
conn_opts.context = this; conn_opts.context = this;
int rc; int rc;
if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS) { if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS) {
qInfo() << "启动连接失败,返回编码" << rc; qInfo() << "启动连接失败,返回编码" << rc;
...@@ -69,7 +71,7 @@ void MqttSubscriber::onConnect(MQTTAsync_successData* response) { ...@@ -69,7 +71,7 @@ void MqttSubscriber::onConnect(MQTTAsync_successData* response) {
static_cast<MqttSubscriber*>(context)->onSubscribeFailure(response); static_cast<MqttSubscriber*>(context)->onSubscribeFailure(response);
}; };
opts.context = this; opts.context = this;
QByteArray bTopic = config.topic.toUtf8(); QByteArray bTopic = config.topic.toUtf8();
char* cTopic=bTopic.data(); char* cTopic=bTopic.data();
int rc; int rc;
...@@ -100,26 +102,63 @@ void MqttSubscriber::connectionLost(char* cause) { ...@@ -100,26 +102,63 @@ void MqttSubscriber::connectionLost(char* cause) {
int MqttSubscriber::messageArrived(char* topicName, int topicLen, MQTTAsync_message* m) { int MqttSubscriber::messageArrived(char* topicName, int topicLen, MQTTAsync_message* m) {
QString topic(topicName); QString topic(topicName);
QString payload = QString::fromUtf8(reinterpret_cast<const char*>(m->payload), m->payloadlen); QString payload = QString::fromUtf8(reinterpret_cast<const char*>(m->payload), m->payloadlen);
qInfo() << "Message arrived"; vides_data::responseMqttData response;
qInfo() << "Topic:" << topic; QJsonDocument jsonDoc = QJsonDocument::fromJson(payload.toUtf8());
qInfo() << "Payload:" << payload; if (!jsonDoc.isNull() && jsonDoc.isObject()) {
qInfo()<<"topicLen"<<topicLen; 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_freeMessage(&m);
MQTTAsync_free(topicName); MQTTAsync_free(topicName);
return 1; return 1;
} }
void MqttSubscriber::sendSubscriptionConfirmation(const std::string& messageId) { void MqttSubscriber::sendSubscriptionConfirmation(const vides_data::requestMqttData& response) {
std::string confirmationTopic = "confirmation/subscription";
std::string confirmationMessage = "Subscription confirmed with message ID: " + messageId; 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; 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.qos = config.qos;
pubmsg.retained = 0; pubmsg.retained = 0;
MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer; MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
opts.onSuccess = [](void* context, MQTTAsync_successData* response) { opts.onSuccess = [](void* context, MQTTAsync_successData* response) {
static_cast<MqttSubscriber*>(context)->onPublishSuccess(response); static_cast<MqttSubscriber*>(context)->onPublishSuccess(response);
...@@ -128,9 +167,9 @@ void MqttSubscriber::sendSubscriptionConfirmation(const std::string& messageId) ...@@ -128,9 +167,9 @@ void MqttSubscriber::sendSubscriptionConfirmation(const std::string& messageId)
static_cast<MqttSubscriber*>(context)->onPublishFailure(response); static_cast<MqttSubscriber*>(context)->onPublishFailure(response);
}; };
opts.context = this; opts.context = this;
int rc; 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; qInfo() << "发送消息失败,返回编码" << rc;
} }
} }
......
...@@ -34,7 +34,7 @@ private: ...@@ -34,7 +34,7 @@ private:
void onPublishSuccess(MQTTAsync_successData* response); void onPublishSuccess(MQTTAsync_successData* response);
void onPublishFailure(MQTTAsync_failureData* response); void onPublishFailure(MQTTAsync_failureData* response);
void sendSubscriptionConfirmation(const std::string& messageId); void sendSubscriptionConfirmation(const vides_data::requestMqttData& response);
static MqttSubscriber* instance; static MqttSubscriber* instance;
}; };
......
...@@ -294,14 +294,17 @@ struct responseConfig { ...@@ -294,14 +294,17 @@ struct responseConfig {
Camera camera; Camera camera;
MqttConfig mqttConfig; MqttConfig mqttConfig;
}; };
struct Devices{
QString id;
QString state;
};
struct responseMqttData{ struct responseMqttData{
uint8_t msg_type; uint8_t msg_type;
std::list<Devices>devices; QString sn;
QString uniq;
};
struct requestMqttData{
QString sn;
int code;
QString uniq;
}; };
inline bool isVirtualMachine() 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