-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
110 lines (98 loc) · 3.67 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <ros/ros.h>
#include <ros/package.h>
#include <crow.h>
#include <unordered_set>
#include <mutex>
std::string get_mime_type(const std::string &path)
{
std::size_t last_dot = path.find_last_of(".");
std::string extension = path.substr(last_dot + 1);
if (extension != "")
{
std::string mimeType = crow::mime_types.at(extension);
if (mimeType != "")
return mimeType;
else
return "text/plain";
}
return "";
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "robot_gui");
ros::NodeHandle nh;
ROS_INFO("Starting the Robot GUI..");
if (ros::console::set_logger_level(ROSCONSOLE_DEFAULT_NAME, ros::console::levels::Debug))
ros::console::notifyLoggerLevelsChanged();
std::string host;
if (ros::param::has("~gui_host"))
ros::param::get("~gui_host", host);
else
host = "127.0.0.1";
int port;
if (ros::param::has("~gui_port"))
ros::param::get("~gui_port", port);
else
port = 8080;
std::mutex mtx;
std::unordered_set<crow::websocket::connection *> users;
const std::string templates_path = ros::package::getPath("robot_gui") + "/templates/";
const std::string static_path = ros::package::getPath("robot_gui") + "/static/";
crow::SimpleApp app;
CROW_ROUTE(app, "/")
([templates_path]()
{
crow::mustache::context ctx;
ctx["title"] = "Ohmni";
crow::mustache::set_base(templates_path);
const auto test = crow::mustache::load("index.html").render(ctx);
return crow::mustache::load("index.html").render(ctx); });
CROW_ROUTE(app, "/static/<string>")
([static_path](crow::response &res, std::string path)
{
std::ifstream ifl(static_path + path);
std::ostringstream buffer;
buffer << ifl.rdbuf();
res.body = buffer.str();
res.add_header("Content-length", std::to_string(res.body.size()));
std::string mimeType = get_mime_type(path);
if (mimeType != "")
res.add_header("Content-Type", mimeType);
res.end(); });
CROW_ROUTE(app, "/static/faces/<string>")
([static_path](crow::response &res, std::string path)
{
std::ifstream ifl(static_path + "faces/" + path);
std::ostringstream buffer;
buffer << ifl.rdbuf();
res.body = buffer.str();
res.add_header("Content-length", std::to_string(res.body.size()));
std::string mimeType = get_mime_type(path);
if (mimeType != "")
res.add_header("Content-Type", mimeType);
res.end(); });
CROW_ROUTE(app, "/solver")
.websocket()
.onopen([&](crow::websocket::connection &conn)
{ std::lock_guard<std::mutex> _(mtx);
users.insert(&conn); })
.onclose([&](crow::websocket::connection &conn, const std::string &reason)
{ std::lock_guard<std::mutex> _(mtx);
users.erase(&conn); });
ROS_INFO("Starting GUI Server on host %s with port %d..", host.c_str(), port);
auto srv_st = std::async(std::launch::async, [&]
{ app.bindaddr(host).port(port).run(); });
app.wait_for_server_start();
ROS_INFO("GUI Server is now running..");
bool is_happy = true;
ros::Rate loop_rate(0.2);
while (ros::ok())
{
ros::spinOnce();
std::string msg = crow::json::wvalue({{"type", "show_face"}, {"facial_expression", is_happy ? "happy" : "idle"}}).dump();
for (const auto &conn : users)
conn->send_text(msg);
is_happy = !is_happy;
loop_rate.sleep();
}
}