TSI_Config/seismic_monitor.cpp
2025-04-01 15:03:59 +08:00

386 lines
19 KiB
C++

#include "seismic_monitor.h"
#include "ui_seismic_monitor.h"
#include <QDebug>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
#include <QFile>
#include <QListView>
#include "acceleration.h"
#include "velocity.h"
#include "radial_vibration.h"
#include "data_config.h"
#include "config_mgr.h"
#include "vibrationdata.h"
Seismic_monitor::Seismic_monitor(int slot, QWidget *parent) :
QWidget(parent),
ui(new Ui::Seismic_monitor) {
ui->setupUi(this);
ui->widget_body->setProperty("flag", "body");
ui->comboBox_chan_type_1->setView(new QListView());
ui->comboBox_chan_type_2->setView(new QListView());
ui->comboBox_chan_type_3->setView(new QListView());
ui->comboBox_chan_type_4->setView(new QListView());
ui->comboBox_transducer_name_1->setView(new QListView());
ui->comboBox_transducer_name_2->setView(new QListView());
ui->comboBox_transducer_name_3->setView(new QListView());
ui->comboBox_transducer_name_4->setView(new QListView());
ui->comboBox_sample_rate_1->setView(new QListView());
ui->comboBox_sample_rate_2->setView(new QListView());
ui->comboBox_sample_rate_3->setView(new QListView());
ui->comboBox_sample_rate_4->setView(new QListView());
slot_no = slot;
QString slot_no_ = QString("%1").arg(slot_no);
ui->label_slot_no->setText(slot_no_);
// QString filePath = QCoreApplication::applicationDirPath() + QString("\\config\\%1\\seismic_monitor_slot.json").arg(slot_no);
// readJsonFile(filePath);
Init();
}
Seismic_monitor::~Seismic_monitor() {
delete ui;
}
//void Seismic_monitor::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();
// card_type = json_obj["card_type"].toInt();
// QJsonArray chan_array = json_obj["chan"].toArray();
// for (int i = 0; i < chan_array.size(); i++) {
// QJsonObject temp_obj = chan_array[i].toObject();
// seismic_monitor[i].id = temp_obj["id"].toInt();
//// seismic_monitor[i].channel_name = temp_obj["channle_name"].toString();
// seismic_monitor[i].standby = temp_obj["standby"].toBool();
// seismic_monitor[i].active = temp_obj["active"].toBool();
// seismic_monitor[i].rack_type = temp_obj["rack_type"].toInt();
// seismic_monitor[i].tmr_group = temp_obj["tmr_group"].toString();
// seismic_monitor[i].channel_type = temp_obj["channel_type"].toInt();
// seismic_monitor[i].transducer_name = temp_obj["transducer_name"].toString();
// seismic_monitor[i].scale_factor = temp_obj["scale_factor"].toDouble();
// seismic_monitor[i].sampling_rate = temp_obj["sampling_rate"].toInt();
// QJsonArray voltage_range_array = temp_obj["normal_voltage_range"].toArray();
// seismic_monitor[i].normal_voltage_low = voltage_range_array[0].toDouble();
// seismic_monitor[i].normal_voltage_high = voltage_range_array[1].toDouble();
// }
// QFile file_transducer(QCoreApplication::applicationDirPath() + QString("\\config\\transducer.json"));
// if (!file_transducer.open(QIODevice::ReadOnly | QIODevice::Text)) {
// qDebug() << "Cannot open file for reading:" << filePath;
// return;
// }
// QString content_transducer = file_transducer.readAll();
// file_transducer.close();
// QByteArray jsonData2 = content_transducer.toUtf8();
// QJsonDocument jsonDoc2 = QJsonDocument::fromJson(jsonData2);
// if (jsonDoc2.isNull()) {
// qDebug() << "Cannot parse JSON document";
// return;
// }
// if (!jsonDoc2.isObject() && !jsonDoc2.isArray()) {
// qDebug() << "JSON document is not an object or an array";
// return;
// }
// QJsonArray json_array = jsonDoc2.array();
// for (int var = 0; var < json_array.size(); ++var) {
// Transducer transducer;
// QJsonObject temp_obj = json_array[var].toObject();
// transducer.transducer_name = temp_obj["transducer_name"].toString();
// transducer.scale_factor = temp_obj["scale_factor"].toDouble();
// vec_transducer.push_back(transducer);
// }
//}
void Seismic_monitor::Init() {
// for (int var = 0; var < vec_transducer.size(); ++var) {
// ui->comboBox_transducer_name_1->addItem(vec_transducer[var].transducer_name);
// ui->comboBox_transducer_name_2->addItem(vec_transducer[var].transducer_name);
// ui->comboBox_transducer_name_3->addItem(vec_transducer[var].transducer_name);
// ui->comboBox_transducer_name_4->addItem(vec_transducer[var].transducer_name);
// }
std::shared_ptr<CardBase> base_ptr = ConfigMgr::Instance()->GetSlotPtr(slot_no);
if (base_ptr == nullptr) {
// do nothing or use template to init it.
std::shared_ptr<VibrationData> vib_data = std::make_shared<VibrationData>();
ConfigMgr::Instance()->AddCard(vib_data);
UpdateData(vib_data);
return;
}
std::shared_ptr<VibrationData> vib_data = std::dynamic_pointer_cast<VibrationData>(base_ptr);
for (int i = 0; i < CHANNEL_COUNT; i++) {
if (i + 1 == 1) {
qDebug() << vib_data->base_config_[i].channel_type ;
ui->comboBox_chan_type_1->setCurrentIndex(vib_data->base_config_[i].channel_type);
ui->comboBox_transducer_name_1->setCurrentIndex(vib_data->base_config_[i].transducer_id);
ui->doubleSpinBox_scale_factor_1->setValue(vib_data->base_config_[i].scale_factor);
ui->checkBox_enable_1->setCheckable(vib_data->base_config_[i].active);
ui->doubleSpinBox_low_1->setValue(vib_data->base_config_[i].normal_voltage_low);
ui->doubleSpinBox_high_1->setValue(vib_data->base_config_[i].normal_voltage_high);
ui->comboBox_sample_rate_1->setCurrentIndex(vib_data->base_config_[i].sampling_rate);
} else if (i + 1 == 2) {
ui->comboBox_chan_type_2->setCurrentIndex(vib_data->base_config_[i].channel_type);
ui->comboBox_transducer_name_2->setCurrentIndex(vib_data->base_config_[i].transducer_id);
ui->doubleSpinBox_scale_factor_2->setValue(vib_data->base_config_[i].scale_factor);
ui->checkBox_enable_2->setCheckable(vib_data->base_config_[i].active);
ui->doubleSpinBox_low_2->setValue(vib_data->base_config_[i].normal_voltage_low);
ui->doubleSpinBox_high_2->setValue(vib_data->base_config_[i].normal_voltage_high);
ui->comboBox_sample_rate_2->setCurrentIndex(vib_data->base_config_[i].sampling_rate);
} else if (i + 1 == 3) {
ui->comboBox_chan_type_3->setCurrentIndex(vib_data->base_config_[i].channel_type);
ui->comboBox_transducer_name_3->setCurrentIndex(vib_data->base_config_[i].transducer_id);
ui->doubleSpinBox_scale_factor_3->setValue(vib_data->base_config_[i].scale_factor);
ui->checkBox_enable_3->setCheckable(vib_data->base_config_[i].active);
ui->doubleSpinBox_low_3->setValue(vib_data->base_config_[i].normal_voltage_low);
ui->doubleSpinBox_high_3->setValue(vib_data->base_config_[i].normal_voltage_high);
ui->comboBox_sample_rate_3->setCurrentIndex(vib_data->base_config_[i].sampling_rate);
} else if (i + 1 == 4) {
ui->comboBox_chan_type_4->setCurrentIndex(vib_data->base_config_[i].channel_type);
ui->comboBox_transducer_name_4->setCurrentIndex(vib_data->base_config_[i].transducer_id);
ui->doubleSpinBox_scale_factor_4->setValue(vib_data->base_config_[i].scale_factor);
ui->checkBox_enable_4->setCheckable(vib_data->base_config_[i].active);
ui->doubleSpinBox_low_4->setValue(vib_data->base_config_[i].normal_voltage_low);
ui->doubleSpinBox_high_4->setValue(vib_data->base_config_[i].normal_voltage_high);
ui->comboBox_sample_rate_4->setCurrentIndex(vib_data->base_config_[i].sampling_rate);
}
}
}
void Seismic_monitor::UpdateData(std::shared_ptr<VibrationData> vib_data) {
for (int var = 0; var < CHANNEL_COUNT; ++var) {
if (var + 1 == 1) {
vib_data->base_config_[var].standby = ui->checkBox_standby_1->isChecked();
vib_data->base_config_[var].active = ui->checkBox_enable_1->isChecked();
vib_data->base_config_[var].rack_type = 0;
vib_data->base_config_[var].channel_type = ui->comboBox_chan_type_1->currentIndex();
vib_data->base_config_[var].transducer_id = ui->comboBox_transducer_name_1->currentIndex();
vib_data->base_config_[var].scale_factor = ui->doubleSpinBox_scale_factor_1->value();
vib_data->base_config_[var].sampling_rate = ui->comboBox_sample_rate_1->currentIndex();
vib_data->base_config_[var].normal_voltage_low = ui->doubleSpinBox_low_1->value();
vib_data->base_config_[var].normal_voltage_high = ui->doubleSpinBox_high_1->value();
} else if (var + 1 == 2) {
vib_data->base_config_[var].standby = ui->checkBox_standby_1->isChecked();
vib_data->base_config_[var].active = ui->checkBox_enable_2->isChecked();
vib_data->base_config_[var].rack_type = 0;
vib_data->base_config_[var].channel_type = ui->comboBox_chan_type_2->currentIndex();
vib_data->base_config_[var].transducer_id = ui->comboBox_transducer_name_2->currentIndex();
vib_data->base_config_[var].scale_factor = ui->doubleSpinBox_scale_factor_2->value();
vib_data->base_config_[var].sampling_rate = ui->comboBox_sample_rate_2->currentIndex();
vib_data->base_config_[var].normal_voltage_low = ui->doubleSpinBox_low_2->value();
vib_data->base_config_[var].normal_voltage_high = ui->doubleSpinBox_high_2->value();
} else if (var + 1 == 3) {
vib_data->base_config_[var].standby = ui->checkBox_standby_2->isChecked();
vib_data->base_config_[var].active = ui->checkBox_enable_3->isChecked();
vib_data->base_config_[var].rack_type = 0;
vib_data->base_config_[var].channel_type = ui->comboBox_chan_type_3->currentIndex();
vib_data->base_config_[var].transducer_id = ui->comboBox_transducer_name_3->currentIndex();
vib_data->base_config_[var].scale_factor = ui->doubleSpinBox_scale_factor_3->value();
vib_data->base_config_[var].sampling_rate = ui->comboBox_sample_rate_3->currentIndex();
vib_data->base_config_[var].normal_voltage_low = ui->doubleSpinBox_low_3->value();
vib_data->base_config_[var].normal_voltage_high = ui->doubleSpinBox_high_3->value();
} else if (var + 1 == 4) {
vib_data->base_config_[var].standby = ui->checkBox_standby_2->isChecked();
vib_data->base_config_[var].active = ui->checkBox_enable_4->isChecked();
vib_data->base_config_[var].rack_type = 0;
vib_data->base_config_[var].channel_type = ui->comboBox_chan_type_4->currentIndex();
vib_data->base_config_[var].transducer_id = ui->comboBox_transducer_name_4->currentIndex();
vib_data->base_config_[var].scale_factor = ui->doubleSpinBox_scale_factor_4->value();
vib_data->base_config_[var].sampling_rate = ui->comboBox_sample_rate_4->currentIndex();
vib_data->base_config_[var].normal_voltage_low = ui->doubleSpinBox_low_4->value();
vib_data->base_config_[var].normal_voltage_high = ui->doubleSpinBox_high_4->value();
}
}
}
void Seismic_monitor::on_pushButton_confirm_clicked() {
std::shared_ptr<CardBase> base_ptr = ConfigMgr::Instance()->GetSlotPtr(slot_no);
if (base_ptr == nullptr) {
qCritical() << " should not be here";
// std::shared_ptr<VibrationData> vib_data = std::make_shared<VibrationData>();
// ConfigMgr::Instance()->AddCard(vib_data);
// UpdateData(vib_data);
return;
}
std::shared_ptr<VibrationData> vib_data = std::dynamic_pointer_cast<VibrationData>(base_ptr);
UpdateData(vib_data);
this->close();
}
void Seismic_monitor::on_pushButton_set_default_clicked() {
}
void Seismic_monitor::on_pushButton_config_1_clicked() {
channel = 1;
if (ui->comboBox_chan_type_1->currentIndex() == kVibAcc) {
Acceleration *acceleration = new Acceleration(slot_no, channel, ui->checkBox_enable_1->isChecked());
acceleration->setWindowModality(Qt::ApplicationModal);
acceleration->show();
} else if (ui->comboBox_chan_type_1->currentIndex() == kVibRadial) {
Radial_vibration *radial_vibration = new Radial_vibration(slot_no, channel, ui->checkBox_enable_1->isChecked());
radial_vibration->setWindowModality(Qt::ApplicationModal);
radial_vibration->show();
} else if (ui->comboBox_chan_type_1->currentIndex() == kVibVelocity) {
Velocity *velocity = new Velocity(slot_no, channel, ui->checkBox_enable_1->isChecked());
velocity->setWindowModality(Qt::ApplicationModal);
velocity->show();
}
}
void Seismic_monitor::on_pushButton_config_2_clicked() {
channel = 2;
if (ui->comboBox_chan_type_2->currentIndex() == kVibAcc) {
Acceleration *acceleration = new Acceleration(slot_no, channel, ui->checkBox_enable_2->isChecked());
acceleration->setWindowModality(Qt::ApplicationModal);
acceleration->show();
} else if (ui->comboBox_chan_type_2->currentIndex() == kVibRadial) {
Radial_vibration *radial_vibration = new Radial_vibration(slot_no, channel, ui->checkBox_enable_2->isChecked());
radial_vibration->setWindowModality(Qt::ApplicationModal);
radial_vibration->show();
} else if (ui->comboBox_chan_type_2->currentIndex() == kVibVelocity) {
Velocity *velocity = new Velocity(slot_no, channel, ui->checkBox_enable_2->isChecked());
velocity->setWindowModality(Qt::ApplicationModal);
velocity->show();
}
}
void Seismic_monitor::on_pushButton_config_3_clicked() {
channel = 3;
if (ui->comboBox_chan_type_3->currentIndex() == kVibAcc) {
Acceleration *acceleration = new Acceleration(slot_no, channel, ui->checkBox_enable_3->isChecked());
acceleration->setWindowModality(Qt::ApplicationModal);
acceleration->show();
} else if (ui->comboBox_chan_type_3->currentIndex() == kVibRadial) {
Radial_vibration *radial_vibration = new Radial_vibration(slot_no, channel, ui->checkBox_enable_3->isChecked());
radial_vibration->setWindowModality(Qt::ApplicationModal);
radial_vibration->show();
} else if (ui->comboBox_chan_type_3->currentIndex() == kVibVelocity) {
Velocity *velocity = new Velocity(slot_no, channel, ui->checkBox_enable_3->isChecked());
velocity->setWindowModality(Qt::ApplicationModal);
velocity->show();
}
}
void Seismic_monitor::on_pushButton_config_4_clicked() {
channel = 4;
if (ui->comboBox_chan_type_4->currentIndex() == kVibAcc) {
Acceleration *acceleration = new Acceleration(slot_no, channel, ui->checkBox_enable_4->isChecked());
acceleration->setWindowModality(Qt::ApplicationModal);
acceleration->show();
} else if (ui->comboBox_chan_type_4->currentIndex() == kVibRadial) {
Radial_vibration *radial_vibration = new Radial_vibration(slot_no, channel, ui->checkBox_enable_4->isChecked());
radial_vibration->setWindowModality(Qt::ApplicationModal);
radial_vibration->show();
} else if (ui->comboBox_chan_type_4->currentIndex() == kVibVelocity) {
Velocity *velocity = new Velocity(slot_no, channel, ui->checkBox_enable_4->isChecked());
velocity->setWindowModality(Qt::ApplicationModal);
velocity->show();
}
}
void Seismic_monitor::on_comboBox_chan_type_1_currentTextChanged(const QString &arg1) {
switch (ui->comboBox_chan_type_1->currentIndex()) {
case kVibRadial:
ui->label_unit_1->setText("mV / mm");
break;
case kVibAcc:
ui->label_unit_1->setText("mV / m/s^2");
break;
case kVibVelocity:
ui->label_unit_1->setText("mV / mm/s");
break;
}
}
void Seismic_monitor::on_comboBox_chan_type_2_currentTextChanged(const QString &arg1) {
switch (ui->comboBox_chan_type_1->currentIndex()) {
case kVibRadial:
ui->label_unit_2->setText("mV / mm");
break;
case kVibAcc:
ui->label_unit_2->setText("mV / m/s^2");
break;
case kVibVelocity:
ui->label_unit_2->setText("mV / mm/s");
break;
}
}
void Seismic_monitor::on_comboBox_chan_type_3_currentTextChanged(const QString &arg1) {
switch (ui->comboBox_chan_type_1->currentIndex()) {
case kVibRadial:
ui->label_unit_3->setText("mV / mm");
break;
case kVibAcc:
ui->label_unit_3->setText("mV / m/s^2");
break;
case kVibVelocity:
ui->label_unit_3->setText("mV / mm/s");
break;
}
}
void Seismic_monitor::on_comboBox_chan_type_4_currentTextChanged(const QString &arg1) {
switch (ui->comboBox_chan_type_1->currentIndex()) {
case kVibRadial:
ui->label_unit_4->setText("mV / mm");
break;
case kVibAcc:
ui->label_unit_4->setText("mV / m/s^2");
break;
case kVibVelocity:
ui->label_unit_4->setText("mV / mm/s");
break;
}
}
void Seismic_monitor::on_pushButton_cancel_clicked() {
this->close();
}
void Seismic_monitor::on_comboBox_transducer_name_1_currentTextChanged(const QString &arg1) {
for (int var = 0; var < vec_transducer.size(); ++var) {
if (ui->comboBox_transducer_name_1->currentText() == vec_transducer[var].transducer_name) {
ui->doubleSpinBox_scale_factor_1->setValue(vec_transducer[var].scale_factor);
}
}
}
void Seismic_monitor::on_comboBox_transducer_name_2_currentTextChanged(const QString &arg1) {
for (int var = 0; var < vec_transducer.size(); ++var) {
if (ui->comboBox_transducer_name_2->currentText() == vec_transducer[var].transducer_name) {
ui->doubleSpinBox_scale_factor_2->setValue(vec_transducer[var].scale_factor);
}
}
}
void Seismic_monitor::on_comboBox_transducer_name_3_currentTextChanged(const QString &arg1) {
for (int var = 0; var < vec_transducer.size(); ++var) {
if (ui->comboBox_transducer_name_3->currentText() == vec_transducer[var].transducer_name) {
ui->doubleSpinBox_scale_factor_3->setValue(vec_transducer[var].scale_factor);
}
}
}
void Seismic_monitor::on_comboBox_transducer_name_4_currentTextChanged(const QString &arg1) {
for (int var = 0; var < vec_transducer.size(); ++var) {
if (ui->comboBox_transducer_name_4->currentText() == vec_transducer[var].transducer_name) {
ui->doubleSpinBox_scale_factor_4->setValue(vec_transducer[var].scale_factor);
}
}
}