Fawkes API Fawkes Development Version
mod_navgraph.cpp
1/***************************************************************************
2 * mod_navgraph.cpp - OpenPRS navgraph module
3 *
4 * Created: Fri Sep 05 16:22:26 2014
5 * Copyright 2014-2015 Tim Niemueller [www.niemueller.de]
6 ****************************************************************************/
7
8/* This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Library General Public License for more details.
17 *
18 * Read the full text in the LICENSE.GPL file in the doc directory.
19 */
20
21// this must come first due to a define of enqueue in OpenPRS' slistPack_f.h
22#include <config/netconf.h>
23#include <navgraph/navgraph.h>
24#include <navgraph/yaml_navgraph.h>
25#include <netcomm/fawkes/client.h>
26#include <plugins/openprs/mod_utils.h>
27
28#include <oprs_f-pub.h>
29
30using namespace fawkes;
31
32extern "C" void finalize();
33
34// Global variables
35FawkesNetworkClient * g_fnet_client = NULL;
36NetworkConfiguration *g_config = NULL;
37NavGraph * g_navgraph = NULL;
38
39extern "C" Term *
40action_navgraph_load(TermList terms)
41{
42 ACTION_ASSERT_ARG_LENGTH("navgraph-load", terms, 0);
43
44 try {
45 std::string graph_file = g_config->get_string("/navgraph/graph_file");
46
47 if (graph_file[0] != '/') {
48 graph_file = std::string(CONFDIR) + "/" + graph_file;
49 }
50
51 g_navgraph = load_yaml_navgraph(graph_file);
52
53 const std::vector<NavGraphNode> &nodes = g_navgraph->nodes();
54 const std::vector<NavGraphEdge> &edges = g_navgraph->edges();
55
56 TermList graph_tl = sl_make_slist();
57 graph_tl = build_term_list(graph_tl, build_string(g_navgraph->name().c_str()));
58 graph_tl = build_term_list(graph_tl, build_string(graph_file.c_str()));
59 add_external_fact((char *)"navgraph", graph_tl);
60
61 for (auto n : nodes) {
62 TermList props = sl_make_slist();
63 const std::map<std::string, std::string> &properties = n.properties();
64 for (auto p : properties) {
65 TermList prop = sl_make_slist();
66 prop = build_term_list(prop, build_string(p.first.c_str()));
67 prop = build_term_list(prop, build_string(p.second.c_str()));
68 props = build_term_list(props, build_term_l_list_from_c_list(prop));
69 }
70
71 TermList node_tl = sl_make_slist();
72 node_tl = build_term_list(node_tl, build_string(n.name().c_str()));
73 node_tl = build_term_list(node_tl, build_float(n.x()));
74 node_tl = build_term_list(node_tl, build_float(n.y()));
75 node_tl = build_term_list(node_tl, build_term_l_list_from_c_list(props));
76
77 add_external_fact((char *)"navgraph-node", node_tl);
78 }
79
80 for (auto e : edges) {
81 TermList props = sl_make_slist();
82 const std::map<std::string, std::string> &properties = e.properties();
83 for (auto p : properties) {
84 TermList prop = sl_make_slist();
85 prop = build_term_list(prop, build_string(p.first.c_str()));
86 prop = build_term_list(prop, build_string(p.second.c_str()));
87 props = build_term_list(props, build_term_l_list_from_c_list(prop));
88 }
89
90 TermList edge_tl = sl_make_slist();
91 edge_tl = build_term_list(edge_tl, build_string(e.from().c_str()));
92 edge_tl = build_term_list(edge_tl, build_string(e.to().c_str()));
93 edge_tl = build_term_list(edge_tl, e.is_directed() ? build_t() : build_nil());
94 edge_tl = build_term_list(edge_tl, build_term_l_list_from_c_list(props));
95
96 add_external_fact((char *)"navgraph-edge", edge_tl);
97 }
98
99 } catch (Exception &e) {
100 fprintf(stderr, "Failed to open navgraph: %s\n", e.what_no_backtrace());
101 ACTION_FAIL();
102 }
103
104 ACTION_FINAL();
105}
106
107/** Entry function for the OpenPRS module. */
108extern "C" void
109init()
110{
111 printf("*** LOADING mod_navgraph\n");
112
113 std::string fawkes_host;
114 unsigned short fawkes_port = 0;
115 get_fawkes_host_port(fawkes_host, fawkes_port);
116
117 printf("Connecting to Fawkes at %s:%u\n", fawkes_host.c_str(), fawkes_port);
118 try {
119 g_fnet_client = new FawkesNetworkClient(fawkes_host.c_str(), fawkes_port);
120 g_fnet_client->connect();
121 g_config = new NetworkConfiguration(g_fnet_client);
122 g_config->set_mirror_mode(true);
123 } catch (Exception &e) {
124 fprintf(stderr, "Error: cannot establish network connection: %s\n", e.what_no_backtrace());
125 }
126
127 make_and_declare_action("navgraph-load", action_navgraph_load, 0);
128 add_user_end_kernel_hook(finalize);
129}
130
131/** Finalization function for the OpenPRS module. */
132extern "C" void
133finalize()
134{
135 printf("*** DESTROYING mod_navgraph\n");
136 delete g_config;
137 g_config = NULL;
138 delete g_fnet_client;
139 g_fnet_client = NULL;
140 delete g_navgraph;
141 g_navgraph = NULL;
142}
Base class for exceptions in Fawkes.
Definition: exception.h:36
virtual const char * what_no_backtrace() const noexcept
Get primary string (does not implicitly print the back trace).
Definition: exception.cpp:663
Simple Fawkes network client.
Definition: client.h:52
void connect()
Connect to remote.
Definition: client.cpp:424
Topological map graph.
Definition: navgraph.h:50
const std::vector< NavGraphNode > & nodes() const
Get nodes of the graph.
Definition: navgraph.cpp:133
const std::vector< NavGraphEdge > & edges() const
Get edges of the graph.
Definition: navgraph.cpp:142
std::string name() const
Get graph name.
Definition: navgraph.cpp:124
Remote configuration via Fawkes net.
Definition: netconf.h:50
virtual std::string get_string(const char *path)
Get value from configuration which is of type string.
Definition: netconf.cpp:463
virtual void set_mirror_mode(bool mirror)
Enable or disable mirror mode.
Definition: netconf.cpp:1269
Fawkes library namespace.
NavGraph * load_yaml_navgraph(std::string filename, bool allow_multi_graph)
Load topological map graph stored in RCSoft format.