From fcae5b823675f78d2a4a665d4518e3ee5643c6d6 Mon Sep 17 00:00:00 2001 From: pandx Date: Wed, 2 Apr 2025 14:28:53 +0800 Subject: [PATCH] add keyphase codes. --- TSI_Config.pro | 2 + data_config.h | 10 ++ keyphase.cpp | 232 ++++++++++++++++++---------------------------- keyphase.h | 8 +- keyphase_data.cpp | 6 ++ keyphase_data.h | 11 +++ 6 files changed, 126 insertions(+), 143 deletions(-) create mode 100644 keyphase_data.cpp create mode 100644 keyphase_data.h diff --git a/TSI_Config.pro b/TSI_Config.pro index 1e61790..28c34e0 100644 --- a/TSI_Config.pro +++ b/TSI_Config.pro @@ -15,6 +15,7 @@ SOURCES += \ common.cpp \ config_mgr.cpp \ keyphase.cpp \ + keyphase_data.cpp \ main.cpp \ mainwindow.cpp \ radial_vibration.cpp \ @@ -37,6 +38,7 @@ HEADERS += \ data_config.h \ displacement_ds.h \ keyphase.h \ + keyphase_data.h \ mainwindow.h \ radial_vibration.h \ relaysetting.h \ diff --git a/data_config.h b/data_config.h index e46c9f1..754562d 100644 --- a/data_config.h +++ b/data_config.h @@ -178,6 +178,16 @@ typedef struct { bool normal_latching; } TachometerVariables; +typedef struct { + bool active; + float normal_voltage_low; + float normal_voltage_high; + bool automatic_threshold; + float threshold; + float hysteresis; + int events_per_revolution; +} KeyphaseVariables; + typedef struct { QString transducer_name; double scale_factor; diff --git a/keyphase.cpp b/keyphase.cpp index 20aaedc..c7018f6 100644 --- a/keyphase.cpp +++ b/keyphase.cpp @@ -6,6 +6,10 @@ #include #include +#include "data_config.h" +#include "config_mgr.h" +#include "keyphase_data.h" + KeyPhase::KeyPhase(int slot_no_, QWidget *parent) : QDialog(parent) , ui(new Ui::KeyPhase) { @@ -15,8 +19,8 @@ KeyPhase::KeyPhase(int slot_no_, QWidget *parent) 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); +// QString filePath_keyphase = QCoreApplication::applicationDirPath() + QString("\\config\\%1\\keyphase.json").arg(slot_no); +// readJsonFile(filePath_keyphase); Init(); 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); @@ -72,169 +76,117 @@ void KeyPhase::on_manual_threshold_4_clicked(bool checked) { ui->doubleSpinBox_hysteresis_4->setEnabled(false); } -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"].toInt(); - 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::UpdateData(std::shared_ptr &keyphase_data) { + keyphase_data->card_type_ = kCardKeyphase; + keyphase_data->slot_ = slot_no; + keyphase_data->version_ = 1; + for (int i = 0; i < CHANNEL_COUNT; i++) { + if (i + 1 == 1) { + keyphase_data->variables_[i].active = ui->checkBox_enable_1->isChecked(); + keyphase_data->variables_[i].normal_voltage_high = ui->doubleSpinBox_high_1->value(); + keyphase_data->variables_[i].normal_voltage_low = ui->doubleSpinBox_low_1->value(); + keyphase_data->variables_[i].automatic_threshold = ui->radioButton_automatic_threshold_1->isChecked(); + keyphase_data->variables_[i].threshold = ui->doubleSpinBox_threshold_1->value(); + keyphase_data->variables_[i].hysteresis = ui->doubleSpinBox_hysteresis_1->value(); + keyphase_data->variables_[i].events_per_revolution = ui->spinBox_events_per_revolution_1->value(); + } else if (i + 1 == 2) { + keyphase_data->variables_[i].active = ui->checkBox_enable_2->isChecked(); + keyphase_data->variables_[i].normal_voltage_high = ui->doubleSpinBox_high_2->value(); + keyphase_data->variables_[i].normal_voltage_low = ui->doubleSpinBox_low_2->value(); + keyphase_data->variables_[i].automatic_threshold = ui->radioButton_automatic_threshold_2->isChecked(); + keyphase_data->variables_[i].threshold = ui->doubleSpinBox_threshold_2->value(); + keyphase_data->variables_[i].hysteresis = ui->doubleSpinBox_hysteresis_2->value(); + keyphase_data->variables_[i].events_per_revolution = ui->spinBox_events_per_revolution_2->value(); + } else if (i + 1 == 3) { + keyphase_data->variables_[i].active = ui->checkBox_enable_3->isChecked(); + keyphase_data->variables_[i].normal_voltage_high = ui->doubleSpinBox_high_3->value(); + keyphase_data->variables_[i].normal_voltage_low = ui->doubleSpinBox_low_3->value(); + keyphase_data->variables_[i].automatic_threshold = ui->radioButton_automatic_threshold_3->isChecked(); + keyphase_data->variables_[i].threshold = ui->doubleSpinBox_threshold_3->value(); + keyphase_data->variables_[i].hysteresis = ui->doubleSpinBox_hysteresis_3->value(); + keyphase_data->variables_[i].events_per_revolution = ui->spinBox_events_per_revolution_3->value(); + } else if (i + 1 == 4) { + keyphase_data->variables_[i].active = ui->checkBox_enable_4->isChecked(); + keyphase_data->variables_[i].normal_voltage_high = ui->doubleSpinBox_high_4->value(); + keyphase_data->variables_[i].normal_voltage_low = ui->doubleSpinBox_low_4->value(); + keyphase_data->variables_[i].automatic_threshold = ui->radioButton_automatic_threshold_4->isChecked(); + keyphase_data->variables_[i].threshold = ui->doubleSpinBox_threshold_4->value(); + keyphase_data->variables_[i].hysteresis = ui->doubleSpinBox_hysteresis_4->value(); + keyphase_data->variables_[i].events_per_revolution = ui->spinBox_events_per_revolution_4->value(); + } } } + void KeyPhase::Init() { + std::shared_ptr base_ptr = ConfigMgr::Instance()->GetSlotPtr(slot_no); + if (base_ptr == nullptr) { + // do nothing or use template to init it. + std::shared_ptr keyphase_data = std::make_shared(); + ConfigMgr::Instance()->AddCard(keyphase_data); + UpdateData(keyphase_data); + return; + } + std::shared_ptr keyphase_data = std::dynamic_pointer_cast(base_ptr); for (int i = 0; i < CHANNEL_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) { + if (i + 1 == 1) { + ui->checkBox_enable_1->setChecked(keyphase_data->variables_[i].active); + ui->doubleSpinBox_high_1->setValue(keyphase_data->variables_[i].normal_voltage_high); + ui->doubleSpinBox_low_1->setValue(keyphase_data->variables_[i].normal_voltage_low); + if (keyphase_data->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->doubleSpinBox_threshold_1->setValue(keyphase_data->variables_[i].threshold); + ui->doubleSpinBox_hysteresis_1->setValue(keyphase_data->variables_[i].hysteresis); + ui->spinBox_events_per_revolution_1->setValue(keyphase_data->variables_[i].events_per_revolution); + } else if (i + 1 == 2) { + ui->checkBox_enable_2->setChecked(keyphase_data->variables_[i].active); + ui->doubleSpinBox_high_2->setValue(keyphase_data->variables_[i].normal_voltage_high); + ui->doubleSpinBox_low_2->setValue(keyphase_data->variables_[i].normal_voltage_low); + if (keyphase_data->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->doubleSpinBox_threshold_2->setValue(keyphase_data->variables_[i].threshold); + ui->doubleSpinBox_hysteresis_2->setValue(keyphase_data->variables_[i].hysteresis); + ui->spinBox_events_per_revolution_2->setValue(keyphase_data->variables_[i].events_per_revolution); + } else if (i + 1 == 3) { + ui->checkBox_enable_3->setChecked(keyphase_data->variables_[i].active); + ui->doubleSpinBox_high_3->setValue(keyphase_data->variables_[i].normal_voltage_high); + ui->doubleSpinBox_low_3->setValue(keyphase_data->variables_[i].normal_voltage_low); + if (keyphase_data->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->doubleSpinBox_threshold_3->setValue(keyphase_data->variables_[i].threshold); + ui->doubleSpinBox_hysteresis_3->setValue(keyphase_data->variables_[i].hysteresis); + ui->spinBox_events_per_revolution_3->setValue(keyphase_data->variables_[i].events_per_revolution); + } else if (i + 1 == 4) { + ui->checkBox_enable_4->setChecked(keyphase_data->variables_[i].active); + ui->doubleSpinBox_high_4->setValue(keyphase_data->variables_[i].normal_voltage_high); + ui->doubleSpinBox_low_4->setValue(keyphase_data->variables_[i].normal_voltage_low); + if (keyphase_data->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); + ui->doubleSpinBox_threshold_4->setValue(keyphase_data->variables_[i].threshold); + ui->doubleSpinBox_hysteresis_4->setValue(keyphase_data->variables_[i].hysteresis); + ui->spinBox_events_per_revolution_4->setValue(keyphase_data->variables_[i].events_per_revolution); } } } void KeyPhase::on_pushButton_confirm_clicked() { - for (int i = 0; i < CHANNEL_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"; + std::shared_ptr base_ptr = ConfigMgr::Instance()->GetSlotPtr(slot_no); + if (base_ptr == nullptr) { + qCritical() << " should not be here"; return; } - QJsonObject json_obj; - QJsonArray chan_array; - for (int i = 0; i < CHANNEL_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(); + std::shared_ptr keyphase_data = std::dynamic_pointer_cast(base_ptr); + UpdateData(keyphase_data); + this->close(); } diff --git a/keyphase.h b/keyphase.h index 7eb2475..2f92667 100644 --- a/keyphase.h +++ b/keyphase.h @@ -1,8 +1,9 @@ #ifndef KEYPHASE_H #define KEYPHASE_H - +#include #include #include "data_config.h" +#include "keyphase_data.h" namespace Ui { class KeyPhase; } @@ -24,8 +25,9 @@ class KeyPhase : public QDialog { void on_manual_threshold_4_clicked(bool checked); private: Ui::KeyPhase *ui; - TachometerVariables keyphase_variables[4]; - void readJsonFile(const QString &filePath); + void UpdateData(std::shared_ptr &keyphase_data); +// TachometerVariables keyphase_variables[4]; +// void readJsonFile(const QString &filePath); void Init(); }; diff --git a/keyphase_data.cpp b/keyphase_data.cpp new file mode 100644 index 0000000..1573bb6 --- /dev/null +++ b/keyphase_data.cpp @@ -0,0 +1,6 @@ +#include "keyphase_data.h" + +KeyphaseData::KeyphaseData() +{ + +} diff --git a/keyphase_data.h b/keyphase_data.h new file mode 100644 index 0000000..3f83025 --- /dev/null +++ b/keyphase_data.h @@ -0,0 +1,11 @@ +#ifndef KEYPHASEDATA_H +#define KEYPHASEDATA_H + +#include "cardbase.h" +class KeyphaseData : public CardBase { + public: + KeyphaseData(); + KeyphaseVariables variables_[4]; +}; + +#endif // KEYPHASEDATA_H