Commit 4e92ddc7 by “liusq”

修改一建配置Ip的问题

parent e93312cf
......@@ -17,12 +17,12 @@ NonConnectedCameraHandle* NonConnectedCameraHandle::getInstance()
{
m_instance = new NonConnectedCameraHandle(); // 创建新的实例并指向它
}
return m_instance; // 返回指向实例的指针
}
bool NonConnectedCameraHandle::changeCameraIp(vides_data::localDevice &device){
QString localMac, subnetMask, gateway;
bool success =vides_data::GetNetworkInfoByQNetworkInterface(localMac, subnetMask, gateway);
if (!success) {
......@@ -31,27 +31,29 @@ bool NonConnectedCameraHandle::changeCameraIp(vides_data::localDevice &device){
}
QByteArray && byJson = localMac.toUtf8();
const char * mac= byJson.data();
QByteArray && byJsonIp = device.reachableIp.toUtf8();
const char * charIp= byJsonIp.data();
signed char newEncryptPsw[512] = { 0 };
int nMaxCount = 100;
SXSDK_CONFIG_NET_COMMON* pRet = new SXSDK_CONFIG_NET_COMMON[nMaxCount];
SXSDK_CONFIG_NET_COMMON* pDevice = nullptr;
ScopeSemaphoreExit guard([&pRet]() {
delete [] pRet;
});
memset(pRet, 0, sizeof(SXSDK_CONFIG_NET_COMMON) * nMaxCount);
int nCount = XSDK_SearchDevicesSyn(pRet, nMaxCount);
if (nCount <= 0) {
qInfo() << QString("SN(%1): 有线搜索不到设备").arg(device.sSn);
//搜索不到设备也当不在,当不在同一网段
device.isMask=false;
return false;
}
for (int i = 0; i < nCount; i++) {
qInfo() << QString("[%1][IP:%2.%3.%4.%5][SN:%6][Mac:%7]")
.arg(i)
......@@ -61,7 +63,7 @@ bool NonConnectedCameraHandle::changeCameraIp(vides_data::localDevice &device){
.arg(pRet[i].HostIP.c[3])
.arg(pRet[i].sSn)
.arg(pRet[i].sMac);
if (QString(pRet[i].sSn) == device.sSn) {
pDevice = new SXSDK_CONFIG_NET_COMMON;
memset(pDevice, 0, sizeof(SXSDK_CONFIG_NET_COMMON));
......@@ -69,7 +71,7 @@ bool NonConnectedCameraHandle::changeCameraIp(vides_data::localDevice &device){
break;
}
}
if (pDevice == nullptr) {
qInfo() << QString("SN(%1): 有线未找到匹配的设备").arg(device.sSn);
} else {
......@@ -81,33 +83,40 @@ bool NonConnectedCameraHandle::changeCameraIp(vides_data::localDevice &device){
.arg(pDevice->HostIP.c[2])
.arg(pDevice->HostIP.c[3])
.arg(pDevice->sMac);
// 在这里进行设备IP地址的更改操作
SXSDK_CONFIG_NET_COMMON_V2 devInfo;
memset(&devInfo, 0, sizeof(SXSDK_CONFIG_NET_COMMON_V2));
QString new_ip=QString("%1.%2.%3.%4")
.arg(pDevice->HostIP.c[0])
.arg(pDevice->HostIP.c[1])
.arg(pDevice->HostIP.c[2])
.arg(pDevice->HostIP.c[3]);
if(vides_data::isInSameSubnet(device.reachableIp,new_ip,subnetMask)){
QString new_Submask=QString("%1.%2.%3.%4")
.arg(pDevice->Submask.c[0])
.arg(pDevice->Submask.c[1])
.arg(pDevice->Submask.c[2])
.arg(pDevice->Submask.c[3]);
if(vides_data::isInSameSubnet(device.reachableIp,new_ip,new_Submask)){
device.isMask=true;
}else{
device.isMask=false;
}
device.nDevPort=pDevice->TCPPort;
XSDK_EncryptPassword("", newEncryptPsw, 512);
device.UserName= QString::fromUtf8(pDevice->DefaultUser,sizeof(pDevice->DefaultUser));
device.password= QString::fromUtf8(pDevice->DefaultPwd,sizeof(pDevice->DefaultPwd));
devInfo.HostIP.l= inet_addr(charIp);
devInfo.Gateway.l = inet_addr(gateway.toStdString().c_str());
devInfo.Submask.l = inet_addr(subnetMask.toStdString().c_str());
strcpy(devInfo.HostName, pDevice->HostName);
devInfo.HttpPort = pDevice->HttpPort;
strcpy(devInfo.LocalMac, mac);
......@@ -124,19 +133,19 @@ bool NonConnectedCameraHandle::changeCameraIp(vides_data::localDevice &device){
strcpy(devInfo.UserName, pDevice->DefaultUser);
int nRet = XSDK_ModifyDevIP((const char*)&devInfo, sizeof(SXSDK_CONFIG_NET_COMMON_V2), 10000);
delete pDevice;
if(nRet<0){
qInfo()<<QString("SN(%1): 有线修复ip失败错误码:%2").arg(device.sSn).arg(nRet);
return false;
}
}
return true;
}
bool NonConnectedCameraHandle::wifiChangeIp(QString &Ip, bool is_connect,
int h_device,QString &sSn){
Common & instace= Common::getInstance();
XSDK_CFG::NetWork_Wifi wif;
printWifi(h_device,wif);
if(is_connect){
......@@ -146,7 +155,7 @@ bool NonConnectedCameraHandle::wifiChangeIp(QString &Ip, bool is_connect,
}
}
QString hex=instace.DecIpToHexIp(Ip);
QByteArray && byIp = hex.toUtf8();
const char * cpIp= byIp.data();
wif.HostIP.SetValue(cpIp);
......@@ -154,7 +163,7 @@ bool NonConnectedCameraHandle::wifiChangeIp(QString &Ip, bool is_connect,
wif.KeyType.SetValue(1);
wif.EncrypType.SetValue("AES");
wif.Auth.SetValue("WPA2PSK");
const char* wipCfg = wif.ToString();
char szOutBuffer[512] = { 0 };
int nLen = sizeof(szOutBuffer);
......@@ -168,11 +177,11 @@ bool NonConnectedCameraHandle::wifiChangeIp(QString &Ip, bool is_connect,
bool NonConnectedCameraHandle::isWifiConnect(XSDK_HANDLE hDevice,XSDK_CFG::NetWork_Wifi &cfg){
char szOutBuffer[4000] = { 0 };
int nInOutSize = sizeof(szOutBuffer);
// 获取并解析配置
int nResult = XSDK_DevGetSysConfigSyn(hDevice, JK_NetWork_Wifi, szOutBuffer, &nInOutSize, 3000, EXCMD_CONFIG_GET);
qInfo()<<szOutBuffer;
if (nResult >= 0) {
cfg.Parse(szOutBuffer);
return strlen(cfg.SSID.Value())>0;
......@@ -185,11 +194,11 @@ bool NonConnectedCameraHandle::isWifiConnect(XSDK_HANDLE hDevice,XSDK_CFG::NetWo
void NonConnectedCameraHandle::printWifi(XSDK_HANDLE hDevice,XSDK_CFG::NetWork_Wifi &cfg){
char szOutBuffer[4000] = { 0 };
int nInOutSize = sizeof(szOutBuffer);
// 获取并解析配置
int nResult = XSDK_DevGetSysConfigSyn(hDevice, JK_NetWork_Wifi, szOutBuffer, &nInOutSize, 3000, EXCMD_CONFIG_GET);
qInfo()<<szOutBuffer;
if (nResult >= 0) {
cfg.Parse(szOutBuffer);
} else {
......@@ -200,8 +209,6 @@ void NonConnectedCameraHandle::printWifi(XSDK_HANDLE hDevice,XSDK_CFG::NetWork_W
int NonConnectedCameraHandle::sdkDevLoginSyn(QString sDevId, int nDevPort,
QString sUserName, QString sPassword, int nTimeout) {
SXSDKLoginParam *loginParam=new SXSDKLoginParam();
ScopeSemaphoreExit guard([&loginParam]() {
Common & instace= Common::getInstance();
MainWindow::sp_this->setIsResetting(false);
......@@ -209,16 +216,16 @@ int NonConnectedCameraHandle::sdkDevLoginSyn(QString sDevId, int nDevPort,
});
QByteArray byteArray = sDevId.toUtf8();
char* cDevid=byteArray.data();
strcpy(loginParam->sDevId, cDevid);
loginParam->nDevPort=nDevPort;
QByteArray byteName = sUserName.toUtf8();
char* cName=byteName.data();
strcpy(loginParam->sUserName, cName);
if(sPassword.length()>0){
QByteArray bytePassword = sPassword.toUtf8();
strcpy(loginParam->sPassword, bytePassword.constData());
......@@ -239,7 +246,7 @@ bool NonConnectedCameraHandle::distributionNetwork(QString &ip,QString &sSn,int
return false;
}
int old=hDevice;
MainWindow::sp_this->setIsResetting(true);
ScopeSemaphoreExit guard([]() {
MainWindow::sp_this->setIsResetting(false);
......@@ -250,7 +257,7 @@ bool NonConnectedCameraHandle::distributionNetwork(QString &ip,QString &sSn,int
reachableIp=ip;
qInfo()<<QString("SN(%1): reachableIpIP地址%2").arg(sSn).arg(ip);
qInfo()<<QString("SN(%1): IP地址%2").arg(sSn).arg(reachableIp);
}else {
reachableIp= vides_data::findReachableIp();
if (!reachableIp.isEmpty()) {
......
......@@ -438,27 +438,49 @@ 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) {
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)) {
interface.flags().testFlag(QNetworkInterface::IsRunning) &&
!interface.flags().testFlag(QNetworkInterface::IsLoopBack)) {
mac = interface.hardwareAddress();
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;
// 获取网关地址
QProcess process;
process.start("ip", QStringList() << "route");
if (!process.waitForStarted()) {
qInfo() << "Failed to start 'ip route' process.";
continue;
}
if (!process.waitForFinished()) {
process.kill();
qInfo() << "Process 'ip route' timed out.";
continue;
}
QString output = process.readAllStandardOutput();
QStringList lines = output.split('\n');
foreach (QString line, lines) {
if (line.startsWith("default via")) {
QStringList parts = line.split(QRegExp("\\s+"));
if (parts.size() >= 3) {
gateway = parts[2];
return true;
}
}
}
}
}
}
}
return false; // Return false if no suitable interface is found
return false; // Return false if no suitable interface is found or gateway not found
}
inline bool pingAddress(const QString &address) {
......
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