From 6bccbceb810eac0d35efc91c38260099eeb75af5 Mon Sep 17 00:00:00 2001 From: linuswck Date: Wed, 10 Apr 2024 16:24:18 +0800 Subject: [PATCH] main: Add a state to only save network settings --- pykirdy/driver/kirdy_async.py | 7 +++---- src/main.rs | 30 ++++++++++++++++++------------ src/net/cmd_handler.rs | 3 ++- 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/pykirdy/driver/kirdy_async.py b/pykirdy/driver/kirdy_async.py index 82fe908..f20f5aa 100644 --- a/pykirdy/driver/kirdy_async.py +++ b/pykirdy/driver/kirdy_async.py @@ -95,8 +95,7 @@ class Device: async def set_ip_settings(self, addr=[192, 168, 1, 128], port=1337, prefix_len=24, gateway=[192, 168, 1, 1]): """ - After calling this fn, the user needs to issue the SaveFlashSettings cmd and then issue a - Hard Reset/Power Cycle Kirdy for the new network settings to be effective. + After calling this fn, the ip settings are immediately saved into flash and will be effective on next reboot. """ if not(isinstance(addr, list) and isinstance(gateway, list)): raise InvalidDataType @@ -252,13 +251,13 @@ class Device: async def save_current_settings_to_flash(self): """ - Save the current device configurations into flash. + Save the current laser diode and thermostat configurations into flash. """ return await self._send_cmd(TARGET_DEVICE, "SaveFlashSettings") async def load_current_settings_to_flash(self): """ - Restore stored settings in flash + Restore the laser diode and thermostat settings from flash """ return await self._send_cmd(TARGET_DEVICE, "LoadFlashSettings") diff --git a/src/main.rs b/src/main.rs index 486dfbd..cb55f01 100644 --- a/src/main.rs +++ b/src/main.rs @@ -42,7 +42,8 @@ pub enum State { #[default] LoadFlashSettings, MainLoop, - SaveFlashSettings, + SaveLdThermostatSettings, + SaveDeviceSettings, PrepareForHardReset, HardReset, } @@ -180,22 +181,12 @@ fn main() -> ! { }) }; } - State::SaveFlashSettings => { + State::SaveLdThermostatSettings => { // State Transition state = State::MainLoop; wd.feed(); let mut store_value_buf = [0u8; 1024]; - match flash_store.write_value(CONFIG_KEY[0], &device_settings, &mut store_value_buf) { - Ok(()) => { - debug!("Device Settings is stored in flash"); - } - Err(e) => { - debug!("Cannot Store Flash: {:?}", e); - } - } - - wd.feed(); match flash_store.write_value(CONFIG_KEY[1], &laser.get_settings_summary(), &mut store_value_buf) { Ok(()) => { debug!("Laser Diode Settings is stored in flash"); @@ -215,6 +206,21 @@ fn main() -> ! { } } } + State::SaveDeviceSettings => { + // State Transition + state = State::MainLoop; + + wd.feed(); + let mut store_value_buf = [0u8; 1024]; + match flash_store.write_value(CONFIG_KEY[0], &device_settings, &mut store_value_buf) { + Ok(()) => { + debug!("Device Settings is stored in flash"); + } + Err(e) => { + debug!("Cannot Store Flash: {:?}", e); + } + } + } State::PrepareForHardReset => { // State Transition state = State::HardReset; diff --git a/src/net/cmd_handler.rs b/src/net/cmd_handler.rs index 31b48c1..a357a02 100644 --- a/src/net/cmd_handler.rs +++ b/src/net/cmd_handler.rs @@ -255,6 +255,7 @@ pub fn execute_cmd(buffer: &mut [u8], buffer_size: usize, socket: &mut SocketHan Some(val) => { send_response(buffer, ResponseEnum::Acknowledge, None, socket); device_settings.ip_settings = val; + *state = State::SaveDeviceSettings; } None => { send_response(buffer, ResponseEnum::InvalidDatatype, Some(ERR_MSG_MISSING_IP_SETTINGS), socket); @@ -289,7 +290,7 @@ pub fn execute_cmd(buffer: &mut [u8], buffer_size: usize, socket: &mut SocketHan } Some(DeviceCmd::SaveFlashSettings) => { send_response(buffer, ResponseEnum::Acknowledge, None, socket); - *state = State::SaveFlashSettings; + *state = State::SaveLdThermostatSettings; } Some(DeviceCmd::LoadFlashSettings) => { send_response(buffer, ResponseEnum::Acknowledge, None, socket);