Skip to content

Commit

Permalink
Add write and filter
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert-Steiner committed Apr 5, 2020
1 parent 1a4bb7a commit 493ba7c
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 48 deletions.
38 changes: 37 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,37 @@
/target
# Generated by Cargo
# will have compiled files and executables
/target/

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk

# General
.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon

# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
15 changes: 9 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ edition = "2018"
btleplug = "0.4.1"
influxdb = { version = "0.1.0", features = ["derive"] }
chrono = { version = "0.4", features = ["serde"] }
tokio = { version = "0.2.15", features = ["rt-core", "rt-threaded", "sync", "stream"] }
tokio = { version = "0.2.16", features = ["rt-core", "rt-threaded", "sync", "stream"] }
ruuvi-sensor-protocol = "0.4.1"
structopt = { version = "0.3", features = [ "paw" ] }
paw = "1.0"
colored = "1.9"
failure = "0.1.7"
exitfailure = "0.5.1"
atty = "0.2.14"
serde = "1.0.106"
serde_json = "1.0"

[[bin]]
name = "ruuvi-cli"
Expand Down
10 changes: 6 additions & 4 deletions src/bin/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ enum Args {
#[structopt(
short = "m",
long = "mac",
required = true,
help = "MAC address of the RuuviTag.",
parse(try_from_str = parse_address)
)]
Expand All @@ -40,7 +39,10 @@ enum Args {
influxdb_measurement_name: String,
},
Find {},
Show {},
Write {
#[structopt(short = "n", long = "normalize", help = "Normalize sensor values.")]
normalize: bool,
},
}

#[paw::main]
Expand Down Expand Up @@ -74,8 +76,8 @@ fn main(args: Args) -> Result<(), ExitFailure> {
Args::Find {} => {
controller.find();
}
Args::Show {} => {
controller.show();
Args::Write { normalize } => {
controller.write(normalize);
}
}
Ok(())
Expand Down
39 changes: 33 additions & 6 deletions src/controller.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use crate::ble::Event;
use crate::ble::Event::{DeviceDiscovered, DeviceUpdated};
use crate::influx::{run_influx_db, InfluxDBConnector};
use crate::ruuvitag::RuuviTag;
use atty::Stream;
use btleplug::api::BDAddr;
use serde_json;
use std::sync::mpsc;
use std::sync::mpsc::{Receiver, Sender};
use std::thread;
Expand Down Expand Up @@ -35,12 +38,24 @@ impl Controller {
rt.block_on(async move { run_influx_db(influx_client, &measurement_name[..]).await });
});

loop {
let event = self.receiver.recv().unwrap();
if let DeviceUpdated(tag) = event {
let func = if ruuvitags_macs.is_empty() {
let without_filter: Box<dyn Fn(RuuviTag) -> ()> = Box::new(|tag: RuuviTag| {
let _ = sender.send(tag);
});
without_filter
} else {
let with_filter: Box<dyn Fn(RuuviTag) -> ()> = Box::new(|tag: RuuviTag| {
if ruuvitags_macs.contains(&tag.mac) {
let _ = sender.send(tag);
}
});
with_filter
};

loop {
let event = self.receiver.recv().unwrap();
if let DeviceUpdated(tag) = event {
func(tag);
}
}
}
Expand All @@ -54,12 +69,24 @@ impl Controller {
}
}

pub fn show(self) {
pub fn write(self, normalize: bool) {
loop {
let event = self.receiver.recv().unwrap();
match event {
DeviceDiscovered(tag) | DeviceUpdated(tag) => println!("{}", tag),
};
DeviceDiscovered(tag) | DeviceUpdated(tag) => {
let maybe_normalized = if normalize {
tag.normalize_sensor_values()
} else {
tag
};

if atty::is(Stream::Stdout) {
println!("{}", maybe_normalized);
} else {
println!("{}", serde_json::to_string(&maybe_normalized).unwrap());
}
}
}
}
}
}
7 changes: 5 additions & 2 deletions src/influx.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::ruuvitag::{RuuviTag, SensorValuesNormalized};
use crate::ruuvitag::{RuuviTag, SensorValuesNormalized, SensorValuesType};
use chrono::{DateTime, Utc};
use influxdb::{Client, InfluxDbWriteable};
use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender};
Expand All @@ -22,7 +22,10 @@ struct RuuviTagMeasurement {

impl From<RuuviTag> for RuuviTagMeasurement {
fn from(tag: RuuviTag) -> Self {
let values_normalized = SensorValuesNormalized::from(&tag.sensor_values);
let values_normalized = match tag.sensor_values {
SensorValuesType::Normalized(values_normalized) => values_normalized,
SensorValuesType::Raw(values_raw) => SensorValuesNormalized::from(&values_raw),
};

RuuviTagMeasurement {
time: tag.time,
Expand Down
Loading

0 comments on commit 493ba7c

Please sign in to comment.