Skip to content

Commit b47eac6

Browse files
authored
Add resync interval (#4)
* Add a resync interval and make some log messages better * Add to changelog * code fmt
1 parent d64dff7 commit b47eac6

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
## NEXT
2+
- Add resync interval
3+
- Add some better log messages
4+
5+
## 0.1.0
6+
- First release, not in github

src/main.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,12 @@ pub async fn main() -> Result<(), Box<dyn Error>> {
107107
.multiple(true)
108108
.takes_value(false)
109109
.about("verbosity level"))
110+
.arg(Arg::with_name("resync-interval")
111+
.short('i')
112+
.required(false)
113+
.takes_value(true)
114+
.about("how frequently to check if the light changed state (e.g. via Wink or other external means)")
115+
.default_value("10000"))
110116
.arg(Arg::with_name("mqtt-uri")
111117
.short('s')
112118
.required(true)
@@ -129,6 +135,10 @@ pub async fn main() -> Result<(), Box<dyn Error>> {
129135
.default_value("homeassistant/status"))
130136
.get_matches();
131137

138+
let resync_interval: u64 = matches
139+
.value_of_t("resync-interval")
140+
.unwrap_or_else(|e| e.exit());
141+
132142
let _guard = init_logger(&matches);
133143

134144
let options = init_mqtt_client(&matches)?;
@@ -141,6 +151,7 @@ pub async fn main() -> Result<(), Box<dyn Error>> {
141151
matches.value_of("topic-prefix").unwrap(),
142152
matches.value_of("discovery-prefix"),
143153
matches.value_of("discovery-listen-topic"),
154+
resync_interval,
144155
controller,
145156
)
146157
.await;

src/syncer.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@ where
3434
topic_prefix: &str,
3535
discovery_prefix: Option<&str>,
3636
discovery_listen_topic: Option<&str>,
37+
resync_interval: u64,
3738
controller: T,
3839
) -> Arc<DeviceSyncer<T>> {
39-
info!(slog_scope::logger(), "opening_client"; "host" => options.broker_address().0, "port" => options.broker_address().1);
40+
info!(slog_scope::logger(), "opening_client"; "host" => options.broker_address().0, "port" => options.broker_address().1, "client_id" => &options.client_id());
4041
options.set_clean_session(true);
4142
let ev = EventLoop::new(options, 100).await;
4243
let (repoll_sender, repoll_rx) = bounded(10);
@@ -52,8 +53,12 @@ where
5253
let ptr_clone = ptr.clone();
5354
trace!(slog_scope::logger(), "start_thread");
5455
tokio::task::spawn(async move { Self::run_mqtt(ptr, ev).await });
56+
5557
let ptr_2 = ptr_clone.clone();
56-
tokio::task::spawn(async move { Self::run_poller(ptr_2, repoll_rx).await });
58+
tokio::task::spawn(
59+
async move { Self::run_poller(ptr_2, resync_interval, repoll_rx).await },
60+
);
61+
5762
if ptr_clone.discovery_prefix.is_some() {
5863
let ptr_3 = ptr_clone.clone();
5964
tokio::task::spawn(async move { Self::broadcast_discovery(ptr_3).await });
@@ -343,10 +348,11 @@ where
343348
Self::report_async_result("poll_all", Self::poll_all_(this).await)
344349
}
345350

346-
async fn run_poller(this: Arc<Self>, rx: Receiver<DeviceId>) -> () {
351+
async fn run_poller(this: Arc<Self>, resync_interval: u64, rx: Receiver<DeviceId>) -> () {
347352
let that = this.clone();
353+
info!(slog_scope::logger(), "poller_starting"; "resync_interval" => resync_interval);
348354
tokio::task::spawn(async move {
349-
let mut timer = tokio::time::interval(Duration::from_secs(10));
355+
let mut timer = tokio::time::interval(Duration::from_millis(resync_interval));
350356
loop {
351357
timer.tick().await;
352358
let _ = that.repoll.send(0).await;
@@ -387,6 +393,7 @@ where
387393
device.id
388394
);
389395
let config = v.discovery_info.to_string();
396+
info!(slog_scope::logger(), "discovered_device"; "id" => id, "name" => &device.name);
390397
debug!(slog_scope::logger(), "broadcast_discovery_result"; "id" => id, "topic" => &topic, "config" => &config);
391398
this.sender
392399
.send(Request::Publish(Publish::new(

0 commit comments

Comments
 (0)