-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bd3e913
commit 10d543d
Showing
8 changed files
with
194 additions
and
70 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use crate::ble::Event; | ||
use crate::ble::Event::{DeviceDiscovered, DeviceUpdated}; | ||
use crate::influx::{run_influx_db, InfluxDBConnector}; | ||
use btleplug::api::BDAddr; | ||
use std::sync::mpsc; | ||
use std::sync::mpsc::{Receiver, Sender}; | ||
use std::thread; | ||
use tokio::runtime; | ||
|
||
pub struct Controller { | ||
receiver: Receiver<Event>, | ||
} | ||
|
||
impl Controller { | ||
pub fn new() -> (Controller, Sender<Event>) { | ||
let (sender, receiver) = mpsc::channel(); | ||
(Controller { receiver }, sender) | ||
} | ||
|
||
pub fn collect( | ||
self, | ||
ruuvitags_macs: &Vec<BDAddr>, | ||
influxdb_url: &str, | ||
influxdb_db_name: &str, | ||
influxdb_measurement_name: &str, | ||
) { | ||
let (influx_client, sender) = InfluxDBConnector::new(influxdb_url, influxdb_db_name); | ||
let measurement_name = influxdb_measurement_name.to_owned(); | ||
thread::spawn(move || { | ||
let mut rt = runtime::Builder::new() | ||
.threaded_scheduler() | ||
.enable_all() | ||
.build() | ||
.unwrap(); | ||
let _ = rt | ||
.block_on(async move { run_influx_db(influx_client, &measurement_name[..]).await }); | ||
}); | ||
|
||
loop { | ||
let event = self.receiver.recv().unwrap(); | ||
match event { | ||
DeviceUpdated(tag) => { | ||
if ruuvitags_macs.contains(&tag.mac) { | ||
let _ = sender.send(tag); | ||
} | ||
} | ||
_ => (), | ||
} | ||
} | ||
} | ||
|
||
pub fn find(self) { | ||
loop { | ||
let event = self.receiver.recv().unwrap(); | ||
match event { | ||
DeviceDiscovered(tag) => println!("Found RuuviTag: {}", tag.mac), | ||
_ => (), | ||
}; | ||
} | ||
} | ||
|
||
pub fn show(self) { | ||
loop { | ||
let event = self.receiver.recv().unwrap(); | ||
match event { | ||
DeviceDiscovered(tag) | DeviceUpdated(tag) => println!("{}", tag), | ||
}; | ||
} | ||
} | ||
} |
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,3 +1,4 @@ | ||
pub mod ble; | ||
pub mod controller; | ||
pub mod influx; | ||
pub mod ruuvitag; |
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