Skip to content

Commit

Permalink
Add support for remote config changes
Browse files Browse the repository at this point in the history
  • Loading branch information
VanuPhantom committed Jul 12, 2024
1 parent 93920a5 commit 864a6cc
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 32 deletions.
4 changes: 2 additions & 2 deletions src/common.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use serde::Deserialize;
use serde::{Deserialize, Serialize};

#[derive(Debug, Deserialize)]
#[derive(Debug, Deserialize, Serialize)]
pub struct Config {
pub id: Option<String>,
pub host: String,
Expand Down
10 changes: 10 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,14 @@ impl Config {

serde_json::from_reader(BufReader::new(file)).or(Err(Error::ParsingError))
}

pub fn save(&self) -> Result<(), Error> {
let file = File::options()
.write(true)
.truncate(true)
.open("config.json")
.or(Err(Error::FileIoError))?;

serde_json::to_writer(file, self).or(Err(Error::SerializationError))
}
}
1 change: 1 addition & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ pub(crate) enum Error {
NoConfigFileFound,
FileIoError,
ParsingError,
SerializationError,
}
9 changes: 3 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,17 @@ fn main() -> wry::Result<()> {

println!("The CPU serial number is {}", serial);

println!("Loading the config...");
let config = Config::load(cli.default_config_path.map(|p| PathBuf::from(p))).unwrap();
let default_config_path = cli.default_config_path.map(|p| PathBuf::from(p));

println!("Opening {}...", cli.url);

let (sender, receiver) = channel();

let listener = mqtt::Listener {
id: config.id.unwrap_or(serial),
host: config.host,
port: config.port,
get_config: move || Config::load(default_config_path.clone()).unwrap(),
sender,
};

listener.start().unwrap();
listener.start();
browser::spawn_browser(cli.url, Some(receiver))
}
56 changes: 32 additions & 24 deletions src/mqtt.rs
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(())
}
}

0 comments on commit 864a6cc

Please sign in to comment.