94 lines
2.5 KiB
Rust
Executable File
94 lines
2.5 KiB
Rust
Executable File
use std::{collections::HashMap, path::PathBuf};
|
|
|
|
use bevy::ecs::system::Resource;
|
|
use guardian_core::{
|
|
components::{GrpcBaseUrl, SrsSocketAddr, SttBaseUrl},
|
|
srs::Modulation as SrsModulation,
|
|
};
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
#[derive(Resource, Clone, Debug, Deserialize, Serialize, Default)]
|
|
pub struct Channels(pub HashMap<String, ChannelConfig>);
|
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize, Default)]
|
|
pub struct Config {
|
|
pub srs: SrsConfig,
|
|
pub grpc: GrpcBaseUrl,
|
|
pub stt: SttBaseUrl,
|
|
pub channels: Channels,
|
|
}
|
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize, Default)]
|
|
pub struct SrsConfig {
|
|
pub server: SrsSocketAddr,
|
|
}
|
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize, Default)]
|
|
pub struct ChannelConfig {
|
|
pub frequency: u64,
|
|
pub modulation: Modulation, // 0=AM, 1=FM?
|
|
}
|
|
|
|
impl TryFrom<PathBuf> for Config {
|
|
type Error = Box<dyn std::error::Error + Send + Sync + 'static>;
|
|
|
|
fn try_from(value: PathBuf) -> Result<Self, Self::Error> {
|
|
use std::io::Read;
|
|
let mut config = std::fs::File::open(value)?;
|
|
let mut config_str = String::new();
|
|
config.read_to_string(&mut config_str)?;
|
|
Ok(toml::from_str(&config_str)?)
|
|
}
|
|
}
|
|
|
|
impl Config {
|
|
pub fn load(file: PathBuf) -> Result<Self, Box<dyn std::error::Error + Send + Sync + 'static>> {
|
|
if file.exists() {
|
|
return file.try_into();
|
|
}
|
|
|
|
use std::io::Write;
|
|
|
|
let mut defaults = Config::default();
|
|
|
|
defaults.channels.0.insert(
|
|
"Overlord1-1".to_string(),
|
|
ChannelConfig {
|
|
frequency: 251000000,
|
|
modulation: Modulation::Am,
|
|
},
|
|
);
|
|
|
|
// write the file
|
|
std::fs::File::create(file)?.write_all(toml::to_string_pretty(&defaults)?.as_bytes())?;
|
|
|
|
Ok(defaults)
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, Deserialize, Serialize, Default)]
|
|
pub enum Modulation {
|
|
#[default]
|
|
Am,
|
|
Fm,
|
|
Intercom,
|
|
Disabled,
|
|
HaveQuick,
|
|
Satcom,
|
|
Mids,
|
|
}
|
|
|
|
impl From<Modulation> for SrsModulation {
|
|
fn from(value: Modulation) -> Self {
|
|
match value {
|
|
Modulation::Am => SrsModulation::Am,
|
|
Modulation::Fm => SrsModulation::Fm,
|
|
Modulation::Intercom => SrsModulation::Intercom,
|
|
Modulation::Disabled => SrsModulation::Disabled,
|
|
Modulation::HaveQuick => SrsModulation::HaveQuick,
|
|
Modulation::Satcom => SrsModulation::Satcom,
|
|
Modulation::Mids => SrsModulation::Mids,
|
|
}
|
|
}
|
|
}
|