TSI_Config/tachometer.cpp

264 lines
15 KiB
C++

#include "tachometer.h"
#include "ui_tachometer.h"
#include <QDebug>
#include <QFile>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonParseError>
#include <QJsonArray>
Tachometer::Tachometer(int slot_no_,QWidget *parent)
: QDialog(parent)
, ui(new Ui::Tachometer)
{
ui->setupUi(this);
ui->widget_body->setProperty("flag", "body");
slot_no = slot_no_;
QString slot = QString("%1").arg(slot_no);
ui->label_slot->setText(slot);
QString filePath_tachometer = QCoreApplication::applicationDirPath() + QString("\\config\\%1\\tachometer.json").arg(slot_no);
readJsonFile(filePath_tachometer);
Init();
}
Tachometer::~Tachometer()
{
delete ui;
}
void Tachometer::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();
tachometer_variables[i].id = temp_obj["id"].toInt();
tachometer_variables[i].active = temp_obj["active"].toBool();
QJsonArray voltage_range_array = temp_obj["normal_voltage_range"].toArray();
tachometer_variables[i].normal_voltage_high = voltage_range_array[1].toDouble();
tachometer_variables[i].normal_voltage_low = voltage_range_array[0].toDouble();
tachometer_variables[i].threshold = temp_obj["threshold"].toDouble();
tachometer_variables[i].hysteresis = temp_obj["hysteresis"].toDouble();
tachometer_variables[i].events_per_revolution = temp_obj["events_per_revolution"].toInt();
tachometer_variables[i].record_output = temp_obj["record_output"].toString();
tachometer_variables[i].two_ma_clamp = temp_obj["two_ma_clamp"].toBool();
tachometer_variables[i].alert_latching = temp_obj["alert_latching"].toBool();
tachometer_variables[i].overspeed_latching = temp_obj["overspeed_latching"].toBool();
tachometer_variables[i].normal_latching = temp_obj["normal_latching"].toBool();
tachometer_variables[i].alert_response_time = temp_obj["alert_response_time"].toInt();
tachometer_variables[i].danger_response_time = temp_obj["danger_response_time"].toInt();
}
}
void Tachometer::Init()
{
for (int i = 0; i < CHANNLE_COUNT; i++) {
if(tachometer_variables[i].id == 1){
ui->checkBox_chan_1->setChecked(tachometer_variables[i].active);
ui->doubleSpinBox_high_1->setValue(tachometer_variables[i].normal_voltage_high);
ui->doubleSpinBox_low_1->setValue(tachometer_variables[i].normal_voltage_low);
if(tachometer_variables[i].automatic_threshold)
ui->radioButton_automatic_threshold_1->setChecked(true);
else
ui->radioButton_manual_threshold_1->setChecked(true);
ui->doubleSpinBox_threshold_1->setValue(tachometer_variables[i].threshold);
ui->doubleSpinBox_hysteresis_1->setValue(tachometer_variables[i].hysteresis);
ui->spinBox_events_per_revolution_1->setValue(tachometer_variables[i].events_per_revolution);
ui->comboBox_record_output_1->setCurrentText(tachometer_variables[i].record_output);
ui->checkBox_two_ma_clamp_1->setChecked(tachometer_variables[i].two_ma_clamp);
if(tachometer_variables[i].alert_latching)
ui->radioButton_alert_latching_1->setCheckable(true);
else
ui->radioButton_alert_latching_1->setCheckable(false);
if(tachometer_variables[i].overspeed_latching)
ui->radioButton_overspeed_latching_1->setCheckable(true);
else
ui->radioButton_overspeed_latching_1->setCheckable(false);
}
if(tachometer_variables[i].id == 2){
ui->checkBox_chan_2->setChecked(tachometer_variables[i].active);
ui->doubleSpinBox_high_2->setValue(tachometer_variables[i].normal_voltage_high);
ui->doubleSpinBox_low_2->setValue(tachometer_variables[i].normal_voltage_low);
if(tachometer_variables[i].automatic_threshold)
ui->radioButton_automatic_threshold_2->setChecked(true);
else
ui->radioButton_manual_threshold_2->setChecked(true);
ui->doubleSpinBox_threshold_2->setValue(tachometer_variables[i].threshold);
ui->doubleSpinBox_hysteresis_2->setValue(tachometer_variables[i].hysteresis);
ui->spinBox_events_per_revolution_2->setValue(tachometer_variables[i].events_per_revolution);
ui->comboBox_record_output_2->setCurrentText(tachometer_variables[i].record_output);
ui->checkBox_two_ma_clamp_2->setChecked(tachometer_variables[i].two_ma_clamp);
if(tachometer_variables[i].alert_latching)
ui->radioButton_alert_latching_2->setCheckable(true);
else
ui->radioButton_alert_latching_2->setCheckable(false);
if(tachometer_variables[i].overspeed_latching)
ui->radioButton_overspeed_latching_2->setCheckable(true);
else
ui->radioButton_overspeed_latching_2->setCheckable(false);
}
if(tachometer_variables[i].id == 3){
ui->checkBox_chan_3->setChecked(tachometer_variables[i].active);
ui->doubleSpinBox_high_3->setValue(tachometer_variables[i].normal_voltage_high);
ui->doubleSpinBox_low_3->setValue(tachometer_variables[i].normal_voltage_low);
if(tachometer_variables[i].automatic_threshold)
ui->radioButton_automatic_threshold_3->setChecked(true);
else
ui->radioButton_manual_threshold_3->setChecked(true);
ui->doubleSpinBox_threshold_3->setValue(tachometer_variables[i].threshold);
ui->doubleSpinBox_hysteresis_3->setValue(tachometer_variables[i].hysteresis);
ui->spinBox_events_per_revolution_3->setValue(tachometer_variables[i].events_per_revolution);
ui->comboBox_record_output_3->setCurrentText(tachometer_variables[i].record_output);
ui->checkBox_two_ma_clamp_3->setChecked(tachometer_variables[i].two_ma_clamp);
if(tachometer_variables[i].alert_latching)
ui->radioButton_alert_latching_3->setCheckable(true);
else
ui->radioButton_alert_latching_3->setCheckable(false);
if(tachometer_variables[i].overspeed_latching)
ui->radioButton_overspeed_latching_3->setCheckable(true);
else
ui->radioButton_overspeed_latching_3->setCheckable(false);
}
if(tachometer_variables[i].id == 4){
ui->checkBox_chan_4->setChecked(tachometer_variables[i].active);
ui->doubleSpinBox_high_4->setValue(tachometer_variables[i].normal_voltage_high);
ui->doubleSpinBox_low_4->setValue(tachometer_variables[i].normal_voltage_low);
if(tachometer_variables[i].automatic_threshold)
ui->radioButton_automatic_threshold_4->setChecked(true);
else
ui->radioButton_manual_threshold_4->setChecked(true);
ui->doubleSpinBox_threshold_4->setValue(tachometer_variables[i].threshold);
ui->doubleSpinBox_hysteresis_4->setValue(tachometer_variables[i].hysteresis);
ui->spinBox_events_per_revolution_4->setValue(tachometer_variables[i].events_per_revolution);
ui->comboBox_record_output_4->setCurrentText(tachometer_variables[i].record_output);
ui->checkBox_two_ma_clamp_4->setChecked(tachometer_variables[i].two_ma_clamp);
if(tachometer_variables[i].alert_latching)
ui->radioButton_alert_latching_4->setCheckable(true);
else
ui->radioButton_alert_latching_4->setCheckable(false);
if(tachometer_variables[i].overspeed_latching)
ui->radioButton_overspeed_latching_4->setCheckable(true);
else
ui->radioButton_overspeed_latching_4->setCheckable(false);
}
}
}
void Tachometer::on_pushButton_confirm_clicked()
{
for (int i = 0; i < CHANNLE_COUNT; i++) {
if(tachometer_variables[i].id == 1){
tachometer_variables[i].active = ui->checkBox_chan_1->isChecked();
tachometer_variables[i].normal_voltage_high = ui->doubleSpinBox_high_1->value();
tachometer_variables[i].normal_voltage_low = ui->doubleSpinBox_low_1->value();
tachometer_variables[i].automatic_threshold = ui->radioButton_automatic_threshold_1->isChecked();
tachometer_variables[i].threshold = ui->doubleSpinBox_threshold_1->value();
tachometer_variables[i].hysteresis = ui->doubleSpinBox_hysteresis_1->value();
tachometer_variables[i].events_per_revolution = ui->spinBox_events_per_revolution_1->value();
tachometer_variables[i].record_output = ui->comboBox_record_output_1->currentText();
tachometer_variables[i].two_ma_clamp = ui->checkBox_two_ma_clamp_1->isChecked();
tachometer_variables[i].alert_latching = ui->radioButton_alert_latching_1->isChecked();
tachometer_variables[i].overspeed_latching = ui->radioButton_overspeed_latching_1->isChecked();
}
if(tachometer_variables[i].id == 2){
tachometer_variables[i].active = ui->checkBox_chan_2->isChecked();
tachometer_variables[i].normal_voltage_high = ui->doubleSpinBox_high_2->value();
tachometer_variables[i].normal_voltage_low = ui->doubleSpinBox_low_2->value();
tachometer_variables[i].automatic_threshold = ui->radioButton_automatic_threshold_2->isChecked();
tachometer_variables[i].threshold = ui->doubleSpinBox_threshold_2->value();
tachometer_variables[i].hysteresis = ui->doubleSpinBox_hysteresis_2->value();
tachometer_variables[i].events_per_revolution = ui->spinBox_events_per_revolution_2->value();
tachometer_variables[i].record_output = ui->comboBox_record_output_2->currentText();
tachometer_variables[i].two_ma_clamp = ui->checkBox_two_ma_clamp_2->isChecked();
tachometer_variables[i].alert_latching = ui->radioButton_alert_latching_2->isChecked();
tachometer_variables[i].overspeed_latching = ui->radioButton_overspeed_latching_2->isChecked();
}
if(tachometer_variables[i].id == 3){
tachometer_variables[i].active = ui->checkBox_chan_3->isChecked();
tachometer_variables[i].normal_voltage_high = ui->doubleSpinBox_high_3->value();
tachometer_variables[i].normal_voltage_low = ui->doubleSpinBox_low_3->value();
tachometer_variables[i].automatic_threshold = ui->radioButton_automatic_threshold_3->isChecked();
tachometer_variables[i].threshold = ui->doubleSpinBox_threshold_3->value();
tachometer_variables[i].hysteresis = ui->doubleSpinBox_hysteresis_3->value();
tachometer_variables[i].events_per_revolution = ui->spinBox_events_per_revolution_3->value();
tachometer_variables[i].record_output = ui->comboBox_record_output_3->currentText();
tachometer_variables[i].two_ma_clamp = ui->checkBox_two_ma_clamp_3->isChecked();
tachometer_variables[i].alert_latching = ui->radioButton_alert_latching_3->isChecked();
tachometer_variables[i].overspeed_latching = ui->radioButton_overspeed_latching_3->isChecked();
}
if(tachometer_variables[i].id == 4){
tachometer_variables[i].active = ui->checkBox_chan_4->isChecked();
tachometer_variables[i].normal_voltage_high = ui->doubleSpinBox_high_4->value();
tachometer_variables[i].normal_voltage_low = ui->doubleSpinBox_low_4->value();
tachometer_variables[i].automatic_threshold = ui->radioButton_automatic_threshold_4->isChecked();
tachometer_variables[i].threshold = ui->doubleSpinBox_threshold_4->value();
tachometer_variables[i].hysteresis = ui->doubleSpinBox_hysteresis_4->value();
tachometer_variables[i].events_per_revolution = ui->spinBox_events_per_revolution_4->value();
tachometer_variables[i].record_output = ui->comboBox_record_output_4->currentText();
tachometer_variables[i].two_ma_clamp = ui->checkBox_two_ma_clamp_4->isChecked();
tachometer_variables[i].alert_latching = ui->radioButton_alert_latching_4->isChecked();
tachometer_variables[i].overspeed_latching = ui->radioButton_overspeed_latching_4->isChecked();
}
}
QString slot = QString("%1").arg(slot_no);
QString filePath_tachometer = QCoreApplication::applicationDirPath() + QString("\\config\\%1\\tachometer.json").arg(slot_no);
QFile file(filePath_tachometer);
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", tachometer_variables[i].id);
temp_obj.insert("active", tachometer_variables[i].active);
QJsonArray voltage_range_array;
voltage_range_array.append(tachometer_variables[i].normal_voltage_low);
voltage_range_array.append(tachometer_variables[i].normal_voltage_high);
temp_obj.insert("normal_voltage_range", voltage_range_array);
temp_obj.insert("threshold", tachometer_variables[i].threshold);
temp_obj.insert("hysteresis", tachometer_variables[i].hysteresis);
temp_obj.insert("events_per_revolution", tachometer_variables[i].events_per_revolution);
temp_obj.insert("record_output", tachometer_variables[i].record_output);
temp_obj.insert("two_ma_clamp", tachometer_variables[i].two_ma_clamp);
temp_obj.insert("alert_latching", tachometer_variables[i].alert_latching);
temp_obj.insert("overspeed_latching", tachometer_variables[i].overspeed_latching);
temp_obj.insert("normal_latching", tachometer_variables[i].normal_latching);
temp_obj.insert("alert_response_time", tachometer_variables[i].alert_response_time);
temp_obj.insert("danger_response_time", tachometer_variables[i].danger_response_time);
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();
}