2020-01-21 14:20:25 +01:00
|
|
|
/** 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.
|
2020-01-21 14:20:25 +01:00
|
|
|
*
|
2022-12-14 17:41:58 +01:00
|
|
|
* @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
|
2020-01-21 14:20:25 +01:00
|
|
|
*********************************************************************************/
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
#include <villas/node_compat.hpp>
|
2020-01-21 14:20:25 +01:00
|
|
|
#include <villas/nodes/example.hpp>
|
|
|
|
#include <villas/utils.hpp>
|
2021-08-10 10:12:48 -04:00
|
|
|
#include <villas/sample.hpp>
|
2021-02-16 14:15:14 +01:00
|
|
|
#include <villas/exceptions.hpp>
|
2020-01-21 14:20:25 +01:00
|
|
|
#include <villas/super_node.hpp>
|
2021-02-16 14:15:14 +01:00
|
|
|
#include <villas/exceptions.hpp>
|
2020-01-21 14:20:25 +01:00
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
using namespace villas;
|
2020-01-21 14:20:25 +01:00
|
|
|
using namespace villas::node;
|
|
|
|
using namespace villas::utils;
|
|
|
|
|
2023-06-30 10:51:01 +02:00
|
|
|
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
|
|
|
{ }
|
2020-01-21 14:20:25 +01:00
|
|
|
|
2022-04-01 14:55:08 +02:00
|
|
|
ExampleNode::~ExampleNode()
|
|
|
|
{ }
|
2020-01-21 14:20:25 +01:00
|
|
|
|
2022-04-01 14:55:08 +02:00
|
|
|
int ExampleNode::prepare()
|
2020-01-21 14:20:25 +01:00
|
|
|
{
|
2022-04-01 14:55:08 +02:00
|
|
|
state1 = setting1;
|
2020-01-21 14:20:25 +01:00
|
|
|
|
2022-04-01 14:55:08 +02:00
|
|
|
if (setting2 == "double")
|
|
|
|
state1 *= 2;
|
2020-01-21 14:20:25 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-06-30 10:51:01 +02:00
|
|
|
int ExampleNode::parse(json_t *json)
|
2020-01-21 14:20:25 +01:00
|
|
|
{
|
|
|
|
/* TODO: Add implementation here. The following is just an example */
|
|
|
|
|
2022-04-01 14:55:08 +02:00
|
|
|
const char *setting2_str = nullptr;
|
2020-01-21 14:20:25 +01:00
|
|
|
|
|
|
|
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
|
2020-01-21 14:20:25 +01:00
|
|
|
);
|
|
|
|
if (ret)
|
2021-02-16 14:15:14 +01:00
|
|
|
throw ConfigError(json, err, "node-config-node-example");
|
2020-01-21 14:20:25 +01:00
|
|
|
|
2022-04-01 14:55:08 +02:00
|
|
|
if (setting2_str)
|
|
|
|
setting2 = setting2_str;
|
2020-01-21 14:20:25 +01:00
|
|
|
|
2022-04-01 14:55:08 +02:00
|
|
|
return 0;
|
2020-01-21 14:20:25 +01:00
|
|
|
}
|
|
|
|
|
2022-04-01 14:55:08 +02:00
|
|
|
int ExampleNode::check()
|
2020-01-21 14:20:25 +01:00
|
|
|
{
|
2022-04-01 14:55:08 +02:00
|
|
|
if (setting1 > 100 || setting1 < 0)
|
2020-01-21 14:20:25 +01:00
|
|
|
return -1;
|
|
|
|
|
2022-04-01 14:55:08 +02:00
|
|
|
if (setting2.empty() || setting2.size() > 10)
|
2020-01-21 14:20:25 +01:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-04-01 14:55:08 +02:00
|
|
|
int ExampleNode::start()
|
2020-01-21 14:20:25 +01:00
|
|
|
{
|
2022-04-01 14:55:08 +02:00
|
|
|
// TODO add implementation here
|
2020-01-21 14:20:25 +01:00
|
|
|
|
2022-04-01 14:55:08 +02:00
|
|
|
start_time = time_now();
|
2020-01-21 14:20:25 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-04-01 14:55:08 +02:00
|
|
|
// int ExampleNode::stop()
|
|
|
|
// {
|
|
|
|
// // TODO add implementation here
|
|
|
|
// return 0;
|
|
|
|
// }
|
2020-01-21 14:20:25 +01:00
|
|
|
|
2022-04-01 14:55:08 +02:00
|
|
|
// int ExampleNode::pause()
|
|
|
|
// {
|
|
|
|
// // TODO add implementation here
|
|
|
|
// return 0;
|
|
|
|
// }
|
2020-01-21 14:20:25 +01:00
|
|
|
|
2022-04-01 14:55:08 +02:00
|
|
|
// int ExampleNode::resume()
|
|
|
|
// {
|
|
|
|
// // TODO add implementation here
|
|
|
|
// return 0;
|
|
|
|
// }
|
2020-01-21 14:20:25 +01:00
|
|
|
|
2022-04-01 14:55:08 +02:00
|
|
|
// int ExampleNode::restart()
|
|
|
|
// {
|
|
|
|
// // TODO add implementation here
|
|
|
|
// return 0;
|
|
|
|
// }
|
2020-01-21 14:20:25 +01:00
|
|
|
|
2022-04-01 14:55:08 +02:00
|
|
|
// int ExampleNode::reverse()
|
|
|
|
// {
|
|
|
|
// // TODO add implementation here
|
|
|
|
// return 0;
|
|
|
|
// }
|
2020-01-21 14:20:25 +01:00
|
|
|
|
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)
|
2020-01-21 14:20:25 +01:00
|
|
|
{
|
|
|
|
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);
|
2020-01-21 14:20:25 +01:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
/* Dont forget to set other flags in struct Sample::flags
|
2020-07-06 15:07:05 +02:00
|
|
|
* 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);
|
2020-07-06 15:07:05 +02:00
|
|
|
|
2020-01-21 14:20:25 +01:00
|
|
|
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)
|
2020-01-21 14:20:25 +01:00
|
|
|
{
|
|
|
|
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;
|