kirdy/src/net/cmd_handler.rs

259 lines
8.6 KiB
Rust
Raw Normal View History

use core::fmt::Debug;
2024-02-05 15:14:23 +08:00
use miniconf::{JsonCoreSlash, Tree};
use serde::{Deserialize, Serialize};
2024-02-05 15:14:23 +08:00
use uom::si::{
electric_current::{ampere, milliampere, ElectricCurrent},
electric_potential::{volt, ElectricPotential}
};
use crate::laser_diode::laser_diode::LdDrive;
2024-02-05 15:14:23 +08:00
use crate::thermostat::thermostat::Thermostat;
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,
}
2024-02-05 15:14:23 +08:00
#[derive(Deserialize, Serialize, Copy, Clone, Default, Debug)]
enum ThermostatCmdEnum {
#[default]
Reserved,
PowerUp,
PowerDown,
// TEC
SetTecMaxV,
SetTecMaxIPos,
SetTecMaxINeg,
SetTecIOut, // Constant Current Mode
// Control MOde
SetConstantCurrentMode,
SetPidControlMode,
// PID
SetPidKp,
SetPidKi,
SetPidKd,
SetPidOutMin,
SetPidOutMax,
SetPidUpdateInterval, // Update Interval is set based on the sampling rate of ADC
// Steinhart-Hart Equation
SetShT0,
SetShR0,
SetShBeta,
// Report Related
GetTecStatus,
GetPidStatus,
GetShParams,
}
#[derive(Deserialize, Serialize, Copy, Clone, Debug, Default, Tree)]
pub struct CmdJsonObj{
rev: u8,
2024-02-05 15:14:23 +08:00
laser_diode_cmd: Option<LdCmdEnum>,
thermostat_cmd: Option<ThermostatCmdEnum>,
data_f32: Option<f32>,
data_f64: Option<f64>,
}
#[derive(Deserialize, Serialize, Copy, Clone, Debug, Default, Tree)]
pub struct Cmd {
json: CmdJsonObj
}
2024-02-05 15:14:23 +08:00
pub fn execute_cmd(buffer: &mut [u8], buffer_size: usize, mut laser: LdDrive, mut tec: Thermostat)->(LdDrive, Thermostat){
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 {
2024-02-05 15:14:23 +08:00
Some(LdCmdEnum::PowerUp) => {
laser.power_up()
}
2024-02-05 15:14:23 +08:00
Some(LdCmdEnum::PowerDown) => {
laser.power_down()
}
2024-02-05 15:14:23 +08:00
Some(LdCmdEnum::LdTermsShort) => {
laser.ld_short();
}
2024-02-05 15:14:23 +08:00
Some(LdCmdEnum::LdTermsOpen) => {
laser.ld_open();
}
2024-02-05 15:14:23 +08:00
Some(LdCmdEnum::SetI) => {
match cmd.json.data_f64 {
Some(val) => {
laser.ld_set_i(ElectricCurrent::new::<milliampere>(val));
}
None => {
info!("Wrong Data type is received")
}
}
}
2024-02-05 15:14:23 +08:00
Some(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")
}
}
}
2024-02-05 15:14:23 +08:00
Some(LdCmdEnum::SetPdResponsitivity) => {
info!("Not supported Yet")
}
2024-02-05 15:14:23 +08:00
Some(LdCmdEnum::SetPdDarkCurrent) => {
info!("Not supported Yet")
}
2024-02-05 15:14:23 +08:00
Some(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")
}
}
}
2024-02-05 15:14:23 +08:00
Some(LdCmdEnum::SetLdPwrLimit) => {
info!("Not supported Yet")
}
2024-02-05 15:14:23 +08:00
Some(LdCmdEnum::ClearAlarmStatus) => {
laser.pd_mon_clear_alarm()
}
2024-02-05 15:14:23 +08:00
Some(LdCmdEnum::GetModInTermStatus) => {
info!("Not supported Yet")
}
2024-02-05 15:14:23 +08:00
Some(LdCmdEnum::GetLdStatus) => {
info!("Not supported Yet")
}
2024-02-05 15:14:23 +08:00
Some(LdCmdEnum::GetAlramStatus) => {
info!("Not supported Yet")
}
2024-02-05 15:14:23 +08:00
None => { /* Do Nothing*/ }
_ => {
info!("Unimplemented Command")
}
}
2024-02-05 15:14:23 +08:00
match cmd.json.thermostat_cmd {
Some(ThermostatCmdEnum::PowerUp) => {
tec.power_up()
}
Some(ThermostatCmdEnum::PowerDown) => {
tec.power_down()
}
Some(ThermostatCmdEnum::SetTecMaxV) => {
match cmd.json.data_f64 {
Some(val) => {
tec.set_max_v(ElectricPotential::new::<volt>(val));
}
None => {
info!("Wrong Data type is received")
}
}
}
Some(ThermostatCmdEnum::SetTecMaxIPos) => {
match cmd.json.data_f64 {
Some(val) => {
tec.set_max_i_pos(ElectricCurrent::new::<ampere>(val));
}
None => {
info!("Wrong Data type is received")
}
}
}
Some(ThermostatCmdEnum::SetTecMaxINeg) => {
match cmd.json.data_f64 {
Some(val) => {
tec.set_max_i_pos(ElectricCurrent::new::<milliampere>(val));
}
None => {
info!("Wrong Data type is received")
}
}
}
Some(ThermostatCmdEnum::SetTecIOut) => {
match cmd.json.data_f64 {
Some(val) => {
tec.set_i(ElectricCurrent::new::<milliampere>(val));
}
None => {
info!("Wrong Data type is received")
}
}
}
Some(ThermostatCmdEnum::SetConstantCurrentMode) => {
info!("Not supported Yet")
}
Some(ThermostatCmdEnum::SetPidControlMode) => {
info!("Not supported Yet")
}
Some(ThermostatCmdEnum::SetPidKp) => {
info!("Not supported Yet")
}
Some(ThermostatCmdEnum::SetPidKi) => {
info!("Not supported Yet")
}
Some(ThermostatCmdEnum::SetPidKd) => {
info!("Not supported Yet")
}
Some(ThermostatCmdEnum::SetPidOutMin) => {
info!("Not supported Yet")
}
Some(ThermostatCmdEnum::SetPidOutMax) => {
info!("Not supported Yet")
}
Some(ThermostatCmdEnum::SetPidUpdateInterval) => {
info!("Not supported Yet")
}
Some(ThermostatCmdEnum::SetShT0) => {
info!("Not supported Yet")
}
Some(ThermostatCmdEnum::SetShR0) => {
info!("Not supported Yet")
}
Some(ThermostatCmdEnum::SetShBeta) => {
info!("Not supported Yet")
}
Some(ThermostatCmdEnum::GetTecStatus) => {
info!("Not supported Yet")
}
Some(ThermostatCmdEnum::GetPidStatus) => {
info!("Not supported Yet")
}
Some(ThermostatCmdEnum::GetShParams) => {
info!("Not supported Yet")
}
None => { /* Do Nothing*/ }
_ => {
info!("Unimplemented Command")
}
}
}
Err(err) => {
info!("Invalid Command: {:?}", err);
}
}
2024-02-05 15:14:23 +08:00
(laser, tec)
}