qt/LedOK/devicepanel.cpp

481 lines
21 KiB
C++
Raw Normal View History

2022-08-25 18:37:24 +08:00
#include "devicepanel.h"
#include "devicectrlpanel.h"
#include "tools.h"
2023-04-18 14:14:46 +08:00
#include "gutil/qgui.h"
2022-08-25 18:37:24 +08:00
#include <QAction>
#include <QBoxLayout>
#include <QEvent>
#include <QHeaderView>
#include <QLineEdit>
#include <QNetworkDatagram>
2022-09-28 18:21:57 +08:00
#include <QNetworkInterface>
#include <QMessageBox>
2022-08-25 18:37:24 +08:00
DevicePanel *gDevicePanel;
QList<LedCard*> *gSelCards;
//设备管理响应页面
2022-09-30 18:08:39 +08:00
DevicePanel::DevicePanel(QSettings &settings, QWidget *parent) : QWidget(parent) {
2022-08-25 18:37:24 +08:00
gDevicePanel = this;
gSelCards = &mSelCards;
setAutoFillBackground(true);
QPalette pal;
pal.setBrush(QPalette::Window, QColor(0xeeeeee));
setPalette(pal);
auto vBox = new VBox(this);
2022-08-25 18:37:24 +08:00
vBox->setContentsMargins(0, 0, 0, 0);
vBox->setSpacing(0);
auto hBox = new HBox(vBox);
2022-08-25 18:37:24 +08:00
hBox->setContentsMargins(6, 2, 6, 2);
hBox->setSpacing(2);
auto label = new QLabel();
2023-04-18 14:14:46 +08:00
label->setPixmap({":/res/device/DeviceNum_All.png"});
2022-08-25 18:37:24 +08:00
label->setScaledContents(true);
label->setFixedSize(33, 24);
hBox->addWidget(label);
label_3 = new QLabel(tr("All"));
hBox->addWidget(label_3);
nDeviceNum = new QLabel();
nDeviceNum->setFixedSize(40, 20);
hBox->addWidget(nDeviceNum);
auto fdSearch = new QLineEdit();
fdSearch->setClearButtonEnabled(true);
fdSearch->setFixedSize(220, 30);
2023-04-18 14:14:46 +08:00
fdSearch->addAction(new QAction(QIcon(":/res/program/bnSearch.png"), QString()), QLineEdit::LeadingPosition);
2022-08-25 18:37:24 +08:00
fdSearch->setStyleSheet("border: 1px solid #aaaaaa;");
2023-04-18 14:14:46 +08:00
connect(fdSearch, &QLineEdit::textChanged, this, [this](const QString &search) {
auto cnt = mDeviceTable->topLevelItemCount();
if(search.isEmpty()) for(int i=0; i<cnt; i++) mDeviceTable->topLevelItem(i)->setHidden(false);
else for(int i=0; i<cnt; i++) {
auto item = mDeviceTable->topLevelItem(i);
item->setHidden(! (item->text(DeviceTable_ID).contains(search) || item->text(DeviceTable_Remark).contains(search) || item->text(DeviceTable_IP).contains(search)));
}
});
2022-08-25 18:37:24 +08:00
hBox->addWidget(fdSearch);
hBox->addSpacing(10);
fdCardNumInfo = new QLabel(tr("Current Screen")+": "+tr("none"));
fdCardNumInfo->setStyleSheet("QLabel{color: #15f;}");
fdCardNumInfo->hide();
hBox->addWidget(fdCardNumInfo);
hBox->addStretch();
auto areaFlash = new QWidget();
areaFlash->setFixedSize(100, 34);
bnSpecifyIP = new QComboBox(areaFlash);
bnSpecifyIP->addItem(tr("Specify IP"));
bnSpecifyIP->setGeometry(0, 0, areaFlash->width(), areaFlash->height());
bnSpecifyIP->setItemDelegate(new QStyledItemDelegate);
connect(bnSpecifyIP, &QComboBox::textActivated, this, [this] {
2022-09-28 18:21:57 +08:00
mUdpTimer.stop();
specifyIPDlg->exec();
mUdpTimer.start(30000);
2022-08-25 18:37:24 +08:00
});
bnSpecifyIP->setStyleSheet(R"rrr(
QComboBox {
border: none;
border-radius: 5px;
background-color: #19c;
}
QComboBox:hover {background-color: #08b;}
QComboBox::drop-down {width: 25px;}
2023-04-18 14:14:46 +08:00
QComboBox::down-arrow {image: url(:/res/device/FlashArrow.png);}
2022-08-25 18:37:24 +08:00
QComboBox QAbstractItemView::item {
height: 28px;
color: #fff;
}
QComboBox QAbstractItemView::item:selected {background-color: #09c;}
)rrr");
2022-09-28 18:21:57 +08:00
btnRefresh = new QPushButton(tr("Refresh"), areaFlash);
btnRefresh->setGeometry(0, 0, 75, areaFlash->height());
connect(btnRefresh, &QPushButton::clicked, this, [this] {
2023-04-18 14:14:46 +08:00
mLedCards.clear();
2022-08-25 18:37:24 +08:00
mDeviceTable->onCheckAll(false);
mDeviceTable->clear();
nDeviceNum->setText("0");
sendGetInfo();
});
2022-09-28 18:21:57 +08:00
btnRefresh->setStyleSheet(R"rrr(
2022-08-25 18:37:24 +08:00
QPushButton {
border-radius: 5px;
background-color: #19c;
color: #fff;
}
QPushButton:hover {background-color: #08b;}
)rrr");
hBox->addWidget(areaFlash);
mHBox = new QHBoxLayout();
mDeviceTable = new LoQTreeWidget(this);
mDeviceTable->setIndentation(0);
mDeviceTable->setSortingEnabled(true);
mHBox->addWidget(mDeviceTable);
vBox->addLayout(mHBox);
2022-09-28 18:21:57 +08:00
connect(&mUdpTimer, &QTimer::timeout, this, &DevicePanel::sendGetInfo);
connect(&mUdpSocket, &QUdpSocket::readyRead, this, [this] {
while(mUdpSocket.hasPendingDatagrams()) {
auto gram = mUdpSocket.receiveDatagram();
2022-08-25 18:37:24 +08:00
auto data = gram.data();
if(data.isEmpty()) continue;
auto senderAddress = gram.senderAddress();
bool ok = true;
if(senderAddress.protocol()==QUdpSocket::IPv6Protocol) senderAddress.setAddress(senderAddress.toIPv4Address(&ok));
2022-09-26 18:10:47 +08:00
auto addr = ok ? senderAddress.toString() : "";
2022-08-25 18:37:24 +08:00
if(data.startsWith("{\"")) {
QJsonParseError error;
QJsonDocument document = QJsonDocument::fromJson(gram.data(), &error);
if(error.error != QJsonParseError::NoError) {
qDebug()<<"DeviceItem OnProHttpResponse 解析json文件错误";
continue;
}
2022-09-28 18:21:57 +08:00
parseInfo(document.object(), addr);
2022-08-25 18:37:24 +08:00
} else {
2022-09-30 18:08:39 +08:00
auto bytes = gram.data();
2023-04-18 14:14:46 +08:00
auto packet = (Packet *)bytes.data();
2022-09-28 18:21:57 +08:00
LedCard *pLedCard = new LedCard(this);//alahover 没有释放
2023-04-18 14:14:46 +08:00
pLedCard->m_strCardId = packet->serialCode;
2022-09-28 18:21:57 +08:00
pLedCard->m_strCardIp = addr;
2023-04-18 14:14:46 +08:00
for(int i=0;i<mLedCards.count();i++) if(mLedCards.at(i)->m_strCardId==pLedCard->m_strCardId ) {//如果已经存在在列表中
auto pOldLedCard = mLedCards.at(i);//获取到旧的对象指针
2022-09-28 18:21:57 +08:00
pOldLedCard->m_bOnLine = true;
if(pOldLedCard->CompareHTTPGetParam(pLedCard)==false) {//如果ip地址变化了
pOldLedCard->copyLedCardHTTPGetParam(pLedCard);
2022-09-30 18:08:39 +08:00
addLedCard(pOldLedCard);//发送信号到界面
2022-09-28 18:21:57 +08:00
} else {
//post失败的概率比较小所以这里是否需要平凡的更新不需要刻意屏蔽该操作改为在其他点击操作中进行
2022-09-30 18:08:39 +08:00
clearUdpHeartbeat(pOldLedCard);
2022-09-28 18:21:57 +08:00
}
delete pLedCard;
return;
}
2023-04-18 14:14:46 +08:00
mLedCards.append(pLedCard);
2022-09-30 18:08:39 +08:00
addLedCard(pLedCard);//发送信号到界面
2022-08-25 18:37:24 +08:00
}
}
});
m_headerItem = new QTreeWidgetItem();
for(int i=1; i<DeviceTable_End; i++) m_headerItem->setTextAlignment(i, Qt::AlignCenter);
mDeviceTable->setHeaderItem(m_headerItem);
mDeviceTable->header()->setSectionResizeMode(DeviceTable_Check, QHeaderView::Fixed);
2023-04-18 14:14:46 +08:00
mDeviceTable->setColumnWidth(DeviceTable_Check, 36);
2022-08-25 18:37:24 +08:00
mDeviceTable->header()->setSectionResizeMode(DeviceTable_Online, QHeaderView::Fixed);
mDeviceTable->setColumnWidth(DeviceTable_Online, 48);
mDeviceTable->header()->setSectionResizeMode(DeviceTable_ID, QHeaderView::Stretch);
mDeviceTable->header()->setSectionResizeMode(DeviceTable_IP, QHeaderView::Stretch);
mDeviceTable->header()->setSectionResizeMode(DeviceTable_ScreenSize, QHeaderView::Fixed);
mDeviceTable->setColumnWidth(DeviceTable_ScreenSize, 108);
mDeviceTable->header()->setSectionResizeMode(DeviceTable_Remark, QHeaderView::Stretch);
mDeviceTable->header()->setSectionResizeMode(DeviceTable_Brightness, QHeaderView::Fixed);
mDeviceTable->setColumnWidth(DeviceTable_Brightness, 108);
mDeviceTable->header()->setSectionResizeMode(DeviceTable_Power, QHeaderView::Fixed);
mDeviceTable->setColumnWidth(DeviceTable_Power, 84);
mDeviceTable->header()->setSectionResizeMode(DeviceTable_Password, QHeaderView::Fixed);
mDeviceTable->setColumnWidth(DeviceTable_Password, 72);
mDeviceTable->header()->setSectionResizeMode(DeviceTable_Info, QHeaderView::Fixed);
mDeviceTable->setColumnWidth(DeviceTable_Info, 72);
mDeviceTable->header()->setSectionResizeMode(DeviceTable_Screenshot, QHeaderView::Fixed);
mDeviceTable->setColumnWidth(DeviceTable_Screenshot, 72);
connect(mDeviceTable, SIGNAL(sigCheckStateChanged(int)), this, SLOT(onCheckStateChanged(int)));
mDeviceTable->setSelectionMode(QAbstractItemView::SingleSelection);
mDeviceTable->hideColumn(0);
mDeviceTable->fdIsSelAll->hide();
specifyIPDlg = new QDialog(this);
specifyIPDlg->setWindowTitle(tr("Specify IP"));
vBox = new VBox(specifyIPDlg);
vBox->addStretch();
label = new QLabel(tr("Specify IP list"));
label->setAlignment(Qt::AlignCenter);
vBox->addWidget(label);
fdIP = new QTextEdit;
auto ipstr = settings.value("SpecifyIP").toString();
if(! ipstr.isEmpty()) fdIP->setText(ipstr);
fdIP->setPlaceholderText("192.168.1.1\n192.168.1.2\n ...");
fdIP->setMinimumWidth(160);
vBox->addWidget(fdIP);
hBox = new HBox(vBox);
hBox->addStretch();
auto bnSearch = new QPushButton(tr("Search"));
connect(bnSearch, &QPushButton::clicked, this, [this] {
QString ipsStr = fdIP->toPlainText();
if(ipsStr.isEmpty()) {
QMessageBox::warning(gMainWin, tr("Attention"), tr("Please input IP address!"));
return;
}
auto ips = ipsStr.split("\n", Qt::SkipEmptyParts);
if(ips.isEmpty()) {
QMessageBox::warning(gMainWin, tr("Attention"), tr("Please input IP address!"));
return;
}
QByteArray data = QJsonDocument({{"action", "getInfo"}}).toJson(QJsonDocument::Compact);
foreach(auto ip, ips) {
if(gDevicePanel->mUdpSocket.writeDatagram(data, QHostAddress(ip), 22222) != data.length()) qDebug() << "Specify IP write Failed." << ip;
}
specifyIPDlg->accept();
});
hBox->addWidget(bnSearch);
auto btnCancel = new QPushButton(tr("Cancel"));
connect(btnCancel, &QPushButton::clicked, specifyIPDlg, &QDialog::reject);
hBox->addWidget(btnCancel);
hBox->addStretch();
2022-08-25 18:37:24 +08:00
transUi();
sendGetInfo();
mUdpTimer.start(30000);
2022-08-25 18:37:24 +08:00
}
DevicePanel::~DevicePanel() {
2022-09-28 18:21:57 +08:00
mUdpSocket.close();
mUdpTimer.stop();
2022-08-25 18:37:24 +08:00
}
void DevicePanel::sendGetInfo() {
2023-04-18 14:14:46 +08:00
const QByteArray data = QJsonDocument({{"action", "getInfo"}}).toJson(QJsonDocument::Compact);
uchar ccc[]{0x7E, 0x7E, 0x7E, 0x90, 0x42, 0x72, 0x6F, 0x61, 0x64, 0x63, 0x61, 0x73, 0x74, 0x21,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x1C, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x9F};
if(mUdpSocket.writeDatagram(data, QHostAddress("255.255.255.255"), 22222) != data.length()) qDebug() << "getInfo write to 255.255.255.255 failed";
if(mUdpSocket.writeDatagram((char *)ccc, sizeof(ccc), QHostAddress("255.255.255.255"), 31296) != sizeof(ccc)) qDebug() << "getInfo write to 255.255.255.255 failed";
2022-08-25 18:37:24 +08:00
QList<QNetworkInterface> networkinterfaces = QNetworkInterface::allInterfaces();
foreach(QNetworkInterface face, networkinterfaces) {
auto flags = face.flags();
bool can = (flags & QNetworkInterface::IsRunning) && (flags & QNetworkInterface::CanBroadcast) && ! (flags & QNetworkInterface::IsLoopBack);
if(! can) continue;
auto addrEntries = face.addressEntries();
foreach(QNetworkAddressEntry addrEntry, addrEntries) {
auto ip = addrEntry.ip();
if(ip.protocol()!=QAbstractSocket::IPv4Protocol) continue;
auto broadcast = addrEntry.broadcast();
2023-04-18 14:14:46 +08:00
if(mUdpSocket.writeDatagram(data, broadcast, 22222) != data.length()) qDebug() << "getInfo write failed." << ip.toString() << "->" << broadcast.toString();
2022-08-25 18:37:24 +08:00
}
}
auto ipstr = fdIP->toPlainText();
if(! ipstr.isEmpty()) {
auto ips = ipstr.split("\n", Qt::SkipEmptyParts);
2023-04-18 14:14:46 +08:00
foreach(auto ip, ips) if(mUdpSocket.writeDatagram(data, QHostAddress(ip), 22222) != data.length()) qDebug() << "getInfo specify IP write failed." << ip;
}
2022-08-25 18:37:24 +08:00
}
void DevicePanel::ShowDevicesInfoOrControlWidget(int index){
m_intIndexFlagOfInfoOrControl=index;
if(index==MainPage_DeviceManager) { //设备信息列表页面
mDeviceTable->hideColumn(0);
mDeviceTable->fdIsSelAll->hide();
if(mDeviceCtrlPanel!=nullptr) {
for(int j=DeviceTable_ScreenSize;j<DeviceTable_End;j++) mDeviceTable->showColumn(j);
mDeviceTable->setMaximumWidth(0xffffff);
mDeviceCtrlPanel->hide();
fdCardNumInfo->hide();
}
} else if(index==MainPage_Setting) { //终端控制页面
mDeviceTable->showColumn(0);
mDeviceTable->fdIsSelAll->show();
for(int j=DeviceTable_ScreenSize; j<DeviceTable_End; j++) mDeviceTable->hideColumn(j);
if(mDeviceCtrlPanel==nullptr) mHBox->addWidget(mDeviceCtrlPanel = new DeviceCtrlPanel(this));
else mDeviceCtrlPanel->show();
fdCardNumInfo->show();
mDeviceTable->setMaximumWidth(340);
GetSelectDeviceNum();
emit sigSelectedDeviceList(&mSelCards);
}
int cnt = mDeviceTable->topLevelItemCount();
if(cnt>0)//切换到终点管理页面是刷新列表中控制卡的状态参数
for(int i=0; i<cnt; i++) static_cast<DeviceItem*>(mDeviceTable->topLevelItem(i))->SetMainTabWidgetIndex(m_intIndexFlagOfInfoOrControl);
}
void DevicePanel::changeEvent(QEvent *event) {
QWidget::changeEvent(event);
if(event->type() == QEvent::LanguageChange) transUi();
}
void DevicePanel::transUi() {
2022-09-28 18:21:57 +08:00
btnRefresh->setText(tr("Refresh"));
2022-08-25 18:37:24 +08:00
bnSpecifyIP->setItemText(0,tr("Specify IP"));
label_3->setText(tr("All"));
2023-04-18 14:14:46 +08:00
m_headerItem->setText(DeviceTable_Online, tr("Online"));
m_headerItem->setText(DeviceTable_ID, tr("Screen ID"));
m_headerItem->setText(DeviceTable_IP, tr("Screen IP"));
m_headerItem->setText(DeviceTable_ScreenSize, tr("Screen Size"));
m_headerItem->setText(DeviceTable_Remark, tr("Remark Name"));
m_headerItem->setText(DeviceTable_Screenshot, tr("readback pic"));
m_headerItem->setText(DeviceTable_Brightness, tr("Cur Brightness"));
m_headerItem->setText(DeviceTable_Power, tr("Power Status"));
m_headerItem->setText(DeviceTable_Password, tr("Security"));
m_headerItem->setText(DeviceTable_Info, tr("More Info"));
2022-08-25 18:37:24 +08:00
auto cnt = mDeviceTable->topLevelItemCount();
for(int i=0; i<cnt; i++) static_cast<DeviceItem*>(mDeviceTable->topLevelItem(i))->refreshLable();
}
void DevicePanel::OnControlTcpSend(int iProgramIndex){
int cnt = mDeviceTable->topLevelItemCount();
2022-09-28 18:21:57 +08:00
for(int i=0; i<cnt; i++) if(mDeviceTable->topLevelItem(i)->checkState(0) == Qt::Checked) static_cast<DeviceItem*>(mDeviceTable->topLevelItem(i))->OnControlTcpSend(iProgramIndex);
2022-08-25 18:37:24 +08:00
}
void DevicePanel::OnControlSendJson(QJsonObject &json,QString strFanYi){
int cnt = mDeviceTable->topLevelItemCount();
for(int i=0; i<cnt; i++) if(mDeviceTable->topLevelItem(i)->checkState(0) == Qt::Checked) {
DeviceItem *item = static_cast<DeviceItem*>(mDeviceTable->topLevelItem(i));
item->HttpPostByTypeJsonObject(json);
2023-04-18 14:14:46 +08:00
QString strCardId=item->mCard->m_strCardId;
2022-08-25 18:37:24 +08:00
mDeviceCtrlPanel->OnOutputInfo(strCardId+":"+tr("send")+"->"+strFanYi);
}
}
int DevicePanel::GetSelectDeviceNum() {
int cnt = mDeviceTable->topLevelItemCount();
int iSelectCount=0;
mSelCards.clear();
for(int i=0; i<cnt; i++) if(mDeviceTable->topLevelItem(i)->checkState(0) == Qt::Checked) {
iSelectCount++;
2023-04-18 14:14:46 +08:00
mSelCards.append(static_cast<DeviceItem*>(mDeviceTable->topLevelItem(i))->mCard);
2022-08-25 18:37:24 +08:00
}
return iSelectCount;
}
void DevicePanel::OnOutputInfo(QString strInfo) {
if(m_intIndexFlagOfInfoOrControl==MainPage_Setting && mDeviceCtrlPanel!=nullptr) mDeviceCtrlPanel->OnOutputInfo(strInfo);
}
void DevicePanel::onCheckStateChanged(int f){
Q_UNUSED(f);
if(m_intIndexFlagOfInfoOrControl==MainPage_Setting){
GetSelectDeviceNum();
emit sigSelectedDeviceList(&mSelCards);
}
}
2023-04-18 14:14:46 +08:00
void DevicePanel::addLedCard(LedCard *card) {
2022-09-28 18:21:57 +08:00
int cnt = mDeviceTable->topLevelItemCount();
for(int i=0; i<cnt; i++) {
auto item = static_cast<DeviceItem*>(mDeviceTable->topLevelItem(i));
2023-04-18 14:14:46 +08:00
if(item->mCard->m_strCardId != card->m_strCardId) continue;
item->setData(DeviceTable_ID, 0, card->m_strCardId);
item->setData(DeviceTable_IP, 0, card->m_strCardIp);
item->setData(DeviceTable_ScreenSize, 0, QString("%1 x %2").arg(card->m_iWidth).arg(card->m_iHeight));
2022-09-28 18:21:57 +08:00
return;
}
2023-04-18 14:14:46 +08:00
new DeviceItem(card, mDeviceTable, this);
2022-09-28 18:21:57 +08:00
nDeviceNum->setText(QString::number(mDeviceTable->topLevelItemCount()));
2023-04-18 14:14:46 +08:00
QJsonObject json;
json.insert("_id", "SyncTime");
json.insert("_type", "SyncTime");
json.insert("time", QDateTime::currentDateTime().toMSecsSinceEpoch());
2023-04-23 17:01:35 +08:00
auto reply = NetReq("http://"+card->m_strCardIp+":2016/settings").timeout(120000).post(json);
2023-04-18 14:14:46 +08:00
connect(reply, &QNetworkReply::finished, reply, &QNetworkReply::deleteLater);
2022-09-28 18:21:57 +08:00
}
void DevicePanel::clearUdpHeartbeat(LedCard *p) {
int cnt = mDeviceTable->topLevelItemCount();
for(int i=0; i<cnt; i++) {
auto item = static_cast<DeviceItem*>(mDeviceTable->topLevelItem(i));
2023-04-18 14:14:46 +08:00
if(item->mCard->m_strCardId != p->m_strCardId) continue;
2022-09-28 18:21:57 +08:00
item->m_HeartbeatCount = 0;
if(item->m_ImageOnline) {
2023-04-18 14:14:46 +08:00
item->mCard->m_bOnLine = true;
item->m_ImageOnline->setPixmap(QPixmap(":/res/device/O_Online.png"));
2022-09-28 18:21:57 +08:00
}
return;
}
}
void DevicePanel::parseInfo(const QJsonObject &subObj, const QString &strIp) {
LedCard *pLedCard = new LedCard(this);//alahover 没有释放
pLedCard->m_strCardId=subObj["cardId"].toString();
if(! strIp.isEmpty()) pLedCard->m_strCardIp = strIp;
else {
pLedCard->m_strCardIp = subObj["newIp"].toString();
if(pLedCard->m_strCardIp.isEmpty()) pLedCard->m_strCardIp = subObj["ip"].toString();
}
pLedCard->m_iWidth=subObj["width"].toInt();
pLedCard->m_iHeight=subObj["height"].toInt();
pLedCard->starterversionName=subObj["versionName"].toString();
pLedCard->starterPort=subObj["starterPort"].toInt();
pLedCard->androidVersion=subObj["androidVersion"].toString();
pLedCard->iHttpGetId=subObj["id"].toInt();
pLedCard->androidHeight=subObj["androidHeight"].toInt();
pLedCard->androidWidth=subObj["androidWidth"].toInt();
pLedCard->lastActive=subObj["lastActive"].toInt();
2023-04-18 14:14:46 +08:00
for(int i=0;i<mLedCards.count();i++) if(mLedCards.at(i)->m_strCardId==pLedCard->m_strCardId ) { //如果已经存在在列表中
auto pOldLedCard = mLedCards.at(i);//获取到旧的对象指针
2022-09-28 18:21:57 +08:00
pOldLedCard->m_bOnLine = true;
if(! pOldLedCard->CompareHTTPGetParam(pLedCard)) {//如果ip地址变化了
pOldLedCard->copyLedCardHTTPGetParam(pLedCard);
addLedCard(pOldLedCard);//发送信号到界面
} else {
//post失败的概率比较小所以这里是否需要平凡的更新不需要刻意屏蔽该操作改为在其他点击操作中进行
clearUdpHeartbeat(pOldLedCard);
}
delete pLedCard;
return;
}
2023-04-18 14:14:46 +08:00
mLedCards.append(pLedCard);
2022-09-28 18:21:57 +08:00
addLedCard(pLedCard);//发送信号到界面
}
STREAM_PACKET LedOkMakeStreamPacket(int iComType,int iLen,unsigned char *ucBuf,QString str) {
STREAM_PACKET Temp;
memset(&Temp, 0, sizeof(STREAM_PACKET));
if(iLen>2048) return Temp;
2023-04-18 14:14:46 +08:00
memset(Temp.SyncHead, 0x7e, 3);
2022-09-28 18:21:57 +08:00
memset(Temp.ucSerialCode,0x00,20);
char* ch;
QByteArray ba = str.toLatin1(); // must
ch=ba.data();
memcpy(Temp.ucSerialCode,ch,ba.size());
Temp.iLength=iLen;
Temp.ucCommType=iComType;
memcpy(Temp.pDataBuffer,ucBuf,iLen);
Temp.pDataBuffer[Temp.iLength] = GetCheckCodeIn8(&Temp.ucCommType,Temp.iLength+sizeof(Temp.ucCommType)+sizeof(char)*20+sizeof(Temp.iLength));
return Temp;
}
//UDP多网卡广播
void DevicePanel::RestoreLedCardIpByUdpCmd() {
QList<QNetworkInterface> networkinterfaces = QNetworkInterface::allInterfaces();
foreach (QNetworkInterface interfaces, networkinterfaces) {//networkinterfaces负责提供主机的IP地址和网络接口的列表
foreach (QNetworkAddressEntry entry, interfaces.addressEntries()) {//QNetworkAddressEntry存储了一个IP地址子网掩码和广播地址
entry.setBroadcast(QHostAddress::Broadcast);
QHostAddress broadcastAddress("255.255.255.255");
entry.setBroadcast(QHostAddress::Broadcast);
RESTORE_IP stTempIp;
memset(&stTempIp,0x00,sizeof(RESTORE_IP));
QString strNew=entry.ip().toString();
int itempOffset = strNew.lastIndexOf(".");
QString sstrNew=strNew.left(itempOffset)+".254";
if(sstrNew.length()<=20) memcpy(stTempIp.cNewIp,sstrNew.toLatin1().data(),sstrNew.length());
QString strMask=entry.netmask().toString();
if(strMask.length()<=20)
memcpy(stTempIp.cMask,strMask.toLatin1().data(),strMask.length());
QString strGateway=entry.ip().toString();
if(strGateway.length()<=20)
memcpy(stTempIp.cGateway,strGateway.toLatin1().data(),strGateway.length());
if(strGateway.length()<=20)
memcpy(stTempIp.cDns,strGateway.toLatin1().data(),strGateway.length());
STREAM_PACKET tempStreadPacket = LedOkMakeStreamPacket(COMMAND_ONLY_RESET_CARD_IP, 80, (unsigned char *)&stTempIp,"Broadcast!");
2023-04-18 14:14:46 +08:00
int iLenPacket=tempStreadPacket.iLength+sizeof(int)+3*sizeof(unsigned char)+sizeof(char)+20*sizeof(char)+sizeof(char);/////除正文外的协议结构大小;
2022-09-28 18:21:57 +08:00
QUdpSocket *tempUdpSocket = new QUdpSocket(this);
if(!tempUdpSocket->bind(entry.ip())) break;
tempUdpSocket->writeDatagram((char *)&tempStreadPacket,iLenPacket,broadcastAddress, 31296);
}
}
}