kirdy/src/net/cmd_handler.rs

126 lines
3.9 KiB
Rust
Raw Normal View History

use core::fmt::Debug;
use miniconf::{Error, JsonCoreSlash, Tree, TreeKey};
use serde::{Deserialize, Serialize};
use uom::si::electric_current::{milliampere, ElectricCurrent};
use crate::laser_diode::laser_diode::LdDrive;
use log::info;
#[derive(Deserialize, Serialize, Copy, Clone, Default, Debug)]
enum LdCmdEnum {
#[default]
Reserved,
// LD Drive Related
PowerUp,
PowerDown,
LdTermsShort,
LdTermsOpen,
SetI,
SetISoftLimit,
// PD Mon Related
SetPdResponsitivity,
SetPdDarkCurrent,
SetPdILimit,
SetLdPwrLimit,
ClearAlarmStatus,
// Report Related
GetModInTermStatus,
GetLdStatus,
GetAlramStatus,
}
#[derive(Deserialize, Serialize, Copy, Clone, Debug, Default, Tree)]
pub struct CmdJsonObj{
rev: u8,
laser_diode_cmd: LdCmdEnum,
data_f32: Option<f32>,
data_f64: Option<f64>,
}
#[derive(Deserialize, Serialize, Copy, Clone, Debug, Default, Tree)]
pub struct Cmd {
json: CmdJsonObj
}
pub fn execute_cmd(buffer: &mut [u8], buffer_size: usize, mut laser: LdDrive)->(LdDrive){
let mut cmd = Cmd {
json: CmdJsonObj::default()
};
match cmd.set_json("/json", &buffer[0..buffer_size]){
Ok(_) => {
info!("############ Command Received {:?}", cmd.json.laser_diode_cmd);
match cmd.json.laser_diode_cmd {
LdCmdEnum::PowerUp => {
laser.power_up()
}
LdCmdEnum::PowerDown => {
laser.power_down()
}
LdCmdEnum::LdTermsShort => {
laser.ld_short();
}
LdCmdEnum::LdTermsOpen => {
laser.ld_open();
}
LdCmdEnum::SetI => {
match cmd.json.data_f64 {
Some(val) => {
laser.ld_set_i(ElectricCurrent::new::<milliampere>(val));
}
None => {
info!("Wrong Data type is received")
}
}
}
LdCmdEnum::SetISoftLimit => {
match cmd.json.data_f64 {
Some(val) => {
laser.set_ld_drive_current_limit(ElectricCurrent::new::<milliampere>(val))
}
None => {
info!("Wrong Data type is received")
}
}
}
LdCmdEnum::SetPdResponsitivity => {
info!("Not supported Yet")
}
LdCmdEnum::SetPdDarkCurrent => {
info!("Not supported Yet")
}
LdCmdEnum::SetPdILimit => {
match cmd.json.data_f64 {
Some(val) => {
laser.set_pd_i_limit(ElectricCurrent::new::<milliampere>(val))
}
None => {
info!("Wrong Data type is received")
}
}
}
LdCmdEnum::SetLdPwrLimit => {
info!("Not supported Yet")
}
LdCmdEnum::ClearAlarmStatus => {
laser.pd_mon_clear_alarm()
}
LdCmdEnum::GetModInTermStatus => {
info!("Not supported Yet")
}
LdCmdEnum::GetLdStatus => {
info!("Not supported Yet")
}
LdCmdEnum::GetAlramStatus => {
info!("Not supported Yet")
}
_ => {
info!("Unimplemented Command")
}
}
}
Err(err) => {
info!("Invalid Command: {:?}", err);
}
}
laser
}