TSI_Config/keyphase.cpp
2025-03-27 10:16:01 +08:00

197 lines
10 KiB
C++

#include "keyphase.h"
#include "ui_keyphase.h"
#include <QDebug>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
#include <QFile>
KeyPhase::KeyPhase(int slot_no_, QWidget *parent)
: QDialog(parent)
, ui(new Ui::KeyPhase) {
ui->setupUi(this);
ui->widget_body->setProperty("flag", "body");
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
slot_no = slot_no_;
QString slot = QString("%1").arg(slot_no);
ui->label_slot->setText(slot);
QString filePath_keyphase = QCoreApplication::applicationDirPath() + QString("\\config\\%1\\keyphase.json").arg(slot_no);
readJsonFile(filePath_keyphase);
Init();
}
KeyPhase::~KeyPhase() {
delete ui;
}
void KeyPhase::on_pushButton_cancel_clicked() {
this->close();
}
void KeyPhase::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;
}
QJsonObject json_obj = jsonDoc.object();
QJsonArray chan_array = json_obj["chan"].toArray();
for (int i = 0; i < chan_array.size(); i++) {
QJsonObject temp_obj = chan_array[i].toObject();
keyphase_variables[i].id = temp_obj["id"].toInt();
keyphase_variables[i].active = temp_obj["active"].toBool();
QJsonArray voltage_range_array = temp_obj["normal_voltage_range"].toArray();
keyphase_variables[i].normal_voltage_high = voltage_range_array[1].toDouble();
keyphase_variables[i].normal_voltage_low = voltage_range_array[0].toDouble();
keyphase_variables[i].threshold = temp_obj["threshold"].toDouble();
keyphase_variables[i].hysteresis = temp_obj["hysteresis"].toDouble();
keyphase_variables[i].events_per_revolution = temp_obj["events_per_revolution"].toInt();
keyphase_variables[i].record_output = temp_obj["record_output"].toString();
keyphase_variables[i].two_ma_clamp = temp_obj["two_ma_clamp"].toBool();
keyphase_variables[i].alert_latching = temp_obj["alert_latching"].toBool();
keyphase_variables[i].overspeed_latching = temp_obj["overspeed_latching"].toBool();
keyphase_variables[i].normal_latching = temp_obj["normal_latching"].toBool();
}
}
void KeyPhase::Init() {
for (int i = 0; i < CHANNLE_COUNT; i++) {
if (keyphase_variables[i].id == 1) {
ui->checkBox_enable_1->setChecked(keyphase_variables[i].active);
ui->doubleSpinBox_high_1->setValue(keyphase_variables[i].normal_voltage_high);
ui->doubleSpinBox_low_1->setValue(keyphase_variables[i].normal_voltage_low);
if (keyphase_variables[i].automatic_threshold) {
ui->radioButton_automatic_threshold_1->setChecked(true);
} else {
ui->radioButton_manual_threshold_1->setChecked(true);
}
ui->doubleSpinBox_threshold_1->setValue(keyphase_variables[i].threshold);
ui->doubleSpinBox_hysteresis_1->setValue(keyphase_variables[i].hysteresis);
ui->spinBox_events_per_revolution_1->setValue(keyphase_variables[i].events_per_revolution);
}
if (keyphase_variables[i].id == 2) {
ui->checkBox_enable_2->setChecked(keyphase_variables[i].active);
ui->doubleSpinBox_high_2->setValue(keyphase_variables[i].normal_voltage_high);
ui->doubleSpinBox_low_2->setValue(keyphase_variables[i].normal_voltage_low);
if (keyphase_variables[i].automatic_threshold) {
ui->radioButton_automatic_threshold_2->setChecked(true);
} else {
ui->radioButton_manual_threshold_2->setChecked(true);
}
ui->doubleSpinBox_threshold_2->setValue(keyphase_variables[i].threshold);
ui->doubleSpinBox_hysteresis_2->setValue(keyphase_variables[i].hysteresis);
ui->spinBox_events_per_revolution_2->setValue(keyphase_variables[i].events_per_revolution);
}
if (keyphase_variables[i].id == 3) {
ui->checkBox_enable_3->setChecked(keyphase_variables[i].active);
ui->doubleSpinBox_high_3->setValue(keyphase_variables[i].normal_voltage_high);
ui->doubleSpinBox_low_3->setValue(keyphase_variables[i].normal_voltage_low);
if (keyphase_variables[i].automatic_threshold) {
ui->radioButton_automatic_threshold_3->setChecked(true);
} else {
ui->radioButton_manual_threshold_3->setChecked(true);
}
ui->doubleSpinBox_threshold_3->setValue(keyphase_variables[i].threshold);
ui->doubleSpinBox_hysteresis_3->setValue(keyphase_variables[i].hysteresis);
ui->spinBox_events_per_revolution_3->setValue(keyphase_variables[i].events_per_revolution);
}
if (keyphase_variables[i].id == 4) {
ui->checkBox_enable_4->setChecked(keyphase_variables[i].active);
ui->doubleSpinBox_high_4->setValue(keyphase_variables[i].normal_voltage_high);
ui->doubleSpinBox_low_4->setValue(keyphase_variables[i].normal_voltage_low);
if (keyphase_variables[i].automatic_threshold) {
ui->radioButton_automatic_threshold_4->setChecked(true);
} else {
ui->radioButton_manual_threshold_4->setChecked(true);
}
ui->doubleSpinBox_threshold_4->setValue(keyphase_variables[i].threshold);
ui->doubleSpinBox_hysteresis_4->setValue(keyphase_variables[i].hysteresis);
ui->spinBox_events_per_revolution_4->setValue(keyphase_variables[i].events_per_revolution);
}
}
}
void KeyPhase::on_pushButton_confirm_clicked() {
for (int i = 0; i < CHANNLE_COUNT; i++) {
if (keyphase_variables[i].id == 1) {
keyphase_variables[i].active = ui->checkBox_enable_1->isChecked();
keyphase_variables[i].normal_voltage_high = ui->doubleSpinBox_high_1->value();
keyphase_variables[i].normal_voltage_low = ui->doubleSpinBox_low_1->value();
keyphase_variables[i].automatic_threshold = ui->radioButton_automatic_threshold_1->isChecked();
keyphase_variables[i].threshold = ui->doubleSpinBox_threshold_1->value();
keyphase_variables[i].hysteresis = ui->doubleSpinBox_hysteresis_1->value();
keyphase_variables[i].events_per_revolution = ui->spinBox_events_per_revolution_1->value();
}
if (keyphase_variables[i].id == 2) {
keyphase_variables[i].active = ui->checkBox_enable_2->isChecked();
keyphase_variables[i].normal_voltage_high = ui->doubleSpinBox_high_2->value();
keyphase_variables[i].normal_voltage_low = ui->doubleSpinBox_low_2->value();
keyphase_variables[i].automatic_threshold = ui->radioButton_automatic_threshold_2->isChecked();
keyphase_variables[i].threshold = ui->doubleSpinBox_threshold_2->value();
keyphase_variables[i].hysteresis = ui->doubleSpinBox_hysteresis_2->value();
keyphase_variables[i].events_per_revolution = ui->spinBox_events_per_revolution_2->value();
}
if (keyphase_variables[i].id == 3) {
keyphase_variables[i].active = ui->checkBox_enable_3->isChecked();
keyphase_variables[i].normal_voltage_high = ui->doubleSpinBox_high_3->value();
keyphase_variables[i].normal_voltage_low = ui->doubleSpinBox_low_3->value();
keyphase_variables[i].automatic_threshold = ui->radioButton_automatic_threshold_3->isChecked();
keyphase_variables[i].threshold = ui->doubleSpinBox_threshold_3->value();
keyphase_variables[i].hysteresis = ui->doubleSpinBox_hysteresis_3->value();
keyphase_variables[i].events_per_revolution = ui->spinBox_events_per_revolution_3->value();
}
if (keyphase_variables[i].id == 4) {
keyphase_variables[i].active = ui->checkBox_enable_4->isChecked();
keyphase_variables[i].normal_voltage_high = ui->doubleSpinBox_high_4->value();
keyphase_variables[i].normal_voltage_low = ui->doubleSpinBox_low_4->value();
keyphase_variables[i].automatic_threshold = ui->radioButton_automatic_threshold_4->isChecked();
keyphase_variables[i].threshold = ui->doubleSpinBox_threshold_4->value();
keyphase_variables[i].hysteresis = ui->doubleSpinBox_hysteresis_4->value();
keyphase_variables[i].events_per_revolution = ui->spinBox_events_per_revolution_4->value();
}
}
QString slot = QString("%1").arg(slot_no);
QString filePath_keyphase = QCoreApplication::applicationDirPath() + QString("\\config\\%1\\keyphase.json").arg(slot_no);
QFile file(filePath_keyphase);
if (!file.open(QIODevice::WriteOnly)) {
qDebug() << "Could not open file for writing";
return;
}
QJsonObject json_obj;
QJsonArray chan_array;
for (int i = 0; i < CHANNLE_COUNT; i++) {
QJsonObject temp_obj;
temp_obj.insert("id", keyphase_variables[i].id);
temp_obj.insert("active", keyphase_variables[i].active);
QJsonArray voltage_range_array;
voltage_range_array.append(keyphase_variables[i].normal_voltage_low);
voltage_range_array.append(keyphase_variables[i].normal_voltage_high);
temp_obj.insert("normal_voltage_range", voltage_range_array);
temp_obj.insert("threshold", keyphase_variables[i].threshold);
temp_obj.insert("hysteresis", keyphase_variables[i].hysteresis);
temp_obj.insert("events_per_revolution", keyphase_variables[i].events_per_revolution);
temp_obj.insert("automatic_threshold", keyphase_variables[i].automatic_threshold);
chan_array.append(temp_obj);
}
json_obj.insert("chan", chan_array);
json_obj.insert("version", 1);
json_obj.insert("slot", slot_no);
json_obj.insert("card_type", 2);
QJsonDocument json_doc;
json_doc.setObject(json_obj);
QByteArray byte_array = json_doc.toJson();
file.write(byte_array);
file.close();
}