这是indexloc提供的服务,不要输入任何密码
Skip to content

Conversation

@qwandor-google
Copy link
Collaborator

Moving here instead of alsuren/reading-xiaomi-temp#32.

Fixes #1.

.bt_session
.connect(&sensor.id)
.await
.with_context(|| std::line!().to_string())?;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the connection_status be updated if this fails, rather than leaving it in the Connecting state?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds reasonable

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is now unnecessary because the state will revert to what it was before it was updated to Connecting. Now I think the only reason the Connecting state has an effect is if an update for the sensor comes in before this function returns. We can probably solve this better with locking.

Err(e) => {
// If starting notifications failed a second time, disconnect so
// that we start again from a clean state next time.
match sensor.connection_status {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the connection_status ever anything other than Connecting here?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hrm. I guess not. This suggests that we want to do the retrying here until SENSOR_CONECT_LOCK_TIMEOUT/2 or something and kill off the failed once state? I think this is something I wanted to do anyway eventually.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed by moving the clone_sensor_at_idx call in connect_sensor_at_idx higher up, so it clones the sensor before changing the state of the original to Connecting.

.bt_session
.connect(&sensor.id)
.await
.with_context(|| std::line!().to_string())?;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds reasonable

Err(e) => {
// If starting notifications failed a second time, disconnect so
// that we start again from a clean state next time.
match sensor.connection_status {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hrm. I guess not. This suggests that we want to do the retrying here until SENSOR_CONECT_LOCK_TIMEOUT/2 or something and kill off the failed once state? I think this is something I wanted to do anyway eventually.

sensors_connected: &mut Vec<Sensor>,
sensors_to_connect: &mut VecDeque<Sensor>,
/// If the sensor hasn't sent any updates in a while, disconnect it so we will try to reconnect.
async fn check_for_stale_sensor(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we fix the to-do then this could be called disconnect_if_stale()?

@qwandor-google qwandor-google marked this pull request as ready for review September 24, 2020 22:35
This means that the previous state is used by connect_start_sensor to
determine the next state rather than always the 'Connecting' state. It
also means that the state will revert to the previous state if
connecting fails.
}
let mut sensor = clone_sensor_at_idx(state.clone(), idx).await;
println!("Trying to connect to {}", sensor.name);
let status = connect_start_sensor(session, &mut sensor).await;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit sad that connect_start_sensor() mutates sensor rather than just returning the new connection_status, but think let's merge this and I'll have a crack at it over the weekend.

@alsuren alsuren merged commit f8bd8f3 into master Sep 25, 2020
@qwandor-google qwandor-google deleted the less-lockey branch September 25, 2020 18:12
qwandor-google pushed a commit that referenced this pull request Sep 29, 2020
Refactor SensorState to have a single set of sensors, and use states
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refactor SensorState::sensors_* into a single SensorState::sensors

4 participants