Skip to content

Commit faa3f3f

Browse files
committed
refactor: CommandRequest handling
Signed-off-by: Jan Zachmann <50990105+JanZachmann@users.noreply.github.com>
1 parent fab0de9 commit faa3f3f

File tree

10 files changed

+309
-306
lines changed

10 files changed

+309
-306
lines changed

Cargo.lock

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ license = "MIT OR Apache-2.0"
88
name = "omnect-device-service"
99
readme = "README.md"
1010
repository = "https://github.com/omnect/omnect-device-service.git"
11-
version = "0.41.5"
11+
version = "0.41.6"
1212

1313
[dependencies]
1414
actix-server = { version = "2.6", default-features = false }
@@ -64,6 +64,7 @@ tokio-stream = { version = "0.1", default-features = false, features = [
6464
"time",
6565
] }
6666
toml = { version = "0.8", default-features = false, features = ["parse"] }
67+
typeid = { version = "1.0", default-features = false }
6768
uuid = { version = "1.17", default-features = false }
6869
x509-parser = { version = "0.17", default-features = false }
6970
zbus = { version = "5.7", default-features = false, features = ["tokio"] }

src/twin/consent.rs

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{
55
use anyhow::{Context, Result, bail, ensure};
66
use azure_iot_sdk::client::IotMessage;
77
use log::{info, warn};
8-
use notify_debouncer_full::{Debouncer, NoCache, notify::*};
8+
use notify_debouncer_full::notify::*;
99
use serde::{Deserialize, Serialize};
1010
use serde_json::json;
1111
use std::{collections::HashMap, env, path::Path};
@@ -53,9 +53,7 @@ pub struct ConsentConfig {
5353
reset_consent_on_fail: bool,
5454
}
5555

56-
#[derive(Default)]
5756
pub struct DeviceUpdateConsent {
58-
file_observer: Option<Debouncer<INotifyWatcher, NoCache>>,
5957
tx_reported_properties: Option<Sender<serde_json::Value>>,
6058
}
6159

@@ -87,19 +85,9 @@ impl Feature for DeviceUpdateConsent {
8785
.await
8886
}
8987

90-
fn command_request_stream(&mut self) -> CommandRequestStreamResult {
91-
let (file_observer, stream) = file_modified_stream::<DeviceUpdateConsent>(vec![
92-
request_consent_path!().as_path(),
93-
history_consent_path!().as_path(),
94-
])
95-
.context("command_request_stream: cannot create file_modified_stream")?;
96-
self.file_observer = Some(file_observer);
97-
Ok(Some(stream))
98-
}
99-
10088
async fn command(&mut self, cmd: &Command) -> CommandResult {
10189
match cmd {
102-
Command::FileModified(file) => {
90+
Command::WatchPath(file) => {
10391
self.report_consent(from_json_file(&file.path)?).await?;
10492
}
10593
Command::DesiredGeneralConsent(cmd) => {
@@ -119,6 +107,26 @@ impl DeviceUpdateConsent {
119107
const USER_CONSENT_VERSION: u8 = 1;
120108
const ID: &'static str = "device_update_consent";
121109

110+
pub fn new() -> Result<Self> {
111+
for path in [request_consent_path!(), history_consent_path!()] {
112+
watch_path(
113+
Watch {
114+
command: PathCommand {
115+
feature_id: typeid::ConstTypeId::of::<Self>(),
116+
path,
117+
},
118+
event_kinds: vec![EventKind::Modify(event::ModifyKind::Data(
119+
event::DataChange::Content,
120+
))],
121+
},
122+
RecursiveMode::Recursive,
123+
)?;
124+
}
125+
Ok(DeviceUpdateConsent {
126+
tx_reported_properties: None,
127+
})
128+
}
129+
122130
fn user_consent(&self, cmd: &UserConsentCommand) -> CommandResult {
123131
info!("user consent requested: {cmd:?}");
124132

src/twin/factory_reset.rs

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::{
88
use anyhow::{Context, Result, bail};
99
use azure_iot_sdk::client::IotMessage;
1010
use log::{debug, info, warn};
11-
use notify_debouncer_full::{Debouncer, NoCache, notify::*};
11+
use notify_debouncer_full::notify::*;
1212
use serde::{Deserialize, Serialize};
1313
use serde_json::{from_reader, json};
1414
use serde_repr::*;
@@ -17,7 +17,7 @@ use std::{
1717
env,
1818
fs::{File, read_dir},
1919
io::BufReader,
20-
path::Path,
20+
path::PathBuf,
2121
};
2222
use tokio::sync::mpsc::Sender;
2323

@@ -37,8 +37,10 @@ macro_rules! config_path {
3737

3838
macro_rules! custom_config_dir_path {
3939
() => {
40-
env::var("FACTORY_RESET_CUSTOM_CONFIG_DIR_PATH")
41-
.unwrap_or("/etc/omnect/factory-reset.d".to_string())
40+
PathBuf::from(
41+
env::var("FACTORY_RESET_CUSTOM_CONFIG_DIR_PATH")
42+
.unwrap_or("/etc/omnect/factory-reset.d".to_string()),
43+
)
4244
};
4345
}
4446

@@ -92,7 +94,6 @@ struct FactoryResetReport {
9294
pub struct FactoryReset {
9395
tx_reported_properties: Option<Sender<serde_json::Value>>,
9496
report: FactoryResetReport,
95-
dir_observer: Option<Debouncer<INotifyWatcher, NoCache>>,
9697
}
9798

9899
impl Feature for FactoryReset {
@@ -136,17 +137,9 @@ impl Feature for FactoryReset {
136137
Ok(())
137138
}
138139

139-
fn command_request_stream(&mut self) -> CommandRequestStreamResult {
140-
let (dir_observer, stream) =
141-
dir_modified_stream::<FactoryReset>(vec![&Path::new(&custom_config_dir_path!())])
142-
.context("command_request_stream: cannot create dir_modified_stream")?;
143-
self.dir_observer = Some(dir_observer);
144-
Ok(Some(stream))
145-
}
146-
147140
async fn command(&mut self, cmd: &Command) -> CommandResult {
148141
match cmd {
149-
Command::DirModified(_) => {
142+
Command::WatchPath(_) => {
150143
let keys = FactoryReset::factory_reset_keys()?;
151144

152145
if keys != self.report.keys {
@@ -193,10 +186,23 @@ impl FactoryReset {
193186
result: FactoryReset::factory_reset_result()?,
194187
};
195188

189+
watch_path(
190+
Watch {
191+
command: PathCommand {
192+
feature_id: typeid::ConstTypeId::of::<Self>(),
193+
path: custom_config_dir_path!(),
194+
},
195+
event_kinds: vec![
196+
EventKind::Create(event::CreateKind::File),
197+
EventKind::Remove(event::RemoveKind::File),
198+
],
199+
},
200+
RecursiveMode::Recursive,
201+
)?;
202+
196203
Ok(FactoryReset {
197204
tx_reported_properties: None,
198205
report,
199-
dir_observer: None,
200206
})
201207
}
202208

0 commit comments

Comments
 (0)