Skip to content

Commit

Permalink
As an experiment try using pico-args and see if it works. Clap is a v…
Browse files Browse the repository at this point in the history
…ery heavy weight crate with more deps.
  • Loading branch information
locka99 committed Jan 22, 2020
1 parent ad4ea40 commit 2786970
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 35 deletions.
8 changes: 7 additions & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion samples/simple-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ authors = ["Adam Lock <[email protected]>"]
edition = "2018"

[dependencies]
clap = "2.33"
pico-args="0.3"

[dependencies.opcua-client]
path = "../../client"
Expand Down
62 changes: 34 additions & 28 deletions samples/simple-client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,48 @@
//! 3. Subscribe to values and loop forever printing out their values
use std::sync::{Arc, RwLock};

use clap::{App, Arg};

use opcua_client::prelude::*;

fn main() {
struct Args {
help: bool,
url: String,
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
// Read command line arguments
let m = App::new("Simple OPC UA Client")
.arg(Arg::with_name("url")
.long("url")
.help("Specify the OPC UA endpoint to connect to")
.takes_value(true)
.default_value("opc.tcp://localhost:4855")
.required(false))
.get_matches();
let url = m.value_of("url").unwrap().to_string();
let mut args = pico_args::Arguments::from_env();
let args = Args {
help: args.contains(["-h", "--help"]),
url: args.opt_value_from_str("--url")?.unwrap_or(String::from("opc.tcp://localhost:4855")),
};

// Optional - enable OPC UA logging
opcua_console_logging::init();
if args.help {
println!(r#"Simple Client"#);
println!(r#"Usage: simple-client --url [url]"#);
}
else {
// Optional - enable OPC UA logging
opcua_console_logging::init();

// Make the client configuration
let mut client = ClientBuilder::new()
.application_name("Simple Client")
.application_uri("urn:SimpleClient")
.trust_server_certs(true)
.create_sample_keypair(true)
.session_retry_limit(3)
.client().unwrap();
// Make the client configuration
let mut client = ClientBuilder::new()
.application_name("Simple Client")
.application_uri("urn:SimpleClient")
.trust_server_certs(true)
.create_sample_keypair(true)
.session_retry_limit(3)
.client().unwrap();

if let Ok(session) = client.connect_to_endpoint((url.as_ref(), SecurityPolicy::None.to_str(), MessageSecurityMode::None, UserTokenPolicy::anonymous()), IdentityToken::Anonymous) {
if let Err(result) = subscribe_to_variables(session.clone()) {
println!("ERROR: Got an error while subscribing to variables - {}", result);
} else {
// Loops forever. The publish thread will call the callback with changes on the variables
let _ = Session::run(session);
if let Ok(session) = client.connect_to_endpoint((args.url.as_ref(), SecurityPolicy::None.to_str(), MessageSecurityMode::None, UserTokenPolicy::anonymous()), IdentityToken::Anonymous) {
if let Err(result) = subscribe_to_variables(session.clone()) {
println!("ERROR: Got an error while subscribing to variables - {}", result);
} else {
// Loops forever. The publish thread will call the callback with changes on the variables
let _ = Session::run(session);
}
}
}
Ok(())
}

fn subscribe_to_variables(session: Arc<RwLock<Session>>) -> Result<(), StatusCode> {
Expand Down
10 changes: 5 additions & 5 deletions types/src/url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn opc_url_from_str(s: &str) -> Result<Url, ()> {
url
})
.map_err(|err| {
error!("Cannot parse url {}, error = {:?}", s, err);
error!("Cannot parse url \"{}\", error = {:?}", s, err);
})
}

Expand All @@ -37,10 +37,10 @@ pub fn url_matches(url1: &str, url2: &str) -> bool {
if let Ok(url2) = opc_url_from_str(url2) {
return url1 == url2;
} else {
error!("Cannot parse url {}", url2);
error!("Cannot parse url \"{}\"", url2);
}
} else {
error!("Cannot parse url {}", url1);
error!("Cannot parse url \"{}\"", url1);
}
false
}
Expand All @@ -56,10 +56,10 @@ pub fn url_matches_except_host(url1: &str, url2: &str) -> bool {
return url1 == url2;
}
} else {
error!("Cannot parse url {}", url2);
error!("Cannot parse url \"{}\"", url2);
}
} else {
error!("Cannot parse url {}", url1);
error!("Cannot parse url \"{}\"", url1);
}
false
}
Expand Down

0 comments on commit 2786970

Please sign in to comment.