diff options
Diffstat (limited to 'src/entity_number.rs')
| -rw-r--r-- | src/entity_number.rs | 34 |
1 files changed, 23 insertions, 11 deletions
diff --git a/src/entity_number.rs b/src/entity_number.rs index 90d849c..f96c3c7 100644 --- a/src/entity_number.rs +++ b/src/entity_number.rs | |||
| @@ -1,4 +1,6 @@ | |||
| 1 | use crate::{Entity, EntityCommonConfig, EntityConfig, NumberUnit, constants}; | 1 | use crate::{ |
| 2 | Entity, EntityCommonConfig, EntityConfig, NumberCommand, NumberState, NumberUnit, constants, | ||
| 3 | }; | ||
| 2 | 4 | ||
| 3 | #[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] | 5 | #[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] |
| 4 | pub enum NumberMode { | 6 | pub enum NumberMode { |
| @@ -167,27 +169,37 @@ impl<'a> Number<'a> { | |||
| 167 | Self(entity) | 169 | Self(entity) |
| 168 | } | 170 | } |
| 169 | 171 | ||
| 170 | pub fn value(&mut self) -> Option<f32> { | 172 | pub fn get(&mut self) -> Option<f32> { |
| 171 | self.0.with_data(|data| { | 173 | self.0.with_data(|data| { |
| 172 | str::from_utf8(&data.command_value) | 174 | let storage = data.storage.as_number_mut(); |
| 173 | .ok() | 175 | storage.state.as_ref().map(|s| s.value) |
| 174 | .and_then(|v| v.parse::<f32>().ok()) | ||
| 175 | }) | 176 | }) |
| 176 | } | 177 | } |
| 177 | 178 | ||
| 178 | pub async fn value_wait(&mut self) -> f32 { | 179 | pub async fn wait(&mut self) -> f32 { |
| 179 | loop { | 180 | loop { |
| 180 | self.0.wait_command().await; | 181 | self.0.wait_command().await; |
| 181 | match self.value() { | 182 | match self.get() { |
| 182 | Some(value) => return value, | 183 | Some(value) => return value, |
| 183 | None => continue, | 184 | None => continue, |
| 184 | } | 185 | } |
| 185 | } | 186 | } |
| 186 | } | 187 | } |
| 187 | 188 | ||
| 188 | pub fn value_set(&mut self, value: f32) { | 189 | pub fn set(&mut self, value: f32) { |
| 189 | use core::fmt::Write; | 190 | let publish = self.0.with_data(|data| { |
| 190 | self.0 | 191 | let storage = data.storage.as_number_mut(); |
| 191 | .publish_with(|view| write!(view, "{}", value).unwrap()); | 192 | let timestamp = embassy_time::Instant::now(); |
| 193 | let publish = match &storage.command { | ||
| 194 | Some(command) => command.value != value, | ||
| 195 | None => true, | ||
| 196 | }; | ||
| 197 | storage.state = Some(NumberState { value, timestamp }); | ||
| 198 | storage.command = Some(NumberCommand { value, timestamp }); | ||
| 199 | publish | ||
| 200 | }); | ||
| 201 | if publish { | ||
| 202 | self.0.queue_publish(); | ||
| 203 | } | ||
| 192 | } | 204 | } |
| 193 | } | 205 | } |
