qt/LedOK/device/badpointdetectdialog.cpp
2025-12-30 17:22:43 +08:00

311 lines
12 KiB
C++

#include "badpointdetectdialog.h"
#include "main.h"
#include "gutil/qgui.h"
#include "gutil/qnetwork.h"
#include "gutil/qwaitingdlg.h"
#include <QFileDialog>
#include <QLineEdit>
#include <QMessageBox>
#include <QHeaderView>
#include <QKeyEvent>
#include <QJsonArray>
#include <QHttpMultiPart>
#include <QDialogButtonBox>
#include <QJsonObject>
#include <QInputDialog>
#include <QRadioButton>
#include <QPushButton>
#include <QScrollArea>
#include <QPainter>
BadPointDetectDialog::BadPointDetectDialog(QWidget *parent) : QDialog(parent) {
resize(1280, 720);
setWindowFlag(Qt::WindowMaximizeButtonHint);
setWindowTitle(tr("Bad Point Detection"));
auto vBox = new VBox(this);
vBox->setContentsMargins(0, 0, 0, 0);
auto tab = new QTabWidget;
vBox->addWidget(tab);
auto badPointPanel = new QWidget;
tab->addTab(badPointPanel, tr("Bad Point Detection"));
vBox = new VBox(badPointPanel);
vBox->setContentsMargins(0, 0, 0, 0);
auto hBox = new HBox(vBox);
hBox->addLabel(tr("Port"));
auto bnPort1 = new QRadioButton(tr("Port")+"1");
bnPort1->setChecked(true);
auto bnPort2 = new QRadioButton(tr("Port")+"2");
hBox->addWidget(bnPort1);
hBox->addWidget(bnPort2);
auto bnDetect = new QPushButton(tr("Detect"));
hBox->addWidget(bnDetect);
hBox->addSpacing(20);
auto bnUpload = new QPushButton(tr("Upload Point Table"));
hBox->addWidget(bnUpload);
auto bnCheck = new QPushButton(tr("Check If Has"));
hBox->addWidget(bnCheck);
hBox->addStretch();
auto lbInfo = vBox->addLabel();
hBox = new HBox(vBox);
auto edCoordinates = new QTextEdit;
edCoordinates->setReadOnly(true);
edCoordinates->setMaximumWidth(200);
hBox->addWidget(edCoordinates);
auto table = new TableWidget;
table->setSelectionMode(QAbstractItemView::NoSelection);
table->setColWidth(20)->setRowHeight(20);
table->setShowGrid(true);
hBox->addWidget(table);
connect(bnDetect, &QPushButton::clicked, this, [=] {
JObj json;
json.insert("_id", "IsHaveTable");
json.insert("_type", "IsHaveTable");
json.insert("port", bnPort2->isChecked() ? 1 : 0);
auto waitingDlg = new WaitingDlg(this, tr("Detect")+" ...");
Def_CtrlReqPre
connect(reply, &QNetworkReply::finished, this, [=] {
{
Def_CtrlSingleGetReply
if(! json["isHave"].toBool()) {
waitingDlg->close();
QMessageBox::critical(this, translate("","Error"), "It hasn't a Point Table, please upload one");
return;
}
}
JObj json;
json.insert("_id", "GetBoxSize");
json.insert("_type", "GetBoxSize");
json.insert("port", bnPort2->isChecked() ? 1 : 0);
Def_CtrlReqPre
connect(reply, &QNetworkReply::finished, this, [=] {
Def_CtrlSingleGetReply
auto width = json["width"].toInt();
auto height = json["height"].toInt();
table->clear();
table->setColumnCount(width);
table->setRowCount(height);
edCoordinates->setPlainText(tr("Coordinates")+":");
lbInfo->clear();
if(width==0 || height==0) {
waitingDlg->close();
QMessageBox::critical(this, translate("","Error"), "width==0 or height==0");
return;
}
{
JObj json;
json.insert("_id", "StartPointCheck");
json.insert("_type", "StartPointCheck");
Def_CtrlReqPre
connect(reply, &QNetworkReply::finished, this, [=] {
auto err = errStrWithJson(reply);
if(! err.isEmpty()) {
waitingDlg->close();
QMessageBox::critical(this, translate("","Error"), err);
return;
}
JObj json;
json.insert("_id", "GetSpotCheckData");
json.insert("_type", "GetSpotCheckData");
Def_CtrlReqPre
connect(reply, &QNetworkReply::finished, this, [=] {
Def_CtrlSingleGetReply
waitingDlg->success();
auto pointCheckData = json["pointCheckData"];
int rgbCnt = 0, rCnt = 0, gCnt = 0, bCnt = 0;
for(auto &line : pointCheckData) {
auto str = line.toStr();
auto coors = str.split("-", Qt::SkipEmptyParts);
if(coors.size()<2) continue;
auto bad = new BadPointWgt;
if(coors.size()==2) {
rgbCnt++;
bad->rgb = true;
} else {
if(str.contains("R")) {rCnt++;bad->r = true;}
if(str.contains("G")) {gCnt++;bad->g = true;}
if(str.contains("B")) {bCnt++;bad->b = true;}
}
table->setCellWidget(coors[1].toInt(), coors[0].toInt(), bad);
edCoordinates->append(str);
}
lbInfo->setText(tr("Size")+": "+QString::number(width)+" x "+QString::number(height)+" "+tr("Bad Point")+" "+tr("Total")+": "+QString::number(rgbCnt+rCnt+gCnt+bCnt)+" RGB: "+QString::number(rgbCnt)+" R: "+QString::number(rCnt)+" G: "+QString::number(gCnt)+" B: "+QString::number(bCnt));
});
});
}
});
});
});
connect(bnUpload, &QPushButton::clicked, this, [=] {
auto file = QFileDialog::getOpenFileName(this, "Open file", gApkHome, tr("Point Table")+" (*.xml)");
if(file.isEmpty()) return;
QFileInfo info(file);
gApkHome = info.absolutePath();
QFile qfile(file);
if(! qfile.exists()) return;
if(! qfile.open(QIODevice::ReadOnly)) return;
auto fileData = qfile.readAll();
qfile.close();
if(fileData.size() != qfile.size()) {
QMessageBox::information(this, translate("","Tip"), tr("Failed to Read File"));
return;
}
auto nameBytes = file.toUtf8();
auto Boundary = "----QtLedOK_.oOo._"+QUuid::createUuid().toByteArray(QUuid::WithoutBraces);
QByteArray data;
data.append("--").append(Boundary).append("\r\nContent-Disposition: form-data; name=\"username\"\r\n\r\n10005\r\n");
data.append("--").append(Boundary).append("\r\nContent-Disposition: form-data; name=\"").append(nameBytes).append("\"; filename=\"").append(nameBytes).append("\"\r\n\r\n").append(fileData).append("\r\n");
data.append("--").append(Boundary).append("--\r\n");
auto waitingDlg = new WaitingDlg(this, tr("Uploading"));
waitingDlg->show();
auto card = gSelCards[0];
auto reply = NetReq("http://"+card.ip+":2016/upload?type=badPoint_upload").type("multipart/form-data; boundary="+Boundary).post(data);
connect(waitingDlg, &WaitingDlg::rejected, reply, &QNetworkReply::deleteLater);
connect(reply, &QNetworkReply::finished, this, [=] {
auto err = errStrWithData(reply);
if(! err.isEmpty()) {
waitingDlg->close();
QMessageBox::critical(this, translate("","Error"), err);
return;
}
waitingDlg->success();
});
});
connect(bnCheck, &QPushButton::clicked, this, [=] {
JObj json;
json.insert("_id", "IsHaveTable");
json.insert("_type", "IsHaveTable");
json.insert("port", bnPort2->isChecked() ? 1 : 0);
auto waitingDlg = new WaitingDlg(this, tr("Check")+" ...");
Def_CtrlReqPre
connect(reply, &QNetworkReply::finished, this, [=] {
Def_CtrlSingleGetReply
waitingDlg->close();
if(json["isHave"].toBool()) QMessageBox::information(this, translate("","Info"), "Yes, it has");
else QMessageBox::critical(this, translate("","Info"), "No, it hasn't");
});
});
auto sensorsPanel = new QWidget;
tab->addTab(sensorsPanel, tr("Sensors"));
vBox = new VBox(sensorsPanel);
vBox->setContentsMargins(0, 0, 0, 0);
hBox = new HBox(vBox);
auto bnRelayOn = new QPushButton(tr("Relay On"));
hBox->addWidget(bnRelayOn);
auto bnRelayOff = new QPushButton(tr("Relay Off"));
hBox->addWidget(bnRelayOff);
connect(bnRelayOn, &QPushButton::clicked, this, [=] {
JObj json;
json.insert("_id", "ControllerRecCardRelaySwitch");
json.insert("_type", "ControllerRecCardRelaySwitch");
json.insert("isSwitch", true);
auto waitingDlg = new WaitingDlg(this, tr("Get")+" ...");
Def_CtrlReqPre
connect(reply, &QNetworkReply::finished, this, [=] {
Def_CtrlSetReqAfter
});
});
connect(bnRelayOff, &QPushButton::clicked, this, [=] {
JObj json;
json.insert("_id", "ControllerRecCardRelaySwitch");
json.insert("_type", "ControllerRecCardRelaySwitch");
json.insert("isSwitch", false);
auto waitingDlg = new WaitingDlg(this, tr("Get")+" ...");
Def_CtrlReqPre
connect(reply, &QNetworkReply::finished, this, [=] {
Def_CtrlSetReqAfter
});
});
auto bnSensorDataGet = new QPushButton(tr("Get Sensor Data"));
hBox->addWidget(bnSensorDataGet);
hBox->addStretch();
auto edInfo = new QTextEdit;
vBox->addWidget(edInfo);
vBox->addStretch();
connect(bnSensorDataGet, &QPushButton::clicked, this, [=] {
JObj json;
json.insert("_id", "GetRecCardInfo");
json.insert("_type", "GetRecCardInfo");
auto waitingDlg = new WaitingDlg(this, tr("Get Rece Card Info")+" ...");
Def_CtrlReqPre
connect(reply, &QNetworkReply::finished, this, [=] {
Def_CtrlSingleGetReply
waitingDlg->success();
edInfo->clear();
edInfo->append(tr("Temperature")+": "+json["temperature"].toDouble());
edInfo->append(tr("Humidity")+": "+json["humidity"].toDouble());
edInfo->append(tr("Voltage")+": "+json["voltage"].toDouble());
edInfo->append(tr("Light")+": "+json["light"].toInt());
edInfo->append(tr("Has Smoke")+": "+json["isSmoke"].toBool());
edInfo->append(tr("Power")+" 1: "+json["power1"].toBool());
edInfo->append(tr("Power")+" 2: "+json["power2"].toBool());
edInfo->append(tr("Power")+" 3: "+json["power3"].toBool());
edInfo->append(tr("Power")+" 4: "+json["power4"].toBool());
edInfo->append(tr("Power")+" 5: "+json["power5"].toBool());
edInfo->append(tr("Door Open")+" 5: "+json["isOpen"].toBool());
edInfo->append(tr("Relay On")+" 5: "+json["relaySwitch"].toBool());
});
});
setAttribute(Qt::WA_DeleteOnClose);
setModal(true);
show();
}
void BadPointDetectDialog::keyPressEvent(QKeyEvent *event) {
if(event->key() == Qt::Key_F3) {
}
}
#define Distance 6
void BadPointWgt::paintEvent(QPaintEvent *) {
QPainter painter(this);
painter.translate(10, 9);
QPen pen;
pen.setCapStyle(Qt::RoundCap);
if(rgb) {
pen.setWidth(18);
pen.setColor(Qt::black);
painter.setPen(pen);
painter.drawPoint(0, 0);
} else {
pen.setWidth(9);
if(r) {
pen.setColor(Qt::red);
painter.setPen(pen);
painter.drawPoint(QPointF(-sin(60*3.14159265/180)*Distance, -sin(30*3.14159265/180)*Distance));
}
if(g) {
pen.setColor(QColor(0x00cc00));
painter.setPen(pen);
painter.drawPoint(QPointF(sin(60*3.14159265/180)*Distance, -sin(30*3.14159265/180)*Distance));
}
if(b) {
pen.setColor(Qt::blue);
painter.setPen(pen);
painter.drawPoint(0, Distance);
}
}
}