main: Add a state to only save network settings

This commit is contained in:
linuswck 2024-04-10 16:24:18 +08:00
parent eaebdb390d
commit 6bccbceb81
3 changed files with 23 additions and 17 deletions

View File

@ -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]): 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 After calling this fn, the ip settings are immediately saved into flash and will be effective on next reboot.
Hard Reset/Power Cycle Kirdy for the new network settings to be effective.
""" """
if not(isinstance(addr, list) and isinstance(gateway, list)): if not(isinstance(addr, list) and isinstance(gateway, list)):
raise InvalidDataType raise InvalidDataType
@ -252,13 +251,13 @@ class Device:
async def save_current_settings_to_flash(self): 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") return await self._send_cmd(TARGET_DEVICE, "SaveFlashSettings")
async def load_current_settings_to_flash(self): 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") return await self._send_cmd(TARGET_DEVICE, "LoadFlashSettings")

View File

@ -42,7 +42,8 @@ pub enum State {
#[default] #[default]
LoadFlashSettings, LoadFlashSettings,
MainLoop, MainLoop,
SaveFlashSettings, SaveLdThermostatSettings,
SaveDeviceSettings,
PrepareForHardReset, PrepareForHardReset,
HardReset, HardReset,
} }
@ -180,22 +181,12 @@ fn main() -> ! {
}) })
}; };
} }
State::SaveFlashSettings => { State::SaveLdThermostatSettings => {
// State Transition // State Transition
state = State::MainLoop; state = State::MainLoop;
wd.feed(); wd.feed();
let mut store_value_buf = [0u8; 1024]; 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) { match flash_store.write_value(CONFIG_KEY[1], &laser.get_settings_summary(), &mut store_value_buf) {
Ok(()) => { Ok(()) => {
debug!("Laser Diode Settings is stored in flash"); 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::PrepareForHardReset => {
// State Transition // State Transition
state = State::HardReset; state = State::HardReset;

View File

@ -255,6 +255,7 @@ pub fn execute_cmd(buffer: &mut [u8], buffer_size: usize, socket: &mut SocketHan
Some(val) => { Some(val) => {
send_response(buffer, ResponseEnum::Acknowledge, None, socket); send_response(buffer, ResponseEnum::Acknowledge, None, socket);
device_settings.ip_settings = val; device_settings.ip_settings = val;
*state = State::SaveDeviceSettings;
} }
None => { None => {
send_response(buffer, ResponseEnum::InvalidDatatype, Some(ERR_MSG_MISSING_IP_SETTINGS), socket); 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) => { Some(DeviceCmd::SaveFlashSettings) => {
send_response(buffer, ResponseEnum::Acknowledge, None, socket); send_response(buffer, ResponseEnum::Acknowledge, None, socket);
*state = State::SaveFlashSettings; *state = State::SaveLdThermostatSettings;
} }
Some(DeviceCmd::LoadFlashSettings) => { Some(DeviceCmd::LoadFlashSettings) => {
send_response(buffer, ResponseEnum::Acknowledge, None, socket); send_response(buffer, ResponseEnum::Acknowledge, None, socket);