-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0131a85
commit e3eb1b7
Showing
2 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
use opcua::client::prelude::*; | ||
use std::sync::Arc; | ||
use opcua::server::session; | ||
use opcua::sync::*; | ||
|
||
fn main() { | ||
// 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") | ||
.product_uri("urn:SimpleClient") | ||
.trust_server_certs(true) | ||
.create_sample_keypair(true) | ||
.session_retry_limit(3) | ||
.client() | ||
.unwrap(); | ||
|
||
// Create an endpoint. The EndpointDescription can be made from a tuple consisting of | ||
// the endpoint url, security policy, message security mode and user token policy. | ||
let endpoint: EndpointDescription = ( | ||
"opc.tcp://localhost:48401/", | ||
"None", | ||
MessageSecurityMode::None, | ||
UserTokenPolicy::anonymous(), | ||
) | ||
.into(); | ||
|
||
let session = client | ||
.connect_to_endpoint(endpoint, IdentityToken::Anonymous) | ||
.unwrap(); | ||
|
||
let session = session.write(); | ||
// let nodes_to_read:ReadValueId = (NodeId::new(1, 100007), AttributeId::Value, | ||
// session.read(nodes_to_read, timestamps_to_return, max_age) | ||
// let _ = Session::run(session); | ||
} | ||
|
||
fn subscribe_to_values(session: Arc<RwLock<Session>>) -> Result<(), StatusCode> { | ||
let session = session.write(); | ||
// Create a subscription polling every 2s with a callback | ||
let subscription_id = session.create_subscription( | ||
2000.0, | ||
10, | ||
30, | ||
0, | ||
0, | ||
true, | ||
DataChangeCallback::new(|changed_monitored_items| { | ||
println!("Data change from server:"); | ||
changed_monitored_items | ||
.iter() | ||
.for_each(|item| print_value(item)); | ||
}), | ||
)?; | ||
// Create some monitored items | ||
let items_to_create: Vec<MonitoredItemCreateRequest> = ["100007", "100903", "100909"] | ||
.iter() | ||
.map(|v| NodeId::new(1, *v).into()) | ||
.collect(); | ||
let _ = session.create_monitored_items( | ||
subscription_id, | ||
TimestampsToReturn::Both, | ||
&items_to_create, | ||
)?; | ||
Ok(()) | ||
} | ||
|
||
fn print_value(item: &MonitoredItem) { | ||
let node_id = &item.item_to_monitor().node_id; | ||
let data_value = item.last_value(); | ||
if let Some(ref value) = data_value.value { | ||
println!("Item \"{}\", Value = {:?}", node_id, value); | ||
} else { | ||
println!( | ||
"Item \"{}\", Value not found, error: {}", | ||
node_id, | ||
data_value.status.as_ref().unwrap() | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
use opcua::server::prelude::*; | ||
|
||
fn main() { | ||
let server: Server = ServerBuilder::new() | ||
.application_name("Demo Server") | ||
.application_uri("urn:server_uri") | ||
.discovery_urls(vec!["opc.tcp://localhost:4855/".to_string()]) | ||
.create_sample_keypair(true) | ||
.trust_client_certs() | ||
.pki_dir("./pki-server") | ||
.discovery_server_url(None) | ||
.host_and_port("localhost", 4855) | ||
.endpoint( | ||
"none", | ||
ServerEndpoint::new( | ||
"/", | ||
SecurityPolicy::None, | ||
MessageSecurityMode::None, | ||
&vec![String::from("ANONYMOUS")], | ||
), | ||
) | ||
.server() | ||
.unwrap(); | ||
|
||
let address_space = server.address_space(); | ||
|
||
let folder_id = address_space.write() | ||
.add_folder("Variables", "Variables", &NodeId::objects_folder_id()) | ||
.unwrap(); | ||
|
||
let node_id = NodeId::new(1, "MyVar"); | ||
VariableBuilder::new(&node_id, "MyVar", "MyVar") | ||
.organized_by(&folder_id) | ||
.value(0u8) | ||
.insert(&mut address_space.write()); | ||
|
||
let now = DateTime::now(); | ||
let value = 123.456f64; | ||
let _ = address_space.write().set_variable_value(node_id, value, &now, &now); | ||
|
||
server.run(); | ||
|
||
// println!("0"); | ||
// let ns = { | ||
// let address_space = server.address_space(); | ||
// let mut address_space = address_space.write(); | ||
// address_space.register_namespace("urn:demo-server").unwrap() | ||
// }; | ||
// println!("1"); | ||
// let static_folder_id = { | ||
// let address_space = server.address_space(); | ||
// let mut address_space = address_space.write(); | ||
// address_space | ||
// .add_folder("Static", "Static", &NodeId::objects_folder_id()) | ||
// .unwrap() | ||
// }; | ||
// println!("2"); | ||
// let address_space = server.address_space(); | ||
// let mut address_space = address_space.write(); | ||
|
||
// // Create a folder under static folder | ||
// let folder_id = address_space | ||
// .add_folder("Scalar", "Scalar", &static_folder_id) | ||
// .unwrap(); | ||
|
||
// println!("3"); | ||
// let value = 42; | ||
// VariableBuilder::new(&NodeId::new(ns,"Float".to_string()), "Float", "Float") | ||
// .data_type(DataTypeId::Float) | ||
// .value(value) | ||
// .organized_by(&folder_id) | ||
// .insert(&mut server.address_space().write()); | ||
// println!("Starting OPC UA server."); | ||
// server.run(); | ||
} // |