extern crate chrono;
extern crate sensd;
extern crate serde;
use sensd::action::{Action, actions, IOCommand, Trigger};
use sensd::errors::ErrorType;
use sensd::io::{IOKind, RawValue, Input, Device};
use sensd::storage::{Group, Persistent};
const FREQUENCY: std::time::Duration = std::time::Duration::from_secs(1);
fn init(name: &str) -> Group {
let group = Group::new(name.clone());
println!("Initialized poll group: \"{}\"", name);
group
}
fn poll(poller: &mut Group) -> Result<(), ErrorType> {
match poller.poll() {
Ok(_) => match poller.save() {
Ok(_) => println!("\n"),
Err(t) => {
return Err(t);
}
},
_ => (),
};
Ok(())
}
fn main() {
let mut poller = init("main");
{
let name = "test name";
let id = 0;
let kind = IOKind::PH;
let command = IOCommand::Input(|| RawValue::Float(1.2));
let mut input =
Input::new(
name,
id,
Some(kind),
).set_command(
command
).init_log(
).init_publisher();
input.publisher_mut().as_mut().unwrap()
.subscribe(
actions::Threshold::new(
format!("Subscriber for Input:{}", id),
RawValue::Float(1.0),
Trigger::GT,
).into_boxed()
);
poller.push_input(input);
}
{
let name = "second sensor";
let id = 1;
let kind = IOKind::PH;
let command = IOCommand::Input(|| RawValue::Float(1.2));
let mut input = Input::new(
name,
id,
kind,
)
.set_command(command)
.init_log()
.init_publisher();
input.publisher_mut().as_mut().unwrap()
.subscribe(
actions::Threshold::new(
format!("Subscriber for Input:{}", id),
RawValue::Float(1.0),
Trigger::GT,
).into_boxed()
);
poller.push_input(input);
}
println!("█▓▒░ Beginning polling ░▒▓█\n");
loop {
poll(&mut poller).expect("Error occurred during polling");
std::thread::sleep(FREQUENCY);
}
}