-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for remote config changes
- Loading branch information
1 parent
93920a5
commit 864a6cc
Showing
5 changed files
with
48 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ pub(crate) enum Error { | |
NoConfigFileFound, | ||
FileIoError, | ||
ParsingError, | ||
SerializationError, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,53 @@ | ||
use std::{sync::mpsc::Sender, thread}; | ||
|
||
use rumqttc::{Client, ClientError, MqttOptions, Packet, Publish}; | ||
use rumqttc::{Client, MqttOptions, Packet, Publish}; | ||
|
||
use crate::common::Command; | ||
use crate::{ | ||
common::{Command, Config}, | ||
system::get_cpu_serial, | ||
}; | ||
|
||
pub(crate) struct Listener { | ||
pub id: String, | ||
pub host: String, | ||
pub port: u16, | ||
pub(crate) struct Listener<F: Fn() -> Config + std::marker::Send> { | ||
pub get_config: F, | ||
pub sender: Sender<Command>, | ||
} | ||
|
||
impl Listener { | ||
pub(crate) fn start(self) -> Result<(), ClientError> { | ||
let (client, mut connection) = | ||
Client::new(MqttOptions::new(&self.id, self.host, self.port), 64); | ||
impl<F: Fn() -> Config + std::marker::Send + 'static> Listener<F> { | ||
pub(crate) fn start(self) -> () { | ||
thread::spawn(move || 'outer: loop { | ||
let config = (self.get_config)(); | ||
let id = config.id.unwrap_or(get_cpu_serial().unwrap()); | ||
let (client, mut connection) = | ||
Client::new(MqttOptions::new(&id, config.host, config.port), 64); | ||
|
||
client.subscribe("screens", rumqttc::QoS::AtLeastOnce)?; | ||
client.subscribe(format!("screens/{}", self.id), rumqttc::QoS::AtLeastOnce)?; | ||
client | ||
.subscribe("screens", rumqttc::QoS::AtLeastOnce) | ||
.unwrap(); | ||
client | ||
.subscribe(format!("screens/{}", id), rumqttc::QoS::AtLeastOnce) | ||
.unwrap(); | ||
|
||
let sender = self.sender.clone(); | ||
|
||
thread::spawn(move || { | ||
for event in connection.iter() { | ||
println!("{:?}", event); | ||
|
||
if let Ok(rumqttc::Event::Incoming(Packet::Publish(Publish { | ||
topic, | ||
payload, | ||
.. | ||
}))) = event | ||
if let Ok(rumqttc::Event::Incoming(Packet::Publish(Publish { payload, .. }))) = | ||
event | ||
{ | ||
if topic == "commands" { | ||
if let Ok(command) = serde_json::from_slice::<Command>(&payload) { | ||
println!("{:?}", command); | ||
if let Ok(command) = serde_json::from_slice::<Command>(&payload) { | ||
println!("{:?}", command); | ||
|
||
self.sender.send(command).unwrap(); | ||
if let Command::SetConfig(config) = command { | ||
config.save().unwrap(); | ||
client.disconnect().unwrap(); | ||
continue 'outer; | ||
} | ||
|
||
sender.send(command).unwrap(); | ||
} | ||
} | ||
} | ||
}); | ||
|
||
Ok(()) | ||
} | ||
} |