use std::sync::mpsc;
use std::thread;
use std::time::Duration;
extern crate enocean;
fn main() {
let mut nb_received = 0;
let mut nb_sended = 0;
let port_name = "/dev/ttyUsb300".to_string(); let (enocean_emiter, enocean_event_receiver) = mpsc::channel();
let (enocean_command_receiver, enocean_commander) = mpsc::channel();
let _enocean_listener = thread::spawn(move || {
enocean::communicator::start(port_name, enocean_emiter, enocean_commander).expect("Unable to start communicator thread");
});
let power_query = enocean::eep::create_smart_plug_command(
[0x05, 0x0a, 0x3d, 0x6a],
enocean::eep::D201CommandList::QueryPower,
)
.unwrap();
let power_off = enocean::eep::create_smart_plug_command(
[0x05, 0x0a, 0x3d, 0x6a],
enocean::eep::D201CommandList::Off,
)
.unwrap();
let power_on = enocean::eep::create_smart_plug_command(
[0x05, 0x0a, 0x3d, 0x6a],
enocean::eep::D201CommandList::On,
)
.unwrap();
let _command_emiter = thread::spawn(move || loop {
match enocean_command_receiver.send(power_query.clone()) {
Ok(_t) => {}
Err(e) => eprintln!("erreur lors de l'envoi : {:?}", e),
}
nb_sended = nb_sended + 1;
thread::sleep(Duration::from_millis(1000));
match enocean_command_receiver.send(power_off.clone()) {
Ok(_t) => {}
Err(e) => eprintln!("erreur lors de l'envoi : {:?}", e),
}
nb_sended = nb_sended + 1;
thread::sleep(Duration::from_millis(1000));
match enocean_command_receiver.send(power_on.clone()) {
Ok(_t) => {}
Err(e) => eprintln!("erreur lors de l'envoi : {:?}", e),
}
nb_sended = nb_sended + 1;
thread::sleep(Duration::from_millis(1000));
println!("---> SENDED : {}", nb_sended);
thread::sleep(Duration::from_millis(3000));
});
loop {
let message = enocean_event_receiver.try_recv();
match message {
Ok(esp3_packet) => {
println! {"Received ESP3 packet : {}", esp3_packet};
nb_received = nb_received + 1;
println!("---> RECEIVED : {}", nb_received);
}
Err(ref e) if e == &std::sync::mpsc::TryRecvError::Empty => (),
Err(e) => eprintln!("{:?}", e),
}
thread::sleep(Duration::from_millis(10));
}
}