libswarmio/src/swarmio/simulator/main.cpp
Go to the documentation of this file.
4 #include <swarmio/Exception.h>
5 #include <czmq.h>
6 #include <iostream>
7 #include <chrono>
8 #include <g3log/g3log.hpp>
9 #include <g3log/logworker.hpp>
10 
11 using namespace swarmio;
12 using namespace swarmio::simulator;
13 using namespace std::chrono_literals;
14 
15 int main(int argc, const char *argv[])
16 {
17  // Wrap to trigger destructors before shutdown
18  {
19  // Create Zyre endpoint
20  char *hostname = zsys_hostname();
21  swarmio::transport::zyre::ZyreEndpoint endpoint(hostname, "simulator");
22  zstr_free(&hostname);
23 
24  // Initialize logging
25  auto worker = g3::LogWorker::createLogWorker();
26  worker->addDefaultLogger("swarmio-simulator", ".");
27  initializeLogging(worker.get());
28  std::string configFilePath = "";
29  for (int i = 1; i < argc; ++i)
30  {
31  const char *current = argv[i];
32  if (current[0] == '-')
33  {
34  if (current[1] == 'C')
35  {
36  // Config file remapping
37  configFilePath = current + 2;
38  endpoint.SetConfig(configFilePath);
39  }
40  else
41  {
42  LOG(FATAL) << "Command line argument " << i << " is unknown";
43  return -10;
44  }
45  }
46  else
47  {
48  LOG(FATAL) << "Command line argument " << i << " cannot be processed";
49  return -10;
50  }
51  }
52 
53  // Print UUID
54  std::cout << "Local node started with UUID: " << endpoint.GetUUID() << "\n";
55 
56  // Create device
57  ExampleDevice device(&endpoint);
58 
59  // Register static telemetry value
60  data::Variant value;
61  value.set_string_value("This won't ever change.");
62  device.AddConstantTelemetryValue("static_value", value, false);
63 
64  // Register static telemetry and status value
65  value.set_string_value("And neither will this.");
66  device.AddConstantTelemetryValue("static_status_value", value, true);
67 
68  // Register complex static telemetry value
69  value.Clear();
70  auto &pairs = *value.mutable_map_value()->mutable_pairs();
71  pairs["key1"].set_string_value("value1");
72  pairs["key2"].set_uint_value(2);
73  pairs["key3"].set_int_value(-3);
74  auto &pairs2 = *pairs["key4"].mutable_map_value()->mutable_pairs();
75  pairs2["embedded1"].set_string_value("subvalue1");
76  pairs2["embedded2"].set_uint_value(2);
77  device.AddConstantTelemetryValue("complex_value", value, false);
78 
79  // Register array static telemetry value
80  value.Clear();
81  auto &elements = *value.mutable_int_array();
82  elements.add_elements(30);
83  elements.add_elements(55);
84  elements.add_elements(0);
85  elements.add_elements(-45);
86  elements.add_elements(900);
87  device.AddConstantTelemetryValue("array_value", value, false);
88 
89  // Register some parameters
90  InMemoryParameter p1("examples/boolParameter", false);
91  device.AddInMemoryParameter(&p1);
92  InMemoryParameter p2("examples/stringParameter", "unknown");
93  device.AddInMemoryParameter(&p2);
94  InMemoryParameter p3("examples/intParameter", 1024);
95  device.AddInMemoryParameter(&p3);
96  InMemoryParameter p4("examples/doubleParameter", 2.5);
97  device.AddInMemoryParameter(&p4);
98  InMemoryParameter p5("examples/readOnlyParameter", "Can't change this", true);
99  device.AddInMemoryParameter(&p5);
100 
101  // Register some events
102  FauxEventHandler e1("emergency");
104  e1.AddParameter("severity", data::discovery::Type::INT);
105  device.AddFauxEventHandler(&e1);
106  FauxEventHandler e2("blackHawkDown");
108  device.AddFauxEventHandler(&e2);
109 
110  // Simulate position
111  LinearPathTelemetrySimulator pathSimulator(device.GetTelemetryService(), "location", SimulatedLocation(19.040235, 47.497912, 18.0), SimulatedLocation(48.210033, 16.363449, 23.0), 1h);
112 
113  // Start sending and receiving
114  endpoint.Start();
115 
116  // Loop endlessly
117  std::cout << "Press ENTER to stop the simulator."
118  << "\n";
119 
120  // Get a character
121  std::cin.get();
122 
123  // Stop telemetry
124  pathSimulator.Stop();
125 
126  // Stop endpoint
127  endpoint.Stop();
128  }
129 
130  // Shut down zsys manually to avoid assertion failure on Windows
131  zsys_shutdown();
132 
133  // All is well
134  return 0;
135 }
A special event handler that serves discovery information and ignores the event itself.
virtual void Stop() override
Send a termination signal and wait until the endpoint finished processing messages.
Simulates linear back-and-forth movement between two points.
void SetConfig(std::string cfg)
Set the config file path.
virtual const std::string & GetUUID() override
Get the UUID of the local node.
Definition: ZyreEndpoint.h:172
A strongly typed in-memory parameter target that can be made read-only.
void AddFauxEventHandler(FauxEventHandler *eventHandler)
Add an event handler and register it.
void AddInMemoryParameter(InMemoryParameter *parameter)
Add an In-Memory Parameter and register it.
A location described by a latitude, a longitude and an altitude.
swarmio::services::telemetry::Service & GetTelemetryService()
Get a reference for the Telemetry service.
Definition: Profile.h:81
void AddParameter(const std::string &key, data::discovery::Type type)
Add a new parameter to serve for discovery.
An Endpoint implementation using the Zyre protocol.
Definition: ZyreEndpoint.h:29
int main(int argc, const char *argv[])
Represents a collection of standard services that can be used to simulate an ordinary device...
Definition: ExampleDevice.h:16
virtual void Start() override
Start the background thread, announce the Zyre node and start processing messages.
void AddConstantTelemetryValue(const std::string &key, const data::Variant &value, bool includeInStatus=false)
Add a constant telemetry value.


swarmros
Author(s):
autogenerated on Fri Apr 3 2020 03:42:48