Commit 6654fa06 by 郭峰

Merge branch 'feature-1007488' into 'release'

修改一建配置Ip的问题

See merge request !35
parents a9b859c3 4e92ddc7
...@@ -17,12 +17,12 @@ NonConnectedCameraHandle* NonConnectedCameraHandle::getInstance() ...@@ -17,12 +17,12 @@ NonConnectedCameraHandle* NonConnectedCameraHandle::getInstance()
{ {
m_instance = new NonConnectedCameraHandle(); // 创建新的实例并指向它 m_instance = new NonConnectedCameraHandle(); // 创建新的实例并指向它
} }
return m_instance; // 返回指向实例的指针 return m_instance; // 返回指向实例的指针
} }
bool NonConnectedCameraHandle::changeCameraIp(vides_data::localDevice &device){ bool NonConnectedCameraHandle::changeCameraIp(vides_data::localDevice &device){
QString localMac, subnetMask, gateway; QString localMac, subnetMask, gateway;
bool success =vides_data::GetNetworkInfoByQNetworkInterface(localMac, subnetMask, gateway); bool success =vides_data::GetNetworkInfoByQNetworkInterface(localMac, subnetMask, gateway);
if (!success) { if (!success) {
...@@ -31,27 +31,29 @@ bool NonConnectedCameraHandle::changeCameraIp(vides_data::localDevice &device){ ...@@ -31,27 +31,29 @@ bool NonConnectedCameraHandle::changeCameraIp(vides_data::localDevice &device){
} }
QByteArray && byJson = localMac.toUtf8(); QByteArray && byJson = localMac.toUtf8();
const char * mac= byJson.data(); const char * mac= byJson.data();
QByteArray && byJsonIp = device.reachableIp.toUtf8(); QByteArray && byJsonIp = device.reachableIp.toUtf8();
const char * charIp= byJsonIp.data(); const char * charIp= byJsonIp.data();
signed char newEncryptPsw[512] = { 0 }; signed char newEncryptPsw[512] = { 0 };
int nMaxCount = 100; int nMaxCount = 100;
SXSDK_CONFIG_NET_COMMON* pRet = new SXSDK_CONFIG_NET_COMMON[nMaxCount]; SXSDK_CONFIG_NET_COMMON* pRet = new SXSDK_CONFIG_NET_COMMON[nMaxCount];
SXSDK_CONFIG_NET_COMMON* pDevice = nullptr; SXSDK_CONFIG_NET_COMMON* pDevice = nullptr;
ScopeSemaphoreExit guard([&pRet]() { ScopeSemaphoreExit guard([&pRet]() {
delete [] pRet; delete [] pRet;
}); });
memset(pRet, 0, sizeof(SXSDK_CONFIG_NET_COMMON) * nMaxCount); memset(pRet, 0, sizeof(SXSDK_CONFIG_NET_COMMON) * nMaxCount);
int nCount = XSDK_SearchDevicesSyn(pRet, nMaxCount); int nCount = XSDK_SearchDevicesSyn(pRet, nMaxCount);
if (nCount <= 0) { if (nCount <= 0) {
qInfo() << QString("SN(%1): 有线搜索不到设备").arg(device.sSn); qInfo() << QString("SN(%1): 有线搜索不到设备").arg(device.sSn);
//搜索不到设备也当不在,当不在同一网段
device.isMask=false;
return false; return false;
} }
for (int i = 0; i < nCount; i++) { for (int i = 0; i < nCount; i++) {
qInfo() << QString("[%1][IP:%2.%3.%4.%5][SN:%6][Mac:%7]") qInfo() << QString("[%1][IP:%2.%3.%4.%5][SN:%6][Mac:%7]")
.arg(i) .arg(i)
...@@ -61,7 +63,7 @@ bool NonConnectedCameraHandle::changeCameraIp(vides_data::localDevice &device){ ...@@ -61,7 +63,7 @@ bool NonConnectedCameraHandle::changeCameraIp(vides_data::localDevice &device){
.arg(pRet[i].HostIP.c[3]) .arg(pRet[i].HostIP.c[3])
.arg(pRet[i].sSn) .arg(pRet[i].sSn)
.arg(pRet[i].sMac); .arg(pRet[i].sMac);
if (QString(pRet[i].sSn) == device.sSn) { if (QString(pRet[i].sSn) == device.sSn) {
pDevice = new SXSDK_CONFIG_NET_COMMON; pDevice = new SXSDK_CONFIG_NET_COMMON;
memset(pDevice, 0, sizeof(SXSDK_CONFIG_NET_COMMON)); memset(pDevice, 0, sizeof(SXSDK_CONFIG_NET_COMMON));
...@@ -69,7 +71,7 @@ bool NonConnectedCameraHandle::changeCameraIp(vides_data::localDevice &device){ ...@@ -69,7 +71,7 @@ bool NonConnectedCameraHandle::changeCameraIp(vides_data::localDevice &device){
break; break;
} }
} }
if (pDevice == nullptr) { if (pDevice == nullptr) {
qInfo() << QString("SN(%1): 有线未找到匹配的设备").arg(device.sSn); qInfo() << QString("SN(%1): 有线未找到匹配的设备").arg(device.sSn);
} else { } else {
...@@ -81,33 +83,40 @@ bool NonConnectedCameraHandle::changeCameraIp(vides_data::localDevice &device){ ...@@ -81,33 +83,40 @@ bool NonConnectedCameraHandle::changeCameraIp(vides_data::localDevice &device){
.arg(pDevice->HostIP.c[2]) .arg(pDevice->HostIP.c[2])
.arg(pDevice->HostIP.c[3]) .arg(pDevice->HostIP.c[3])
.arg(pDevice->sMac); .arg(pDevice->sMac);
// 在这里进行设备IP地址的更改操作 // 在这里进行设备IP地址的更改操作
SXSDK_CONFIG_NET_COMMON_V2 devInfo; SXSDK_CONFIG_NET_COMMON_V2 devInfo;
memset(&devInfo, 0, sizeof(SXSDK_CONFIG_NET_COMMON_V2)); memset(&devInfo, 0, sizeof(SXSDK_CONFIG_NET_COMMON_V2));
QString new_ip=QString("%1.%2.%3.%4") QString new_ip=QString("%1.%2.%3.%4")
.arg(pDevice->HostIP.c[0]) .arg(pDevice->HostIP.c[0])
.arg(pDevice->HostIP.c[1]) .arg(pDevice->HostIP.c[1])
.arg(pDevice->HostIP.c[2]) .arg(pDevice->HostIP.c[2])
.arg(pDevice->HostIP.c[3]); .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; device.isMask=true;
}else{ }else{
device.isMask=false; device.isMask=false;
} }
device.nDevPort=pDevice->TCPPort; device.nDevPort=pDevice->TCPPort;
XSDK_EncryptPassword("", newEncryptPsw, 512); XSDK_EncryptPassword("", newEncryptPsw, 512);
device.UserName= QString::fromUtf8(pDevice->DefaultUser,sizeof(pDevice->DefaultUser)); device.UserName= QString::fromUtf8(pDevice->DefaultUser,sizeof(pDevice->DefaultUser));
device.password= QString::fromUtf8(pDevice->DefaultPwd,sizeof(pDevice->DefaultPwd)); device.password= QString::fromUtf8(pDevice->DefaultPwd,sizeof(pDevice->DefaultPwd));
devInfo.HostIP.l= inet_addr(charIp); devInfo.HostIP.l= inet_addr(charIp);
devInfo.Gateway.l = inet_addr(gateway.toStdString().c_str()); devInfo.Gateway.l = inet_addr(gateway.toStdString().c_str());
devInfo.Submask.l = inet_addr(subnetMask.toStdString().c_str()); devInfo.Submask.l = inet_addr(subnetMask.toStdString().c_str());
strcpy(devInfo.HostName, pDevice->HostName); strcpy(devInfo.HostName, pDevice->HostName);
devInfo.HttpPort = pDevice->HttpPort; devInfo.HttpPort = pDevice->HttpPort;
strcpy(devInfo.LocalMac, mac); strcpy(devInfo.LocalMac, mac);
...@@ -124,19 +133,19 @@ bool NonConnectedCameraHandle::changeCameraIp(vides_data::localDevice &device){ ...@@ -124,19 +133,19 @@ bool NonConnectedCameraHandle::changeCameraIp(vides_data::localDevice &device){
strcpy(devInfo.UserName, pDevice->DefaultUser); strcpy(devInfo.UserName, pDevice->DefaultUser);
int nRet = XSDK_ModifyDevIP((const char*)&devInfo, sizeof(SXSDK_CONFIG_NET_COMMON_V2), 10000); int nRet = XSDK_ModifyDevIP((const char*)&devInfo, sizeof(SXSDK_CONFIG_NET_COMMON_V2), 10000);
delete pDevice; delete pDevice;
if(nRet<0){ if(nRet<0){
qInfo()<<QString("SN(%1): 有线修复ip失败错误码:%2").arg(device.sSn).arg(nRet); qInfo()<<QString("SN(%1): 有线修复ip失败错误码:%2").arg(device.sSn).arg(nRet);
return false; return false;
} }
} }
return true; return true;
} }
bool NonConnectedCameraHandle::wifiChangeIp(QString &Ip, bool is_connect, bool NonConnectedCameraHandle::wifiChangeIp(QString &Ip, bool is_connect,
int h_device,QString &sSn){ int h_device,QString &sSn){
Common & instace= Common::getInstance(); Common & instace= Common::getInstance();
XSDK_CFG::NetWork_Wifi wif; XSDK_CFG::NetWork_Wifi wif;
printWifi(h_device,wif); printWifi(h_device,wif);
if(is_connect){ if(is_connect){
...@@ -146,7 +155,7 @@ bool NonConnectedCameraHandle::wifiChangeIp(QString &Ip, bool is_connect, ...@@ -146,7 +155,7 @@ bool NonConnectedCameraHandle::wifiChangeIp(QString &Ip, bool is_connect,
} }
} }
QString hex=instace.DecIpToHexIp(Ip); QString hex=instace.DecIpToHexIp(Ip);
QByteArray && byIp = hex.toUtf8(); QByteArray && byIp = hex.toUtf8();
const char * cpIp= byIp.data(); const char * cpIp= byIp.data();
wif.HostIP.SetValue(cpIp); wif.HostIP.SetValue(cpIp);
...@@ -154,7 +163,7 @@ bool NonConnectedCameraHandle::wifiChangeIp(QString &Ip, bool is_connect, ...@@ -154,7 +163,7 @@ bool NonConnectedCameraHandle::wifiChangeIp(QString &Ip, bool is_connect,
wif.KeyType.SetValue(1); wif.KeyType.SetValue(1);
wif.EncrypType.SetValue("AES"); wif.EncrypType.SetValue("AES");
wif.Auth.SetValue("WPA2PSK"); wif.Auth.SetValue("WPA2PSK");
const char* wipCfg = wif.ToString(); const char* wipCfg = wif.ToString();
char szOutBuffer[512] = { 0 }; char szOutBuffer[512] = { 0 };
int nLen = sizeof(szOutBuffer); int nLen = sizeof(szOutBuffer);
...@@ -168,11 +177,11 @@ bool NonConnectedCameraHandle::wifiChangeIp(QString &Ip, bool is_connect, ...@@ -168,11 +177,11 @@ bool NonConnectedCameraHandle::wifiChangeIp(QString &Ip, bool is_connect,
bool NonConnectedCameraHandle::isWifiConnect(XSDK_HANDLE hDevice,XSDK_CFG::NetWork_Wifi &cfg){ bool NonConnectedCameraHandle::isWifiConnect(XSDK_HANDLE hDevice,XSDK_CFG::NetWork_Wifi &cfg){
char szOutBuffer[4000] = { 0 }; char szOutBuffer[4000] = { 0 };
int nInOutSize = sizeof(szOutBuffer); int nInOutSize = sizeof(szOutBuffer);
// 获取并解析配置 // 获取并解析配置
int nResult = XSDK_DevGetSysConfigSyn(hDevice, JK_NetWork_Wifi, szOutBuffer, &nInOutSize, 3000, EXCMD_CONFIG_GET); int nResult = XSDK_DevGetSysConfigSyn(hDevice, JK_NetWork_Wifi, szOutBuffer, &nInOutSize, 3000, EXCMD_CONFIG_GET);
qInfo()<<szOutBuffer; qInfo()<<szOutBuffer;
if (nResult >= 0) { if (nResult >= 0) {
cfg.Parse(szOutBuffer); cfg.Parse(szOutBuffer);
return strlen(cfg.SSID.Value())>0; return strlen(cfg.SSID.Value())>0;
...@@ -185,11 +194,11 @@ bool NonConnectedCameraHandle::isWifiConnect(XSDK_HANDLE hDevice,XSDK_CFG::NetWo ...@@ -185,11 +194,11 @@ bool NonConnectedCameraHandle::isWifiConnect(XSDK_HANDLE hDevice,XSDK_CFG::NetWo
void NonConnectedCameraHandle::printWifi(XSDK_HANDLE hDevice,XSDK_CFG::NetWork_Wifi &cfg){ void NonConnectedCameraHandle::printWifi(XSDK_HANDLE hDevice,XSDK_CFG::NetWork_Wifi &cfg){
char szOutBuffer[4000] = { 0 }; char szOutBuffer[4000] = { 0 };
int nInOutSize = sizeof(szOutBuffer); int nInOutSize = sizeof(szOutBuffer);
// 获取并解析配置 // 获取并解析配置
int nResult = XSDK_DevGetSysConfigSyn(hDevice, JK_NetWork_Wifi, szOutBuffer, &nInOutSize, 3000, EXCMD_CONFIG_GET); int nResult = XSDK_DevGetSysConfigSyn(hDevice, JK_NetWork_Wifi, szOutBuffer, &nInOutSize, 3000, EXCMD_CONFIG_GET);
qInfo()<<szOutBuffer; qInfo()<<szOutBuffer;
if (nResult >= 0) { if (nResult >= 0) {
cfg.Parse(szOutBuffer); cfg.Parse(szOutBuffer);
} else { } else {
...@@ -200,8 +209,6 @@ void NonConnectedCameraHandle::printWifi(XSDK_HANDLE hDevice,XSDK_CFG::NetWork_W ...@@ -200,8 +209,6 @@ void NonConnectedCameraHandle::printWifi(XSDK_HANDLE hDevice,XSDK_CFG::NetWork_W
int NonConnectedCameraHandle::sdkDevLoginSyn(QString sDevId, int nDevPort, int NonConnectedCameraHandle::sdkDevLoginSyn(QString sDevId, int nDevPort,
QString sUserName, QString sPassword, int nTimeout) { QString sUserName, QString sPassword, int nTimeout) {
SXSDKLoginParam *loginParam=new SXSDKLoginParam(); SXSDKLoginParam *loginParam=new SXSDKLoginParam();
ScopeSemaphoreExit guard([&loginParam]() { ScopeSemaphoreExit guard([&loginParam]() {
Common & instace= Common::getInstance(); Common & instace= Common::getInstance();
MainWindow::sp_this->setIsResetting(false); MainWindow::sp_this->setIsResetting(false);
...@@ -209,16 +216,16 @@ int NonConnectedCameraHandle::sdkDevLoginSyn(QString sDevId, int nDevPort, ...@@ -209,16 +216,16 @@ int NonConnectedCameraHandle::sdkDevLoginSyn(QString sDevId, int nDevPort,
}); });
QByteArray byteArray = sDevId.toUtf8(); QByteArray byteArray = sDevId.toUtf8();
char* cDevid=byteArray.data(); char* cDevid=byteArray.data();
strcpy(loginParam->sDevId, cDevid); strcpy(loginParam->sDevId, cDevid);
loginParam->nDevPort=nDevPort; loginParam->nDevPort=nDevPort;
QByteArray byteName = sUserName.toUtf8(); QByteArray byteName = sUserName.toUtf8();
char* cName=byteName.data(); char* cName=byteName.data();
strcpy(loginParam->sUserName, cName); strcpy(loginParam->sUserName, cName);
if(sPassword.length()>0){ if(sPassword.length()>0){
QByteArray bytePassword = sPassword.toUtf8(); QByteArray bytePassword = sPassword.toUtf8();
strcpy(loginParam->sPassword, bytePassword.constData()); strcpy(loginParam->sPassword, bytePassword.constData());
...@@ -239,7 +246,7 @@ bool NonConnectedCameraHandle::distributionNetwork(QString &ip,QString &sSn,int ...@@ -239,7 +246,7 @@ bool NonConnectedCameraHandle::distributionNetwork(QString &ip,QString &sSn,int
return false; return false;
} }
int old=hDevice; int old=hDevice;
MainWindow::sp_this->setIsResetting(true); MainWindow::sp_this->setIsResetting(true);
ScopeSemaphoreExit guard([]() { ScopeSemaphoreExit guard([]() {
MainWindow::sp_this->setIsResetting(false); MainWindow::sp_this->setIsResetting(false);
...@@ -250,7 +257,7 @@ bool NonConnectedCameraHandle::distributionNetwork(QString &ip,QString &sSn,int ...@@ -250,7 +257,7 @@ bool NonConnectedCameraHandle::distributionNetwork(QString &ip,QString &sSn,int
reachableIp=ip; reachableIp=ip;
qInfo()<<QString("SN(%1): reachableIpIP地址%2").arg(sSn).arg(ip); qInfo()<<QString("SN(%1): reachableIpIP地址%2").arg(sSn).arg(ip);
qInfo()<<QString("SN(%1): IP地址%2").arg(sSn).arg(reachableIp); qInfo()<<QString("SN(%1): IP地址%2").arg(sSn).arg(reachableIp);
}else { }else {
reachableIp= vides_data::findReachableIp(); reachableIp= vides_data::findReachableIp();
if (!reachableIp.isEmpty()) { if (!reachableIp.isEmpty()) {
......
...@@ -438,27 +438,49 @@ inline bool isInSameSubnet(const QString &ip1, const QString &ip2, const QString ...@@ -438,27 +438,49 @@ inline bool isInSameSubnet(const QString &ip1, const QString &ip2, const QString
// 比较结果 // 比较结果
return result1 == result2; return result1 == result2;
} }
// 获取本地 MAC 地址、子网掩码和网关IP
inline bool GetNetworkInfoByQNetworkInterface(QString &mac, QString &subnetMask, QString &gateway) { inline bool GetNetworkInfoByQNetworkInterface(QString &mac, QString &subnetMask, QString &gateway) {
QList<QNetworkInterface> interfaces = QNetworkInterface::allInterfaces(); QList<QNetworkInterface> interfaces = QNetworkInterface::allInterfaces();
foreach (QNetworkInterface interface, interfaces) { foreach (QNetworkInterface interface, interfaces) {
if (interface.flags().testFlag(QNetworkInterface::IsUp) && if (interface.flags().testFlag(QNetworkInterface::IsUp) &&
interface.flags().testFlag(QNetworkInterface::IsRunning) && interface.flags().testFlag(QNetworkInterface::IsRunning) &&
!interface.flags().testFlag(QNetworkInterface::IsLoopBack)) { !interface.flags().testFlag(QNetworkInterface::IsLoopBack)) {
mac = interface.hardwareAddress(); mac = interface.hardwareAddress();
QList<QNetworkAddressEntry> addressEntries = interface.addressEntries(); QList<QNetworkAddressEntry> addressEntries = interface.addressEntries();
foreach (QNetworkAddressEntry entry, addressEntries) { foreach (QNetworkAddressEntry entry, addressEntries) {
if (entry.ip().protocol() == QAbstractSocket::IPv4Protocol) { if (entry.ip().protocol() == QAbstractSocket::IPv4Protocol) {
subnetMask = entry.netmask().toString(); 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) { 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