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 <netcomm/fawkes/client.h>
23 
24 #include <plugins/openprs/mod_utils.h>
25 #include <config/netconf.h>
26 #include <navgraph/yaml_navgraph.h>
27 #include <navgraph/navgraph.h>
28 #include <oprs_f-pub.h>
29 
30 using namespace fawkes;
31 
32 extern "C" void finalize();
33 
34 // Global variables
35 FawkesNetworkClient *g_fnet_client = NULL;
36 NetworkConfiguration *g_config = NULL;
37 NavGraph *g_navgraph = NULL;
38 
39 
40 extern "C"
41 Term *
42 action_navgraph_load(TermList terms)
43 {
44  ACTION_ASSERT_ARG_LENGTH("navgraph-load", terms, 0);
45 
46  try {
47  std::string graph_file =
48  g_config->get_string("/navgraph/graph_file");
49 
50  if (graph_file[0] != '/') {
51  graph_file = std::string(CONFDIR) + "/" + graph_file;
52  }
53 
54  g_navgraph = load_yaml_navgraph(graph_file);
55 
56  const std::vector<NavGraphNode> &nodes = g_navgraph->nodes();
57  const std::vector<NavGraphEdge> &edges = g_navgraph->edges();
58 
59  TermList graph_tl = sl_make_slist();
60  graph_tl = build_term_list(graph_tl, build_string(g_navgraph->name().c_str()));
61  graph_tl = build_term_list(graph_tl, build_string(graph_file.c_str()));
62  add_external_fact((char *)"navgraph", graph_tl);
63 
64  for (auto n : nodes) {
65  TermList props = sl_make_slist();
66  const std::map<std::string, std::string> &properties = n.properties();
67  for (auto p : properties) {
68  TermList prop = sl_make_slist();
69  prop = build_term_list(prop, build_string(p.first.c_str()));
70  prop = build_term_list(prop, build_string(p.second.c_str()));
71  props = build_term_list(props, build_term_l_list_from_c_list(prop));
72  }
73 
74  TermList node_tl = sl_make_slist();
75  node_tl = build_term_list(node_tl, build_string(n.name().c_str()));
76  node_tl = build_term_list(node_tl, build_float(n.x()));
77  node_tl = build_term_list(node_tl, build_float(n.y()));
78  node_tl = build_term_list(node_tl, build_term_l_list_from_c_list(props));
79 
80  add_external_fact((char *)"navgraph-node", node_tl);
81  }
82 
83  for (auto e : edges) {
84  TermList props = sl_make_slist();
85  const std::map<std::string, std::string> &properties = e.properties();
86  for (auto p : properties) {
87  TermList prop = sl_make_slist();
88  prop = build_term_list(prop, build_string(p.first.c_str()));
89  prop = build_term_list(prop, build_string(p.second.c_str()));
90  props = build_term_list(props, build_term_l_list_from_c_list(prop));
91  }
92 
93  TermList edge_tl = sl_make_slist();
94  edge_tl = build_term_list(edge_tl, build_string(e.from().c_str()));
95  edge_tl = build_term_list(edge_tl, build_string(e.to().c_str()));
96  edge_tl = build_term_list(edge_tl, e.is_directed() ? build_t() : build_nil());
97  edge_tl = build_term_list(edge_tl, build_term_l_list_from_c_list(props));
98 
99  add_external_fact((char *)"navgraph-edge", edge_tl);
100  }
101 
102  } catch (Exception &e) {
103  fprintf(stderr, "Failed to open navgraph: %s\n", e.what_no_backtrace());
104  ACTION_FAIL();
105  }
106 
107  ACTION_FINAL();
108 }
109 
110 
111 /** Entry function for the OpenPRS module. */
112 extern "C"
113 void init()
114 {
115  printf("*** LOADING mod_navgraph\n");
116 
117  std::string fawkes_host;
118  unsigned short fawkes_port = 0;
119  get_fawkes_host_port(fawkes_host, fawkes_port);
120 
121  printf("Connecting to Fawkes at %s:%u\n", fawkes_host.c_str(), fawkes_port);
122  try {
123  g_fnet_client = new FawkesNetworkClient(fawkes_host.c_str(), fawkes_port);
124  g_fnet_client->connect();
125  g_config = new NetworkConfiguration(g_fnet_client);
126  g_config->set_mirror_mode(true);
127  } catch (Exception &e) {
128  fprintf(stderr, "Error: cannot establish network connection: %s\n",
129  e.what_no_backtrace());
130  }
131 
132  make_and_declare_action("navgraph-load", action_navgraph_load, 0);
133  add_user_end_kernel_hook(finalize);
134 }
135 
136 /** Finalization function for the OpenPRS module. */
137 extern "C"
138 void finalize()
139 {
140  printf("*** DESTROYING mod_navgraph\n");
141  delete g_config;
142  g_config = NULL;
143  delete g_fnet_client;
144  g_fnet_client = NULL;
145  delete g_navgraph;
146  g_navgraph = NULL;
147 }
Simple Fawkes network client.
Definition: client.h:52
Fawkes library namespace.
Topological map graph.
Definition: navgraph.h:57
void connect()
Connect to remote.
Definition: client.cpp:417
virtual std::string get_string(const char *path)
Get value from configuration which is of type string.
Definition: netconf.cpp:482
const std::vector< NavGraphNode > & nodes() const
Get nodes of the graph.
Definition: navgraph.cpp:124
std::string name() const
Get graph name.
Definition: navgraph.cpp:114
Base class for exceptions in Fawkes.
Definition: exception.h:36
NavGraph * load_yaml_navgraph(std::string filename)
Load topological map graph stored in RCSoft format.
const std::vector< NavGraphEdge > & edges() const
Get edges of the graph.
Definition: navgraph.cpp:134
virtual const char * what_no_backtrace() const
Get primary string (does not implicitly print the back trace).
Definition: exception.cpp:686
virtual void set_mirror_mode(bool mirror)
Enable or disable mirror mode.
Definition: netconf.cpp:1324
Remote configuration via Fawkes net.
Definition: netconf.h:49