2021-06-07 10:07:05 +08:00
|
|
|
use super::{
|
|
|
|
ad7172,
|
2024-10-14 11:52:15 +08:00
|
|
|
channels::{Channels, CHANNELS},
|
|
|
|
command_parser::{
|
|
|
|
CenterPoint, Command, Ipv4Config, PidParameter, Polarity, PwmPin, ShParameter, ShowCommand,
|
2021-06-07 10:07:05 +08:00
|
|
|
},
|
|
|
|
config::ChannelConfig,
|
|
|
|
dfu,
|
|
|
|
flash_store::FlashStore,
|
2023-03-22 17:15:49 +08:00
|
|
|
hw_rev::HWRev,
|
2024-10-14 11:52:15 +08:00
|
|
|
net, FanCtrl, CHANNEL_CONFIG_KEY,
|
2021-06-07 10:07:05 +08:00
|
|
|
};
|
2024-10-14 11:52:15 +08:00
|
|
|
use core::fmt::Write;
|
|
|
|
use heapless::{consts::U1024, Vec};
|
|
|
|
use log::{error, warn};
|
|
|
|
use smoltcp::socket::TcpSocket;
|
2021-06-07 10:07:05 +08:00
|
|
|
|
2024-10-14 11:52:15 +08:00
|
|
|
use uom::si::{
|
|
|
|
electric_current::ampere,
|
|
|
|
electric_potential::volt,
|
|
|
|
electrical_resistance::ohm,
|
|
|
|
f64::{ElectricCurrent, ElectricPotential, ElectricalResistance, ThermodynamicTemperature},
|
|
|
|
thermodynamic_temperature::degree_celsius,
|
2021-06-07 10:07:05 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
pub enum Handler {
|
|
|
|
Handled,
|
|
|
|
CloseSocket,
|
|
|
|
NewIPV4(Ipv4Config),
|
|
|
|
Reset,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
|
|
pub enum Error {
|
2024-10-07 12:54:19 +08:00
|
|
|
Report,
|
|
|
|
PostFilterRate,
|
|
|
|
Flash,
|
2021-06-07 10:07:05 +08:00
|
|
|
}
|
|
|
|
|
2023-03-22 17:15:49 +08:00
|
|
|
pub type JsonBuffer = Vec<u8, U1024>;
|
|
|
|
|
2021-06-07 10:07:05 +08:00
|
|
|
fn send_line(socket: &mut TcpSocket, data: &[u8]) -> bool {
|
|
|
|
let send_free = socket.send_capacity() - socket.send_queue();
|
|
|
|
if data.len() > send_free + 1 {
|
|
|
|
// Not enough buffer space, skip report for now,
|
|
|
|
// instead of sending incomplete line
|
|
|
|
warn!(
|
|
|
|
"TCP socket has only {}/{} needed {}",
|
2024-10-14 11:52:15 +08:00
|
|
|
send_free + 1,
|
|
|
|
socket.send_capacity(),
|
|
|
|
data.len(),
|
2021-06-07 10:07:05 +08:00
|
|
|
);
|
|
|
|
} else {
|
2024-10-07 12:54:19 +08:00
|
|
|
match socket.send_slice(data) {
|
2021-06-07 10:07:05 +08:00
|
|
|
Ok(sent) if sent == data.len() => {
|
|
|
|
let _ = socket.send_slice(b"\n");
|
|
|
|
// success
|
2024-10-14 11:52:15 +08:00
|
|
|
return true;
|
2021-06-07 10:07:05 +08:00
|
|
|
}
|
2024-10-14 11:52:15 +08:00
|
|
|
Ok(sent) => warn!("sent only {}/{} bytes", sent, data.len()),
|
|
|
|
Err(e) => error!("error sending line: {:?}", e),
|
2021-06-07 10:07:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// not success
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Handler {
|
|
|
|
fn show_report(socket: &mut TcpSocket, channels: &mut Channels) -> Result<Handler, Error> {
|
|
|
|
match channels.reports_json() {
|
|
|
|
Ok(buf) => {
|
|
|
|
send_line(socket, &buf[..]);
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
error!("unable to serialize report: {:?}", e);
|
|
|
|
let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e);
|
2024-10-07 12:54:19 +08:00
|
|
|
return Err(Error::Report);
|
2021-06-07 10:07:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(Handler::Handled)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn show_pid(socket: &mut TcpSocket, channels: &mut Channels) -> Result<Handler, Error> {
|
|
|
|
match channels.pid_summaries_json() {
|
|
|
|
Ok(buf) => {
|
|
|
|
send_line(socket, &buf);
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
error!("unable to serialize pid summary: {:?}", e);
|
|
|
|
let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e);
|
2024-10-07 12:54:19 +08:00
|
|
|
return Err(Error::Report);
|
2021-06-07 10:07:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(Handler::Handled)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn show_pwm(socket: &mut TcpSocket, channels: &mut Channels) -> Result<Handler, Error> {
|
|
|
|
match channels.pwm_summaries_json() {
|
|
|
|
Ok(buf) => {
|
|
|
|
send_line(socket, &buf);
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
error!("unable to serialize pwm summary: {:?}", e);
|
|
|
|
let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e);
|
2024-10-07 12:54:19 +08:00
|
|
|
return Err(Error::Report);
|
2021-06-07 10:07:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(Handler::Handled)
|
|
|
|
}
|
|
|
|
|
2024-10-14 11:52:15 +08:00
|
|
|
fn show_steinhart_hart(
|
|
|
|
socket: &mut TcpSocket,
|
|
|
|
channels: &mut Channels,
|
|
|
|
) -> Result<Handler, Error> {
|
2021-06-07 10:07:05 +08:00
|
|
|
match channels.steinhart_hart_summaries_json() {
|
|
|
|
Ok(buf) => {
|
|
|
|
send_line(socket, &buf);
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
error!("unable to serialize steinhart-hart summaries: {:?}", e);
|
|
|
|
let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e);
|
2024-10-07 12:54:19 +08:00
|
|
|
return Err(Error::Report);
|
2021-06-07 10:07:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(Handler::Handled)
|
|
|
|
}
|
|
|
|
|
2024-10-14 11:52:15 +08:00
|
|
|
fn show_post_filter(socket: &mut TcpSocket, channels: &mut Channels) -> Result<Handler, Error> {
|
2021-06-07 10:07:05 +08:00
|
|
|
match channels.postfilter_summaries_json() {
|
|
|
|
Ok(buf) => {
|
|
|
|
send_line(socket, &buf);
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
error!("unable to serialize postfilter summary: {:?}", e);
|
|
|
|
let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e);
|
2024-10-07 12:54:19 +08:00
|
|
|
return Err(Error::Report);
|
2021-06-07 10:07:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(Handler::Handled)
|
|
|
|
}
|
|
|
|
|
2024-10-14 11:52:15 +08:00
|
|
|
fn show_ipv4(socket: &mut TcpSocket, ipv4_config: &mut Ipv4Config) -> Result<Handler, Error> {
|
2021-06-07 10:07:05 +08:00
|
|
|
let (cidr, gateway) = net::split_ipv4_config(ipv4_config.clone());
|
|
|
|
let _ = write!(socket, "{{\"addr\":\"{}\"", cidr);
|
|
|
|
gateway.map(|gateway| write!(socket, ",\"gateway\":\"{}\"", gateway));
|
|
|
|
let _ = writeln!(socket, "}}");
|
|
|
|
Ok(Handler::Handled)
|
|
|
|
}
|
|
|
|
|
2024-10-14 11:52:15 +08:00
|
|
|
fn engage_pid(
|
|
|
|
socket: &mut TcpSocket,
|
|
|
|
channels: &mut Channels,
|
|
|
|
channel: usize,
|
|
|
|
) -> Result<Handler, Error> {
|
2021-06-07 10:07:05 +08:00
|
|
|
channels.channel_state(channel).pid_engaged = true;
|
|
|
|
send_line(socket, b"{}");
|
|
|
|
Ok(Handler::Handled)
|
|
|
|
}
|
|
|
|
|
2024-10-14 11:52:15 +08:00
|
|
|
fn set_polarity(
|
|
|
|
socket: &mut TcpSocket,
|
|
|
|
channels: &mut Channels,
|
|
|
|
channel: usize,
|
|
|
|
polarity: Polarity,
|
|
|
|
) -> Result<Handler, Error> {
|
2024-08-20 17:42:44 +08:00
|
|
|
channels.set_polarity(channel, polarity);
|
|
|
|
send_line(socket, b"{}");
|
|
|
|
Ok(Handler::Handled)
|
|
|
|
}
|
|
|
|
|
2024-10-14 11:52:15 +08:00
|
|
|
fn set_pwm(
|
|
|
|
socket: &mut TcpSocket,
|
|
|
|
channels: &mut Channels,
|
|
|
|
channel: usize,
|
|
|
|
pin: PwmPin,
|
|
|
|
value: f64,
|
|
|
|
) -> Result<Handler, Error> {
|
2021-06-07 10:07:05 +08:00
|
|
|
match pin {
|
|
|
|
PwmPin::ISet => {
|
|
|
|
channels.channel_state(channel).pid_engaged = false;
|
|
|
|
let current = ElectricCurrent::new::<ampere>(value);
|
|
|
|
channels.set_i(channel, current);
|
|
|
|
channels.power_up(channel);
|
|
|
|
}
|
|
|
|
PwmPin::MaxV => {
|
|
|
|
let voltage = ElectricPotential::new::<volt>(value);
|
|
|
|
channels.set_max_v(channel, voltage);
|
|
|
|
}
|
|
|
|
PwmPin::MaxIPos => {
|
|
|
|
let current = ElectricCurrent::new::<ampere>(value);
|
|
|
|
channels.set_max_i_pos(channel, current);
|
|
|
|
}
|
|
|
|
PwmPin::MaxINeg => {
|
|
|
|
let current = ElectricCurrent::new::<ampere>(value);
|
|
|
|
channels.set_max_i_neg(channel, current);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
send_line(socket, b"{}");
|
|
|
|
Ok(Handler::Handled)
|
|
|
|
}
|
|
|
|
|
2024-10-14 11:52:15 +08:00
|
|
|
fn set_center_point(
|
|
|
|
socket: &mut TcpSocket,
|
|
|
|
channels: &mut Channels,
|
|
|
|
channel: usize,
|
|
|
|
center: CenterPoint,
|
|
|
|
) -> Result<Handler, Error> {
|
2024-02-14 17:01:19 +08:00
|
|
|
let i_set = channels.get_i(channel);
|
2021-06-07 10:07:05 +08:00
|
|
|
let state = channels.channel_state(channel);
|
|
|
|
state.center = center;
|
|
|
|
if !state.pid_engaged {
|
2024-02-14 17:01:19 +08:00
|
|
|
channels.set_i(channel, i_set);
|
2021-06-07 10:07:05 +08:00
|
|
|
}
|
|
|
|
send_line(socket, b"{}");
|
|
|
|
Ok(Handler::Handled)
|
|
|
|
}
|
|
|
|
|
2024-10-14 11:52:15 +08:00
|
|
|
fn set_pid(
|
|
|
|
socket: &mut TcpSocket,
|
|
|
|
channels: &mut Channels,
|
|
|
|
channel: usize,
|
|
|
|
parameter: PidParameter,
|
|
|
|
value: f64,
|
|
|
|
) -> Result<Handler, Error> {
|
2021-06-07 10:07:05 +08:00
|
|
|
let pid = &mut channels.channel_state(channel).pid;
|
|
|
|
use super::command_parser::PidParameter::*;
|
|
|
|
match parameter {
|
2024-10-14 11:52:15 +08:00
|
|
|
Target => pid.target = value,
|
|
|
|
KP => pid.parameters.kp = value as f32,
|
|
|
|
KI => pid.update_ki(value as f32),
|
|
|
|
KD => pid.parameters.kd = value as f32,
|
|
|
|
OutputMin => pid.parameters.output_min = value as f32,
|
|
|
|
OutputMax => pid.parameters.output_max = value as f32,
|
2021-06-07 10:07:05 +08:00
|
|
|
}
|
|
|
|
send_line(socket, b"{}");
|
|
|
|
Ok(Handler::Handled)
|
|
|
|
}
|
|
|
|
|
2024-10-14 11:52:15 +08:00
|
|
|
fn set_steinhart_hart(
|
|
|
|
socket: &mut TcpSocket,
|
|
|
|
channels: &mut Channels,
|
|
|
|
channel: usize,
|
|
|
|
parameter: ShParameter,
|
|
|
|
value: f64,
|
|
|
|
) -> Result<Handler, Error> {
|
2021-06-07 10:07:05 +08:00
|
|
|
let sh = &mut channels.channel_state(channel).sh;
|
|
|
|
use super::command_parser::ShParameter::*;
|
|
|
|
match parameter {
|
|
|
|
T0 => sh.t0 = ThermodynamicTemperature::new::<degree_celsius>(value),
|
|
|
|
B => sh.b = value,
|
|
|
|
R0 => sh.r0 = ElectricalResistance::new::<ohm>(value),
|
|
|
|
}
|
|
|
|
send_line(socket, b"{}");
|
|
|
|
Ok(Handler::Handled)
|
|
|
|
}
|
|
|
|
|
2024-10-14 11:52:15 +08:00
|
|
|
fn reset_post_filter(
|
|
|
|
socket: &mut TcpSocket,
|
|
|
|
channels: &mut Channels,
|
|
|
|
channel: usize,
|
|
|
|
) -> Result<Handler, Error> {
|
2021-06-07 10:07:05 +08:00
|
|
|
channels.adc.set_postfilter(channel as u8, None).unwrap();
|
|
|
|
send_line(socket, b"{}");
|
|
|
|
Ok(Handler::Handled)
|
|
|
|
}
|
|
|
|
|
2024-10-14 11:52:15 +08:00
|
|
|
fn set_post_filter(
|
|
|
|
socket: &mut TcpSocket,
|
|
|
|
channels: &mut Channels,
|
|
|
|
channel: usize,
|
|
|
|
rate: f32,
|
|
|
|
) -> Result<Handler, Error> {
|
2021-06-07 10:07:05 +08:00
|
|
|
let filter = ad7172::PostFilter::closest(rate);
|
|
|
|
match filter {
|
|
|
|
Some(filter) => {
|
2024-10-14 11:52:15 +08:00
|
|
|
channels
|
|
|
|
.adc
|
|
|
|
.set_postfilter(channel as u8, Some(filter))
|
|
|
|
.unwrap();
|
2021-06-07 10:07:05 +08:00
|
|
|
send_line(socket, b"{}");
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
error!("unable to choose postfilter for rate {:.3}", rate);
|
2024-10-14 11:52:15 +08:00
|
|
|
send_line(
|
|
|
|
socket,
|
|
|
|
b"{{\"error\": \"unable to choose postfilter rate\"}}",
|
|
|
|
);
|
2024-10-07 12:54:19 +08:00
|
|
|
return Err(Error::PostFilterRate);
|
2021-06-07 10:07:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(Handler::Handled)
|
|
|
|
}
|
|
|
|
|
2024-10-14 11:52:15 +08:00
|
|
|
fn load_channel(
|
|
|
|
socket: &mut TcpSocket,
|
|
|
|
channels: &mut Channels,
|
|
|
|
store: &mut FlashStore,
|
|
|
|
channel: Option<usize>,
|
|
|
|
) -> Result<Handler, Error> {
|
2024-10-07 12:54:19 +08:00
|
|
|
for (c, key) in CHANNEL_CONFIG_KEY.iter().enumerate().take(CHANNELS) {
|
2021-06-07 10:07:05 +08:00
|
|
|
if channel.is_none() || channel == Some(c) {
|
2024-10-07 12:54:19 +08:00
|
|
|
match store.read_value::<ChannelConfig>(key) {
|
2021-06-07 10:07:05 +08:00
|
|
|
Ok(Some(config)) => {
|
|
|
|
config.apply(channels, c);
|
|
|
|
send_line(socket, b"{}");
|
|
|
|
}
|
|
|
|
Ok(None) => {
|
|
|
|
error!("flash config not found");
|
|
|
|
send_line(socket, b"{{\"error\": \"flash config not found\"}}");
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
error!("unable to load config from flash: {:?}", e);
|
|
|
|
let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e);
|
2024-10-07 12:54:19 +08:00
|
|
|
return Err(Error::Flash);
|
2021-06-07 10:07:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(Handler::Handled)
|
|
|
|
}
|
|
|
|
|
2024-10-14 11:52:15 +08:00
|
|
|
fn save_channel(
|
|
|
|
socket: &mut TcpSocket,
|
|
|
|
channels: &mut Channels,
|
|
|
|
channel: Option<usize>,
|
|
|
|
store: &mut FlashStore,
|
|
|
|
) -> Result<Handler, Error> {
|
2024-10-07 12:54:19 +08:00
|
|
|
for (c, key) in CHANNEL_CONFIG_KEY.iter().enumerate().take(CHANNELS) {
|
2021-06-07 10:07:05 +08:00
|
|
|
let mut store_value_buf = [0u8; 256];
|
|
|
|
if channel.is_none() || channel == Some(c) {
|
|
|
|
let config = ChannelConfig::new(channels, c);
|
2024-10-07 12:54:19 +08:00
|
|
|
match store.write_value(key, &config, &mut store_value_buf) {
|
2021-06-07 10:07:05 +08:00
|
|
|
Ok(()) => {
|
|
|
|
send_line(socket, b"{}");
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
error!("unable to save channel {} config to flash: {:?}", c, e);
|
|
|
|
let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e);
|
2024-10-07 12:54:19 +08:00
|
|
|
return Err(Error::Flash);
|
2021-06-07 10:07:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(Handler::Handled)
|
|
|
|
}
|
|
|
|
|
2024-10-14 11:52:15 +08:00
|
|
|
fn set_ipv4(
|
|
|
|
socket: &mut TcpSocket,
|
|
|
|
store: &mut FlashStore,
|
|
|
|
config: Ipv4Config,
|
|
|
|
) -> Result<Handler, Error> {
|
2021-06-07 10:07:05 +08:00
|
|
|
let _ = store
|
|
|
|
.write_value("ipv4", &config, [0; 16])
|
|
|
|
.map_err(|e| error!("unable to save ipv4 config to flash: {:?}", e));
|
|
|
|
let new_ipv4_config = Some(config);
|
|
|
|
send_line(socket, b"{}");
|
|
|
|
Ok(Handler::NewIPV4(new_ipv4_config.unwrap()))
|
|
|
|
}
|
|
|
|
|
2024-10-14 11:52:15 +08:00
|
|
|
fn reset(channels: &mut Channels) -> Result<Handler, Error> {
|
2021-06-07 10:07:05 +08:00
|
|
|
for i in 0..CHANNELS {
|
|
|
|
channels.power_down(i);
|
|
|
|
}
|
|
|
|
// should_reset = true;
|
|
|
|
Ok(Handler::Reset)
|
|
|
|
}
|
|
|
|
|
2024-10-14 11:52:15 +08:00
|
|
|
fn dfu(channels: &mut Channels) -> Result<Handler, Error> {
|
2021-06-07 10:07:05 +08:00
|
|
|
for i in 0..CHANNELS {
|
|
|
|
channels.power_down(i);
|
|
|
|
}
|
|
|
|
unsafe {
|
|
|
|
dfu::set_dfu_trigger();
|
|
|
|
}
|
|
|
|
// should_reset = true;
|
|
|
|
Ok(Handler::Reset)
|
|
|
|
}
|
|
|
|
|
2024-10-14 11:52:15 +08:00
|
|
|
fn set_fan(
|
|
|
|
socket: &mut TcpSocket,
|
|
|
|
fan_pwm: u32,
|
|
|
|
fan_ctrl: &mut FanCtrl,
|
|
|
|
) -> Result<Handler, Error> {
|
2023-03-22 17:15:49 +08:00
|
|
|
if !fan_ctrl.fan_available() {
|
2024-10-14 11:52:15 +08:00
|
|
|
send_line(
|
|
|
|
socket,
|
|
|
|
b"{ \"warning\": \"this thermostat doesn't have a fan!\" }",
|
|
|
|
);
|
2023-03-22 17:15:49 +08:00
|
|
|
return Ok(Handler::Handled);
|
|
|
|
}
|
|
|
|
fan_ctrl.set_auto_mode(false);
|
|
|
|
fan_ctrl.set_pwm(fan_pwm);
|
|
|
|
if fan_ctrl.fan_pwm_recommended() {
|
|
|
|
send_line(socket, b"{}");
|
|
|
|
} else {
|
|
|
|
send_line(socket, b"{ \"warning\": \"this fan doesn't have full PWM support. Use it at your own risk!\" }");
|
|
|
|
}
|
|
|
|
Ok(Handler::Handled)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn show_fan(socket: &mut TcpSocket, fan_ctrl: &mut FanCtrl) -> Result<Handler, Error> {
|
|
|
|
match fan_ctrl.summary() {
|
|
|
|
Ok(buf) => {
|
|
|
|
send_line(socket, &buf);
|
|
|
|
Ok(Handler::Handled)
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
error!("unable to serialize fan summary: {:?}", e);
|
|
|
|
let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e);
|
2024-10-07 12:54:19 +08:00
|
|
|
Err(Error::Report)
|
2023-03-22 17:15:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fan_auto(socket: &mut TcpSocket, fan_ctrl: &mut FanCtrl) -> Result<Handler, Error> {
|
|
|
|
if !fan_ctrl.fan_available() {
|
2024-10-14 11:52:15 +08:00
|
|
|
send_line(
|
|
|
|
socket,
|
|
|
|
b"{ \"warning\": \"this thermostat doesn't have a fan!\" }",
|
|
|
|
);
|
2023-03-22 17:15:49 +08:00
|
|
|
return Ok(Handler::Handled);
|
|
|
|
}
|
|
|
|
fan_ctrl.set_auto_mode(true);
|
|
|
|
if fan_ctrl.fan_pwm_recommended() {
|
|
|
|
send_line(socket, b"{}");
|
|
|
|
} else {
|
|
|
|
send_line(socket, b"{ \"warning\": \"this fan doesn't have full PWM support. Use it at your own risk!\" }");
|
|
|
|
}
|
|
|
|
Ok(Handler::Handled)
|
|
|
|
}
|
|
|
|
|
2024-10-14 11:52:15 +08:00
|
|
|
fn fan_curve(
|
|
|
|
socket: &mut TcpSocket,
|
|
|
|
fan_ctrl: &mut FanCtrl,
|
|
|
|
k_a: f32,
|
|
|
|
k_b: f32,
|
|
|
|
k_c: f32,
|
|
|
|
) -> Result<Handler, Error> {
|
2023-03-22 17:15:49 +08:00
|
|
|
fan_ctrl.set_curve(k_a, k_b, k_c);
|
|
|
|
send_line(socket, b"{}");
|
|
|
|
Ok(Handler::Handled)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fan_defaults(socket: &mut TcpSocket, fan_ctrl: &mut FanCtrl) -> Result<Handler, Error> {
|
|
|
|
fan_ctrl.restore_defaults();
|
|
|
|
send_line(socket, b"{}");
|
|
|
|
Ok(Handler::Handled)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn show_hwrev(socket: &mut TcpSocket, hwrev: HWRev) -> Result<Handler, Error> {
|
|
|
|
match hwrev.summary() {
|
|
|
|
Ok(buf) => {
|
|
|
|
send_line(socket, &buf);
|
|
|
|
Ok(Handler::Handled)
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
error!("unable to serialize HWRev summary: {:?}", e);
|
|
|
|
let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e);
|
2024-10-07 12:54:19 +08:00
|
|
|
Err(Error::Report)
|
2023-03-22 17:15:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-14 11:52:15 +08:00
|
|
|
pub fn handle_command(
|
|
|
|
command: Command,
|
|
|
|
socket: &mut TcpSocket,
|
|
|
|
channels: &mut Channels,
|
|
|
|
store: &mut FlashStore,
|
|
|
|
ipv4_config: &mut Ipv4Config,
|
|
|
|
fan_ctrl: &mut FanCtrl,
|
|
|
|
hwrev: HWRev,
|
|
|
|
) -> Result<Self, Error> {
|
2021-06-07 10:07:05 +08:00
|
|
|
match command {
|
|
|
|
Command::Quit => Ok(Handler::CloseSocket),
|
2023-03-22 17:15:49 +08:00
|
|
|
Command::Show(ShowCommand::Input) => Handler::show_report(socket, channels),
|
|
|
|
Command::Show(ShowCommand::Pid) => Handler::show_pid(socket, channels),
|
|
|
|
Command::Show(ShowCommand::Pwm) => Handler::show_pwm(socket, channels),
|
2024-10-14 11:52:15 +08:00
|
|
|
Command::Show(ShowCommand::SteinhartHart) => {
|
|
|
|
Handler::show_steinhart_hart(socket, channels)
|
|
|
|
}
|
2023-03-22 17:15:49 +08:00
|
|
|
Command::Show(ShowCommand::PostFilter) => Handler::show_post_filter(socket, channels),
|
2021-06-07 10:07:05 +08:00
|
|
|
Command::Show(ShowCommand::Ipv4) => Handler::show_ipv4(socket, ipv4_config),
|
2023-09-20 11:11:07 +08:00
|
|
|
Command::PwmPid { channel } => Handler::engage_pid(socket, channels, channel),
|
2024-10-14 11:52:15 +08:00
|
|
|
Command::PwmPolarity { channel, polarity } => {
|
|
|
|
Handler::set_polarity(socket, channels, channel, polarity)
|
|
|
|
}
|
|
|
|
Command::Pwm {
|
|
|
|
channel,
|
|
|
|
pin,
|
|
|
|
value,
|
|
|
|
} => Handler::set_pwm(socket, channels, channel, pin, value),
|
|
|
|
Command::CenterPoint { channel, center } => {
|
|
|
|
Handler::set_center_point(socket, channels, channel, center)
|
|
|
|
}
|
|
|
|
Command::Pid {
|
|
|
|
channel,
|
|
|
|
parameter,
|
|
|
|
value,
|
|
|
|
} => Handler::set_pid(socket, channels, channel, parameter, value),
|
|
|
|
Command::SteinhartHart {
|
|
|
|
channel,
|
|
|
|
parameter,
|
|
|
|
value,
|
|
|
|
} => Handler::set_steinhart_hart(socket, channels, channel, parameter, value),
|
|
|
|
Command::PostFilter {
|
|
|
|
channel,
|
|
|
|
rate: None,
|
|
|
|
} => Handler::reset_post_filter(socket, channels, channel),
|
|
|
|
Command::PostFilter {
|
|
|
|
channel,
|
|
|
|
rate: Some(rate),
|
|
|
|
} => Handler::set_post_filter(socket, channels, channel, rate),
|
2021-06-07 10:07:05 +08:00
|
|
|
Command::Load { channel } => Handler::load_channel(socket, channels, store, channel),
|
|
|
|
Command::Save { channel } => Handler::save_channel(socket, channels, channel, store),
|
|
|
|
Command::Ipv4(config) => Handler::set_ipv4(socket, store, config),
|
|
|
|
Command::Reset => Handler::reset(channels),
|
2023-03-22 17:15:49 +08:00
|
|
|
Command::Dfu => Handler::dfu(channels),
|
2024-10-14 11:52:15 +08:00
|
|
|
Command::FanSet { fan_pwm } => Handler::set_fan(socket, fan_pwm, fan_ctrl),
|
2023-03-22 17:15:49 +08:00
|
|
|
Command::ShowFan => Handler::show_fan(socket, fan_ctrl),
|
|
|
|
Command::FanAuto => Handler::fan_auto(socket, fan_ctrl),
|
2024-10-14 11:52:15 +08:00
|
|
|
Command::FanCurve { k_a, k_b, k_c } => {
|
|
|
|
Handler::fan_curve(socket, fan_ctrl, k_a, k_b, k_c)
|
|
|
|
}
|
2023-03-22 17:15:49 +08:00
|
|
|
Command::FanCurveDefaults => Handler::fan_defaults(socket, fan_ctrl),
|
|
|
|
Command::ShowHWRev => Handler::show_hwrev(socket, hwrev),
|
2021-06-07 10:07:05 +08:00
|
|
|
}
|
|
|
|
}
|
2024-10-14 11:52:15 +08:00
|
|
|
}
|