136 lines
3.3 KiB
C++
136 lines
3.3 KiB
C++
#pragma once
|
|
#include <functional>
|
|
#include <future>
|
|
#include <httplib.h>
|
|
#include <memory>
|
|
#include <utility>
|
|
|
|
#include "data_types.h"
|
|
#include "request.h"
|
|
#include "utils.h"
|
|
|
|
#define SIMPLE_GET_TASK(name, api, type) \
|
|
class name : public get_task<type> { \
|
|
public: \
|
|
explicit name() : get_task(api) {} \
|
|
void setup() {} \
|
|
};
|
|
|
|
template<typename T>
|
|
class task {
|
|
public:
|
|
using result_type = T;
|
|
virtual ~task() = default;
|
|
|
|
explicit task(const char* api) : api_(api) {}
|
|
std::future<T> async_process() {
|
|
return std::async(std::launch::async, [this] {
|
|
return process();
|
|
});
|
|
}
|
|
|
|
[[nodiscard]] const char* api() const {
|
|
return api_;
|
|
}
|
|
protected:
|
|
virtual T process() = 0;
|
|
|
|
const char* api_;
|
|
};
|
|
|
|
template<typename T>
|
|
class task_executor {
|
|
public:
|
|
template<typename ...Args>
|
|
static auto run_task(Args&&... args) {
|
|
static T task_instance;
|
|
task_instance.setup(std::forward<Args>(args)...);
|
|
return task_instance.async_process();
|
|
}
|
|
};
|
|
|
|
template<typename T>
|
|
class post_task : public task<T> {
|
|
public:
|
|
explicit post_task(const char* api) : task<T>(api) {
|
|
}
|
|
|
|
void set_params(const httplib::Params& params) {
|
|
params_ = params;
|
|
}
|
|
|
|
protected:
|
|
T process() override {
|
|
T result;
|
|
post_request(task<T>::api(), params_, result);
|
|
return result;
|
|
}
|
|
httplib::Params params_;
|
|
};
|
|
|
|
template<>
|
|
inline void post_task<void>::process() {
|
|
post_request(api(), params_);
|
|
}
|
|
|
|
template<typename T>
|
|
class get_task : public task<T> {
|
|
public:
|
|
explicit get_task(const char* api) : task<T>(api) {
|
|
}
|
|
|
|
protected:
|
|
T process() override {
|
|
T result;
|
|
get_request(task<T>::api(), result);
|
|
return result;
|
|
}
|
|
};
|
|
|
|
class login_task : public post_task<session_data> {
|
|
public:
|
|
login_task() : post_task("WEBSES/create") {}
|
|
|
|
void setup(const std::string& ip, const char* username, const char* password) {
|
|
ip_ = ip;
|
|
httplib::Params params;
|
|
params.emplace("WEBVAR_USERNAME", encode_content(username));
|
|
params.emplace("WEBVAR_PASSWORD", encode_content(password));
|
|
set_params(params);
|
|
}
|
|
protected:
|
|
session_data process() override {
|
|
delete cli;
|
|
headers.clear();
|
|
headers.emplace("Content-Type", "application/json;charset=UTF-8");
|
|
|
|
cli = new httplib::SSLClient(ip_); // host
|
|
cli->enable_server_certificate_verification(false);
|
|
|
|
auto result = post_task::process();
|
|
|
|
headers.emplace("X-CSRFTOKEN", result.csrf_token);
|
|
headers.emplace("Cookie", "test=1; SessionExpired=false; SessionCookie=" + result.session_cookie + "; BMP_IP_ADDR=" + result.bmc_ip + "; ");
|
|
return result;
|
|
}
|
|
private:
|
|
std::string ip_;
|
|
};
|
|
|
|
class set_fan_speed_task : public post_task<void> {
|
|
public:
|
|
set_fan_speed_task() : post_task("setfanspeed") {}
|
|
void setup(int fan_id, int speed_percent) {
|
|
httplib::Params params;
|
|
params.emplace("ID", std::to_string(fan_id));
|
|
params.emplace("PERCENT", std::to_string(speed_percent));
|
|
set_params(params);
|
|
}
|
|
private:
|
|
|
|
};
|
|
|
|
SIMPLE_GET_TASK(get_fan_info_task, "getfaninfo", std::vector<fan_info>)
|
|
SIMPLE_GET_TASK(get_running_time_task, "getrunningtime", running_time)
|
|
SIMPLE_GET_TASK(get_psu_info_task, "getallpsuinfo", std::vector<psu_status>)
|