-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |