2022-08-10 01:56:22 +01:00
|
|
|
mod api;
|
2022-07-30 21:33:31 +01:00
|
|
|
mod persist;
|
|
|
|
mod settings;
|
2022-08-10 01:56:22 +01:00
|
|
|
mod state;
|
2022-07-30 21:33:31 +01:00
|
|
|
|
2022-08-26 22:00:43 +01:00
|
|
|
mod consts;
|
|
|
|
use consts::*;
|
2022-08-10 01:56:22 +01:00
|
|
|
mod resume_worker;
|
2022-11-10 03:09:05 +00:00
|
|
|
//mod save_worker;
|
|
|
|
mod api_worker;
|
2022-08-10 01:56:22 +01:00
|
|
|
mod utility;
|
|
|
|
|
2022-09-01 01:18:15 +01:00
|
|
|
use settings::OnSet;
|
|
|
|
|
2022-08-10 01:56:22 +01:00
|
|
|
use simplelog::{LevelFilter, WriteLogger};
|
2022-07-30 21:33:31 +01:00
|
|
|
|
|
|
|
use usdpl_back::core::serdes::Primitive;
|
2022-08-10 01:56:22 +01:00
|
|
|
use usdpl_back::Instance;
|
2022-07-30 21:33:31 +01:00
|
|
|
|
|
|
|
fn main() -> Result<(), ()> {
|
2022-09-05 22:02:02 +01:00
|
|
|
#[cfg(debug_assertions)]
|
2022-09-05 19:24:01 +01:00
|
|
|
let log_filepath = format!("/home/deck/{}.log", PACKAGE_NAME);
|
2022-09-05 22:02:02 +01:00
|
|
|
#[cfg(not(debug_assertions))]
|
|
|
|
let log_filepath = format!("/tmp/{}.log", PACKAGE_NAME);
|
2022-09-05 19:24:01 +01:00
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
{
|
|
|
|
if std::path::Path::new(&log_filepath).exists() {
|
2022-09-05 22:02:02 +01:00
|
|
|
std::fs::copy(&log_filepath, format!("/home/deck/{}.log.old", PACKAGE_NAME)).unwrap();
|
2022-09-05 19:24:01 +01:00
|
|
|
}
|
|
|
|
}
|
2022-07-30 21:33:31 +01:00
|
|
|
WriteLogger::init(
|
2022-08-10 01:56:22 +01:00
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
{
|
|
|
|
LevelFilter::Debug
|
|
|
|
},
|
|
|
|
#[cfg(not(debug_assertions))]
|
|
|
|
{
|
|
|
|
LevelFilter::Info
|
|
|
|
},
|
2022-07-30 21:33:31 +01:00
|
|
|
Default::default(),
|
2022-08-10 01:56:22 +01:00
|
|
|
std::fs::File::create(&log_filepath).unwrap(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2022-07-30 21:33:31 +01:00
|
|
|
log::info!("Starting back-end ({} v{})", PACKAGE_NAME, PACKAGE_VERSION);
|
|
|
|
println!("Starting back-end ({} v{})", PACKAGE_NAME, PACKAGE_VERSION);
|
2022-08-10 01:56:22 +01:00
|
|
|
|
2022-09-01 01:18:15 +01:00
|
|
|
let mut loaded_settings = persist::SettingsJson::open(utility::settings_dir().join(DEFAULT_SETTINGS_FILE))
|
2022-08-10 01:56:22 +01:00
|
|
|
.map(|settings| settings::Settings::from_json(settings, DEFAULT_SETTINGS_FILE.into()))
|
|
|
|
.unwrap_or_else(|_| settings::Settings::system_default(DEFAULT_SETTINGS_FILE.into()));
|
|
|
|
|
2022-09-01 01:18:15 +01:00
|
|
|
log::debug!("Settings: {:?}", loaded_settings);
|
|
|
|
|
2022-11-10 03:09:05 +00:00
|
|
|
let (api_handler, api_sender) = crate::api::handler::ApiMessageHandler::new();
|
|
|
|
|
|
|
|
//let (_save_handle, save_sender) = save_worker::spawn(loaded_settings.clone());
|
|
|
|
let _resume_handle = resume_worker::spawn(api_sender.clone());
|
2022-08-10 01:56:22 +01:00
|
|
|
|
2022-09-01 01:18:15 +01:00
|
|
|
if let Err(e) = loaded_settings.on_set() {
|
|
|
|
log::error!("Startup Settings.on_set() error: {}", e);
|
|
|
|
}
|
2022-08-10 01:56:22 +01:00
|
|
|
|
2022-11-10 03:09:05 +00:00
|
|
|
let instance = Instance::new(PORT)
|
2022-09-01 01:18:15 +01:00
|
|
|
.register("V_INFO", |_: Vec<Primitive>| {
|
|
|
|
vec![format!("{} v{}", PACKAGE_NAME, PACKAGE_VERSION).into()]
|
2022-08-10 01:56:22 +01:00
|
|
|
})
|
|
|
|
// battery API functions
|
|
|
|
.register("BATTERY_current_now", api::battery::current_now)
|
2022-09-05 22:02:02 +01:00
|
|
|
.register("BATTERY_charge_now", api::battery::charge_now)
|
|
|
|
.register("BATTERY_charge_full", api::battery::charge_full)
|
|
|
|
.register("BATTERY_charge_design", api::battery::charge_design)
|
2022-08-10 01:56:22 +01:00
|
|
|
.register(
|
|
|
|
"BATTERY_set_charge_rate",
|
2022-11-10 03:09:05 +00:00
|
|
|
api::battery::set_charge_rate(api_sender.clone()),
|
2022-08-10 01:56:22 +01:00
|
|
|
)
|
|
|
|
.register(
|
|
|
|
"BATTERY_get_charge_rate",
|
2022-11-10 03:09:05 +00:00
|
|
|
api::battery::get_charge_rate(api_sender.clone()),
|
2022-08-10 01:56:22 +01:00
|
|
|
)
|
|
|
|
.register(
|
|
|
|
"BATTERY_unset_charge_rate",
|
2022-11-10 03:09:05 +00:00
|
|
|
api::battery::unset_charge_rate(api_sender.clone()),
|
2022-08-10 01:56:22 +01:00
|
|
|
)
|
|
|
|
// cpu API functions
|
|
|
|
.register("CPU_count", api::cpu::max_cpus)
|
|
|
|
.register(
|
|
|
|
"CPU_set_online",
|
2022-11-10 03:09:05 +00:00
|
|
|
api::cpu::set_cpu_online(api_sender.clone())
|
2022-08-10 01:56:22 +01:00
|
|
|
)
|
2022-09-06 02:36:01 +01:00
|
|
|
.register(
|
|
|
|
"CPU_set_onlines",
|
2022-11-10 03:09:05 +00:00
|
|
|
api::cpu::set_cpus_online(api_sender.clone())
|
2022-09-06 02:36:01 +01:00
|
|
|
)
|
2022-11-10 03:09:05 +00:00
|
|
|
.register_async(
|
2022-08-23 22:50:46 +01:00
|
|
|
"CPU_get_onlines",
|
2022-11-10 03:09:05 +00:00
|
|
|
api::cpu::get_cpus_online(api_sender.clone())
|
2022-08-10 01:56:22 +01:00
|
|
|
)
|
|
|
|
.register(
|
|
|
|
"CPU_set_clock_limits",
|
2022-11-10 03:09:05 +00:00
|
|
|
api::cpu::set_clock_limits(api_sender.clone())
|
2022-08-10 01:56:22 +01:00
|
|
|
)
|
|
|
|
.register(
|
|
|
|
"CPU_get_clock_limits",
|
2022-11-10 03:09:05 +00:00
|
|
|
api::cpu::get_clock_limits(api_sender.clone())
|
2022-08-10 01:56:22 +01:00
|
|
|
)
|
|
|
|
.register(
|
|
|
|
"CPU_unset_clock_limits",
|
2022-11-10 03:09:05 +00:00
|
|
|
api::cpu::unset_clock_limits(api_sender.clone())
|
2022-08-10 01:56:22 +01:00
|
|
|
)
|
|
|
|
.register(
|
|
|
|
"CPU_set_governor",
|
2022-11-10 03:09:05 +00:00
|
|
|
api::cpu::set_cpu_governor(api_sender.clone())
|
|
|
|
)
|
|
|
|
.register(
|
|
|
|
"CPU_set_governors",
|
|
|
|
api::cpu::set_cpus_governors(api_sender.clone())
|
2022-08-10 01:56:22 +01:00
|
|
|
)
|
|
|
|
.register(
|
|
|
|
"CPU_get_governors",
|
2022-11-10 03:09:05 +00:00
|
|
|
api::cpu::get_cpu_governors(api_sender.clone())
|
2022-08-10 01:56:22 +01:00
|
|
|
)
|
|
|
|
// gpu API functions
|
|
|
|
.register(
|
|
|
|
"GPU_set_ppt",
|
2022-11-10 03:09:05 +00:00
|
|
|
api::gpu::set_ppt(api_sender.clone())
|
2022-08-10 01:56:22 +01:00
|
|
|
)
|
2022-11-10 03:09:05 +00:00
|
|
|
.register_async(
|
2022-08-10 01:56:22 +01:00
|
|
|
"GPU_get_ppt",
|
2022-11-10 03:09:05 +00:00
|
|
|
api::gpu::get_ppt(api_sender.clone())
|
2022-08-10 01:56:22 +01:00
|
|
|
)
|
|
|
|
.register(
|
|
|
|
"GPU_unset_ppt",
|
2022-11-10 03:09:05 +00:00
|
|
|
api::gpu::unset_ppt(api_sender.clone())
|
2022-08-10 01:56:22 +01:00
|
|
|
)
|
|
|
|
.register(
|
|
|
|
"GPU_set_clock_limits",
|
2022-11-10 03:09:05 +00:00
|
|
|
api::gpu::set_clock_limits(api_sender.clone())
|
2022-08-10 01:56:22 +01:00
|
|
|
)
|
2022-11-10 03:09:05 +00:00
|
|
|
.register_async(
|
2022-08-10 01:56:22 +01:00
|
|
|
"GPU_get_clock_limits",
|
2022-11-10 03:09:05 +00:00
|
|
|
api::gpu::get_clock_limits(api_sender.clone())
|
2022-08-10 01:56:22 +01:00
|
|
|
)
|
|
|
|
.register(
|
|
|
|
"GPU_unset_clock_limits",
|
2022-11-10 03:09:05 +00:00
|
|
|
api::gpu::unset_clock_limits(api_sender.clone())
|
2022-08-10 01:56:22 +01:00
|
|
|
)
|
|
|
|
.register(
|
|
|
|
"GPU_set_slow_memory",
|
2022-11-10 03:09:05 +00:00
|
|
|
api::gpu::set_slow_memory(api_sender.clone())
|
2022-08-10 01:56:22 +01:00
|
|
|
)
|
2022-11-10 03:09:05 +00:00
|
|
|
.register_async(
|
2022-08-10 01:56:22 +01:00
|
|
|
"GPU_get_slow_memory",
|
2022-11-10 03:09:05 +00:00
|
|
|
api::gpu::get_slow_memory(api_sender.clone())
|
2022-08-10 01:56:22 +01:00
|
|
|
)
|
2022-08-26 22:00:43 +01:00
|
|
|
// general API functions
|
|
|
|
.register(
|
|
|
|
"GENERAL_set_persistent",
|
2022-11-10 03:09:05 +00:00
|
|
|
api::general::set_persistent(api_sender.clone())
|
2022-08-26 22:00:43 +01:00
|
|
|
)
|
|
|
|
.register(
|
|
|
|
"GENERAL_get_persistent",
|
2022-11-10 03:09:05 +00:00
|
|
|
api::general::get_persistent(api_sender.clone())
|
2022-08-26 22:00:43 +01:00
|
|
|
)
|
|
|
|
.register(
|
|
|
|
"GENERAL_load_settings",
|
2022-11-10 03:09:05 +00:00
|
|
|
api::general::load_settings(api_sender.clone())
|
2022-09-01 01:18:15 +01:00
|
|
|
)
|
|
|
|
.register(
|
|
|
|
"GENERAL_load_default_settings",
|
2022-11-10 03:09:05 +00:00
|
|
|
api::general::load_default_settings(api_sender.clone())
|
2022-08-26 22:00:43 +01:00
|
|
|
)
|
|
|
|
.register(
|
2022-11-10 03:09:05 +00:00
|
|
|
"GENERAL_load_system_settings",
|
|
|
|
api::general::load_system_settings(api_sender.clone())
|
|
|
|
)
|
|
|
|
.register_async(
|
2022-08-26 22:00:43 +01:00
|
|
|
"GENERAL_get_name",
|
2022-11-10 03:09:05 +00:00
|
|
|
api::general::get_name(api_sender.clone())
|
2022-09-01 01:18:15 +01:00
|
|
|
)
|
2022-11-10 03:09:05 +00:00
|
|
|
.register_async(
|
2022-09-01 01:18:15 +01:00
|
|
|
"GENERAL_wait_for_unlocks",
|
2022-11-10 03:09:05 +00:00
|
|
|
api::general::lock_unlock_all(api_sender.clone())
|
|
|
|
);
|
|
|
|
|
|
|
|
api_worker::spawn(loaded_settings, api_handler);
|
|
|
|
|
|
|
|
instance
|
2022-07-30 21:33:31 +01:00
|
|
|
.run_blocking()
|
|
|
|
}
|