2019-01-16 21:46:12 +00:00
|
|
|
use crate::light::{Command, Value};
|
2019-01-20 12:02:39 +00:00
|
|
|
use log::warn;
|
2019-01-16 21:46:12 +00:00
|
|
|
use regex::Regex;
|
|
|
|
use rumqtt;
|
|
|
|
use rumqtt::{Notification, Publish, Receiver};
|
|
|
|
|
|
|
|
pub struct MqttCommands {
|
|
|
|
notifications: Receiver<Notification>,
|
|
|
|
commands: crossbeam_channel::Sender<Command>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MqttCommands {
|
|
|
|
pub fn new(
|
|
|
|
notifications: Receiver<Notification>,
|
|
|
|
commands: crossbeam_channel::Sender<Command>,
|
|
|
|
) -> Self {
|
|
|
|
MqttCommands {
|
|
|
|
notifications,
|
|
|
|
commands,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn listen(&self) {
|
|
|
|
loop {
|
|
|
|
match self.notifications.recv() {
|
|
|
|
Ok(notification) => {
|
|
|
|
println!("MQTT notification received: {:#?}", notification);
|
|
|
|
match notification {
|
|
|
|
Notification::Publish(data) => self.handle_publish(data),
|
|
|
|
Notification::PubAck(_) => {}
|
|
|
|
Notification::PubRec(_) => {}
|
|
|
|
Notification::PubRel(_) => {}
|
|
|
|
Notification::PubComp(_) => {}
|
|
|
|
Notification::SubAck(_) => {}
|
|
|
|
Notification::None => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(recv_error) => {
|
|
|
|
println!("MQTT channel closed: {}", recv_error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_publish(&self, data: Publish) {
|
|
|
|
lazy_static! {
|
|
|
|
static ref matchStr: String = format!(r"^{}/(\w+)/command/(\w+)$", crate::MQTT_ID);
|
|
|
|
static ref RE: Regex = Regex::new(&matchStr).unwrap();
|
|
|
|
}
|
|
|
|
let mut matching = RE.find_iter(&data.topic_name);
|
2019-01-20 12:02:39 +00:00
|
|
|
let lamp = match matching.next() {
|
|
|
|
Some(lamp) => lamp.as_str(),
|
|
|
|
None => {
|
|
|
|
warn!("Failed to parse command (lamp)");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let command = match matching.next() {
|
|
|
|
Some(command) => command.as_str(),
|
|
|
|
None => {
|
|
|
|
warn!("Failed to parse command (command)");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
2019-01-16 21:46:12 +00:00
|
|
|
|
2019-01-20 12:02:39 +00:00
|
|
|
match Value::new(command, data.payload.to_vec()) {
|
|
|
|
Some(value) => {
|
|
|
|
if let Err(err) = self.commands.send(Command {
|
|
|
|
lampname: lamp.to_owned(),
|
|
|
|
command: value,
|
|
|
|
}) {
|
|
|
|
warn!("{}", err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => warn!("Command value could not be created"),
|
|
|
|
}
|
2019-01-16 21:46:12 +00:00
|
|
|
}
|
|
|
|
}
|