From 0e8344186c1052b3b36376139fc08669ae070973 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=A1clav=20Kubern=C3=A1t?= Date: Thu, 7 Nov 2024 16:09:06 +0100 Subject: [PATCH] ShvAlarm: Make fields public --- libshvcore/include/shv/core/utils/shvalarm.h | 34 +--- libshvcore/src/utils/shvalarm.cpp | 180 ++++++------------- 2 files changed, 65 insertions(+), 149 deletions(-) diff --git a/libshvcore/include/shv/core/utils/shvalarm.h b/libshvcore/include/shv/core/utils/shvalarm.h index 5ad8bae58..2dd375cc3 100644 --- a/libshvcore/include/shv/core/utils/shvalarm.h +++ b/libshvcore/include/shv/core/utils/shvalarm.h @@ -16,45 +16,23 @@ class ShvTypeInfo; class SHVCORE_DECL_EXPORT ShvAlarm { public: using Severity = NecroLogLevel; -public: - ShvAlarm(); - explicit ShvAlarm(const std::string &path, bool is_active = false, Severity severity = Severity::Invalid, int level = 0, const std::string &description = {}, const std::string &label = {}); + std::string path; + bool isActive = false; + std::string description; + std::string label; + int level = 0; + Severity severity = Severity::Invalid; public: static Severity severityFromString(const std::string &lvl); const char *severityName() const; bool isLessSevere(const ShvAlarm &a) const; bool operator==(const ShvAlarm &a) const; - Severity severity() const; - void setSeverity(Severity severity); - - const std::string& path() const; - void setPath(const std::string &path); - - const std::string& description() const; - void setDescription(const std::string &description); - - const std::string& label() const; - void setLabel(const std::string &label); - - int level() const; - void setLevel(int level); - - bool isActive() const; - void setIsActive(bool is_active); - bool isValid() const; shv::chainpack::RpcValue toRpcValue(bool all_fields_if_not_active = false) const; static ShvAlarm fromRpcValue(const shv::chainpack::RpcValue &rv); static std::vector checkAlarms(const ShvTypeInfo &type_info, const std::string &shv_path, const shv::chainpack::RpcValue &value); static std::vector checkAlarms(const ShvTypeInfo &type_info, const std::string &shv_path, const std::string &type_name, const shv::chainpack::RpcValue &value); -protected: - std::string m_path; - bool m_isActive = false; - std::string m_description; - std::string m_label; - int m_level = 0; - Severity m_severity = Severity::Invalid; }; } // namespace shv::core::utils diff --git a/libshvcore/src/utils/shvalarm.cpp b/libshvcore/src/utils/shvalarm.cpp index d87c44a15..3bcd17c7b 100644 --- a/libshvcore/src/utils/shvalarm.cpp +++ b/libshvcore/src/utils/shvalarm.cpp @@ -8,17 +8,6 @@ using namespace std; namespace shv::core::utils { -ShvAlarm::ShvAlarm() = default; - -ShvAlarm::ShvAlarm(const std::string &path, bool is_active, Severity severity, int level, const std::string &description, const std::string &label) - : m_path(path) - , m_isActive(is_active) - , m_description(description) - , m_label(label) - , m_level(level) - , m_severity(severity) -{} - ShvAlarm::Severity ShvAlarm::severityFromString(const std::string &lvl) { return NecroLog::stringToLevel(lvl.c_str()); @@ -26,109 +15,48 @@ ShvAlarm::Severity ShvAlarm::severityFromString(const std::string &lvl) const char* ShvAlarm::severityName() const { - return NecroLog::levelToString(m_severity); + return NecroLog::levelToString(severity); } bool ShvAlarm::isLessSevere(const ShvAlarm &a) const { - if (m_severity == a.m_severity) - return m_level < a.m_level; - if(m_severity == Severity::Invalid) + if (severity == a.severity) + return level < a.level; + if(severity == Severity::Invalid) return true; - return (m_severity > a.m_severity); + return (severity > a.severity); } bool ShvAlarm::operator==(const ShvAlarm &a) const { - return m_severity == a.m_severity - && m_isActive == a.m_isActive - && m_level == a.m_level - && m_path == a.m_path; -} - -ShvAlarm::Severity ShvAlarm::severity() const -{ - return m_severity; -} - -void ShvAlarm::setSeverity(Severity severity) -{ - m_severity = severity; -} - -const std::string& ShvAlarm::path() const -{ - return m_path; -} - -void ShvAlarm::setPath(const std::string &path) -{ - m_path = path; -} - -const std::string& ShvAlarm::description() const -{ - return m_description; -} - -void ShvAlarm::setDescription(const std::string &description) -{ - m_description = description; -} - -const std::string& ShvAlarm::label() const -{ - return m_label; -} - -void ShvAlarm::setLabel(const std::string &label) -{ - m_label = label; -} - - -int ShvAlarm::level() const -{ - return m_level; -} - -void ShvAlarm::setLevel(int level) -{ - m_level = level; -} - -bool ShvAlarm::isActive() const -{ - return m_isActive; -} - -void ShvAlarm::setIsActive(bool is_active) -{ - m_isActive = is_active; + return severity == a.severity + && isActive == a.isActive + && level == a.level + && path == a.path; } bool ShvAlarm::isValid() const { - return !path().empty(); + return !path.empty(); } shv::chainpack::RpcValue ShvAlarm::toRpcValue(bool all_fields_if_not_active) const { shv::chainpack::RpcValue::Map ret; - ret["path"] = path(); - ret["isActive"] = isActive(); - if(all_fields_if_not_active || isActive()) { - if(severity() != Severity::Invalid) { - ret["severity"] = static_cast(severity()); + ret["path"] = path; + ret["isActive"] = isActive; + if(all_fields_if_not_active || isActive) { + if(severity != Severity::Invalid) { + ret["severity"] = static_cast(severity); ret["severityName"] = severityName(); } - if(level() > 0) - ret["alarmLevel"] = level(); - if(!description().empty()) - ret["description"] = description(); - if(!label().empty()) - ret["label"] = label(); + if(level > 0) + ret["alarmLevel"] = level; + if(!description.empty()) + ret["description"] = description; + if(!label.empty()) + ret["label"] = label; } return ret; } @@ -137,12 +65,12 @@ ShvAlarm ShvAlarm::fromRpcValue(const chainpack::RpcValue &rv) { const chainpack::RpcValue::Map &m = rv.asMap(); ShvAlarm a { - m.value("path").asString(), - m.value("isActive").toBool(), - static_cast(m.value("severity").toInt()), - m.value("alarmLevel").toInt(), - m.value("description").asString(), - m.value("label").asString() + .path = m.value("path").asString(), + .isActive = m.value("isActive").toBool(), + .description = m.value("description").asString(), + .label = m.value("label").asString(), + .level = m.value("alarmLevel").toInt(), + .severity = static_cast(m.value("severity").toInt()), }; return a; } @@ -156,13 +84,14 @@ vector ShvAlarm::checkAlarms(const ShvTypeInfo &type_info, const std:: if(auto path_info = type_info.pathInfo(shv_path); path_info.propertyDescription.isValid()) { nDebug() << shv_path << path_info.propertyDescription.toRpcValue().toCpon(); if(string alarm = path_info.propertyDescription.alarm(); !alarm.empty()) { - return {ShvAlarm(shv_path, - value.toBool(), // is active - ShvAlarm::severityFromString(alarm), - path_info.propertyDescription.alarmLevel(), - path_info.propertyDescription.description(), - path_info.propertyDescription.label() - )}; + return {ShvAlarm{ + .path = shv_path, + .isActive = value.toBool(), + .description = path_info.propertyDescription.description(), + .label = path_info.propertyDescription.label(), + .level = path_info.propertyDescription.alarmLevel(), + .severity = ShvAlarm::severityFromString(alarm), + }}; } return checkAlarms(type_info, shv_path, path_info.propertyDescription.typeName(), value); @@ -179,13 +108,14 @@ std::vector ShvAlarm::checkAlarms(const ShvTypeInfo &type_info, const for (auto& fld_descr : flds) { if(string alarm = fld_descr.alarm(); !alarm.empty()) { bool is_alarm = fld_descr.bitfieldValue(value.toUInt64()).toBool(); - alarms.emplace_back(shv_path + '/' + fld_descr.name(), - is_alarm, - ShvAlarm::severityFromString(alarm), - fld_descr.alarmLevel(), - fld_descr.description(), - fld_descr.label() - ); + alarms.emplace_back(ShvAlarm { + .path = shv_path + '/' + fld_descr.name(), + .isActive = is_alarm, + .description = fld_descr.description(), + .label = fld_descr.label(), + .level = fld_descr.alarmLevel(), + .severity = ShvAlarm::severityFromString(alarm), + }); } else { auto alarms2 = checkAlarms(type_info, shv_path + '/' + fld_descr.name(), fld_descr.typeName(), fld_descr.bitfieldValue(value.toUInt64())); @@ -209,16 +139,24 @@ std::vector ShvAlarm::checkAlarms(const ShvTypeInfo &type_info, const if(has_alarm_definition) { if(active_alarm_ix < flds.size()) { const ShvFieldDescr &fld_descr = flds[active_alarm_ix]; - return {ShvAlarm(shv_path, - true, - ShvAlarm::severityFromString(fld_descr.alarm()), - fld_descr.alarmLevel(), - fld_descr.description(), - fld_descr.label() - )}; + return {ShvAlarm{ + .path = shv_path, + .isActive = true, + .description = fld_descr.description(), + .label = fld_descr.label(), + .level = fld_descr.alarmLevel(), + .severity = ShvAlarm::severityFromString(fld_descr.alarm()), + }}; } - return {ShvAlarm(shv_path)}; + return {ShvAlarm{ + .path = shv_path, + .isActive = false, + .description = {}, + .label = {}, + .level = 0, + .severity = Severity::Invalid, + }}; } } }