Skip to content

Commit 328af96

Browse files
authored
Merge pull request #6 from Jim-Hodapp-Coaching/basic_command_line_interface
2 parents 5c2fe49 + 8f5a1e7 commit 328af96

File tree

3 files changed

+85
-25
lines changed

3 files changed

+85
-25
lines changed

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ rand = "0.8.4"
1010
reqwest = { version = "0.11.9", features = ["blocking"] }
1111
serde = { version = "1.0", features = ["derive"] }
1212
serde_json = "1.0.74"
13+
clap = { version = "3.1.7", features = ["derive"] }
1314

1415
[dev-dependencies]
1516
regex = "1.5.4"
17+
18+
[lib]
19+
name = "ambi_mock_client"
20+
path = "src/lib.rs"

src/lib.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//! # Provides a mock Ambi client that emulates real sensor hardware such as like
2+
//! an Edge client.
3+
//!
4+
//! This file provides for a separation of concerns from main.rs for application
5+
//! logic, per the standard Rust pattern.
6+
//!
7+
//! See `main.rs` for more details about what this application does.
8+
//!
9+
//! See the `LICENSE` file for Copyright and license details.
10+
//!
11+
//!
12+
use clap::{Parser};
13+
14+
/// Defines the Ambi Mock Client command line interface as a struct
15+
#[derive(Parser, Debug)]
16+
#[clap(name = "Ambi Mock Client")]
17+
#[clap(author = "Rust Never Sleeps community (https://github.com/Jim-Hodapp-Coaching/)")]
18+
#[clap(version = "0.1.0")]
19+
#[clap(about = "Provides a mock Ambi client that emulates real sensor hardware such as an Edge client.")]
20+
#[clap(long_about = "This application emulates a real set of hardware sensors that can report on environmental conditions such as temperature, pressure, humidity, etc.")]
21+
pub struct Cli {
22+
/// Turns verbose console debug output on
23+
#[clap(short, long)]
24+
pub debug: bool,
25+
}
26+
27+
pub fn run(cli: &Cli) {
28+
println!("\r\ncli: {:?}\r\n", cli);
29+
}

src/main.rs

Lines changed: 51 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,24 @@
1+
//! # Provides a mock Ambi client that emulates real sensor hardware such as
2+
//! an Edge client.
3+
//!
4+
//! This application emulates a real set of hardware sensors that can report on
5+
//! environmental conditions such as temperature, pressure, humidity, etc.
6+
//!
7+
//! Please see the `ambi` repository for the web backend that this client connects to
8+
//! and the `edge-rs` repository for what this client is emulating.
9+
//!
10+
//! See the `LICENSE` file for Copyright and license details.
11+
//!
12+
113
use rand::{thread_rng, Rng};
214
use reqwest::blocking::Client;
315
use reqwest::header::CONTENT_TYPE;
416
use serde::{Serialize, Deserialize};
517
use std::fmt;
18+
use clap::{Parser};
19+
20+
// Internal library namespace for separation of app logic
21+
use ambi_mock_client;
622

723
#[derive(Serialize, Deserialize)]
824
struct Reading {
@@ -76,8 +92,8 @@ fn random_gen_temperature() -> String {
7692
}
7793

7894
fn random_gen_pressure() -> String {
79-
let mut rng = thread_rng();
80-
rng.gen_range(900..=1100).to_string()
95+
let mut rng = thread_rng();
96+
rng.gen_range(900..=1100).to_string()
8197
}
8298

8399
fn random_gen_dust_concentration() -> String {
@@ -86,29 +102,39 @@ fn random_gen_dust_concentration() -> String {
86102
}
87103

88104
fn main() {
89-
let dust_concentration = random_gen_dust_concentration();
90-
let air_purity = AirPurity::from_value(dust_concentration.parse::<u16>().unwrap()).to_string();
91-
let reading = Reading::new(
92-
random_gen_temperature(),
93-
random_gen_humidity(),
94-
random_gen_pressure(),
95-
dust_concentration,
96-
air_purity
97-
);
98-
99-
let json = serde_json::to_string(&reading).unwrap();
100-
const URL: &str = "http://localhost:4000/api/readings/add";
101-
102-
println!("Sending POST request to {} as JSON: {}", URL, json);
103-
104-
let client = Client::new();
105-
let res = client
106-
.post(URL)
107-
.header(CONTENT_TYPE, "application/json")
108-
.body(json)
109-
.send();
110-
111-
println!("Response: {:#?}", res);
105+
// Parses the provided command line interface arguments and flags
106+
let cli = ambi_mock_client::Cli::parse();
107+
108+
match cli.debug {
109+
true => println!("Debug mode is now *on*"),
110+
false => println!("Debug mode is now *off*")
111+
}
112+
113+
ambi_mock_client::run(&cli);
114+
115+
let dust_concentration = random_gen_dust_concentration();
116+
let air_purity = AirPurity::from_value(dust_concentration.parse::<u16>().unwrap()).to_string();
117+
let reading = Reading::new(
118+
random_gen_temperature(),
119+
random_gen_humidity(),
120+
random_gen_pressure(),
121+
dust_concentration,
122+
air_purity
123+
);
124+
125+
let json = serde_json::to_string(&reading).unwrap();
126+
const URL: &str = "http://localhost:4000/api/readings/add";
127+
128+
println!("Sending POST request to {} as JSON: {}", URL, json);
129+
130+
let client = Client::new();
131+
let res = client
132+
.post(URL)
133+
.header(CONTENT_TYPE, "application/json")
134+
.body(json)
135+
.send();
136+
137+
println!("Response: {:#?}", res);
112138
}
113139

114140
#[cfg(test)]

0 commit comments

Comments
 (0)