SOIL C++
C++ Unified Device Interface
main.cpp
Go to the documentation of this file.
1#include "MQTT/Publisher.h"
2#include "UDP/Broadcast.h"
3#include "REST/Server.h"
4#include "SOIL/Object.h"
5#include "SOIL/Variable.h"
6#include "SOIL/Types.h"
7#include "SIGN/Hasher.h"
8#include "SIGN/Signer.h"
9#include <thread>
10#include <iostream>
11#include <chrono>
12
13
14
15int main(int argc, char** argv)
16{
17 try
18 {
19 std::cout << "############ SOIL #############" << std::endl;
20 std::shared_ptr<SIGN::Signer> signer(new SIGN::Signer("../../../../assets/private-device-key.pem"));
21 std::cout << "[INFO] " << signer->openssl_version() << std::endl;
22 std::shared_ptr<SOIL::Object> root_object = SOIL::Object::create(NULL, "OBJ-ROOT", "Root Object", "Root Object and entry point for the model.");
23 std::shared_ptr<SOIL::Object> lsm_object = SOIL::Object::create(root_object, "OBJ-LSM", "LSM System", "LSM system root object");
24 std::shared_ptr<SOIL::Object> entities_object = SOIL::Object::create(lsm_object, "OBJ-Entities", "Mobile Entities", "Mobile entities of the LSM system");
25 std::shared_ptr<SOIL::Object> target_A123 = SOIL::Object::create(entities_object, "OBJ-A123", "Target A123", "Target A123 of the LSM system");
26 std::shared_ptr<SOIL::Variable<double, 3> > variable = SOIL::Variable<double, 3>::create(target_A123, "VAR-Position", "Position", "A dummy position variable for testing", "MTR", "");
27 // NOTE: In above calls, the ptr->() function is used to avoid double allocation of shared pointer and double life cycle accounting
28
29 variable->update(std::vector<double>({ 1.0, 1.5, 2.0 }), SOIL::TIME::utc_now(), "LaVA Test");
30 variable->set_covariance(std::vector<std::vector<double> >({ { 0.01, 0, 0 },{0, 0.01, 0},{ 0, 0, 0.01 } }));
31 variable->sign(signer);
32
33
34 std::cout << "[INFO] " << "--- Exemplary JSON Serialization ---" << std::endl;
35 std::cout << variable->json() << std::endl << std::endl;
36 std::cout << "[INFO] " << "------------------------------------" << std::endl;
37
38
39 std::cout << "############ MQTT ############" << std::endl;
40 MQTT::Configuration mqtt_config("../../../../assets/mqtt.json");
41 mqtt_config.certificate_authority = "../../../../assets/MQTT-CA.pem";
42
43 std::shared_ptr<MQTT::Publisher> mqtt_publisher = std::make_shared<MQTT::Publisher>("cpp-test");
44 mqtt_publisher->configure(mqtt_config);
45 std::cout << "[INFO] MQTT Connecting to " << mqtt_config.uri() << std::endl;
46 mqtt_publisher->connect();
47 std::cout << "[OK] MQTT Connected" << std::endl;
48
49 std::cout << "[INFO] Running Loop Test" << std::endl;
50
51 auto begin = std::chrono::steady_clock::now();
52 for (int i = 0; i < 1000; i++)
53 {
54 variable->update(std::vector<double>({ 1.0 * i, 2.0 * i, 3.0 * i }), SOIL::TIME::utc_now(), "Loop Test Update");
55 variable->set_covariance(std::vector<std::vector<double> >({ { 1.0 * i,0,0 },{ 0,1.0 * i,0 },{ 0,0,1.0 * i } }));
56 variable->sign(signer);
57 variable->json();
58 variable->fqid();
59 variable->mqtt(mqtt_publisher);
60 }
61 auto end = std::chrono::steady_clock::now();
62 std::cout << "[INFO] " << "Average update, serialization and publish cycle: " << std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count() / 1000.0 << " ms" << std::endl << std::endl;
63
64 std::this_thread::sleep_for(std::chrono::milliseconds(500));
65 mqtt_publisher->disconnect();
66 std::cout << "[OK] MQTT Disconnect" << std::endl << std::endl;
67 std::this_thread::sleep_for(std::chrono::milliseconds(500));
68
69
70 std::cout << "############ UDP #############" << std::endl;
71 UDP::Configuration udp_config("../../../../assets/udp.json");
72 UDP::Broadcast udp_broadcast(1);
73 udp_broadcast.configure(udp_config);
74 udp_broadcast.send(variable->json());
75 std::cout << "[OK] UDP Broadcast sent to localhost on port " << udp_config.clients["127.0.0.1"] << std::endl << std::endl;
76
77
78 std::cout << "############ REST ############" << std::endl;
79 HTTP::Server http_server("http://localhost:8000");
80 http_server.add("/?(.*)", root_object);
81 http_server.open();
82 std::cout << "[OK] HTTP Server listening on http://localhost:8000. Press Enter to quit..." << std::endl;
83 std::string s;
84 std::getline(std::cin, s);
85 http_server.close();
86 std::cout << "[OK] Closing HTTP Server" << std::endl;
87
88 root_object.reset();
89
90 return 0;
91 }
92 catch (std::exception& e)
93 {
94 std::cout << "[ERROR] " << e.what() << std::endl;
95
96 return 1;
97 }
98
99}
HTPP Server.
Definition: Server.h:22
void close()
Stop listener.
Definition: Server.h:91
void add(std::string path, std::shared_ptr< Resource > resource)
Register resource.
Definition: Server.cpp:39
void open()
Start listener.
Definition: Server.h:84
MQTT publishing configuration.
Definition: Configuration.h:14
std::string certificate_authority
Path to CA PEM-file.
std::string uri()
URI Builder.
Signer.
Definition: Signer.h:19
static std::shared_ptr< Object > create(std::shared_ptr< Element > parent, std::string uuid, std::string name, std::string description, std::string ontology="")
Create new Object.
Definition: Object.cpp:18
static DLL boost::posix_time::ptime utc_now(void)
Current Time.
Definition: Time.cpp:57
static std::shared_ptr< Variable > create(std::shared_ptr< Element > parent, std::string uuid, std::string name, std::string description, std::string unit, std::string ontology="", Range< T > range=Range< T >(), TIME time=TIME(), std::string nonce="")
Create new Variable.
Definition: Variable.h:328
UDP Broadcast.
Definition: Broadcast.h:23
void configure(UDP::Configuration config)
Configure Broadcast.
Definition: Broadcast.cpp:132
void send(std::string message)
Send Message.
Definition: Broadcast.cpp:109
UDP Broadcast Configuration.
Definition: Configuration.h:14
std::map< std::string, int > clients
List of clients.
Definition: Configuration.h:28
int main(int argc, char **argv)
Definition: main.cpp:15