Add /status command in http server

This commit is contained in:
offtkp 2023-07-10 13:50:14 +03:00
parent c8f4d41b47
commit 37188edc3b
2 changed files with 70 additions and 27 deletions

View file

@ -1,7 +1,9 @@
#ifdef PANDA3DS_ENABLE_HTTP_SERVER
#pragma once
#include <array>
#include <atomic>
#include <map>
#include <mutex>
#include "helpers.hpp"
@ -16,7 +18,19 @@ struct HttpServer {
std::mutex actionMutex = {};
u32 pendingKey = 0;
HttpServer();
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

View file

@ -2,41 +2,30 @@
#include "httpserver.hpp"
#include <fstream>
#include <map>
#include <string>
#include <thread>
#include <vector>
#include <sstream>
#include "httplib.h"
#include "services/hid.hpp"
u32 stringToKey(const std::string& key_name) {
namespace Keys = HID::Keys;
static std::map<std::string, u32> keyMap = {
{"A", Keys::A},
{"B", Keys::B},
{"Select", Keys::Select},
{"Start", Keys::Start},
{"Right", Keys::Right},
{"Left", Keys::Left},
{"Up", Keys::Up},
{"Down", Keys::Down},
{"R", Keys::R},
{"L", Keys::L},
{"X", Keys::X},
{"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];
HttpServer::HttpServer() : keyMap(
{
{"A", { HID::Keys::A, false } },
{"B", { HID::Keys::B, false } },
{"Select", { HID::Keys::Select, false } },
{"Start", { HID::Keys::Start, false } },
{"Right", { HID::Keys::Right, false } },
{"Left", { HID::Keys::Left, false } },
{"Up", { HID::Keys::Up, false } },
{"Down", { HID::Keys::Down, false } },
{"R", { HID::Keys::R, false } },
{"L", { HID::Keys::L, false } },
{"X", { HID::Keys::X, false } },
{"Y", { HID::Keys::Y, false } },
}
return 0;
}
) {}
void HttpServer::startHttpServer() {
std::thread http_thread([this]() {
@ -70,8 +59,10 @@ void HttpServer::startHttpServer() {
ok = true;
if (value == "1") {
action = HttpAction::PressKey;
setKeyState(keyStr, true);
} else if (value == "0") {
action = HttpAction::ReleaseKey;
setKeyState(keyStr, false);
} else {
// Should not happen but just in case
pendingAction = false;
@ -92,6 +83,10 @@ void HttpServer::startHttpServer() {
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
printf("Starting HTTP server on port 1234\n");
server.listen("localhost", 1234);
@ -100,4 +95,38 @@ void HttpServer::startHttpServer() {
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