forked from Piorosen/SSvJoy-PC
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
75 lines (65 loc) · 3.05 KB
/
main.cpp
File metadata and controls
75 lines (65 loc) · 3.05 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
74
75
#include <drogon/drogon.h>
#include <thread>
#include <serial/serial.h>
#include <serial/builder.h>
#include <spdlog/spdlog.h>
#include <chrono>
#include <cxxopts.hpp>
using namespace drogon;
int main(int argc, char** argv){
cxxopts::Options options("SSvJoy", "HTTP 통신으로 당신의 조이스틱을 제어합니다.");
options.add_options()
("c,com", "Com Port", cxxopts::value<std::string>())
("b,baudrate", "Baud Rate", cxxopts::value<int>()->default_value("115200"))
;
serial s;
try {
auto result = options.parse(argc, argv);
spdlog::info("open: {}", s.open(result["baudrate"].as<int>(), result["com"].as<std::string>()));
}catch (std::exception e) {
spdlog::error("{}", e.what());
spdlog::error("{}", options.help());
return 0;
}
spdlog::info("Test cURL CODE : \"curl --location http://127.0.0.1:80/serial --header Content-Type:application/json --data {\\\"type\\\":3,\\\"id\\\":1,\\\"value\\\":1023}\"");
// STX TYPE ID DATA01 02 03 04 CHK ETX
app().setLogPath("./")
.setLogLevel(trantor::Logger::kWarn)
.addListener("0.0.0.0", 80)
.setThreadNum(1) // 단일 쓰레드로 통해, 데이터 오류 발생을 최소화함.
.registerHandler("/serial",
[&s](const HttpRequestPtr& req,
std::function<void (const HttpResponsePtr &)> &&callback)
{
try {
spdlog::info("{}", req->body());
auto obj = req->jsonObject();
int type = obj->get("type", 0).asInt();
int id = obj->get("id", 0).asInt();
int value = obj->get("value", 0).asInt();
auto data = builder()
.setType((Type)type)
->setButtonId((byte)id)
->setValue(value)
->build();
s.write(data.data(), data.size());
Json::Value json;
json["result"] = "ok";
json["message"] = s.read();
json["body"] = std::string((char*)data.data());
auto resp=HttpResponse::newHttpJsonResponse(json);
callback(resp);
}catch (std::exception e){
spdlog::error("{}", e.what());
Json::Value json;
json["result"] = "error";
json["message"] = e.what();
auto resp=HttpResponse::newHttpJsonResponse(json);
callback(resp);
}
},
{Post})
.run();
s.close();
return 0;
}