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/nodes/example.cpp

174 lines
3.6 KiB
C++
Raw Permalink Normal View History

/** An example get started with new implementations of new node-types
2022-04-01 14:55:08 +02:00
*
* This example does not do any particulary useful.
* It is just a skeleton to get you started with new node-types.
*
* @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
*********************************************************************************/
#include <villas/node_compat.hpp>
#include <villas/nodes/example.hpp>
#include <villas/utils.hpp>
#include <villas/sample.hpp>
2021-02-16 14:15:14 +01:00
#include <villas/exceptions.hpp>
#include <villas/super_node.hpp>
2021-02-16 14:15:14 +01:00
#include <villas/exceptions.hpp>
2021-02-16 14:15:14 +01:00
using namespace villas;
using namespace villas::node;
using namespace villas::utils;
ExampleNode::ExampleNode(const uuid_t &id, const std::string &name) :
Node(id, name),
2022-04-01 15:02:11 +02:00
setting1(72),
setting2("something"),
state1(0)
2022-04-01 14:55:08 +02:00
{ }
2022-04-01 14:55:08 +02:00
ExampleNode::~ExampleNode()
{ }
2022-04-01 14:55:08 +02:00
int ExampleNode::prepare()
{
2022-04-01 14:55:08 +02:00
state1 = setting1;
2022-04-01 14:55:08 +02:00
if (setting2 == "double")
state1 *= 2;
return 0;
}
int ExampleNode::parse(json_t *json)
{
/* TODO: Add implementation here. The following is just an example */
2022-04-01 14:55:08 +02:00
const char *setting2_str = nullptr;
json_error_t err;
2022-04-01 14:55:08 +02:00
int ret = json_unpack_ex(json, &err, 0, "{ s?: i, s?: s }",
"setting1", &setting1,
"setting2", &setting2_str
);
if (ret)
2021-02-16 14:15:14 +01:00
throw ConfigError(json, err, "node-config-node-example");
2022-04-01 14:55:08 +02:00
if (setting2_str)
setting2 = setting2_str;
2022-04-01 14:55:08 +02:00
return 0;
}
2022-04-01 14:55:08 +02:00
int ExampleNode::check()
{
2022-04-01 14:55:08 +02:00
if (setting1 > 100 || setting1 < 0)
return -1;
2022-04-01 14:55:08 +02:00
if (setting2.empty() || setting2.size() > 10)
return -1;
return 0;
}
2022-04-01 14:55:08 +02:00
int ExampleNode::start()
{
2022-04-01 14:55:08 +02:00
// TODO add implementation here
2022-04-01 14:55:08 +02:00
start_time = time_now();
return 0;
}
2022-04-01 14:55:08 +02:00
// int ExampleNode::stop()
// {
// // TODO add implementation here
// return 0;
// }
2022-04-01 14:55:08 +02:00
// int ExampleNode::pause()
// {
// // TODO add implementation here
// return 0;
// }
2022-04-01 14:55:08 +02:00
// int ExampleNode::resume()
// {
// // TODO add implementation here
// return 0;
// }
2022-04-01 14:55:08 +02:00
// int ExampleNode::restart()
// {
// // TODO add implementation here
// return 0;
// }
2022-04-01 14:55:08 +02:00
// int ExampleNode::reverse()
// {
// // TODO add implementation here
// return 0;
// }
2022-04-01 14:55:08 +02:00
// std::vector<int> ExampleNode::getPollFDs()
// {
// // TODO add implementation here
// return {};
// }
2020-01-21 14:26:56 +01:00
2022-04-01 14:55:08 +02:00
// std::vector<int> ExampleNode::getNetemFDs()
// {
// // TODO add implementation here
// return {};
// }
2020-01-21 14:26:56 +01:00
2022-04-01 14:55:08 +02:00
// struct villas::node::memory::Type * ExampleNode::getMemoryType()
// {
// // TODO add implementation here
// }
2020-01-21 14:26:56 +01:00
2022-04-01 14:55:08 +02:00
const std::string & ExampleNode::getDetails()
2020-01-21 14:26:56 +01:00
{
2022-04-01 14:55:08 +02:00
details = fmt::format("setting1={}, setting2={}", setting1, setting2);
return details;
2020-01-21 14:26:56 +01:00
}
2022-04-01 14:55:08 +02:00
int ExampleNode::_read(struct Sample *smps[], unsigned cnt)
{
int read;
struct timespec now;
/* TODO: Add implementation here. The following is just an example */
assert(cnt >= 1 && smps[0]->capacity >= 1);
now = time_now();
2022-04-01 14:55:08 +02:00
smps[0]->data[0].f = time_delta(&now, &start_time);
/* Dont forget to set other flags in struct Sample::flags
* E.g. for sequence no, timestamps... */
smps[0]->flags = (int) SampleFlags::HAS_DATA;
2022-04-01 14:55:08 +02:00
smps[0]->signals = getInputSignals(false);
read = 1; /* The number of samples read */
return read;
}
2022-04-01 14:55:08 +02:00
int ExampleNode::_write(struct Sample *smps[], unsigned cnt)
{
int written;
/* TODO: Add implementation here. */
written = 0; /* The number of samples written */
return written;
}
2022-04-01 14:55:08 +02:00
static char n[] = "example";
static char d[] = "An example for staring new node-type implementations";
static NodePlugin<ExampleNode, n , d, (int) NodeFactory::Flags::SUPPORTS_READ | (int) NodeFactory::Flags::SUPPORTS_WRITE | (int) NodeFactory::Flags::SUPPORTS_POLL | (int) NodeFactory::Flags::HIDDEN> p;