1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00
VILLASnode/lib/signal.cpp

142 lines
2.8 KiB
C++
Raw Permalink Normal View History

2018-03-21 16:59:19 +01:00
/** Signal meta data.
*
* @author Steffen Vogel <post@steffenvogel.de>
2022-03-15 09:28:57 -04:00
* @copyright 2014-2022, Institute for Automation of Complex Power Systems, EONERC
2022-07-04 18:20:03 +02:00
* @license Apache 2.0
2018-03-21 16:59:19 +01:00
*********************************************************************************/
#include <villas/signal.hpp>
#include <villas/utils.hpp>
#include <villas/exceptions.hpp>
2018-03-21 16:59:19 +01:00
#include <villas/signal_type.hpp>
#include <villas/signal_data.hpp>
2020-08-17 17:04:55 +02:00
2019-06-23 13:35:42 +02:00
using namespace villas;
using namespace villas::node;
2019-06-04 16:55:38 +02:00
using namespace villas::utils;
Signal::Signal(const std::string &n, const std::string &u, enum SignalType t) :
name(n),
unit(u),
init(SignalData::nan()),
type(t)
{ }
int Signal::parse(json_t *json)
2018-03-21 16:59:19 +01:00
{
int ret;
json_error_t err;
2019-04-07 15:13:40 +02:00
json_t *json_init = nullptr;
const char *name_str = nullptr;
const char *unit_str = nullptr;
const char *type_str = "float";
ret = json_unpack_ex(json, &err, 0, "{ s?: s, s?: s, s?: s, s?: o }",
"name", &name_str,
"unit", &unit_str,
"type", &type_str,
"init", &json_init
2018-03-21 16:59:19 +01:00
);
if (ret)
return -1;
if (name_str)
name = name_str;
if (unit_str)
unit = unit_str;
if (type_str) {
type = signalTypeFromString(type_str);
if (type == SignalType::INVALID)
return -1;
}
if (json_init) {
ret = init.parseJson(type, json_init);
if (ret)
return ret;
}
else
init.set(type, 0);
2018-03-21 16:59:19 +01:00
return 0;
}
2020-08-17 17:04:55 +02:00
json_t * Signal::toJson() const
2020-08-17 17:04:55 +02:00
{
2022-03-28 10:42:03 +02:00
json_t *json_sig = json_pack("{ s: s }",
"type", signalTypeToString(type).c_str()
2020-08-17 17:04:55 +02:00
);
2022-03-28 10:42:03 +02:00
auto *json_init = init.toJson(type);
if (json_init)
json_object_set_new(json_sig, "init", json_init);
if (!name.empty())
2022-03-28 10:42:03 +02:00
json_object_set_new(json_sig, "name", json_string(name.c_str()));
2020-08-17 17:04:55 +02:00
if (!unit.empty())
2022-03-28 10:42:03 +02:00
json_object_set_new(json_sig, "unit", json_string(unit.c_str()));
2020-08-17 17:04:55 +02:00
return json_sig;
}
2021-07-06 18:30:46 +02:00
std::string Signal::toString(const union SignalData *d) const
2021-07-06 18:30:46 +02:00
{
std::stringstream ss;
2021-07-06 18:30:46 +02:00
if (!name.empty())
ss << " " << name;
2021-07-06 18:30:46 +02:00
if (!unit.empty())
ss << " [" << unit << "]";
2021-07-06 18:30:46 +02:00
ss << "(" << signalTypeToString(type) << ")";
2021-07-06 18:30:46 +02:00
if (d)
ss << " = " << d->toString(type);
2021-07-06 18:30:46 +02:00
return ss.str();
}
2021-07-06 18:30:46 +02:00
/** Check if two signal names are numbered ascendingly
*
* E.g. signal3 -> signal4
*/
static
bool isNextName(const std::string &a, const std::string &b)
{
/* Find common prefix */
std::string::const_iterator ia, ib;
for (ia = a.cbegin(), ib = b.cbegin();
ia != b.cend() && ib != b.cend() && *ia == *ib;
++ia, ++ib);
/* Suffixes */
auto sa = std::string(ia, a.cend());
auto sb = std::string(ib, b.cend());
try {
size_t ea, eb;
auto na = std::stoul(sa, &ea, 10);
auto nb = std::stoul(sb, &eb, 10);
return na + 1 == nb;
} catch (std::exception &) {
return false;
2021-07-06 18:30:46 +02:00
}
}
bool Signal::isNext(const Signal &sig)
{
if (type != sig.type)
return false;
if (!unit.empty() && !sig.unit.empty() && unit != sig.unit)
return false;
2021-07-06 18:30:46 +02:00
return isNextName(name, sig.name);
2021-07-06 18:30:46 +02:00
}