~jojo/huelia

6434b9553d3ccb3d1001cb744493e0ca734ff6aa — JoJo 25 days ago 3b5eaec master v0.6.2
performer: don't be so eager to reset
1 files changed, 12 insertions(+), 32 deletions(-)

M performer-esp-idf/src/lib.rs
M performer-esp-idf/src/lib.rs => performer-esp-idf/src/lib.rs +12 -32
@@ 63,27 63,24 @@ pub fn main<St: 'static + Send + Clone>(
        let (namespace, rw) = ("huelia", true);
        EspNvs::new(EspCustomNvsPartition::take("nvs").unwrap(), namespace, rw).unwrap()
    }));
    let mut attempts = 0;
    while let Err(e) =
        main_(product_name, version_str, default_color, color_writer_st.clone(), write_colors, storage.clone())
    {
        log::error!("main returned Err: {e}");
        let mut storage = storage.lock().unwrap();
        let max_quick_attempts = 5;
        let quick_snooze = Duration::from_secs(2);
        let slow_snooze = Duration::from_secs(10 * 60);
        let mut attempts = read_start_attempts(&mut storage).unwrap();
        if attempts == max_quick_attempts {
            attempts = 0;
        }
        write_start_attempts(&mut storage, attempts + 1).ok();
        let max_quick_attempts = 10;
        let quick_snooze = Duration::from_secs(1);
        let slow_snooze = Duration::from_secs(60);
        if attempts < max_quick_attempts {
            log::info!("fewer startup attempts than {max_quick_attempts}. quick snooze before reset");
            log::info!("fewer startup attempts than {max_quick_attempts}. quick snooze and go again");
            sleep(quick_snooze);
            attempts += 1;
            continue;
        } else {
            log::info!("too many failed restarts (attempts {attempts} > max {max_quick_attempts}). will go sleep for much longer before attempting a restart again. less annoying chatter & flashing of the white LED that way");
            log::info!("too many failed restarts (attempts {attempts} > max {max_quick_attempts}). will go sleep for longer before attempting a restart");
            sleep(slow_snooze);
            restart()
        }
        restart()
    }
    log::info!("main returned Ok. Powering off - no restart.")
}


@@ 323,10 320,9 @@ fn main_<St: 'static + Send + Clone>(
            ),
        ]
    };
    let on_connected = || {
        let mut storage = storage.lock().unwrap();
        crate::write_start_attempts(&mut storage, 0).unwrap()
    };
    let on_connected = || {};
    // TODO: try reconnect, remembering prev color.
    //       Only revert to "normal" light on hard reset
    mqtt::connect(
        product_name,
        version_str,


@@ 381,19 377,3 @@ pub fn restart() -> ! {
    esp_idf_hal::reset::restart();
    std::process::abort()
}

pub fn read_start_attempts(storage: &mut EspNvs<NvsCustom>) -> anyhow::Result<u8> {
    let mut buf = [0u8; 1];
    let default = 0u8;
    Ok(match storage.get_raw("start-attempts", &mut buf) {
        Ok(Some(_)) => buf[0],
        Ok(None) => default,
        Err(e) if e.code() == ESP_ERR_NVS_INVALID_LENGTH => default,
        Err(e) => Err(e)?,
    })
}

pub fn write_start_attempts(storage: &mut EspNvs<NvsCustom>, new: u8) -> anyhow::Result<()> {
    storage.set_raw("start-attempts", &[new])?;
    Ok(())
}