Skip to content

Commit

Permalink
feat: Device Config
Browse files Browse the repository at this point in the history
  • Loading branch information
dongwlin committed Nov 18, 2023
1 parent f316887 commit 68d7ad7
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 0 deletions.
2 changes: 2 additions & 0 deletions source/cli/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ add_executable(MAABH3_CLI
Config/Config.cpp
Config/ControlConfig.h
Config/ControlConfig.cpp
Config/DeviceConfig.h
Config/DeviceConfig.cpp
)
target_link_libraries(MAABH3_CLI PRIVATE MaaFramework MaaToolKit)

Expand Down
92 changes: 92 additions & 0 deletions source/cli/Config/DeviceConfig.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#include "DeviceConfig.h"

DeviceConfig::DeviceConfig()
{
build_target_path();
}
bool DeviceConfig::parse(const json::value& config_opt)
{
_config_adb = config_opt.get("adb", _config_adb);
_config_device_name = config_opt.get("device_name", _config_device_name);
_config_device_SN = config_opt.get("device_SN", _config_device_SN);

return true;
}
bool DeviceConfig::load()
{
auto config_opt = json::open(_target_path);
if (!config_opt) {
std::cerr << "Failed to open device config file: " << _target_path << std::endl;
return false;
}

auto& config = *config_opt;
if (!config.is_object()) {
std::cerr << "Json is not an object: " << config << std::endl;
return false;
}

if (!parse(config)) {
std::cerr << "Failed to parse control: " << config << std::endl;
return false;
}

return true;
}

json::value DeviceConfig::to_json()
{
json::value root = { { "device_name", _config_device_name },
{ "device_SN", _config_device_SN },
{ "adb", _config_adb } };

return root;
}

bool DeviceConfig::save(const json::value& root)
{
std::filesystem::create_directories(config_dir);
std::ofstream ofs(_target_path, std::ios::out);
if (!ofs.is_open()) {
std::cerr << "Failed to open device config file: " << _target_path << std::endl;
return false;
}
ofs << root;
ofs.close();
return true;
}

void DeviceConfig::set_config_device_name(std::string name)
{
_config_device_name = name;
}

void DeviceConfig::set_config_device_SN(std::string SN)
{
_config_device_SN = SN;
}

void DeviceConfig::set_config_adb(std::string adb)
{
_config_adb = adb;
}

std::string DeviceConfig::get_config_device_name()
{
return _config_device_name;
}

std::string DeviceConfig::get_config_device_SN()
{
return _config_device_SN;
}

std::string DeviceConfig::get_config_adb()
{
return _config_adb;
}

void DeviceConfig::build_target_path()
{
_target_path = config_dir / _target;
}
45 changes: 45 additions & 0 deletions source/cli/Config/DeviceConfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#pragma once

#include <filesystem>
#include <iostream>
#include <fstream>

#include "Config.h"

class DeviceConfig : public Config
{
public:
DeviceConfig();
virtual ~DeviceConfig() = default;

public:
inline static const std::filesystem::path local_dir = ".";
inline static const std::filesystem::path config_dir = local_dir / "config" / "device";

public:
bool parse(const json::value& config_opt) override;
bool load() override;
json::value to_json() override;
bool save(const json::value& root) override;

public:
void set_config_device_name(std::string name);
void set_config_device_SN(std::string SN);
void set_config_adb(std::string adb);

public:
std::string get_config_device_name();
std::string get_config_device_SN();
std::string get_config_adb();

private:
void build_target_path() override;

private:
std::string _config_device_name;
std::string _config_device_SN;
std::string _config_adb;

private:
std::filesystem::path _target_path;
};

0 comments on commit 68d7ad7

Please sign in to comment.