Skip to content

Commit

Permalink
Add sensors subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
Vourhey committed Apr 20, 2020
1 parent b226544 commit 6622491
Show file tree
Hide file tree
Showing 8 changed files with 241 additions and 1 deletion.
130 changes: 129 additions & 1 deletion Cargo.lock

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

3 changes: 3 additions & 0 deletions bin/node/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,13 @@ rosrust = { version = "0.9", optional = true }
substrate-ros-api = { path = "../../../substrate-ros/api", optional = true }
robonomics-ros-api = { path = "../../../robonomics/ros-api", optional = true }
robonomics-protocol = { path = "../../../robonomics/protocol", optional = true }
robonomics-sensors = { path = "../../../robonomics/sensors", optional = true }

[build-dependencies]
ipci-runtime = { path = "../ipci-runtime" }
robonomics-runtime = { path = "../robonomics-runtime" }
robonomics-protocol = { path = "../../../robonomics/protocol", optional = true }
robonomics-sensors = { path = "../../../robonomics/sensors", optional = true }
sc-cli = { version = "0.8.0-alpha.6", optional = true }
frame-benchmarking-cli = { version = "2.0.0-alpha.6", optional = true }
substrate-build-script-utils = "2.0.0-alpha.6"
Expand All @@ -125,6 +127,7 @@ cli = [
"sc-service/rocksdb",
"parity-util-mem/jemalloc-global",
"robonomics-protocol/cli",
"robonomics-sensors/cli",
]

## Enable browser bundle features.
Expand Down
6 changes: 6 additions & 0 deletions bin/node/cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ pub enum Subcommand {
about = "Store hex-encoded data on blockchain."
)]
Datalog(robonomics_protocol::cli::DatalogCmd),
#[cfg(feature = "robonomics-protocol")]
#[structopt(
name = "sensors",
about = "Reads data from sensor."
)]
Sensor(robonomics_sensors::cli::SensorCmd),
/// The custom benchmark subcommmand benchmarking runtime pallets.
#[cfg(feature = "benchmarking-cli")]
#[structopt(
Expand Down
6 changes: 6 additions & 0 deletions bin/node/cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ pub fn run() -> sc_cli::Result<()> {
runner.sync_run(|_|
subcommand.run().map_err(|e| sc_cli::Error::Other(e.to_string())))
}
#[cfg(feature = "robonomics-protocol")]
Some(Subcommand::Sensor(subcommand)) => {
let runner = cli.create_runner(subcommand)?;
runner.sync_run(|_|
subcommand.run().map_err(|e| sc_cli::Error::Other(e.to_string())))
}
#[cfg(feature = "benchmarking-cli")]
Some(Subcommand::Benchmark(subcommand)) => {
use crate::service::{RobonomicsExecutor, IpciExecutor};
Expand Down
1 change: 1 addition & 0 deletions robonomics/sensors/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ robonomics-protocol = { path = "../protocol" }
sc-cli = { version = "0.8.0-alpha.6", optional = true }
structopt = { version = "0.3.8", optional = true }
async-std = { version = "1.5", optional = true }
sds011 = "0.1"

[features]
default = []
Expand Down
52 changes: 52 additions & 0 deletions robonomics/sensors/src/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
///////////////////////////////////////////////////////////////////////////////
//
// Copyright 2018-2020 Airalab <[email protected]>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///////////////////////////////////////////////////////////////////////////////
use robonomics_protocol::error::Result;
use async_std::task;
use crate::sensor;

/// Command for sensor
#[derive(Debug, structopt::StructOpt, Clone)]
pub struct SensorCmd {
/// Sensor serial port
#[structopt(long, default_value = "/dev/ttyUSB0")]
port: String,

#[allow(missing_docs)]
#[structopt(flatten)]
pub shared_params: sc_cli::SharedParams,
#[allow(missing_docs)]
#[structopt(flatten)]
pub import_params: sc_cli::ImportParams,
}

impl sc_cli::CliConfiguration for SensorCmd {
fn shared_params(&self) -> &sc_cli::SharedParams {
&self.shared_params
}

fn import_params(&self) -> Option<&sc_cli::ImportParams> {
Some(&self.import_params)
}
}

impl SensorCmd {
/// Runs the command and node as sensor reader
pub fn run(&self) -> Result<()> {
task::block_on(sensor::read_loop(self.port.to_string().as_ref()))
}
}
5 changes: 5 additions & 0 deletions robonomics/sensors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,8 @@
//
///////////////////////////////////////////////////////////////////////////////
//! Robonomics Network supported sensors.
pub mod sensor;

#[cfg(feature = "cli")]
pub mod cli;
39 changes: 39 additions & 0 deletions robonomics/sensors/src/sensor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
///////////////////////////////////////////////////////////////////////////////
//
// Copyright 2018-2020 Airalab <[email protected]>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///////////////////////////////////////////////////////////////////////////////
use robonomics_protocol::error::Result;
use std::thread::sleep;
use std::time::Duration;
use sds011::SDS011;

pub async fn read_loop(port: &str) -> Result<()> {
match SDS011::new(port) {
Ok(mut sensor) => {
sensor.set_work_period(5u8).unwrap();

loop {
if let Some(m) = sensor.query() {
println!("{:?}", m);
}

sleep(Duration::from_secs(5u64 * 60));
}
},
Err(e) => println!("{:?}", e.description),
};
Ok(())
}

0 comments on commit 6622491

Please sign in to comment.