2017-03-05 10:06:32 -04:00
|
|
|
/** Print hook.
|
|
|
|
*
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
|
|
|
* @copyright 2017, Institute for Automation of Complex Power Systems, EONERC
|
2017-04-27 12:56:43 +02:00
|
|
|
* @license GNU General Public License (version 3)
|
|
|
|
*
|
|
|
|
* VILLASnode
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* any later version.
|
2017-05-05 19:24:16 +00:00
|
|
|
*
|
2017-04-27 12:56:43 +02:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2017-05-05 19:24:16 +00:00
|
|
|
*
|
2017-04-27 12:56:43 +02:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2017-03-05 10:06:32 -04:00
|
|
|
*********************************************************************************/
|
|
|
|
|
|
|
|
/** @addtogroup hooks Hook functions
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2018-06-04 14:22:38 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
2017-12-09 02:19:28 +08:00
|
|
|
#include <villas/hook.h>
|
|
|
|
#include <villas/plugin.h>
|
2018-05-24 09:06:53 +02:00
|
|
|
#include <villas/path.h>
|
2017-12-09 02:19:28 +08:00
|
|
|
#include <villas/sample.h>
|
|
|
|
#include <villas/io.h>
|
2017-03-05 10:06:32 -04:00
|
|
|
|
2017-03-20 09:13:01 -03:00
|
|
|
struct print {
|
2017-08-23 16:50:29 +02:00
|
|
|
struct io io;
|
2018-05-12 13:56:12 +02:00
|
|
|
struct format_type *format;
|
2017-08-03 00:19:27 +02:00
|
|
|
|
2018-05-24 09:06:53 +02:00
|
|
|
char *prefix;
|
|
|
|
char *uri;
|
2017-03-20 09:13:01 -03:00
|
|
|
};
|
|
|
|
|
2017-03-27 12:26:11 +02:00
|
|
|
static int print_init(struct hook *h)
|
2017-03-05 10:06:32 -04:00
|
|
|
{
|
2017-10-18 15:39:53 +02:00
|
|
|
struct print *p = (struct print *) h->_vd;
|
2017-03-27 12:26:11 +02:00
|
|
|
|
|
|
|
p->uri = NULL;
|
2018-05-24 09:06:53 +02:00
|
|
|
p->prefix = NULL;
|
2018-05-12 13:56:12 +02:00
|
|
|
p->format = format_type_lookup("villas.human");
|
2017-03-27 12:26:11 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int print_start(struct hook *h)
|
|
|
|
{
|
2017-10-18 15:39:53 +02:00
|
|
|
struct print *p = (struct print *) h->_vd;
|
2017-08-03 00:19:27 +02:00
|
|
|
int ret;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2018-05-12 15:25:29 +02:00
|
|
|
ret = io_init(&p->io, p->format, h->node, SAMPLE_HAS_ALL);
|
2017-08-03 00:19:27 +02:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2017-08-23 16:50:29 +02:00
|
|
|
ret = io_open(&p->io, p->uri);
|
2017-08-03 00:19:27 +02:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return 0;
|
2017-03-27 12:26:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int print_stop(struct hook *h)
|
|
|
|
{
|
2017-10-18 15:39:53 +02:00
|
|
|
struct print *p = (struct print *) h->_vd;
|
2017-08-03 00:19:27 +02:00
|
|
|
int ret;
|
2017-03-27 12:26:11 +02:00
|
|
|
|
2017-08-23 16:50:29 +02:00
|
|
|
ret = io_close(&p->io);
|
2017-08-03 00:19:27 +02:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return 0;
|
2017-03-27 12:26:11 +02:00
|
|
|
}
|
|
|
|
|
2017-08-03 00:19:27 +02:00
|
|
|
static int print_parse(struct hook *h, json_t *cfg)
|
2017-03-27 12:26:11 +02:00
|
|
|
{
|
2017-10-18 15:39:53 +02:00
|
|
|
struct print *p = (struct print *) h->_vd;
|
2018-05-24 09:06:53 +02:00
|
|
|
const char *format = NULL, *prefix = NULL, *uri = NULL;
|
2017-08-03 00:19:27 +02:00
|
|
|
int ret;
|
|
|
|
json_error_t err;
|
|
|
|
|
2018-05-24 09:06:53 +02:00
|
|
|
ret = json_unpack_ex(cfg, &err, 0, "{ s?: s, s?: s, s?: s }",
|
|
|
|
"output", &uri,
|
|
|
|
"prefix", &prefix,
|
2017-08-23 16:50:29 +02:00
|
|
|
"format", &format
|
2017-08-03 00:19:27 +02:00
|
|
|
);
|
|
|
|
if (ret)
|
|
|
|
jerror(&err, "Failed to parse configuration of hook '%s'", plugin_name(h->_vt));
|
|
|
|
|
2018-05-24 09:06:53 +02:00
|
|
|
if (prefix)
|
|
|
|
p->prefix = strdup(prefix);
|
|
|
|
|
|
|
|
if (uri)
|
|
|
|
p->uri = strdup(uri);
|
|
|
|
|
2017-08-03 00:19:27 +02:00
|
|
|
if (format) {
|
2018-05-12 13:56:12 +02:00
|
|
|
p->format = format_type_lookup(format);
|
2017-08-23 16:50:29 +02:00
|
|
|
if (!p->format)
|
2017-08-03 00:19:27 +02:00
|
|
|
jerror(&err, "Invalid format '%s'", format);
|
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-27 12:26:11 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-05-24 09:06:53 +02:00
|
|
|
static int print_read(struct hook *h, struct sample *smps[], unsigned *cnt)
|
|
|
|
{
|
|
|
|
struct print *p = (struct print *) h->_vd;
|
|
|
|
|
|
|
|
if (p->prefix)
|
|
|
|
printf("%s", p->prefix);
|
|
|
|
else if (h->node)
|
|
|
|
printf("Node %s read: ", node_name(h->node));
|
|
|
|
|
|
|
|
io_print(&p->io, smps, *cnt);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int print_write(struct hook *h, struct sample *smps[], unsigned *cnt)
|
|
|
|
{
|
|
|
|
struct print *p = (struct print *) h->_vd;
|
|
|
|
|
|
|
|
if (p->prefix)
|
|
|
|
printf("%s", p->prefix);
|
|
|
|
else if (h->node)
|
|
|
|
printf("Node %s write: ", node_name(h->node));
|
|
|
|
|
|
|
|
io_print(&p->io, smps, *cnt);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-09-02 14:27:58 +02:00
|
|
|
static int print_process(struct hook *h, struct sample *smps[], unsigned *cnt)
|
2017-03-27 12:26:11 +02:00
|
|
|
{
|
2017-10-18 15:39:53 +02:00
|
|
|
struct print *p = (struct print *) h->_vd;
|
2017-03-27 12:26:11 +02:00
|
|
|
|
2018-05-24 09:06:53 +02:00
|
|
|
if (p->prefix)
|
|
|
|
printf("%s", p->prefix);
|
|
|
|
else if (h->path)
|
|
|
|
printf("Path %s process: ", path_name(h->path));
|
|
|
|
|
2017-08-23 16:50:29 +02:00
|
|
|
io_print(&p->io, smps, *cnt);
|
2017-03-05 10:06:32 -04:00
|
|
|
|
2017-03-20 09:14:10 -03:00
|
|
|
return 0;
|
2017-03-05 10:06:32 -04:00
|
|
|
}
|
|
|
|
|
2018-05-12 15:25:29 +02:00
|
|
|
static int print_destroy(struct hook *h)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct print *p = (struct print *) h->_vd;
|
|
|
|
|
2018-05-24 09:06:53 +02:00
|
|
|
if (p->uri)
|
|
|
|
free(p->uri);
|
|
|
|
|
|
|
|
if (p->prefix)
|
|
|
|
free(p->prefix);
|
|
|
|
|
2018-05-12 15:25:29 +02:00
|
|
|
ret = io_destroy(&p->io);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-03-05 10:06:32 -04:00
|
|
|
static struct plugin p = {
|
|
|
|
.name = "print",
|
|
|
|
.description = "Print the message to stdout",
|
|
|
|
.type = PLUGIN_TYPE_HOOK,
|
|
|
|
.hook = {
|
2017-09-02 14:27:58 +02:00
|
|
|
.flags = HOOK_NODE | HOOK_PATH,
|
2017-03-05 10:06:32 -04:00
|
|
|
.priority = 99,
|
2017-03-27 12:26:11 +02:00
|
|
|
.init = print_init,
|
|
|
|
.parse = print_parse,
|
2018-05-12 15:25:29 +02:00
|
|
|
.destroy= print_destroy,
|
2017-03-27 12:26:11 +02:00
|
|
|
.start = print_start,
|
|
|
|
.stop = print_stop,
|
2018-05-24 09:06:53 +02:00
|
|
|
.read = print_read,
|
|
|
|
.write = print_write,
|
2017-09-02 14:27:58 +02:00
|
|
|
.process= print_process,
|
2017-03-27 12:26:11 +02:00
|
|
|
.size = sizeof(struct print)
|
2017-03-05 10:06:32 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
REGISTER_PLUGIN(&p)
|
|
|
|
|
2017-07-24 19:33:35 +02:00
|
|
|
/** @} */
|