Commit 23173fdd by 郭峰

Merge branch 'release' into 'master'

Release

See merge request !71
parents 25af28ce 07a92e1b
......@@ -425,6 +425,7 @@ void CameraHandle::sdkDevSnapSyn(XSDK_HANDLE hDevice, int nChannel){
ScopeSemaphoreExit guard([this]() {
semaphore.release(); // 释放信号量
});
Common & instace= Common::getInstance();
cv::Mat image;
MediaFaceImage* mediaFaceImage= MediaFaceImage::getInstance();
qint64 currentTime= QDateTime::currentSecsSinceEpoch();
......@@ -434,7 +435,7 @@ void CameraHandle::sdkDevSnapSyn(XSDK_HANDLE hDevice, int nChannel){
if (offlineCount >= 3) { // 判断是否连续3次返回0
qInfo() <<QString("SN(%1): 设备离线").arg(sSn);
QString ip=QString::fromUtf8(loginParam->sDevId);
bool is_ping=vides_data::pingAddress(ip);
bool is_ping=instace.pingAddress(ip);
qDebug() << sSn<<":ping 的结果"<<is_ping;
if(is_ping){
......@@ -1016,15 +1017,21 @@ void CameraHandle::printWifi(XSDK_HANDLE hDevice,XSDK_CFG::NetWork_Wifi &cfg){
需注意和定时任务对比GB28181配置冲突处理
需确认修改IP后抓图是否正常,如不正常需在命令开始前增加删除句柄,命令完成后增加句柄以保证抓图实时性
*/
int CameraHandle::resetGb28181(){
NonConnectedCameraHandle *connectedCameraHandle =NonConnectedCameraHandle::getInstance();
int old=hDevice;
int CameraHandle::resetGb28181(QString &uuiq){
NonConnectedCameraHandle *connectedCameraHandle =NonConnectedCameraHandle::getInstance();
int old=this->hDevice;
qInfo()<<QString("SN(%1): resetGb28181老句柄%2").arg(sSn).arg(old);
MainWindow::sp_this->setIsResetting(true);
bool is_wifi=false;
ScopeSemaphoreExit guard([]() {
MainWindow::sp_this->setIsResetting(false);
});
this->updateSdkDevStatus(false);
int flg=this->updateSdkDevStatus(false);
if(flg<0){
qInfo()<<QString("SN(%1): GB28181关失败:%2").arg(sSn).arg(flg);
return -1;
}
QString ip=QString::fromUtf8(loginParam->sDevId);
QString reachableIp= vides_data::findReachableIp();
if (!reachableIp.isEmpty()) {
......@@ -1042,13 +1049,16 @@ int CameraHandle::resetGb28181(){
bool result= connectedCameraHandle->changeCameraIp(device);
int newHandle=-1;
if(!result ){
result=connectedCameraHandle->wifiChangeIp(reachableIp,false,old,sSn);
qInfo()<<QString("SN(%1): WIFI修改的前的句柄%2").arg(sSn).arg(old);
result=connectedCameraHandle->wifiChangeIp(reachableIp,old,sSn);
is_wifi=true;
if(result){
//老句柄抓图不正常了
int sdk_handle=this->sdkDevLoginSyn(reachableIp,loginParam->nDevPort,loginParam->sUserName,loginParam->sPassword,3000);
if(sdk_handle<0){
qInfo()<<QString("SN(%1): WIFI老句柄:%2").arg(sSn).arg(old);
qInfo()<<QString("SN(%1): WIFI新句柄生成失败").arg(sSn);
this->hDevice=old;
return -1;
}else {
qInfo()<<QString("SN(%1): WIFI新句柄生成成功").arg(sSn);
......@@ -1056,43 +1066,114 @@ int CameraHandle::resetGb28181(){
this->hDevice=-1;
XSDK_DevLogout(old);
}
}else{
qInfo()<<QString("SN(%1): WIFI修改ip 失败").arg(sSn);
return -1;
}
}
// 阻塞等待5秒
QThread::sleep(5);
// 连接 QTimer 的超时信号到匿名槽函数
QString old_ip;
MainWindow::sp_this->findSnMapIp(this->sSn,old_ip);
if(old_ip.length()>0){
if(is_wifi){
result=connectedCameraHandle->wifiChangeIp(old_ip,true,newHandle,sSn);
int sdk_handle=this->sdkDevLoginSyn(old_ip,loginParam->nDevPort,loginParam->sUserName,loginParam->sPassword,3000);
if(sdk_handle<0){
qInfo()<<QString("SN(%1): WIFI新句柄生成失败").arg(sSn);
}else {
int res = 1;
MainWindow::sp_this->findSnMapIp(this->sSn, old_ip);
if (old_ip.length() > 0) {
if (is_wifi) {
result = connectedCameraHandle->wifiChangeIp(old_ip, newHandle, sSn);
int sdk_handle = this->sdkDevLoginSyn(old_ip, loginParam->nDevPort, loginParam->sUserName, loginParam->sPassword, 3000);
if (sdk_handle < 0) {
qInfo() << QString("SN(%1): WIFI新句柄生成失败").arg(sSn);
this->hDevice = newHandle;
res = -1;
} else {
XSDK_DevLogout(newHandle);
qInfo()<<QString("SN(%1): WIFI新句柄生成成功").arg(sSn);
qInfo() << QString("SN(%1): WIFI新句柄生成成功").arg(sSn);
}
}else {
device.reachableIp=old_ip;
device.sSn=sSn;
bool result= connectedCameraHandle->changeCameraIp(device);
if(result){
int sdk_handle=this->sdkDevLoginSyn(old_ip,loginParam->nDevPort,loginParam->sUserName,loginParam->sPassword,3000);
if(sdk_handle<0){
qInfo()<<QString("SN(%1): 有线新句柄生成失败").arg(sSn);
}else {
} else {
device.reachableIp = old_ip;
device.sSn = sSn;
bool result = connectedCameraHandle->changeCameraIp(device);
if (result) {
int sdk_handle = this->sdkDevLoginSyn(old_ip, loginParam->nDevPort, loginParam->sUserName, loginParam->sPassword, 3000);
if (sdk_handle < 0) {
qInfo() << QString("SN(%1): 有线新句柄生成失败").arg(sSn);
this->hDevice = old;
res = -1;
} else {
XSDK_DevLogout(old);
qInfo()<<QString("SN(%1): 有线新句柄生成成功").arg(sSn);
qInfo() << QString("SN(%1): 有线新句柄生成成功").arg(sSn);
}
}
}
}
return this->updateSdkDevStatus(true);
//this->updateSdkDevStatus(true);
return 1;
}
int CameraHandle::forceWriteGb28181(){
Common & instace= Common::getInstance();
HttpService http_gb28181(httpUrl);
vides_data::response *res=http_gb28181.httpFindGb28181Config(sSn);
ScopeSemaphoreExit guard([&instace,&res]() {
instace.deleteObj(res);
});
if(res->code!=0){
qInfo()<<"请求摄像头gb28181配置失败";
return -1;
}
vides_data::responseGb28181 *gb28181 = reinterpret_cast<vides_data::responseGb28181*>(res->data);
XSDK_CFG::NetWork_SPVMN config;
config.Camreaid.InitArraySize(64);
for (int i = 1; i <= 64; ++i) {
if (i == 1) {
QByteArray b_StrValue = gb28181->channel_id.toUtf8();
const char* str_Value = b_StrValue.constData();
JStrObj* newCameraId = &config.Camreaid[i - 1];
newCameraId->operator=(str_Value);
} else {
QString str("3402000000131000001" + QString::number(i, 10).rightJustified(2, '0'));
QByteArray b_Str = str.toUtf8();
const char* s_ct = b_Str.constData();
JStrObj* newCameraId = &config.Camreaid[i - 1];
newCameraId->operator=(s_ct);
}
}
QByteArray && bSip_ip= gb28181->sip_ip.toUtf8();
char* sip_ip = bSip_ip.data();
config.szCsIP.SetValue(sip_ip);
QByteArray && bSzServerNo=gb28181->serial.toUtf8();
char* sz_ServerNo = bSzServerNo.data();
config.szServerNo.SetValue(sz_ServerNo);
config.sCsPort.SetValue(gb28181->sip_port);
config.sUdpPort.SetValue(5060);
QByteArray && bSzServerDn=gb28181->realm.toUtf8();
char* sz_ServerDn = bSzServerDn.data();
config.szServerDn.SetValue(sz_ServerDn);
config.bCsEnable.SetValue(true);
config.iRsAgedTime.SetValue(gb28181->register_validity);
config.iHsIntervalTime.SetValue(gb28181->heartbeat_interval);
QByteArray && bSzConnPass=gb28181->password.toUtf8();
char* sz_connPass = bSzConnPass.data();
config.szConnPass.SetValue(sz_connPass);
QByteArray && bDevice_id=gb28181->device_id.toUtf8();
char* cdevice_id = bDevice_id.data();
config.szDeviceNO.SetValue(cdevice_id);
const char* pCfg = config.ToString();
sdkDevSpvMn(pCfg);
}
bool CameraHandle::sdkWifi(QString &pwd,QString &ssid){
......@@ -1124,12 +1205,11 @@ bool CameraHandle::sdkWifi(QString &pwd,QString &ssid){
char szOutBuffer[512] = { 0 };
int nLen = sizeof(szOutBuffer);
int res =XSDK_DevSetSysConfigSyn(hDevice, JK_NetWork_Wifi, wipCfg, strlen(wipCfg), szOutBuffer, &nLen, 3000, EXCMD_CONFIG_SET);
qInfo()<<QString("SN(%1): 修改WIFI结果:%2").arg(sSn).arg(res);
if(res<0){
qInfo()<<QString("SN(%1): 修改wifi失败:%2").arg(sSn).arg(res);
return false;
}
deviceReboot();
return true;
}
......@@ -1184,7 +1264,6 @@ int CameraHandle::updateSdkDevStatus(bool status){
int res= XSDK_DevGetSysConfigSyn(hDevice,JK_NetWork_SPVMN,szOutBuffer,&nInOutSize,4000,EXCMD_CONFIG_GET);
if(res<0){
qInfo()<<QString("SN(%1): GB28181可远程开关").arg(sSn);
return 0 ;
}
XSDK_CFG::NetWork_SPVMN config;
......@@ -1276,13 +1355,15 @@ void CameraHandle::updateSdkDevSpvMn(vides_data::responseGb28181 *gb28181){
}
void CameraHandle::sdkDevSpvMn(const char *spvMn){
int CameraHandle::sdkDevSpvMn(const char *spvMn){
char szOutBuffer[512] = { 0 };
int nLen = sizeof(szOutBuffer);
int res=XSDK_DevSetSysConfigSyn(hDevice,JK_NetWork_SPVMN,spvMn,strlen(spvMn),szOutBuffer,&nLen,3000,EXCMD_CONFIG_SET);
if(res<0){
qInfo() <<sSn<< ":sdkDevSpvMn 28181->修改失败"<<res;
return -1;
}
return res;
}
int CameraHandle::deviceReboot(){
int nRet=0;
......
......@@ -100,11 +100,13 @@ public:
//配置编码设置
void sdkEncodeCfg(const char *enCode);
//28181更新
void sdkDevSpvMn(const char* spvMn);
int sdkDevSpvMn(const char* spvMn);
//gb218开启
int updateSdkDevStatus(bool status);
void updateSdkDevSpvMn(vides_data::responseGb28181 *gb28181);
//重启设备
int deviceReboot();
//设备关机
......@@ -112,7 +114,10 @@ public:
//获取固件版本
void findFirmwareVersion(QString &firmwareVersion);
//复位GB28181
int resetGb28181();
int resetGb28181(QString &uuiq);
//强制gb28181配置后写入摄像头
int forceWriteGb28181();
//获取ip
void findIp(QString &ip);
......@@ -206,8 +211,7 @@ private :
std::atomic<uint64> faceCount;
std::atomic<bool> isOperateGb28181{false};
uint64 face_frequency;
__uint8_t algorithmPermissions;
......
#include "Common.h"
#include "ScopeSemaphoreExit.h"
Common::Common(){}
......@@ -107,4 +108,223 @@ QString Common::GetLocalIp() {
}
return ipAddress;
}
// 计算校验和
unsigned short Common::calculate_checksum(void *b, int len) {
unsigned short *buf = (unsigned short *)b;
unsigned int sum = 0;
unsigned short result;
for (sum = 0; len > 1; len -= 2)
sum += *buf++;
if (len == 1)
sum += *(unsigned char *)buf;
sum = (sum >> 16) + (sum & 0xFFFF);
sum += (sum >> 16);
result = ~sum;
return result;
}
bool Common::pingAddress(const QString &address) {
QByteArray &&byJsonIp = address.toUtf8();
const char *target = byJsonIp.data();
struct sockaddr_in dest;
struct hostent *host_entity;
// 解析主机名或IP地址
if ((host_entity = gethostbyname(target)) == NULL) {
perror("gethostbyname");
return false; // 解析失败,返回false
}
memset(&dest, 0, sizeof(dest));
dest.sin_family = AF_INET;
dest.sin_addr = *(struct in_addr *)host_entity->h_addr;
// 创建原始套接字
int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_ICMP);
if (sock < 0) {
perror("socket");
return false; // 套接字创建失败,返回false
}
// 设置2秒的接收超时
struct timeval timeout;
timeout.tv_sec = 2; // 2秒
timeout.tv_usec = 0; // 0微秒
if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) < 0) {
perror("setsockopt");
close(sock);
return false; // 设置超时失败,返回false
}
// 设置ICMP包头
struct icmphdr icmp_hdr;
icmp_hdr.type = ICMP_ECHO; // ICMP Echo Request
icmp_hdr.code = 0;
icmp_hdr.un.echo.id = getpid(); // 使用进程ID作为标识符
icmp_hdr.un.echo.sequence = 1;
icmp_hdr.checksum = 0;
// 计算ICMP校验和
icmp_hdr.checksum = calculate_checksum(&icmp_hdr, ICMP_HDRLEN);
// 发送ICMP数据包
if (sendto(sock, &icmp_hdr, ICMP_HDRLEN, 0, (struct sockaddr *)&dest, sizeof(dest)) <= 0) {
perror("sendto");
close(sock);
return false; // 发送失败,返回false
}
// 接收ICMP回复
char buffer[1024];
struct sockaddr_in recv_addr;
socklen_t addr_len = sizeof(recv_addr);
if (recvfrom(sock, buffer, sizeof(buffer), 0, (struct sockaddr *)&recv_addr, &addr_len) <= 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
// 超时错误处理
printf("Ping timeout.\n");
} else {
// 其他接收错误
perror("recvfrom");
}
close(sock);
return false; // 超时或接收失败,返回false
}
// 关闭套接字
close(sock);
return true; // 成功,返回true
}
//获取本机mask
bool GetLocalNetMask(const char *eth_inf,char* netmask_addr)
{
int sock_netmask;
struct ifreq ifr_mask;
struct sockaddr_in *net_mask;
sock_netmask = socket( AF_INET, SOCK_STREAM, 0 );
if( sock_netmask == -1)
{
perror("create socket failture...GetLocalNetMask\n");
return false;
}
memset(&ifr_mask, 0, sizeof(ifr_mask));
strncpy(ifr_mask.ifr_name, eth_inf, sizeof(ifr_mask.ifr_name )-1);
if( (ioctl( sock_netmask, SIOCGIFNETMASK, &ifr_mask ) ) < 0 )
{
printf("mac ioctl error\n");
return false;
}
net_mask = ( struct sockaddr_in * )&( ifr_mask.ifr_netmask );
strcpy( netmask_addr, inet_ntoa( net_mask -> sin_addr ) );
close( sock_netmask );
return true;
}
//获取本机gateway
bool GetLocalGateWay(char* gateway)
{
FILE *fp;
char buf[512];
char cmd[128];
char *tmp;
strcpy(cmd, "ip route");
fp = popen(cmd, "r");
if(NULL == fp)
{
perror("popen error");
return false;
}
while(fgets(buf, sizeof(buf), fp) != NULL)
{
tmp =buf;
while(*tmp && isspace(*tmp))
++ tmp;
if(strncmp(tmp, "default", strlen("default")) == 0)
break;
}
sscanf(buf, "%*s%*s%s", gateway);
pclose(fp);
return true;
}
// 确定当前网络接口
void Common::determine_interface(char *interface) {
struct ifaddrs *ifaddr, *ifa;
int family, s;
char host[NI_MAXHOST];
if (getifaddrs(&ifaddr) == -1) {
perror("getifaddrs");
exit(EXIT_FAILURE);
}
// Walk through linked list, maintaining head pointer so we can free list later
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
if (ifa->ifa_addr == NULL)
continue;
family = ifa->ifa_addr->sa_family;
// Check for IPv4 or IPv6
if (family == AF_INET || family == AF_INET6) {
s = getnameinfo(ifa->ifa_addr,
(family == AF_INET) ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6),
host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
if (s != 0) {
printf("getnameinfo() failed: %s\n", gai_strerror(s));
exit(EXIT_FAILURE);
}
// Check if the interface is up and running
if (ifa->ifa_flags & IFF_UP && ifa->ifa_flags & IFF_RUNNING) {
strncpy(interface, ifa->ifa_name, IFNAMSIZ - 1);
break;
}
}
}
freeifaddrs(ifaddr);
}
QString Common::GetLocalGateWay() {
FILE *fp;
char buf[512];
char cmd[128];
char gateway[INET_ADDRSTRLEN] = {0}; // Store the gateway address
strcpy(cmd, "ip route");
fp = popen(cmd, "r");
if (NULL == fp) {
perror("popen error");
return QString();
}
while (fgets(buf, sizeof(buf), fp) != NULL) {
char *tmp = buf;
while (*tmp && isspace(*tmp))
++tmp;
if (strncmp(tmp, "default", strlen("default")) == 0)
break;
}
sscanf(buf, "%*s%*s%s", gateway); // Capture the third word as gateway
pclose(fp);
return QString(gateway); // Convert C-string to QString
}
Common::~Common(){}
#ifndef COMMON_H
#define COMMON_H
#include <ctime>
#include <chrono>
#include <thread>
......@@ -11,7 +13,36 @@
#include <QMessageAuthenticationCode>
#include <QNetworkInterface>
#include <map>
#include <QDebug>
#include <iostream>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/ip_icmp.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <net/if_arp.h>
#include <linux/route.h>
#include <ifaddrs.h>
#include <netdb.h>
#define ICMP_HDRLEN 8
#define PAYLOAD_SIZE 56
// ICMP 头部结构体
struct ICMPHeader {
uint8_t type; // 类型
uint8_t code; // 代码
uint16_t checksum; // 校验和
uint16_t id; // 标识符
uint16_t sequence; // 序列号
};
class Common
{
public:
......@@ -38,8 +69,16 @@ public:
QString getVideoDownload();
void setVideoDownload(QString videoDownload);
unsigned short calculate_checksum(void *b, int len) ;
//确定当前网络接口
void determine_interface(char *interface);
QString GetLocalGateWay();//获取本机gateway
bool pingAddress(const QString &address) ;
QString getImages();
void setImages(QString images);
......
......@@ -57,6 +57,7 @@ void MqttSubscriber::init(vides_data::MqttConfig &config, QString &httpUrl, QStr
MqttSubscriber::MqttSubscriber(QObject* parent)
: QObject(parent), retryTimer(new QTimer(this)), client(nullptr) {
// 连接信号和槽
connect(this, &MqttSubscriber::connectionLostSignal, this, &MqttSubscriber::reconnectAndFetchConfig, Qt::QueuedConnection);
......@@ -220,14 +221,14 @@ int MqttSubscriber::messageArrived(char* topicName, int topicLen, MQTTAsync_mess
if (msgBodyOb.contains("username") && msgBodyOb.contains("password")) {
QString username = msgBodyOb["username"].toString();
QString password = msgBodyOb["password"].toString();
res = (cameraHandle->sdkWifi(password, username) ? 0 : 0x01);
res = (cameraHandle->sdkWifi(password, username) ? 0x01 :-1);
} else {
qInfo() << "IP username not found in msg_body.";
qInfo() << "IP password not found in msg_body.";
}
break;
}
case 8: res = (cameraHandle->resetGb28181() ? 0 : 0x01); break;
case 8: res = cameraHandle->resetGb28181( response.uniq); break;
default: qInfo() << "Unknown message type"; break;
}
}
......@@ -242,10 +243,8 @@ int MqttSubscriber::messageArrived(char* topicName, int topicLen, MQTTAsync_mess
} else {
qInfo() << "IP address not found in msg_body.";
}
res=connectedCameraHandle->distributionNetwork(ipAddress,response.sn,hDevice);
res=(connectedCameraHandle->distributionNetwork(ipAddress,response.sn,hDevice)?0x01 :-1);
}
vides_data::requestMqttData request;
request.code = (res >= 0) ? 0 : 0x01;
request.msg = (res >= 0) ?"成功":"失败";
......@@ -256,7 +255,6 @@ int MqttSubscriber::messageArrived(char* topicName, int topicLen, MQTTAsync_mess
MQTTAsync_free(topicName);
return 1;
}
void MqttSubscriber::sendSubscriptionConfirmation(const vides_data::requestMqttData& response,QString &sn) {
QString responseTopic = "/thingshub/" +response.uniq+ "/device/post";
QByteArray bResponseTopic = responseTopic.toUtf8();
......
......@@ -17,8 +17,10 @@ public:
signals:
void connectionLostSignal();
private slots:
void reconnectAndFetchConfig();
private:
MqttSubscriber(QObject* parent = nullptr);
MqttSubscriber() = delete;
......@@ -45,6 +47,7 @@ private:
void onPublishSuccess(MQTTAsync_successData* response);
void onPublishFailure(MQTTAsync_failureData* response);
void sendSubscriptionConfirmation(const vides_data::requestMqttData& response,QString &sn);
static MqttSubscriber* instance;
};
......
......@@ -19,7 +19,7 @@ public:
//有线修改相机ip
bool changeCameraIp(vides_data::localDevice &device);
//无线修改相机ip
bool wifiChangeIp(QString &Ip, bool is_connect, int h_device,QString &sn);
bool wifiChangeIp(QString &Ip,int h_device,QString &sn);
bool isWifiConnect(XSDK_HANDLE hDevice,XSDK_CFG::NetWork_Wifi &cfg);
......
......@@ -16,6 +16,7 @@
#include <string>
#include <sstream>
#include <list>
#include "Common.h"
namespace vides_data{
constexpr const char *HEADER_TYPE_KAY="Content-Type";
constexpr const char *HEADER_TYPE_VALUE="application/json";
......@@ -350,6 +351,7 @@ struct DetectionParams {
float recConfidenceThreshold; ///< 识别置信度阈值
};
inline bool isVirtualMachine()
{
QString dmiPath;
......@@ -382,7 +384,7 @@ inline QString getDefaultGateway() {
#endif
process.start(command, arguments);
// 等待进程结束
process.waitForFinished();
......@@ -439,60 +441,80 @@ inline bool isInSameSubnet(const QString &ip1, const QString &ip2, const QString
return result1 == result2;
}
// 获取本地 MAC 地址、子网掩码和网关IP
inline bool GetNetworkInfoByQNetworkInterface(QString &mac, QString &subnetMask, QString &gateway) {
// 获取子网掩码
inline bool GetSubnetMask(QString &subnetMask) {
QList<QNetworkInterface> interfaces = QNetworkInterface::allInterfaces();
foreach (QNetworkInterface interface, interfaces) {
if (interface.flags().testFlag(QNetworkInterface::IsUp) &&
interface.flags().testFlag(QNetworkInterface::IsRunning) &&
!interface.flags().testFlag(QNetworkInterface::IsLoopBack)) {
mac = interface.hardwareAddress();
interface.flags().testFlag(QNetworkInterface::IsRunning) &&
!interface.flags().testFlag(QNetworkInterface::IsLoopBack)) {
QList<QNetworkAddressEntry> addressEntries = interface.addressEntries();
foreach (QNetworkAddressEntry entry, addressEntries) {
if (entry.ip().protocol() == QAbstractSocket::IPv4Protocol) {
subnetMask = entry.netmask().toString();
gateway = entry.broadcast().toString(); // 这里假设网关是广播地址
return true;
return true; // 成功获取子网掩码
}
}
}
}
return false; // Return false if no suitable interface is found
}
// 如果没有找到合适的接口或子网掩码
subnetMask = "Unknown";
return false;
}
inline bool pingAddress(const QString &address) {
QProcess process;
QString program = "ping";
QStringList arguments;
QProcess cmd;
#ifdef Q_OS_WIN
arguments << "-n" << "1" << address;
// Windows 指令 "ping IP -n 1 -w 超时(ms)"
QStringList arguments;
arguments << "-c" << "ping " + address + " -n 1 -w 1000";
#else
// Linux 指令 "ping -c 1 IP"
QStringList arguments;
arguments << "-c" << "1" << address;
#endif
process.start(program, arguments);
if (!process.waitForStarted()) {
// 启动进程
cmd.start("/bin/ping", arguments);
// 等待进程准备好读取
if (!cmd.waitForStarted()) {
qWarning() << "无法启动的‘ping’进程" << address;
qWarning() << "命令: " << arguments.join(" ");
return false;
}
// 使用较长的超时值,确保进程有足够时间完成
if (!process.waitForFinished(3000)) {
process.kill(); // 如果超时,强制终止进程
// 等待进程完成
if (!cmd.waitForFinished(3000)) { // 增加超时时间
cmd.kill();
qWarning() << "Ping process timed out for" << address;
return false;
}
QString output(process.readAllStandardOutput());
// 简单的 Ping 成功检查逻辑
#ifdef Q_OS_WIN
return output.contains("TTL=");
#else
return output.contains("1 packets transmitted, 1 received");
#endif
// 读取输出
QByteArray output = cmd.readAllStandardOutput();
QByteArray errorOutput = cmd.readAllStandardError();
int exitCode = cmd.exitCode();
qInfo() << "Ping Output for" << address << ":" << QString::fromUtf8(output);
qInfo() << "Ping Error Output for" << address << ":" << QString::fromUtf8(errorOutput);
qInfo() << "Ping Exit Code for" << address << ":" << exitCode;
// 判断 Ping 是否成功
if (exitCode == 0) {
qInfo() << address << "ping通";
return true;
} else {
qInfo() << address << "ping不通";
return false;
}
}
inline QString findReachableIp() {
Common & instace= Common::getInstance();
QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
for (const QHostAddress &address : ipAddressesList) {
if (address.protocol() == QAbstractSocket::IPv4Protocol && !address.isLoopback()) {
......@@ -501,7 +523,9 @@ inline QString findReachableIp() {
for (int i = 254; i >= 1; --i) { // 从 254 开始递减
QString ip = currentSubnet + QString::number(i);
if (!pingAddress(ip)) {
qInfo() << "Found findReachableIp IP:" << ip;
if (!instace.pingAddress(ip)) {
qInfo() << "ping 不通的ip:" << ip;
return ip;
}
}
......
......@@ -13,6 +13,7 @@ TEMPLATE = app
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
DEFINES += APP_VERSION=\\\"1.3.2\\\"
DEFINES += QT_MESSAGELOGCONTEXT
DEFINES += QT_NO_DEBUG_OUTPUT
......@@ -130,7 +131,9 @@ HEADERS += \
AlgorithmTaskManage.h \
BaseAlgorithm.h \
MqttSubscriber.h \
NonConnectedCameraHandle.h
NonConnectedCameraHandle.h \
RecognizedInfo.h
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
......
......@@ -133,7 +133,6 @@ MainWindow::MainWindow():isResetting(false)
this->mqttConfig= config.mqttConfig;
runOrRebootMqtt(mqttConfig,httpurl,serialNumber);
}
void MainWindow::runOrRebootMqtt(vides_data::MqttConfig &mqtt_config,QString &httpUrl,QString &serialNumber){
MqttSubscriber* subscriber = MqttSubscriber::getInstance(this);
subscriber->init(mqtt_config,httpUrl,serialNumber);
......@@ -499,7 +498,8 @@ void MainWindow::setIsResetting(bool running) {
//平台没有 盒子有 盒子关闭
void MainWindow::startCamera(const QString &httpurl){
if(this->isResetting.load(std::memory_order_acquire)){
qInfo()<<"正在执行复位GB28181 程序,请等待";
qInfo()<<"正在执行复位GB28181 程序,请等待";
return;
}
Common & instace= Common::getInstance();
MediaFaceImage* mediaFaceImage= MediaFaceImage::getInstance();
......
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