Read params also from config file.

master
flabbergast 4 years ago
parent bc50d5f10f
commit 1e9a344356
  1. 7
      Cargo.lock
  2. 1
      Cargo.toml
  3. 11
      jee_config.yml
  4. 33
      src/cli_args.yml
  5. 86
      src/main.rs

7
Cargo.lock generated

@ -277,6 +277,7 @@ dependencies = [
"log 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)",
"loggerv 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
"mongodb 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
"num-traits 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
"rumqtt 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.69 (registry+https://github.com/rust-lang/crates.io-index)",
@ -430,6 +431,11 @@ name = "num-traits"
version = "0.1.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "num-traits"
version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "num_cpus"
version = "1.7.0"
@ -898,6 +904,7 @@ dependencies = [
"checksum num-integer 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)" = "d1452e8b06e448a07f0e6ebb0bb1d92b8890eea63288c0b627331d53514d0fba"
"checksum num-iter 0.1.34 (registry+https://github.com/rust-lang/crates.io-index)" = "7485fcc84f85b4ecd0ea527b14189281cf27d60e583ae65ebc9c088b13dffe01"
"checksum num-traits 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "99843c856d68d8b4313b03a17e33c4bb42ae8f6610ea81b28abe076ac721b9b0"
"checksum num-traits 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "630de1ef5cc79d0cdd78b7e33b81f083cbfe90de0f4b2b2f07f905867c70e9fe"
"checksum num_cpus 1.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "514f0d73e64be53ff320680ca671b64fe3fb91da01e1ae2ddc99eb51d453b20d"
"checksum openssl 0.9.20 (registry+https://github.com/rust-lang/crates.io-index)" = "8bf434ff6117485dc16478d77a4f5c84eccc9c3645c4da8323b287ad6a15a638"
"checksum openssl-sys 0.9.20 (registry+https://github.com/rust-lang/crates.io-index)" = "0ad395f1cee51b64a8d07cc8063498dc7554db62d5f3ca87a67f4eed2791d0c8"

@ -11,6 +11,7 @@ loggerv = "0.7.1"
error-chain = "~0.12.0"
bus = "~2.0.0"
rand = "0.5.3"
num-traits = "0.2"
rumqtt = "~0.10.1"
bson = "0.10"
serde = "1.0.69"

@ -3,25 +3,24 @@ devices:
id: 17
name: "bedroom/node/high/"
dev_type:
type: "TempHumBattV2"
type: TempHumBattV2
"18":
id: 18
name: "garage/node/low/"
dev_type:
type: "TempBattV1"
type: TempBattV1
"19":
id: 19
name: "kitchen/node/low/"
dev_type:
type: "TempBattV2"
type: TempBattV2
"20":
id: 20
name: "conservatory/node/low/"
dev_type:
type: "TempHumLuxBattV1"
type: TempHumLuxBattV1
"21":
id: 21
name: "outside/node/front/"
dev_type:
type: "TempHumBattV1"
type: TempHumBattV1

@ -10,27 +10,23 @@ args:
- port_in:
short: i
long: port_in
default_value: "5003"
takes_value: true
help: Port to connect to (usually ser2net)
help: Port to connect to (usually ser2net) (default 5003)
- host_in:
short: I
long: host_in
default_value: localhost
takes_value: true
help: Host to connect to (usually ser2net)
help: Host to connect to (usually ser2net) (default localhost)
- port_out:
short: o
long: port_out
default_value: "7777"
takes_value: true
help: Port to bind for serving data out
help: Port to bind for serving data out (default 7777)
- host_out:
short: O
long: host_out
default_value: localhost
takes_value: true
help: Host to bind for serving data out
help: Host to bind for serving data out (default localhost)
- enable_mqtt:
short: t
long: enable_mqtt
@ -38,15 +34,13 @@ args:
- port_mqtt:
short: m
long: port_mqtt
default_value: "1883"
takes_value: true
help: MQTT broker port
help: MQTT broker port (default 1883)
- host_mqtt:
short: M
long: host_mqtt
default_value: localhost
takes_value: true
help: MQTT broker host
help: MQTT broker host (default localhost)
- enable_mongo:
short: g
long: enable_mongo
@ -54,27 +48,23 @@ args:
- port_mongo:
short: b
long: port_mongo
default_value: "27017"
takes_value: true
help: MongoDB server port
help: MongoDB server port (default 27017)
- host_mongo:
short: B
long: host_mongo
default_value: localhost
takes_value: true
help: MongoDB server host
help: MongoDB server host (default localhost)
- mqtt_raw_topic:
short: R
long: mqtt_raw_topic
default_value: jeenodes/in
takes_value: true
help: MQTT topic for posting incoming raw packets
help: MQTT topic for posting incoming raw packets (default jeenodes/in)
- mqtt_post_prefix:
short: P
long: mqtt_post_prefix
default_value: env
takes_value: true
help: MQTT topic prefix for posting processed sensor data
help: MQTT topic prefix for posting processed sensor data (default env)
- enable_csv:
short: s
long: enable_csv
@ -82,7 +72,6 @@ args:
- csv_path:
short: c
long: csv_path
default_value: .
takes_value: true
help: Directory for saving csv files
help: Directory for saving csv files (default '.')

@ -9,6 +9,7 @@ extern crate config;
extern crate bus;
extern crate rand;
extern crate num_traits;
extern crate rumqtt;
@ -29,6 +30,8 @@ use std::sync::{Arc, Mutex};
use std::time::Duration;
use std::collections::HashMap;
use num_traits::cast::{cast,NumCast};
use clap::App;
use rumqtt::{MqttOptions, MqttClient};
@ -83,9 +86,34 @@ fn handle_write_to_client(mut client: TcpStream, mut from_s2n: BusReader<String>
}
// set parameter value (numerical)
fn get_param<T>(matches: &clap::ArgMatches, settings: &mut config::Config, param: &str, deft: T) -> T
where T: std::str::FromStr+NumCast {
if matches.is_present(param) {
value_t!(matches, param, T) .unwrap_or_else(|e| e.exit())
} else {
settings.set_default::<i64>(param, cast(deft).unwrap())
.expect("Problem setting defaults");
cast( settings.get_int(param)
.expect(&format!("Problem parsing '{}' from settings file", param)) ).unwrap()
}
}
// set parameter value (string)
fn get_param_str(matches: &clap::ArgMatches, settings: &mut config::Config, param: &str, deft: &str)
-> String {
if matches.is_present(param) {
matches.value_of(param).unwrap().to_string()
} else {
settings.set_default(param, deft)
.expect("Problem setting defaults");
settings.get_str(param)
.expect(&format!("Problem parsing '{}' from settings file",param))
}
}
quick_main!(|| -> Result<()> {
// parse arguments
// set up argument parsing
let yaml = load_yaml!("cli_args.yml");
let matches = App::from_yaml(yaml)
.name(crate_name!())
@ -93,36 +121,42 @@ quick_main!(|| -> Result<()> {
.author(crate_authors!())
.get_matches();
// initialise logger
loggerv::init_with_verbosity(matches.occurrences_of("v")).unwrap();
warn!("{} v{} starting...", crate_name!(), crate_version!());
// settings
let mut settings = config::Config::default();
// actually read the settings from file
settings.merge(config::File::with_name("jee_config.yml"))
.expect("Problem loading settings");
// set up the variables (cmdline overrides cfgfile overrides default)
let node_list = settings.get::<HashMap<String,decode_jee::DeviceDesc>>("devices")
.unwrap_or_else(|_| HashMap::<String,decode_jee::DeviceDesc>::new());
let port_s2n: u16 = get_param(&matches, &mut settings, "port_in", 5003);
let host_s2n: String = get_param_str(&matches, &mut settings, "host_in", "localhost");
let port_serve: u16 = get_param(&matches, &mut settings, "port_out", 7777);
let host_serve: String = get_param_str(&matches, &mut settings, "host_out", "localhost");
let port_mqtt: u16 = get_param(&matches, &mut settings, "port_mqtt", 1883);
let host_mqtt: String = get_param_str(&matches, &mut settings, "host_mqtt", "localhost");
let port_mongo: u16 = get_param(&matches, &mut settings, "port_mongo", 27017);
let host_mongo: String = get_param_str(&matches, &mut settings, "host_mongo", "localhost");
let mqtt_enabled = matches.is_present("enable_mqtt");
let mongo_enabled = matches.is_present("enable_mongo");
let port_s2n = value_t_or_exit!(matches, "port_in", u16);
let host_s2n = matches.value_of("host_in").unwrap();
let port_serve = value_t_or_exit!(matches, "port_out", u16);
let host_serve = matches.value_of("host_out").unwrap();
let port_mqtt = value_t_or_exit!(matches, "port_mqtt", u16);
let host_mqtt = matches.value_of("host_mqtt").unwrap();
let port_mongo = value_t_or_exit!(matches, "port_mongo", u16);
let host_mongo = matches.value_of("host_mongo").unwrap();
// aux params
let auxopts = decode_jee::AuxOptions {
mqtt_post_raw_topic: matches.value_of("mqtt_raw_topic").unwrap().to_string(),
mqtt_post_processed_prefix: matches.value_of("mqtt_post_prefix").unwrap().to_string(),
csv_path: matches.value_of("csv_path").unwrap().to_string(),
mqtt_post_raw_topic: get_param_str(&matches, &mut settings, "mqtt_raw_topic", "jeenodes/in"),
mqtt_post_processed_prefix: get_param_str(&matches, &mut settings, "mqtt_post_prefix", "env"),
csv_path: get_param_str(&matches, &mut settings, "csv_path", "."),
csv_enabled: matches.is_present("enable_csv"),
};
let mut settings = config::Config::default();
settings.merge(config::File::with_name("jee_config.yml")).unwrap(); // TODO
let node_list = settings.get::<HashMap<String,decode_jee::DeviceDesc>>("devices").unwrap();
// initialise logger
loggerv::init_with_verbosity(matches.occurrences_of("v")).unwrap();
warn!("{} v{} starting...", crate_name!(), crate_version!());
// mqtt setup and connect
let mqtt_client_options = MqttOptions::new()
.set_keep_alive(5)
.set_reconnect(3)
@ -137,8 +171,6 @@ quick_main!(|| -> Result<()> {
//debug!("message --> {:?} {:?}", message, message.payload);
//});
// mqtt setup and connect
let mut mqtt_client : Option<rumqtt::MqttClient> = if mqtt_enabled {
Some(MqttClient::start(mqtt_client_options, None) // Some(print_messages_cb)
.chain_err(|| format!("Can't start mqtt client for {}:{}.", host_mqtt, port_mqtt))?)
@ -152,7 +184,7 @@ quick_main!(|| -> Result<()> {
// mongodb connect
let mongo_client = if mongo_enabled {
Some(Client::connect(host_mongo, port_mongo)
Some(Client::connect(&host_mongo, port_mongo)
.chain_err(|| format!("Can't connect to mongo database at {}:{}.", host_mongo, port_mongo))?)
} else { None };
if mongo_client.is_some() {
@ -169,7 +201,7 @@ quick_main!(|| -> Result<()> {
// ser2net stream connect
let s2n_stream = TcpStream::connect( (host_s2n, port_s2n) )
let s2n_stream = TcpStream::connect( (host_s2n.as_str(), port_s2n) )
.chain_err(|| format!("Can't connect to '{}:{}': ", host_s2n, port_s2n))?;
// create some copies of the stream for reading/writing
let input_s2n_stream = s2n_stream.try_clone().unwrap();
@ -209,7 +241,7 @@ quick_main!(|| -> Result<()> {
// start tcp server for forwarding messages to clients
let listen_serve = TcpListener::bind( (host_serve, port_serve) )
let listen_serve = TcpListener::bind( (host_serve.as_str(), port_serve) )
.chain_err(|| format!("Can't bind to {}:{}: ", host_serve, port_serve))?;
info!("bound TCP listener to {}:{}", host_serve, port_serve);

Loading…
Cancel
Save