2024-12-17 18:53:41 +08:00
|
|
|
#include "keyphase.h"
|
|
|
|
#include "ui_keyphase.h"
|
2025-03-23 14:03:48 +08:00
|
|
|
#include <QDebug>
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonArray>
|
|
|
|
#include <QJsonObject>
|
|
|
|
#include <QFile>
|
2024-12-17 18:53:41 +08:00
|
|
|
|
2025-03-27 10:16:01 +08:00
|
|
|
KeyPhase::KeyPhase(int slot_no_, QWidget *parent)
|
2024-12-17 18:53:41 +08:00
|
|
|
: QDialog(parent)
|
2025-03-27 10:16:01 +08:00
|
|
|
, ui(new Ui::KeyPhase) {
|
2024-12-17 18:53:41 +08:00
|
|
|
ui->setupUi(this);
|
2025-03-11 16:42:00 +08:00
|
|
|
ui->widget_body->setProperty("flag", "body");
|
2025-03-27 10:16:01 +08:00
|
|
|
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
|
2025-03-23 14:03:48 +08:00
|
|
|
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();
|
2025-03-28 10:17:18 +08:00
|
|
|
connect(ui->radioButton_manual_threshold_1, &QRadioButton::toggled, this, &KeyPhase::on_manual_threshold_1_clicked);
|
|
|
|
connect(ui->radioButton_manual_threshold_2, &QRadioButton::toggled, this, &KeyPhase::on_manual_threshold_2_clicked);
|
|
|
|
connect(ui->radioButton_manual_threshold_3, &QRadioButton::toggled, this, &KeyPhase::on_manual_threshold_3_clicked);
|
|
|
|
connect(ui->radioButton_manual_threshold_4, &QRadioButton::toggled, this, &KeyPhase::on_manual_threshold_4_clicked);
|
2024-12-17 18:53:41 +08:00
|
|
|
}
|
|
|
|
|
2025-03-27 10:16:01 +08:00
|
|
|
KeyPhase::~KeyPhase() {
|
2024-12-17 18:53:41 +08:00
|
|
|
delete ui;
|
|
|
|
}
|
2025-03-27 10:16:01 +08:00
|
|
|
|
|
|
|
void KeyPhase::on_pushButton_cancel_clicked() {
|
|
|
|
this->close();
|
|
|
|
}
|
|
|
|
|
2025-03-28 10:17:18 +08:00
|
|
|
void KeyPhase::on_manual_threshold_1_clicked(bool checked) {
|
|
|
|
if (checked) {
|
|
|
|
ui->doubleSpinBox_threshold_1->setEnabled(true);
|
|
|
|
ui->doubleSpinBox_hysteresis_1->setEnabled(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ui->doubleSpinBox_threshold_1->setEnabled(false);
|
|
|
|
ui->doubleSpinBox_hysteresis_1->setEnabled(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void KeyPhase::on_manual_threshold_2_clicked(bool checked) {
|
|
|
|
if (checked) {
|
|
|
|
ui->doubleSpinBox_threshold_2->setEnabled(true);
|
|
|
|
ui->doubleSpinBox_hysteresis_2->setEnabled(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ui->doubleSpinBox_threshold_2->setEnabled(false);
|
|
|
|
ui->doubleSpinBox_hysteresis_2->setEnabled(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void KeyPhase::on_manual_threshold_3_clicked(bool checked) {
|
|
|
|
if (checked) {
|
|
|
|
ui->doubleSpinBox_threshold_3->setEnabled(true);
|
|
|
|
ui->doubleSpinBox_hysteresis_3->setEnabled(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ui->doubleSpinBox_threshold_3->setEnabled(false);
|
|
|
|
ui->doubleSpinBox_hysteresis_3->setEnabled(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void KeyPhase::on_manual_threshold_4_clicked(bool checked) {
|
|
|
|
if (checked) {
|
|
|
|
ui->doubleSpinBox_threshold_4->setEnabled(true);
|
|
|
|
ui->doubleSpinBox_hysteresis_4->setEnabled(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ui->doubleSpinBox_threshold_4->setEnabled(false);
|
|
|
|
ui->doubleSpinBox_hysteresis_4->setEnabled(false);
|
|
|
|
}
|
|
|
|
|
2025-03-27 10:16:01 +08:00
|
|
|
void KeyPhase::readJsonFile(const QString &filePath) {
|
2025-03-23 14:03:48 +08:00
|
|
|
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();
|
2025-03-27 10:16:01 +08:00
|
|
|
for (int i = 0; i < chan_array.size(); i++) {
|
2025-03-23 14:03:48 +08:00
|
|
|
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();
|
2025-04-02 14:07:37 +08:00
|
|
|
keyphase_variables[i].record_output = temp_obj["record_output"].toInt();
|
2025-03-23 14:03:48 +08:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
2025-03-27 10:16:01 +08:00
|
|
|
void KeyPhase::Init() {
|
2025-04-02 09:59:55 +08:00
|
|
|
for (int i = 0; i < CHANNEL_COUNT; i++) {
|
2025-03-27 10:16:01 +08:00
|
|
|
if (keyphase_variables[i].id == 1) {
|
2025-03-23 14:03:48 +08:00
|
|
|
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);
|
2025-03-27 10:16:01 +08:00
|
|
|
if (keyphase_variables[i].automatic_threshold) {
|
2025-03-23 14:03:48 +08:00
|
|
|
ui->radioButton_automatic_threshold_1->setChecked(true);
|
2025-03-27 10:16:01 +08:00
|
|
|
} else {
|
2025-03-23 14:03:48 +08:00
|
|
|
ui->radioButton_manual_threshold_1->setChecked(true);
|
2025-03-27 10:16:01 +08:00
|
|
|
}
|
2025-03-23 14:03:48 +08:00
|
|
|
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);
|
|
|
|
}
|
2025-03-27 10:16:01 +08:00
|
|
|
if (keyphase_variables[i].id == 2) {
|
2025-03-23 14:03:48 +08:00
|
|
|
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);
|
2025-03-27 10:16:01 +08:00
|
|
|
if (keyphase_variables[i].automatic_threshold) {
|
2025-03-23 14:03:48 +08:00
|
|
|
ui->radioButton_automatic_threshold_2->setChecked(true);
|
2025-03-27 10:16:01 +08:00
|
|
|
} else {
|
2025-03-23 14:03:48 +08:00
|
|
|
ui->radioButton_manual_threshold_2->setChecked(true);
|
2025-03-27 10:16:01 +08:00
|
|
|
}
|
2025-03-23 14:03:48 +08:00
|
|
|
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);
|
|
|
|
}
|
2025-03-27 10:16:01 +08:00
|
|
|
if (keyphase_variables[i].id == 3) {
|
2025-03-23 14:03:48 +08:00
|
|
|
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);
|
2025-03-27 10:16:01 +08:00
|
|
|
if (keyphase_variables[i].automatic_threshold) {
|
2025-03-23 14:03:48 +08:00
|
|
|
ui->radioButton_automatic_threshold_3->setChecked(true);
|
2025-03-27 10:16:01 +08:00
|
|
|
} else {
|
2025-03-23 14:03:48 +08:00
|
|
|
ui->radioButton_manual_threshold_3->setChecked(true);
|
2025-03-27 10:16:01 +08:00
|
|
|
}
|
2025-03-23 14:03:48 +08:00
|
|
|
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);
|
|
|
|
}
|
2025-03-27 10:16:01 +08:00
|
|
|
if (keyphase_variables[i].id == 4) {
|
2025-03-23 14:03:48 +08:00
|
|
|
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);
|
2025-03-27 10:16:01 +08:00
|
|
|
if (keyphase_variables[i].automatic_threshold) {
|
2025-03-23 14:03:48 +08:00
|
|
|
ui->radioButton_automatic_threshold_4->setChecked(true);
|
2025-03-27 10:16:01 +08:00
|
|
|
} else {
|
2025-03-23 14:03:48 +08:00
|
|
|
ui->radioButton_manual_threshold_4->setChecked(true);
|
2025-03-27 10:16:01 +08:00
|
|
|
}
|
2025-03-23 14:03:48 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2025-03-27 10:16:01 +08:00
|
|
|
void KeyPhase::on_pushButton_confirm_clicked() {
|
2025-04-02 09:59:55 +08:00
|
|
|
for (int i = 0; i < CHANNEL_COUNT; i++) {
|
2025-03-27 10:16:01 +08:00
|
|
|
if (keyphase_variables[i].id == 1) {
|
2025-03-23 14:03:48 +08:00
|
|
|
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();
|
|
|
|
}
|
2025-03-27 10:16:01 +08:00
|
|
|
if (keyphase_variables[i].id == 2) {
|
2025-03-23 14:03:48 +08:00
|
|
|
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();
|
|
|
|
}
|
2025-03-27 10:16:01 +08:00
|
|
|
if (keyphase_variables[i].id == 3) {
|
2025-03-23 14:03:48 +08:00
|
|
|
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();
|
|
|
|
}
|
2025-03-27 10:16:01 +08:00
|
|
|
if (keyphase_variables[i].id == 4) {
|
2025-03-23 14:03:48 +08:00
|
|
|
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);
|
2025-03-27 10:16:01 +08:00
|
|
|
if (!file.open(QIODevice::WriteOnly)) {
|
2025-03-23 14:03:48 +08:00
|
|
|
qDebug() << "Could not open file for writing";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QJsonObject json_obj;
|
|
|
|
QJsonArray chan_array;
|
2025-04-02 09:59:55 +08:00
|
|
|
for (int i = 0; i < CHANNEL_COUNT; i++) {
|
2025-03-23 14:03:48 +08:00
|
|
|
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);
|
2025-03-27 10:16:01 +08:00
|
|
|
temp_obj.insert("automatic_threshold", keyphase_variables[i].automatic_threshold);
|
2025-03-23 14:03:48 +08:00
|
|
|
chan_array.append(temp_obj);
|
|
|
|
}
|
|
|
|
json_obj.insert("chan", chan_array);
|
2025-03-27 10:16:01 +08:00
|
|
|
json_obj.insert("version", 1);
|
|
|
|
json_obj.insert("slot", slot_no);
|
|
|
|
json_obj.insert("card_type", 2);
|
2025-03-23 14:03:48 +08:00
|
|
|
QJsonDocument json_doc;
|
|
|
|
json_doc.setObject(json_obj);
|
|
|
|
QByteArray byte_array = json_doc.toJson();
|
|
|
|
file.write(byte_array);
|
|
|
|
file.close();
|
2025-02-18 10:55:51 +08:00
|
|
|
}
|
|
|
|
|