forked from spadival/headunit
-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathcommand_server.cpp
More file actions
73 lines (57 loc) · 1.93 KB
/
command_server.cpp
File metadata and controls
73 lines (57 loc) · 1.93 KB
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
#include "command_server.h"
#include "json/json.hpp"
#include "hu_uti.h"
using json = nlohmann::json;
namespace
{
void AddCORSHeaders(WPP::Response& resp)
{
resp.headers.emplace("Access-Control-Allow-Origin", "*");
resp.headers.emplace("Access-Control-Allow-Methods", "POST, GET");
}
}
CommandServer::CommandServer(ICommandServerCallbacks &callbacks)
{
server.get("/status", [&callbacks](WPP::Request& req, WPP::Response& resp)
{
resp.type = "application/json";
json result;
result["connected"] = callbacks.IsConnected();
result["videoFocus"] = callbacks.HasVideoFocus();
result["audioFocus"] = callbacks.HasAudioFocus();
std::string logPath = callbacks.GetLogPath();
if (logPath.length() > 0)
{
result["logPath"] = logPath;
}
result["headunitVersion"] = callbacks.GetVersion();
resp.body << std::setw(4) << result;
logd("Got /status call. response:\n%s\n", resp.body.str().c_str());
AddCORSHeaders(resp);
});
server.get("/updateConfig", [&callbacks](WPP::Request& req, WPP::Response& resp)
{
resp.type = "application/json";
json result;
result["result"] = callbacks.ChangeParameterConfig(req.query["parameter"], req.query["value"], req.query["type"]);
resp.body << std::setw(4) << result;
logd("Got /updateConfig call. response:\n%s\n", resp.body.str().c_str());
AddCORSHeaders(resp);
});
server.post("/takeVideoFocus", [&callbacks](WPP::Request& req, WPP::Response& resp)
{
resp.type = "application/json";
callbacks.TakeVideoFocus();
json result;
resp.body << std::setw(4) << result;
printf("Got /takeVideoFocus call. response:\n%s\n", resp.body.str().c_str());
AddCORSHeaders(resp);
});
}
bool CommandServer::Start()
{
return server.start(9999);
}
void CommandServer::Stop(){
server.stop();
}