Merge pull request #97 from OFFTKP/master

Add /status command in http server
This commit is contained in:
wheremyfoodat 2023-07-10 14:41:02 +03:00 committed by GitHub
commit 5f9db9c1cd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 70 additions and 27 deletions

View file

@ -1,7 +1,9 @@
#ifdef PANDA3DS_ENABLE_HTTP_SERVER #ifdef PANDA3DS_ENABLE_HTTP_SERVER
#pragma once #pragma once
#include <array>
#include <atomic> #include <atomic>
#include <map>
#include <mutex> #include <mutex>
#include "helpers.hpp" #include "helpers.hpp"
@ -16,7 +18,19 @@ struct HttpServer {
std::mutex actionMutex = {}; std::mutex actionMutex = {};
u32 pendingKey = 0; u32 pendingKey = 0;
HttpServer();
void startHttpServer(); void startHttpServer();
std::string status();
private:
std::map<std::string, std::pair<u32, bool>> keyMap;
std::array<bool, 12> pressedKeys = {};
bool paused = false;
u32 stringToKey(const std::string& key_name);
bool getKeyState(const std::string& key_name);
void setKeyState(const std::string& key_name, bool state);
}; };
#endif // PANDA3DS_ENABLE_HTTP_SERVER #endif // PANDA3DS_ENABLE_HTTP_SERVER

View file

@ -2,41 +2,30 @@
#include "httpserver.hpp" #include "httpserver.hpp"
#include <fstream> #include <fstream>
#include <map>
#include <string> #include <string>
#include <thread> #include <thread>
#include <vector> #include <vector>
#include <sstream>
#include "httplib.h" #include "httplib.h"
#include "services/hid.hpp" #include "services/hid.hpp"
u32 stringToKey(const std::string& key_name) { HttpServer::HttpServer() : keyMap(
namespace Keys = HID::Keys; {
static std::map<std::string, u32> keyMap = { {"A", { HID::Keys::A, false } },
{"A", Keys::A}, {"B", { HID::Keys::B, false } },
{"B", Keys::B}, {"Select", { HID::Keys::Select, false } },
{"Select", Keys::Select}, {"Start", { HID::Keys::Start, false } },
{"Start", Keys::Start}, {"Right", { HID::Keys::Right, false } },
{"Right", Keys::Right}, {"Left", { HID::Keys::Left, false } },
{"Left", Keys::Left}, {"Up", { HID::Keys::Up, false } },
{"Up", Keys::Up}, {"Down", { HID::Keys::Down, false } },
{"Down", Keys::Down}, {"R", { HID::Keys::R, false } },
{"R", Keys::R}, {"L", { HID::Keys::L, false } },
{"L", Keys::L}, {"X", { HID::Keys::X, false } },
{"X", Keys::X}, {"Y", { HID::Keys::Y, false } },
{"Y", Keys::Y},
{"CirclePadRight", Keys::CirclePadRight},
{"CirclePadLeft", Keys::CirclePadLeft},
{"CirclePadUp", Keys::CirclePadUp},
{"CirclePadDown", Keys::CirclePadDown},
};
if (keyMap.find(key_name) != keyMap.end()) {
return keyMap[key_name];
} }
) {}
return 0;
}
void HttpServer::startHttpServer() { void HttpServer::startHttpServer() {
std::thread http_thread([this]() { std::thread http_thread([this]() {
@ -70,8 +59,10 @@ void HttpServer::startHttpServer() {
ok = true; ok = true;
if (value == "1") { if (value == "1") {
action = HttpAction::PressKey; action = HttpAction::PressKey;
setKeyState(keyStr, true);
} else if (value == "0") { } else if (value == "0") {
action = HttpAction::ReleaseKey; action = HttpAction::ReleaseKey;
setKeyState(keyStr, false);
} else { } else {
// Should not happen but just in case // Should not happen but just in case
pendingAction = false; pendingAction = false;
@ -92,6 +83,10 @@ void HttpServer::startHttpServer() {
response.set_content("ok", "text/plain"); response.set_content("ok", "text/plain");
}); });
server.Get("/status", [this](const httplib::Request&, httplib::Response& response) {
response.set_content(status(), "text/plain");
});
// TODO: ability to specify host and port // TODO: ability to specify host and port
printf("Starting HTTP server on port 1234\n"); printf("Starting HTTP server on port 1234\n");
server.listen("localhost", 1234); server.listen("localhost", 1234);
@ -100,4 +95,38 @@ void HttpServer::startHttpServer() {
http_thread.detach(); http_thread.detach();
} }
std::string HttpServer::status() {
std::stringstream stringStream;
stringStream << "Panda3DS\n";
stringStream << "Status: " << (paused ? "Paused" : "Running") << "\n";
for (auto& [keyStr, value] : keyMap) {
stringStream << keyStr << ": " << value.second << "\n";
}
return stringStream.str();
}
u32 HttpServer::stringToKey(const std::string& key_name) {
if (keyMap.find(key_name) != keyMap.end()) {
return keyMap[key_name].first;
}
return 0;
}
bool HttpServer::getKeyState(const std::string& key_name) {
if (keyMap.find(key_name) != keyMap.end()) {
return keyMap[key_name].second;
}
return false;
}
void HttpServer::setKeyState(const std::string& key_name, bool state) {
if (keyMap.find(key_name) != keyMap.end()) {
keyMap[key_name].second = state;
}
}
#endif // PANDA3DS_ENABLE_HTTP_SERVER #endif // PANDA3DS_ENABLE_HTTP_SERVER