qt/LedOK/deviceitem.cpp

441 lines
18 KiB
C++
Raw Normal View History

2022-08-25 18:37:24 +08:00
#include "deviceitem.h"
2023-05-11 11:47:00 +08:00
#include "base/waitingdlg.h"
#include "gutil/qgui.h"
2022-08-25 18:37:24 +08:00
#include <QMessageBox>
#include <QJsonArray>
2023-05-11 11:47:00 +08:00
#include <QJsonDocument>
#include <QPainter>
2022-08-25 18:37:24 +08:00
#include <globaldefine.h>
#include <QSettings>
#include <cfg.h>
#include <QHttpMultiPart>
#include <QUuid>
#include <QBuffer>
#include <QThread>
#include <QTcpSocket>
#include <QHostAddress>
#include <QInputDialog>
2023-05-11 11:47:00 +08:00
DeviceItem::DeviceItem(LedCard *pLedCard, LoQTreeWidget *parent) : QObject(parent), QTreeWidgetItem(UserType), m_parent(parent) {
mCard = pLedCard;
2022-08-25 18:37:24 +08:00
pHpptClient = new HpptClient(this);
setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
setCheckState(0, Qt::Unchecked);
m_parent->addTopLevelItem(this);
m_bnCardDetailInfo = new QPushButton();
m_bnCardDetailInfo->setToolTip(tr("GetScreenDetailInfo"));
m_bnCardDetailInfo->setCursor(QCursor(Qt::PointingHandCursor));
m_bnCardDetailInfo->setStyleSheet(R"rrr(
QPushButton {
border-radius: 4px;
2023-04-18 14:14:46 +08:00
image: url(:/res/device/bnDetail.png);
2022-08-25 18:37:24 +08:00
}
2023-04-18 14:14:46 +08:00
QPushButton:hover{background-color: #ccc;}
2022-08-25 18:37:24 +08:00
)rrr");
connect(m_bnCardDetailInfo, &QPushButton::clicked, this, [this]{
QString strTip="";
2023-04-18 14:14:46 +08:00
strTip.append(tr("cur brightness:")).append(QString::number(mCard->bright)).append("\r\n");
2022-08-25 18:37:24 +08:00
//strTip.append("cardsystem apk version:"+QString::number(m_LedCard.iCardSystemVersion)+"\r\n");
//strTip.append("device apk:"+m_LedCard.starterversionName+"\r\n");
2023-04-18 14:14:46 +08:00
strTip.append(tr("android version:")).append(mCard->androidVersion).append("\r\n");
2022-08-25 18:37:24 +08:00
//strTip.append(tr("android width:")+QString::number(m_LedCard.androidWidth)+"\r\n");
//strTip.append(tr("android height:")+QString::number(m_LedCard.androidHeight)+"\r\n");
2023-04-18 14:14:46 +08:00
strTip.append(tr("IMEI:")).append(mCard->strIMEI).append("\r\n");
strTip.append(tr("HardWard Version:")).append(mCard->HardVersion).append("\r\n");
strTip.append(tr("MAX Brightness level:")).append(QString::number(mCard->BrightnessLevel)).append("\r\n");
strTip.append(tr("Android OS Resolution:")).append(mCard->ScreenResolution).append("\r\n");
strTip.append(tr("Firmware Version:")).append(mCard->FirmwareVersion).append("\r\n");
strTip.append(tr("Player Version:")).append(mCard->strXixunplayerVersion).append("\r\n");
2023-05-11 11:47:00 +08:00
QMessageBox::information(treeWidget(), tr("Detail Info"), strTip);
2022-08-25 18:37:24 +08:00
});
m_bnReadbackPic = new QPushButton();
m_bnReadbackPic->setToolTip(tr("ReadbackPic"));
m_bnReadbackPic->setCursor(QCursor(Qt::PointingHandCursor));
m_bnReadbackPic->setStyleSheet(R"rrr(
QPushButton {
border-radius: 4px;
2023-04-18 14:14:46 +08:00
image: url(:/res/device/deviceReadbackPic.png);
2022-08-25 18:37:24 +08:00
}
2023-04-18 14:14:46 +08:00
QPushButton:hover{background-color: #ccc;}
2022-08-25 18:37:24 +08:00
)rrr");
2023-05-11 11:47:00 +08:00
connect(m_bnReadbackPic, &QPushButton::clicked, this, [this] {
QJsonObject json;
json.insert("_id", "GetScreenshotFull");
json.insert("_type", "GetScreenshotFull");
auto waitingDlg = new WaitingDlg(treeWidget(), tr("GetScreenshotFull")+" ...");
waitingDlg->show();
auto reply = NetReq("http://"+mCard->m_strCardIp+":2016/settings").timeout(120000).post(json);
waitingDlg->connAbort(reply);
connect(reply, &QNetworkReply::finished, this, [=] {
waitingDlg->close();
QJsonDocument json;
QString err = checkReplyForJson(reply, &json);
if(! err.isEmpty()) {
QMessageBox::critical(treeWidget(), tr("Error"), err);
return;
}
ImgDlg dlg(QByteArray::fromBase64(json["data"].toString().toLatin1()), treeWidget());
dlg.exec();
});
});
2022-08-25 18:37:24 +08:00
m_ImageOnline = new QLabel();
2023-04-18 14:14:46 +08:00
mCard->m_bOnLine=true;
m_ImageOnline->setPixmap(QPixmap(":/res/device/O_Online.png"));
setData(DeviceTable_ID, 0, mCard->m_strCardId);
setData(DeviceTable_IP, 0, mCard->m_strCardIp);
setData(DeviceTable_ScreenSize, 0, QString("%1 x %2").arg(mCard->m_iWidth).arg(mCard->m_iHeight));
2022-08-25 18:37:24 +08:00
m_parent->setItemWidget(this, DeviceTable_Info, m_bnCardDetailInfo);
m_parent->setItemWidget(this, DeviceTable_Screenshot, m_bnReadbackPic);
m_parent->setItemWidget(this, DeviceTable_Online, m_ImageOnline);
m_ImageOnline->setAlignment(Qt::AlignCenter);
2023-05-11 11:47:00 +08:00
btnUnlock = new QPushButton;
btnUnlock->setMaximumHeight(40);
if(mCard->bPassword) btnUnlock->setIcon(QIcon(mCard->m_bLockStatus ? ":/res/device/Lock.png" : ":/res/device/UnLock.png"));
else btnUnlock->hide();
connect(btnUnlock, &QPushButton::clicked, this, [this] {
if(! mCard->m_bLockStatus) return;
bool ok;
auto pwd = QInputDialog::getText(treeWidget(), tr("Input password"), tr("Input password"), QLineEdit::Password, QString(), &ok);
if(! ok) return;
QJsonObject json;
json.insert("_id", "VerifyPassword");
json.insert("_type", "VerifyPassword");
json.insert("pwd", pwd);
auto waitingDlg = new WaitingDlg(treeWidget(), tr("VerifyPassword")+" ...");
waitingDlg->show();
auto reply = NetReq("http://"+mCard->m_strCardIp+":2016/settings").timeout(60000).post(json);
waitingDlg->connAbort(reply);
connect(reply, &QNetworkReply::finished, this, [=] {
QJsonDocument json;
QString err = checkReplyForJson(reply, &json);
if(! err.isEmpty()) {
waitingDlg->close();
QMessageBox::critical(treeWidget(), tr("Error"), err);
return;
}
if(! json["result"].toBool()) {
waitingDlg->close();
QMessageBox::critical(treeWidget(), tr("Tip Info"), tr("password is wrong"));
return;
}
waitingDlg->success();
mCard->m_bLockStatus = false;
btnUnlock->setIcon(QIcon(":/res/device/UnLock.png"));
});
});
auto wgt = new QWidget;
auto hBox = new HBox(wgt);
hBox->setContentsMargins(0,0,0,0);
hBox->addWidget(btnUnlock);
m_parent->setItemWidget(this, DeviceTable_Password, wgt);
2022-08-25 18:37:24 +08:00
for(int i=1; i<DeviceTable_Screenshot; i++) setTextAlignment(i, Qt::AlignCenter);
//启动心跳监测设备在线和下线3次超时表示下线
DeviceItemHttpPost();
connect(&mHeartbeatTimer, &QTimer::timeout, this, [this] {
m_HeartbeatCount++;
QJsonObject json4;
json4.insert("_id", getRandomString(10));
json4.insert("_type", "GetCardName");
HttpPostByTypeJsonObject(json4);
if(m_HeartbeatCount>10) {
//下线
2023-04-18 14:14:46 +08:00
mCard->m_bOnLine=false;
m_ImageOnline->setPixmap(QPixmap(":/res/device/O_Offline.png"));
2022-08-25 18:37:24 +08:00
} else {
//在线
2023-04-18 14:14:46 +08:00
mCard->m_bOnLine=true;
m_ImageOnline->setPixmap(QPixmap(":/res/device/O_Online.png"));
2022-08-25 18:37:24 +08:00
}
});
mHeartbeatTimer.start(60000);
2023-05-11 11:47:00 +08:00
2022-08-25 18:37:24 +08:00
m_pGetAskTimer=new QTimer(this);
connect(pHpptClient,SIGNAL(httpPostRspReady(QString , QString , QByteArray)),this,SLOT(OnProHttpResponse(QString , QString , QByteArray)));
//连接item的响应信号到信息输出框的显示槽函数
connect(this,SIGNAL(sigOutputInfo(QString)),parent->parent(),SLOT(OnOutputInfo(QString)));
}
DeviceItem::~DeviceItem() {
mHeartbeatTimer.stop();
if(pHpptClient) {
delete pHpptClient;
pHpptClient=nullptr;
}
}
void DeviceItem::SetMainTabWidgetIndex(int index){
m_intIndexFlagOfInfoOrControl=index;
DeviceItemHttpPost();
}
//针对这个ITEM通过POST获取控制卡的信息两种情况下会调用创建该Item初始化后二、手动刷新
void DeviceItem::DeviceItemHttpPost() {
if(m_intIndexFlagOfInfoOrControl!=MainPage_DeviceManager) return;
QJsonObject json;
json.insert("_id", getRandomString(10));
json.insert("_type", "GetScreenSize");
HttpPostByTypeJsonObject(json);
QJsonObject json1;
json1.insert("_id", getRandomString(10));
json1.insert("_type", "GetBrightness");
HttpPostByTypeJsonObject(json1);
QJsonObject json2;
json2.insert("_id", getRandomString(10));
json2.insert("_type", "IsScreenOn");
HttpPostByTypeJsonObject(json2);
QJsonObject json4;
json4.insert("_id", getRandomString(10));
json4.insert("_type", "GetBuildInformation");
HttpPostByTypeJsonObject(json4);
QJsonObject json5;
json5.insert("_id", getRandomString(10));
json5.insert("_type", "CheckSoftVersions");
HttpPostByTypeJsonObject(json5);
QJsonObject json8;
json8.insert("_id", getRandomString(10));
json8.insert("_type", "GetCardAlias");
HttpPostByTypeJsonObject(json8);
2023-05-11 11:47:00 +08:00
{
QJsonObject json;
json.insert("_id", "HasControllerPassword");
json.insert("_type", "HasControllerPassword");
auto reply = NetReq("http://"+mCard->m_strCardIp+":2016/settings").timeout(120000).post(json);
connect(reply, &QNetworkReply::finished, mCard, [=] {
QJsonDocument json;
QString err = checkReplyForJson(reply, &json);
if(! err.isEmpty()) return;
mCard->bPassword = json["result"].toBool();
if(mCard->bPassword) {//加过密
btnUnlock->show();
btnUnlock->setIcon(QIcon(mCard->m_bLockStatus ? ":/res/device/Lock.png" : ":/res/device/UnLock.png"));
} else btnUnlock->hide();
});
}
2022-08-25 18:37:24 +08:00
}
void DeviceItem::OnControlTcpSend(int iProgramIndex) {
QTcpSocket *send = new QTcpSocket();
connect(send, SIGNAL(connected()), this, SLOT(connect_sucessful()));
connect(send, SIGNAL(error(QAbstractSocket::SocketError)),this, SLOT(show_error(QAbstractSocket::SocketError)));
2023-04-18 14:14:46 +08:00
send->connectToHost(QHostAddress(mCard->m_strCardIp),31299);
2022-08-25 18:37:24 +08:00
ST_ANSY_PROGRAM_PACKET tempStreadPakcet;
tempStreadPakcet.SyncHead[0]=0x7e;
tempStreadPakcet.SyncHead[1]=0x7e;
tempStreadPakcet.SyncHead[2]=0x55;
tempStreadPakcet.ucCommType=0x97;
tempStreadPakcet.iBaoLiu=0;
tempStreadPakcet.iLength=4;
unsigned char uctemp[4]={0};
uctemp[0]=iProgramIndex;
memcpy(tempStreadPakcet.pDataBuffer,uctemp,4);
tempStreadPakcet.pDataBuffer[tempStreadPakcet.iLength]=GetCheckCodeIn8(&tempStreadPakcet.ucCommType,tempStreadPakcet.iLength+sizeof(tempStreadPakcet.iBaoLiu)+sizeof(tempStreadPakcet.ucCommType)+sizeof(tempStreadPakcet.iLength));
int iLenPacket=3*sizeof(unsigned char)+sizeof(char)+sizeof(int)+sizeof(int)+tempStreadPakcet.iLength+sizeof(char);/////除正文外的协议结构大小;
QByteArray databuf = QByteArray(reinterpret_cast<char*>(&tempStreadPakcet), iLenPacket);
if(!send->waitForConnected(10000)) //等待连接返回
{
2023-04-18 14:14:46 +08:00
qDebug()<<mCard->m_strCardIp<<"Connect timeout ";
emit sigOutputInfo(mCard->m_strCardId+":"+tr("receive")+"<-"+tr("Connect")+":"+tr("timeout"));
2022-08-25 18:37:24 +08:00
send->close();
delete send;
return ;
}
send->write(databuf);
if(send->waitForBytesWritten(3000))
{
send->read(send->bytesAvailable());
qDebug()<<"recv anycast success ack";
2023-04-18 14:14:46 +08:00
emit sigOutputInfo(mCard->m_strCardId+":"+tr("receive")+"<-"+tr("Anycast")+":"+tr("success"));
2022-08-25 18:37:24 +08:00
}
send->close();
delete send;
}
void DeviceItem::HttpPostByTypeJsonObject(QJsonObject json)
{
QJsonDocument doc;
doc.setObject(json);
QByteArray post_loginArray = doc.toJson(QJsonDocument::Compact);
2023-04-18 14:14:46 +08:00
m_strUrl="http://"+mCard->m_strCardIp+":2016/settings";
if(mCard->m_strCardId.left(2)!="M8" && mCard->m_strCardId.left(2)!="m8")
2022-08-25 18:37:24 +08:00
{
2023-04-18 14:14:46 +08:00
if(json["_type"]!="GetAllScreenSizeM80") pHpptClient->httpPost(m_strUrl,post_loginArray);
2022-08-25 18:37:24 +08:00
}
2023-04-18 14:14:46 +08:00
else pHpptClient->httpPost(m_strUrl,post_loginArray);
2022-08-25 18:37:24 +08:00
}
void DeviceItem::HttpGetLedCardWidthHeight()
{
QJsonObject json;
json.insert("_id", getRandomString(10));
json.insert("_type", "GetScreenSize");
HttpPostByTypeJsonObject(json);
}
void DeviceItem::HttpGetLedCardRemarkName(){
QJsonObject json;
json.insert("_id", getRandomString(10));
json.insert("_type", "GetCardAlias");
HttpPostByTypeJsonObject(json);
}
void DeviceItem::refreshLable(){
2023-04-18 14:14:46 +08:00
if(mCard->m_bPowerStatus){
2022-08-25 18:37:24 +08:00
this->setForeground(DeviceTable_Power, Qt::green);
setData(DeviceTable_Power, 0, tr("on"));
} else {
this->setForeground(DeviceTable_Power, Qt::red);
setData(DeviceTable_Power, 0, tr("off"));
}
}
//获取到httpPost的应答如果参数变化需要出发修改控件item的内容
void DeviceItem::OnProHttpResponse(QString url, QString postMD5, QByteArray data) {
Q_UNUSED(postMD5)
//qDebug()<< url + "\r\n"+m_strUrl;
if(url == m_strUrl)
{
QJsonParseError parseJsonErr;
QJsonDocument document = QJsonDocument::fromJson(data,&parseJsonErr);
if(!(parseJsonErr.error == QJsonParseError::NoError))
{
if(data.size()==0)
qDebug()<<"json is empty";
else
qDebug()<<"DeviceItem OnProHttpResponse 解析json文件错误";
return;
}
QJsonObject jsonObject = document.object();
QString strType=jsonObject["_type"].toString();
if(jsonObject["success"].toBool()==true)
{
if(strType=="GetScreenSize" )
{
2023-04-18 14:14:46 +08:00
if(mCard->m_iWidth!=jsonObject["width"].toInt()||mCard->m_iHeight!=jsonObject["height"].toInt())
2022-08-25 18:37:24 +08:00
{
2023-04-18 14:14:46 +08:00
mCard->m_iWidth=jsonObject["width"].toInt();
mCard->m_iHeight=jsonObject["height"].toInt();
2022-08-25 18:37:24 +08:00
}
2023-04-18 14:14:46 +08:00
setData(DeviceTable_ScreenSize, 0, QString("%1 x %2").arg(mCard->m_iWidth).arg(mCard->m_iHeight));
2022-08-25 18:37:24 +08:00
}
else if(strType=="GetCardAlias" )
{
2023-04-18 14:14:46 +08:00
mCard->m_strCardRemarkName = QString::fromUtf8(QByteArray::fromBase64(jsonObject["alias"].toString().toLatin1()));
setData(DeviceTable_Remark,0,mCard->m_strCardRemarkName);
2022-08-25 18:37:24 +08:00
}
else if(strType=="GetCardName")
{
m_HeartbeatCount=0;
2023-04-18 14:14:46 +08:00
mCard->m_bOnLine=true;
m_ImageOnline->setPixmap(QPixmap(":/res/device/O_Online.png"));
2022-08-25 18:37:24 +08:00
}
else if(strType=="GetBuildInformation" )
{
2023-04-18 14:14:46 +08:00
mCard->strIMEI=jsonObject["IMEI"].toString();
mCard->HardVersion=jsonObject["HardVersion"].toString();
mCard->BrightnessLevel=jsonObject["BrightnessLevel"].toInt();
mCard->ScreenResolution=jsonObject["ScreenResolution"].toString();
mCard->FirmwareVersion=jsonObject["FirmwareVersion"].toString();
2022-08-25 18:37:24 +08:00
}
2023-05-11 11:47:00 +08:00
else if(strType=="CheckSoftVersions")
2022-08-25 18:37:24 +08:00
{
foreach(QJsonValue value, jsonObject["apps"].toArray()) {
QJsonObject oApp = value.toObject();
if(oApp["packageName"].toString().contains("xixunplayer"))
{
2023-04-18 14:14:46 +08:00
mCard->strXixunplayerVersion=oApp["versionName"].toString();
mCard->strXixunplayerVersionCode=oApp["versionCode"].toInt();
2022-08-25 18:37:24 +08:00
}
}
}
else if(strType == "AliIotSetting")
{
2023-04-18 14:14:46 +08:00
emit sigOutputInfo(mCard->m_strCardId+":"+tr("receive")+"<-"+tr("AliIotSetting")+":"+tr("success"));
2022-08-25 18:37:24 +08:00
}
else if(strType == "IsScreenOn")
{
{
2023-04-18 14:14:46 +08:00
mCard->m_bPowerStatus=jsonObject["on"].toBool();
if(mCard->m_bPowerStatus) {
2022-08-25 18:37:24 +08:00
this->setForeground(DeviceTable_Power, Qt::green);
setData(DeviceTable_Power, 0, tr("on"));
2023-04-18 14:14:46 +08:00
emit sigOutputInfo(mCard->m_strCardId+":"+tr("receive")+"<-"+tr("GetScreenStatus")+tr("success")+","+tr("Status")+":"+tr("on"));
2022-08-25 18:37:24 +08:00
} else {
this->setForeground(DeviceTable_Power, Qt::red);
setData(DeviceTable_Power, 0, tr("off"));
2023-04-18 14:14:46 +08:00
emit sigOutputInfo(mCard->m_strCardId+":"+tr("receive")+"<-"+tr("GetScreenStatus")+tr("success")+","+tr("Status")+":"+tr("off"));
2022-08-25 18:37:24 +08:00
}
}
} else if(strType == "GetBrightness") {
2023-04-18 14:14:46 +08:00
mCard->bright = jsonObject["brightnessPercentage"].toInt(-1);
if(mCard->bright==-1) mCard->bright = qRound(jsonObject["brightness"].toInt() * 100.0 / mCard->BrightnessLevel);
QString bright = QString::number(mCard->bright);
setData(DeviceTable_Brightness, 0, QString::number(mCard->bright)+"%");
emit sigOutputInfo(mCard->m_strCardId+":"+tr("receive")+"<-"+tr("GetBrightness")+tr("success")+","+tr("brightness")+"="+bright+"%");
2022-08-25 18:37:24 +08:00
}
else if(strType == "SetAutoBrightnessTask")
{
2023-04-18 14:14:46 +08:00
emit sigOutputInfo(mCard->m_strCardId+":"+tr("receive")+"<-"+tr("SetAutoBrightnessTask")+":"+tr("success"));
2022-08-25 18:37:24 +08:00
}
else if(strType == "SetAutoVolumeTask")
{
2023-04-18 14:14:46 +08:00
emit sigOutputInfo(mCard->m_strCardId+":"+tr("receive")+"<-"+tr("SetAutoVolumeTask")+":"+tr("success"));
2022-08-25 18:37:24 +08:00
}
else if(strType == "SetTimingScreenTask")
{
2023-04-18 14:14:46 +08:00
emit sigOutputInfo(mCard->m_strCardId+":"+tr("receive")+"<-"+tr("SetTimingScreenTask")+":"+tr("success"));
2022-08-25 18:37:24 +08:00
}
else if(strType == "TestScreen")
{
2023-04-18 14:14:46 +08:00
emit sigOutputInfo(mCard->m_strCardId+":"+tr("receive")+"<-"+tr("TestScreen")+":"+tr("success"));
2022-08-25 18:37:24 +08:00
}
}
}
}
QPixmap DeviceItem::Base64_To_Image(QByteArray bytearray,QString SavePath)
{
QByteArray Ret_bytearray;
Ret_bytearray = QByteArray::fromBase64(bytearray);
QBuffer buffer(&Ret_bytearray);
buffer.open(QIODevice::WriteOnly);
QPixmap imageresult;
imageresult.loadFromData(Ret_bytearray);
if(SavePath != "")
{
qDebug() <<"save" ;
imageresult.save(SavePath);
}
return imageresult;
}
void DeviceItem::DeletePostingDlg()
{
if(m_PostingDlg!=nullptr)
{
delete m_PostingDlg;
m_PostingDlg=nullptr;
}
}
2023-04-25 16:30:58 +08:00
ImgDlg::ImgDlg(const QByteArray &data, QWidget *parent) : QDialog(parent) {
setWindowFlag(Qt::WindowContextHelpButtonHint, 0);
setWindowTitle(tr("Screenshot"));
mImg.loadFromData(data);
auto www = mImg.width();
auto hhh = mImg.height();
if(www <= 1600 && hhh <= 900) resize(www, hhh);
else if(www > hhh) resize(900 * www / hhh, 900);
else resize(1600, 1600 * hhh / www);
}
void ImgDlg::paintEvent(QPaintEvent *) {
QPainter painter(this);
painter.drawImage(rect(), mImg);
}