Skip to content

Commit

Permalink
ShvAlarm: Make fields public
Browse files Browse the repository at this point in the history
  • Loading branch information
syyyr authored and fvacek committed Nov 7, 2024
1 parent 6fafc4b commit 05cb2f0
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 149 deletions.
34 changes: 6 additions & 28 deletions libshvcore/include/shv/core/utils/shvalarm.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<ShvAlarm> checkAlarms(const ShvTypeInfo &type_info, const std::string &shv_path, const shv::chainpack::RpcValue &value);
static std::vector<ShvAlarm> 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
180 changes: 59 additions & 121 deletions libshvcore/src/utils/shvalarm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,127 +8,55 @@ 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());
}

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<int>(severity());
ret["path"] = path;
ret["isActive"] = isActive;
if(all_fields_if_not_active || isActive) {
if(severity != Severity::Invalid) {
ret["severity"] = static_cast<int>(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;
}
Expand All @@ -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<Severity>(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<Severity>(m.value("severity").toInt()),
};
return a;
}
Expand All @@ -156,13 +84,14 @@ vector<ShvAlarm> 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);
Expand All @@ -179,13 +108,14 @@ std::vector<ShvAlarm> 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()));
Expand All @@ -209,16 +139,24 @@ std::vector<ShvAlarm> 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,
}};
}
}
}
Expand Down

0 comments on commit 05cb2f0

Please sign in to comment.