TSI_Config/config_mgr.cpp

370 lines
21 KiB
C++
Raw Normal View History

2025-03-29 18:05:12 +08:00
#include "config_mgr.h"
#include <QDebug>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
#include "data_config.h"
#include "vibrationdata.h"
ConfigMgr *ConfigMgr::instance = nullptr;
ConfigMgr::~ConfigMgr() {
}
void ConfigMgr::Save() {
2025-04-01 15:03:59 +08:00
QJsonObject doc_obj;
doc_obj["version"] = 1;
int slot = 0;
QJsonArray card_type;
for (int i = 0; i < SLOT_NUM; ++i) {
if (card_type_[i] == kCardNone) {
continue;
}
QJsonObject card_item;
slot = i + 1;
card_item["type"] = card_type_[i];
card_item["slot"] = slot;
card_type.append(card_item);
if (card_type_[i] != kCardVibSingle &&
card_type_[i] != kCardVibTMRPrimary &&
card_type_[i] != kCardSpeedSingle &&
card_type_[i] != kCardSpeedTMRPrimary &&
card_type_[i] != kCardKeyphase &&
card_type_[i] != kCardRelaySingle &&
card_type_[i] != kCardRelayTMRPrimary) {
continue;
}
// process slot
QJsonObject slot_item;
if (card_type_[i] != kCardRelaySingle &&
card_type_[i] != kCardRelayTMRPrimary) {
for (int cid = 0; cid < CHANNEL_COUNT; ++cid) {
QJsonObject channel_item;
if (card_type_[i] == kCardVibSingle ||
card_type_[i] == kCardVibTMRPrimary) {
std::shared_ptr<CardBase> base_ptr = ConfigMgr::Instance()->GetSlotPtr(slot);
if (base_ptr == nullptr) {
continue;
}
std::shared_ptr<VibrationData> ptr = std::dynamic_pointer_cast<VibrationData>(base_ptr);
channel_item["standby"] = ptr->base_config_[cid].standby;
channel_item["active"] = ptr->base_config_[cid].active;
channel_item["rack_type"] = 0; // TODO:
channel_item["channel_type"] = ptr->base_config_[cid].channel_type;
channel_item["transducer_id"] = ptr->base_config_[cid].transducer_id;
channel_item["scale_factor"] = ptr->base_config_[cid].scale_factor;
channel_item["sampling_rate"] = ptr->base_config_[cid].sampling_rate;
QJsonArray voltage_range;
voltage_range.append(ptr->base_config_[cid].normal_voltage_low);
voltage_range.append(ptr->base_config_[cid].normal_voltage_high);
channel_item["normal_voltage_range"] = voltage_range;
// filter
QJsonArray filter;
QJsonObject low_pass;
low_pass["low"] = ptr->filter_[cid].filter[0].low;
low_pass["high"] = ptr->filter_[cid].filter[0].high;
low_pass["checked"] = ptr->filter_[cid].filter[0].checked;
filter.append(low_pass);
QJsonObject high_pass;
high_pass["low"] = ptr->filter_[cid].filter[1].low;
high_pass["high"] = ptr->filter_[cid].filter[1].high;
high_pass["checked"] = ptr->filter_[cid].filter[1].checked;
filter.append(high_pass);
QJsonObject band_pass;
band_pass["low"] = ptr->filter_[cid].filter[2].low;
band_pass["high"] = ptr->filter_[cid].filter[2].high;
band_pass["checked"] = ptr->filter_[cid].filter[2].checked;
filter.append(band_pass);
channel_item["filter"] = filter;
// variables
QJsonObject variables;
std::shared_ptr<VariableBase> base_channel_ptr = ptr->GetChannelPtr(cid + 1);
if (base_channel_ptr->type_ == kVibRadial) {
std::shared_ptr<RadialVariable> radial_ptr = std::dynamic_pointer_cast<RadialVariable>(base_channel_ptr);
QJsonObject direct;
QJsonObject x1;
QJsonObject x2;
QJsonObject recorder_out;
QJsonObject delay;
QJsonObject not1x;
QJsonObject smax;
QJsonArray latching;
direct["full_scale_range"] = radial_ptr->direct_.full_scale_range;
direct["clamp_value"] = radial_ptr->direct_.clamp_value;
direct["custom"] = radial_ptr->direct_.custom;
variables["direct"] = direct;
x1["checked"] = radial_ptr->x1_.checked;
x1["full_scale_range"] = radial_ptr->x1_.full_scale_range;
x1["clamp_value"] = radial_ptr->x1_.clamp_value;
x1["custom"] = radial_ptr->x1_.custom;
x1["phase_lag"] = radial_ptr->x1_.phase_lag;
variables["x1"] = x1;
x2["checked"] = radial_ptr->x2_.checked;
x2["full_scale_range"] = radial_ptr->x2_.full_scale_range;
x2["clamp_value"] = radial_ptr->x2_.clamp_value;
x2["custom"] = radial_ptr->x2_.custom;
x2["phase_lag"] = radial_ptr->x2_.phase_lag;
variables["x2"] = x2;
recorder_out["recorder_output"] = radial_ptr->recorder_out_.recorder_output;
recorder_out["two_ma_clamp"] = radial_ptr->recorder_out_.two_ma_clamp;
recorder_out["trip_multiply"] = radial_ptr->recorder_out_.trip_multiply;
recorder_out["comparision"] = radial_ptr->recorder_out_.comparision;
recorder_out["percentage"] = radial_ptr->recorder_out_.percentage;
variables["recorder_out"] = recorder_out;
delay["alert"] = radial_ptr->delay_.alert;
delay["danger"] = radial_ptr->delay_.danger;
delay["active_100ms"] = radial_ptr->delay_.active_100ms;
variables["delay"] = delay;
not1x["checked"] = radial_ptr->not1x_.checked;
not1x["full_scale_range"] = radial_ptr->not1x_.full_scale_range;
not1x["clamp_value"] = radial_ptr->not1x_.clamp_value;
not1x["custom"] = radial_ptr->not1x_.custom;
variables["not1x"] = not1x;
smax["checked"] = radial_ptr->smax_.checked;
smax["full_scale_range"] = radial_ptr->smax_.full_scale_range;
smax["clamp_value"] = radial_ptr->smax_.clamp_value;
smax["custom"] = radial_ptr->smax_.custom;
variables["smax"] = smax;
latching.append(radial_ptr->alert_latching_);
latching.append(radial_ptr->danger_latching_);
variables["latching"] = latching;
} else {
std::shared_ptr<AccVelVariable> av_ptr = std::dynamic_pointer_cast<AccVelVariable>(base_channel_ptr);
QJsonObject direct;
QJsonObject x1;
QJsonObject x2;
QJsonObject recorder_out;
QJsonObject delay;
QJsonArray latching;
direct["full_scale_range"] = av_ptr->direct_.full_scale_range;
direct["clamp_value"] = av_ptr->direct_.clamp_value;
direct["custom"] = av_ptr->direct_.custom;
variables["direct"] = direct;
x1["checked"] = av_ptr->x1_.checked;
x1["full_scale_range"] = av_ptr->x1_.full_scale_range;
x1["clamp_value"] = av_ptr->x1_.clamp_value;
x1["custom"] = av_ptr->x1_.custom;
x1["phase_lag"] = av_ptr->x1_.phase_lag;
variables["x1"] = x1;
x2["checked"] = av_ptr->x2_.checked;
x2["full_scale_range"] = av_ptr->x2_.full_scale_range;
x2["clamp_value"] = av_ptr->x2_.clamp_value;
x2["custom"] = av_ptr->x2_.custom;
x2["phase_lag"] = av_ptr->x2_.phase_lag;
variables["x2"] = x2;
QJsonObject rms_integrate;
rms_integrate["rms_active"] = av_ptr->rms_active_;
rms_integrate["integrate_active"] = av_ptr->integrate_active_;
variables["rms_integrate"] = rms_integrate;
recorder_out["recorder_output"] = av_ptr->recorder_out_.recorder_output;
recorder_out["two_ma_clamp"] = av_ptr->recorder_out_.two_ma_clamp;
recorder_out["trip_multiply"] = av_ptr->recorder_out_.trip_multiply;
recorder_out["comparision"] = av_ptr->recorder_out_.comparision;
recorder_out["percentage"] = av_ptr->recorder_out_.percentage;
variables["recorder_out"] = recorder_out;
delay["alert"] = av_ptr->delay_.alert;
delay["danger"] = av_ptr->delay_.danger;
delay["active_100ms"] = av_ptr->delay_.active_100ms;
variables["delay"] = delay;
latching.append(av_ptr->alert_latching_);
latching.append(av_ptr->danger_latching_);
latching.append(av_ptr->timed_ok_);
variables["latching"] = latching;
}
channel_item["variable"] = variables;
} else {
// TODO: save speed, keyphase
}
slot_item[QString::number(cid + 1)] = channel_item;
}
} else {
// TODO: save relay data
}
doc_obj[QString::number(slot)] = slot_item;
}
doc_obj["card_type"] = card_type;
// TODO: show success message box
QJsonDocument jsonDoc;
jsonDoc.setObject(doc_obj);
QFile file(filename_);
file.open(QIODevice::WriteOnly);
file.write(jsonDoc.toJson());
file.close();
2025-03-29 18:05:12 +08:00
}
void ConfigMgr::Load(QString filename) {
QFile file(filename);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
2025-04-01 15:03:59 +08:00
// TODO: show message box
2025-03-29 18:05:12 +08:00
qDebug() << "Cannot open file for reading:" << filename;
return;
}
QString content = file.readAll();
file.close();
QByteArray jsonData = content.toUtf8();
QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonData);
if (jsonDoc.isNull()) {
2025-04-01 15:03:59 +08:00
// TODO: show message box
2025-03-29 18:05:12 +08:00
qDebug() << "Cannot parse JSON document";
return;
}
if (!jsonDoc.isObject() && !jsonDoc.isArray()) {
2025-04-01 15:03:59 +08:00
// TODO: show message box
2025-03-29 18:05:12 +08:00
qDebug() << "JSON document is not an object or an array";
return;
}
2025-04-01 15:03:59 +08:00
filename_ = filename;
2025-03-29 18:05:12 +08:00
QJsonObject json_obj = jsonDoc.object();
// parse card_type
QJsonArray card_type = json_obj["card_type"].toArray();
int slot = 0;
QJsonObject temp_obj;
for (int i = 0; i < card_type.size(); ++i) {
temp_obj = card_type[i].toObject();
slot = temp_obj["slot"].toInt();
card_type_[slot - 1] = static_cast<CardType>(temp_obj["type"].toInt());
}
// parse each slot
QJsonObject channel;
for (int i = 0; i < SLOT_NUM; ++i) {
if (card_type_[i] == kCardNone) {
continue;
}
slot = i + 1;
if (json_obj[QString::number(slot)].isNull()) {
continue;
}
temp_obj = json_obj[QString::number(slot)].toObject();
if (card_type_[i] == kCardVibSingle) {
std::shared_ptr<VibrationData> vib_data = std::make_shared<VibrationData>();
for (int j = 0; j < CHANNEL_COUNT; ++j) {
channel = temp_obj[QString::number(j + 1)].toObject();
// base info
vib_data->base_config_[j].standby = channel["standby"].toBool();
vib_data->base_config_[j].active = channel["active"].toBool();
vib_data->base_config_[j].rack_type = channel["rack_type"].toInt();
// vib_data->base_config_[j].tmr_group = channel["tmr_group"].toString();
vib_data->base_config_[j].channel_type = channel["channel_type"].toInt();
2025-04-01 15:03:59 +08:00
vib_data->base_config_[j].transducer_id = channel["transducer_id"].toInt();
2025-03-29 18:05:12 +08:00
vib_data->base_config_[j].scale_factor = channel["scale_factor"].toDouble();
vib_data->base_config_[j].sampling_rate = channel["sampling_rate"].toInt();
QJsonArray voltage_range_array = channel["normal_voltage_range"].toArray();
vib_data->base_config_[j].normal_voltage_low = voltage_range_array[0].toDouble();
vib_data->base_config_[j].normal_voltage_high = voltage_range_array[1].toDouble();
// filter
QJsonArray filter_array = channel["filter"].toArray();
for (int k = 0; k < filter_array.size(); k++) {
QJsonObject filter_ele = filter_array[i].toObject();
vib_data->filter_[j].filter[k].low = filter_ele["low"].toInt();
vib_data->filter_[j].filter[k].high = filter_ele["high"].toInt();
vib_data->filter_[j].filter[k].checked = filter_ele["checked"].toBool();
}
// variables
QJsonObject tmp_variable = channel["variable"].toObject();
switch (vib_data->base_config_[j].channel_type) {
case kVibRadial: {
2025-04-01 15:03:59 +08:00
std::shared_ptr<RadialVariable> variable = std::make_shared<RadialVariable>();
variable->id_ = j + 1;
QJsonObject direct = tmp_variable["direct"].toObject();
variable->direct_.full_scale_range = direct["full_scale_range"].toInt();
variable->direct_.clamp_value = direct["clamp_value"].toDouble();
variable->direct_.custom = direct["custom"].toDouble();
QJsonObject x1 = tmp_variable["x1"].toObject();
variable->x1_.checked = x1["checked"].toBool();
variable->x1_.full_scale_range = x1["full_scale_range"].toInt();
variable->x1_.clamp_value = x1["clamp_value"].toDouble();
variable->x1_.custom = x1["custom"].toDouble();
variable->x1_.phase_lag = x1["phase_lag"].toInt();
QJsonObject x2 = tmp_variable["x2"].toObject();
variable->x2_.checked = x2["checked"].toBool();
variable->x2_.full_scale_range = x2["full_scale_range"].toInt();
variable->x2_.clamp_value = x2["clamp_value"].toDouble();
variable->x2_.custom = x2["custom"].toDouble();
variable->x2_.phase_lag = x2["phase_lag"].toInt();
QJsonObject recorder_out = tmp_variable["recorder_out"].toObject();
variable->recorder_out_.recorder_output = recorder_out["recorder_output"].toInt();
variable->recorder_out_.two_ma_clamp = recorder_out["two_ma_clamp"].toBool();
variable->recorder_out_.trip_multiply = recorder_out["trip_multiply"].toDouble();
variable->recorder_out_.comparision = recorder_out["comparision"].toInt();
variable->recorder_out_.percentage = recorder_out["percentage"].toInt();
QJsonObject delay = tmp_variable["delay"].toObject();
variable->delay_.alert = delay["alert"].toInt();
variable->delay_.danger = delay["danger"].toDouble();
variable->delay_.active_100ms = delay["active_100ms"].toBool();
QJsonObject not1x = tmp_variable["not1x"].toObject();
variable->not1x_.checked = not1x["checked"].toBool();
variable->not1x_.full_scale_range = not1x["full_scale_range"].toInt();
variable->not1x_.clamp_value = not1x["clamp_value"].toDouble();
variable->not1x_.custom = not1x["custom"].toDouble();
QJsonObject smax = tmp_variable["smax"].toObject();
variable->smax_.checked = smax["checked"].toBool();
variable->smax_.full_scale_range = smax["full_scale_range"].toInt();
variable->smax_.clamp_value = smax["clamp_value"].toDouble();
variable->smax_.custom = smax["custom"].toDouble();
QJsonObject latching = tmp_variable["latching"].toObject();
variable->alert_latching_ = latching["alert"].toBool();
variable->danger_latching_ = latching["danger"].toBool();
vib_data->variables_.push_back(variable);
2025-03-29 18:05:12 +08:00
break;
}
case kVibVelocity:
case kVibAcc: {
2025-04-01 15:03:59 +08:00
std::shared_ptr<AccVelVariable> variable = std::make_shared<AccVelVariable>();
variable->id_ = j + 1;
QJsonObject direct = tmp_variable["direct"].toObject();
variable->direct_.full_scale_range = direct["full_scale_range"].toInt();
variable->direct_.clamp_value = direct["clamp_value"].toDouble();
variable->direct_.custom = direct["custom"].toDouble();
QJsonObject x1 = tmp_variable["x1"].toObject();
variable->x1_.checked = x1["checked"].toBool();
variable->x1_.full_scale_range = x1["full_scale_range"].toInt();
variable->x1_.clamp_value = x1["clamp_value"].toDouble();
variable->x1_.custom = x1["custom"].toDouble();
variable->x1_.phase_lag = x1["phase_lag"].toInt();
QJsonObject x2 = tmp_variable["x2"].toObject();
variable->x2_.checked = x2["checked"].toBool();
variable->x2_.full_scale_range = x2["full_scale_range"].toInt();
variable->x2_.clamp_value = x2["clamp_value"].toDouble();
variable->x2_.custom = x2["custom"].toDouble();
variable->x2_.phase_lag = x2["phase_lag"].toInt();
QJsonObject rms_integrate = tmp_variable["rms_integrate"].toObject();
variable->rms_active_ = rms_integrate["rms_active"].toBool();
variable->integrate_active_ = rms_integrate["integrate_active"].toBool();
QJsonObject recorder_out = tmp_variable["recorder_out"].toObject();
variable->recorder_out_.recorder_output = recorder_out["recorder_output"].toInt();
variable->recorder_out_.two_ma_clamp = recorder_out["two_ma_clamp"].toBool();
variable->recorder_out_.trip_multiply = recorder_out["trip_multiply"].toDouble();
variable->recorder_out_.comparision = recorder_out["comparision"].toInt();
variable->recorder_out_.percentage = recorder_out["percentage"].toInt();
QJsonObject delay = tmp_variable["delay"].toObject();
variable->delay_.alert = delay["alert"].toInt();
variable->delay_.danger = delay["danger"].toDouble();
variable->delay_.active_100ms = delay["active_100ms"].toBool();
QJsonObject latching = tmp_variable["latching"].toObject();
variable->alert_latching_ = latching["alert"].toBool();
variable->danger_latching_ = latching["danger"].toBool();
variable->timed_ok_ = latching["timed_ok"].toBool();
vib_data->variables_.push_back(variable);
2025-03-29 18:05:12 +08:00
break;
}
}
}
cards_.push_back(vib_data);
}
}
}
2025-04-01 15:03:59 +08:00
std::shared_ptr<CardBase> ConfigMgr::GetSlotPtr(int slot) {
for (auto &item : cards_) {
if (item->slot_ == slot) {
return item;
}
}
return nullptr;
}
void ConfigMgr::AddCard(std::shared_ptr<CardBase> ptr) {
cards_.push_back(ptr);
}