TSI_Config/mainwindow.cpp
2025-03-01 13:40:11 +08:00

537 lines
23 KiB
C++

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QList>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
#include "keyphase.h"
#include "singlerelay.h"
#include "tachometer.h"
#include "seismic_monitor.h"
#include "setpoint.h"
#include <QMessageBox>
#include <QFileDialog>
#include <stdio.h>
#include <QSysInfo>
#include <qsettings.h>
#include "ftpclient.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
btnGroup_slot = new QButtonGroup(this);
btnGroup_slot->addButton(ui->pushButton_slot3);
btnGroup_slot->addButton(ui->pushButton_slot4);
btnGroup_slot->addButton(ui->pushButton_slot5);
btnGroup_slot->addButton(ui->pushButton_slot6);
btnGroup_slot->addButton(ui->pushButton_slot7);
btnGroup_slot->addButton(ui->pushButton_slot8);
btnGroup_slot->addButton(ui->pushButton_slot9);
btnGroup_slot->addButton(ui->pushButton_slot10);
btnGroup_slot->addButton(ui->pushButton_slot11);
btnGroup_slot->addButton(ui->pushButton_slot12);
btnGroup_slot->addButton(ui->pushButton_slot13);
btnGroup_slot->addButton(ui->pushButton_slot14);
btnGroup_slot->addButton(ui->pushButton_slot15);
btnGroup_slot->addButton(ui->pushButton_slot16);
btnGroup_slot->addButton(ui->pushButton_slot17);
ui->pushButton_slot->setChecked(true);
readJsonFile(QCoreApplication::applicationDirPath() + "\\config\\main.json");
createMenu();
connect(btnGroup_slot, SIGNAL(buttonClicked(QAbstractButton *)), this, SLOT(OnButtonGroup(QAbstractButton *)));
QSettings settingsread(QCoreApplication::applicationDirPath() + "\\config\\config.ini",QSettings::IniFormat);
m_strServerIp = settingsread.value("Server/IP").toString();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::readJsonFile(const QString &filePath)
{
// 创建文件对象
QFile file(filePath);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
qDebug() << "Cannot open file for reading:" << filePath;
return;
}
QString content = file.readAll();
file.close();
QByteArray jsonData = content.toUtf8();
QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonData);
if (jsonDoc.isNull()) {
qDebug() << "Cannot parse JSON document";
return;
}
if (!jsonDoc.isObject() && !jsonDoc.isArray()) {
qDebug() << "JSON document is not an object or an array";
return;
}
int slot_id = 3;
QJsonArray jsonArray = jsonDoc.array();
for (const QJsonValue &value : jsonArray) {
SlotConfig slot_config;
if (value.isObject()) { // 处理数组中的对象,例如每个对象代表一个记录或用户等。
QJsonObject obj = value.toObject();
slot_config.slot = obj["slot"].toInt();
slot_config.slot_type = obj["slot_type"].toInt();
slot_config.chan_display = obj["chan_display"].toString();
slot_config.rack_type = obj["rack_type"].toString();
map_slot_config.insert(slot_id,slot_config);
slot_id ++;
}
}
}
void MainWindow::createMenu()
{
QList<QAbstractButton*> buttonList = btnGroup_slot->buttons();
for (int i = 0; i < buttonList.count(); i++){
buttonList[i]->setText(map_slot_config[i + 3].chan_display);
createMenu(QString("%1").arg(i + 1), (QPushButton*)buttonList[i]);
map_slot_config[ i + 3].slot_btn = (QPushButton*)buttonList[i];
// else
// createMenuSet(QString("%1").arg(i + 1), (QPushButton*)buttonList[i]);
}
}
void MainWindow::createMenu(const QString& rootTitle, QPushButton* parent )
{
// 创建主菜单
QMenu *mainMenu = new QMenu(rootTitle, parent);
// 创建第一层子菜单:/30 振电器模块
QMenu *monitors = new QMenu("监视器", mainMenu);
QMenu *relays = new QMenu("/30 继电器模块", mainMenu);
QMenu *keyphasor = new QMenu("/25 键相模块", mainMenu);
// 创建第二层子菜单:/40 振动板卡
QMenu *proximitor_menu = new QMenu("/40 振动板卡", monitors);
QMenu *rpm_menu = new QMenu("/50 转速板卡", monitors);
// 创建第三层子菜单:/40 单板卡、三冗余板卡
QAction *proximitor_1 = proximitor_menu->addAction("/40 单板卡");
QAction *proximitor_2 = proximitor_menu->addAction("/40 三冗余板卡");
QAction *rpm_1 = rpm_menu->addAction("/50 单板卡");
// 创建第二层子菜单:/25 键相模块
QAction *keyphasor_1 = keyphasor->addAction("/25 单板卡");
QAction *keyphasor_2 = keyphasor->addAction("/25 两板卡");
// 创建第二层子菜单:/30 继电器模块
QAction *relays_1 = relays->addAction("/30 单板卡");
QAction *relays_2 = relays->addAction("/30 三冗余板卡");
// 将子菜单加入上一级菜单
monitors->addMenu(proximitor_menu); // 将第二层加入第一层
monitors->addMenu(rpm_menu); // 第二层另一个子菜单
mainMenu->addMenu(monitors); // 将第一层加入主菜单
mainMenu->addMenu(relays);
mainMenu->addMenu(keyphasor);
QAction *reset = mainMenu->addAction("重置模块");
QAction *upgrade = mainMenu->addAction("升级固件");
parent->setContextMenuPolicy(Qt::CustomContextMenu);
connect(parent,&QPushButton::customContextMenuRequested,[=](const QPoint &pos)
{
qDebug() << pos << endl;
mainMenu->exec(QCursor::pos());
});
// 连接所有菜单项
QObject::connect(proximitor_1, &QAction::triggered,this, &MainWindow::onMenuActionTriggered);
QObject::connect(proximitor_2, &QAction::triggered,this, &MainWindow::onMenuActionTriggered);
QObject::connect(rpm_1, &QAction::triggered,this, &MainWindow::onMenuActionTriggered);
QObject::connect(relays_1, &QAction::triggered,this, &MainWindow::onMenuActionTriggered);
QObject::connect(relays_2, &QAction::triggered,this, &MainWindow::onMenuActionTriggered);
QObject::connect(keyphasor_1, &QAction::triggered,this, &MainWindow::onMenuActionTriggered);
QObject::connect(keyphasor_2, &QAction::triggered,this, &MainWindow::onMenuActionTriggered);
QObject::connect(reset, &QAction::triggered,this, &MainWindow::onMenuActionTriggered);
QObject::connect(upgrade, &QAction::triggered,this, &MainWindow::onMenuActionTriggered);
}
void MainWindow::createMenuSet(const QString& rootTitle, QPushButton* parent )
{
// 创建主菜单
qDebug() << "createMenu" << parent->objectName() << endl;
QMenu *mainMenu = new QMenu(rootTitle, parent);
QAction *option = mainMenu->addAction("通道配置…");
QAction *set_points = mainMenu->addAction("触发配置…");
QAction *point_names = mainMenu->addAction("测点名称");
}
// 清除菜单中所有动作的属性
void MainWindow::clearMenuProperties(QMenu* menu)
{
// 遍历菜单的所有动作
if (!menu) {
qWarning() << "菜单为空,无法清除属性!";
return;
}
// 遍历菜单的所有动作
for (QAction* action : menu->actions()) {
if (!action) continue;
// 检查是否有子菜单
if (QMenu* subMenu = action->menu()) {
clearMenuProperties(subMenu); // 递归处理子菜单
}
// 清除动作的属性
action->setProperty("customProperty", QVariant());
qDebug() << "清除了属性,动作:" << action->text();
}
}
void MainWindow::onMenuActionTriggered()
{
qDebug() << "onMenuActionTriggered()" << endl;
QAction *action = qobject_cast<QAction*>(sender());
if (action) {
// 获取触发动作的父菜单
QMenu *menu = qobject_cast<QMenu*>(action->parent());
// 遍历所有父菜单,直到找到按钮
while (menu) {
QPushButton *button = qobject_cast<QPushButton*>(menu->parent());
if (button) {
qDebug() << "子菜单项被点击,所属按钮:" << button->objectName() << action->text();
int slot_type = action->text().mid(1,2).toInt();
QString rack_type = action->text().right(action->text().length()-4);
int button_id = button->objectName().right(button->objectName().length()-15).toInt();
qDebug() << slot_type << rack_type << button_id << map_slot_config[button_id + 1].slot_type << map_slot_config[button_id + 2].slot_type << endl;
if(rack_type == "三冗余板卡" && (map_slot_config[button_id].slot_type != 0 || map_slot_config[button_id + 1].slot_type != 0 \
|| map_slot_config[button_id + 2].slot_type != 0)){
QMessageBox::information(this, QStringLiteral("提示"), "不要重叠三冗余板卡配置,请在创建新配置之前移除现有的配置!");
return;
}else if(rack_type == "三冗余板卡" && map_slot_config[button_id + 1].slot_type == 0 \
&& map_slot_config[button_id + 2].slot_type == 0){
map_slot_config[button_id].slot_type = slot_type;
map_slot_config[button_id].rack_type = "TMR1";
map_slot_config[button_id].slot_btn->setText(action->text());
map_slot_config[button_id].chan_display = action->text();
map_slot_config[button_id + 1].slot_type = slot_type;
map_slot_config[button_id + 1].rack_type = "TMR2";
map_slot_config[button_id + 1].slot_btn->setText(action->text());
map_slot_config[button_id + 1].chan_display = action->text();
map_slot_config[button_id + 2].slot_type = slot_type;
map_slot_config[button_id + 2].rack_type = "TMR3";
map_slot_config[button_id + 2].slot_btn->setText(action->text());
map_slot_config[button_id + 2].chan_display = action->text();
}
if(rack_type == "两板卡" && (map_slot_config[button_id].slot_type != 0 || map_slot_config[button_id + 1].slot_type != 0 )){
QMessageBox::information(this, QStringLiteral("提示"), "不要重叠两板卡配置,请在创建新配置之前移除现有的配置!");
return;
}else if(rack_type == "两板卡" && map_slot_config[button_id + 1].slot_type == 0){
map_slot_config[button_id].slot_type = slot_type;
map_slot_config[button_id].rack_type = "Double1";
map_slot_config[button_id].slot_btn->setText(action->text());
map_slot_config[button_id].chan_display = action->text();
map_slot_config[button_id + 1].slot_type = slot_type;
map_slot_config[button_id + 1].rack_type = "Double2";
map_slot_config[button_id + 1].slot_btn->setText(action->text());
map_slot_config[button_id + 1].chan_display = action->text();
}
if(rack_type == "单板卡" && map_slot_config[button_id].slot_type != 0){
QMessageBox::information(this, QStringLiteral("提示"), "不要重叠单板卡配置,请在创建新配置之前移除现有的配置!");
return;
}else if(rack_type == "单板卡" && map_slot_config[button_id].slot_type == 0) {
map_slot_config[button_id].slot_type = slot_type;
map_slot_config[button_id].rack_type = "Single";
map_slot_config[button_id].chan_display = action->text();
button->setText(action->text());
}
if(action->text() == "重置模块"){
if(map_slot_config[button_id].rack_type == "TMR1"){
map_slot_config[button_id].slot_type = 0;
map_slot_config[button_id].rack_type = "0";
map_slot_config[button_id].slot_btn->setText("");
map_slot_config[button_id].chan_display = "";
map_slot_config[button_id + 1].slot_type = 0;
map_slot_config[button_id + 1].rack_type = "0";
map_slot_config[button_id + 1].slot_btn->setText("");
map_slot_config[button_id + 1].chan_display = "";
map_slot_config[button_id + 2].slot_type = 0;
map_slot_config[button_id + 2].rack_type = "0";
map_slot_config[button_id + 2].slot_btn->setText("");
map_slot_config[button_id + 2].chan_display = "";
}else if(map_slot_config[button_id].rack_type == "TMR2"){
map_slot_config[button_id - 1].slot_type = 0;
map_slot_config[button_id - 1].rack_type = "0";
map_slot_config[button_id - 1].slot_btn->setText("");
map_slot_config[button_id - 1].chan_display = "";
map_slot_config[button_id].slot_type = 0;
map_slot_config[button_id].rack_type = "0";
map_slot_config[button_id].slot_btn->setText("");
map_slot_config[button_id].chan_display = "";
map_slot_config[button_id + 1].slot_type = 0;
map_slot_config[button_id + 1].rack_type = "0";
map_slot_config[button_id + 1].slot_btn->setText("");
map_slot_config[button_id + 1].chan_display = "";
}else if(map_slot_config[button_id].rack_type == "TMR3"){
map_slot_config[button_id - 2].slot_type = 0;
map_slot_config[button_id - 2].rack_type = "0";
map_slot_config[button_id - 2].slot_btn->setText("");
map_slot_config[button_id - 2].chan_display = "";
map_slot_config[button_id - 1].slot_type = 0;
map_slot_config[button_id - 1].rack_type = "0";
map_slot_config[button_id - 1].slot_btn->setText("");
map_slot_config[button_id - 1].chan_display = "";
map_slot_config[button_id].slot_type = 0;
map_slot_config[button_id].rack_type = "0";
map_slot_config[button_id].slot_btn->setText("");
map_slot_config[button_id].chan_display = "";
}
if(map_slot_config[button_id].rack_type == "Double1"){
map_slot_config[button_id].slot_type = 0;
map_slot_config[button_id].rack_type = "0";
map_slot_config[button_id].slot_btn->setText("");
map_slot_config[button_id].chan_display = "";
map_slot_config[button_id + 1].slot_type = 0;
map_slot_config[button_id + 1].rack_type = "0";
map_slot_config[button_id + 1].slot_btn->setText("");
map_slot_config[button_id + 1].chan_display = "";
}else if(map_slot_config[button_id].rack_type == "Double2"){
map_slot_config[button_id - 1].slot_type = 0;
map_slot_config[button_id - 1].rack_type = "0";
map_slot_config[button_id - 1].slot_btn->setText("");
map_slot_config[button_id - 1].chan_display = "";
map_slot_config[button_id].slot_type = 0;
map_slot_config[button_id].rack_type = "0";
map_slot_config[button_id].slot_btn->setText("");
map_slot_config[button_id].chan_display = "";
}
if(map_slot_config[button_id].rack_type == "Single"){
map_slot_config[button_id].slot_type = 0;
map_slot_config[button_id].rack_type = "0";
map_slot_config[button_id].slot_btn->setText("");
map_slot_config[button_id].chan_display = "";
}
}else if(action->text() == "升级固件"){
sendUpgradePackage(button_id-3);
}
// QMenu* menu = button->menu();
// if (!menu) {
// qWarning() << "菜单为空,无法清除属性!";
// return;
// }
clearMenuProperties(button->menu());
createMenu();
break; // 找到按钮后,跳出循环
}
// 如果没有找到按钮,继续向上查找
menu = qobject_cast<QMenu*>(menu->parent());
}
}
}
void MainWindow::OnButtonGroup(QAbstractButton * slot_btn)
{
if(slot_btn != NULL && ui->pushButton_chan->isChecked())
{
QString object_name = slot_btn->objectName();
qDebug() << object_name << endl;
int button_id = object_name.right(object_name.length()-15).toInt();
SlotConfig slot_config = map_slot_config[button_id];
if (slot_config.slot_type == KEYPHASOR){
KeyPhase *key_phase = new KeyPhase();
key_phase->setWindowModality(Qt::ApplicationModal);
key_phase->show();
}else if (slot_config.slot_type == RELAY){
SingleRelay *single_relay = new SingleRelay();
single_relay->setWindowModality(Qt::ApplicationModal);
single_relay->show();
}else if (slot_config.slot_type == VIBRATE){
Seismic_monitor *seismic_monitor = new Seismic_monitor(button_id);
seismic_monitor->setWindowModality(Qt::ApplicationModal);
seismic_monitor->show();
}else if (slot_config.slot_type == RPM){
Tachometer *tachometer = new Tachometer();
tachometer->setWindowModality(Qt::ApplicationModal);
tachometer->show();
}
}
if(slot_btn != NULL && ui->pushButton_alarm->isChecked()){
QString object_name = slot_btn->objectName();
qDebug() << object_name << endl;
Setpoint *setpoint = new Setpoint();
setpoint->setWindowModality(Qt::ApplicationModal);
setpoint->show();
}
}
void MainWindow::on_pushButton_slot_clicked()
{
if(ui->pushButton_chan->isChecked())
ui->pushButton_chan->setChecked(false);
if(ui->pushButton_alarm->isChecked())
ui->pushButton_alarm->setChecked(false);
if(ui->pushButton_point_name->isChecked())
ui->pushButton_point_name->setChecked(false);
}
void MainWindow::on_pushButton_chan_clicked()
{
if(ui->pushButton_slot->isChecked())
ui->pushButton_slot->setChecked(false);
if(ui->pushButton_alarm->isChecked())
ui->pushButton_alarm->setChecked(false);
if(ui->pushButton_point_name->isChecked())
ui->pushButton_point_name->setChecked(false);
}
void MainWindow::on_pushButton_alarm_clicked()
{
if(ui->pushButton_slot->isChecked())
ui->pushButton_slot->setChecked(false);
if(ui->pushButton_chan->isChecked())
ui->pushButton_chan->setChecked(false);
if(ui->pushButton_point_name->isChecked())
ui->pushButton_point_name->setChecked(false);
}
void MainWindow::on_pushButton_point_name_clicked()
{
if(ui->pushButton_slot->isChecked())
ui->pushButton_slot->setChecked(false);
if(ui->pushButton_chan->isChecked())
ui->pushButton_chan->setChecked(false);
if(ui->pushButton_alarm->isChecked())
ui->pushButton_alarm->setChecked(false);
}
void MainWindow::on_pushButton_save_clicked()
{
QJsonObject itemObj;
QJsonArray slotArray;
for(int i = 0; i < map_slot_config.size();i++){
itemObj["slot"] = map_slot_config[i+3].slot;
itemObj["slot_type"] = map_slot_config[i+3].slot_type;
itemObj["chan_display"] = map_slot_config[i+3].chan_display;
itemObj["rack_type"] = map_slot_config[i+3].rack_type;
slotArray.append(itemObj);
}
QJsonDocument jsonDoc;
jsonDoc.setArray(slotArray);
QFile file(QCoreApplication::applicationDirPath() + "\\config\\main.json");
file.open(QIODevice::WriteOnly);
file.write(jsonDoc.toJson());
file.close();
}
void MainWindow::on_pushButton_open_clicked()
{
}
uint8_t calculate_crc(uint8_t c,const QByteArray &data) {
uint8_t crc = c; // 初始化 CRC 为 0
for (int i = 0; i < data.size(); ++i) {
crc += static_cast<uint8_t>(data[i]); // 累加每个字节
}
return crc;
}
uint32_t myHtonl(uint32_t value) {
return ((value >> 24) & 0x000000FF) | // 提取最高的8位
((value >> 8) & 0x0000FF00) | // 提取中间的8位
((value << 8) & 0x00FF0000) | // 提取次高的8位
((value << 24) & 0xFF000000); // 提取最低的8位
}
void MainWindow::sendUpgradePackage(int slot)
{
QString filepath = QFileDialog::getOpenFileName(this, tr("选择文件"), tr(""), tr("*"));
qDebug() << filepath << slot << endl;
QFileInfo fileinfo;
fileinfo = QFileInfo(filepath);
QString file_suffix = fileinfo.suffix();
QString FileName = fileinfo.fileName();
if(FileName.isEmpty())
return;
QFile file(filepath);
if (!file.open(QIODevice::ReadOnly)) {
qWarning() << "Failed to open update file.";
return;
}
QTcpSocket socket;
// 连接到服务器
socket.connectToHost(m_strServerIp, 10000);
if (!socket.waitForConnected()) {
qDebug() << "Connection failed!";
return;
}
// 读取文件内容
QByteArray fileData = file.readAll();
int fileSize = fileData.size();
qDebug() << "fileSize1" << fileSize;
fileSize = myHtonl(fileSize);
qDebug() << "fileSize2" << fileSize;
// 创建 PackageHead 结构体
PackageHead header = { {0xAA, 0x55, 0xAA}, 3, fileSize,0,{} };
// 计算文件的 CRC 校验和
qDebug() << "filheader.file_md5" << slot <<endl;
UpgradeCardReq upgrade_car_req;
if(slot == 0)
upgrade_car_req.card_id = 0xFF;
else
upgrade_car_req.card_id = slot & 0xFF;
header.crc = calculate_crc(upgrade_car_req.card_id,fileData);
qDebug() << "filheader.crc" << header.crc;
// 发送头部数据
QByteArray packet(reinterpret_cast<char*>(&header), sizeof(PackageHead));
// 发送文件内容
socket.write(packet); // 发送头部
socket.waitForBytesWritten();
QByteArray packet2(reinterpret_cast<char*>(&upgrade_car_req), sizeof(UpgradeCardReq));
socket.write(packet2); // 发送头部
socket.waitForBytesWritten();
socket.write(fileData); // 发送文件数据
socket.waitForBytesWritten();
qDebug() << "File sent successfully";
// 等待服务器响应(根据需要处理响应)
if (socket.waitForReadyRead()) {
QByteArray response = socket.readAll();
UpgradeRsp resp;
QByteArray byteArray = response.mid(sizeof(PackageHead));
QDataStream stream(&byteArray, QIODevice::ReadOnly);
stream >> resp.code ;
if(resp.code == 1){
QMessageBox::information(this, QStringLiteral("提示"), "上传成功!");
}
qDebug() << "Server response: " << resp.code;
}
// 关闭文件和连接
file.close();
socket.disconnectFromHost();
}