OPC
15 min
The OPC UA module manages the OPC UA server on Litmus Edge: service configuration, security policies, and the node hierarchy.
Imports
from litmussdk import opcAll functions are accessible directly from the opc namespace.
Service Management
Start / Stop
opc.start_stop_opc(start=True)
opc.start_stop_opc(start=False)Service Configuration
# Get current config
config = opc.get_service_config()
# {host, port, nodesUnderRoot, ...}
# Update config
opc.change_service_config(
host="0.0.0.0",
port=4840,
nodes_under_root=True,
)Security and Authentication
# Get current settings
settings = opc.get_security_and_auth_settings()
# Update policies
opc.change_security_and_auth_settings(
enabled_security_modes=["None", "Sign", "SignAndEncrypt"],
enabled_security_policies=["Basic256Sha256", "Aes128_Sha256_RsaOaep"],
enabled_auth_modes=["Anonymous", "Password"],
)Valid enabled_security_modes: "None", "Sign", "SignAndEncrypt"
Valid enabled_security_policies: "Basic256Sha256", "Basic256", "Basic128Rsa15", "Aes128_Sha256_RsaOaep", "Aes256_Sha256_RsaPss"
Valid enabled_auth_modes: "Anonymous", "Certificate", "Password"
Certificates and Metrics
certs = opc.get_server_certificates() # dict
metrics = opc.get_server_metrics() # dict
Hierarchy
The OPC hierarchy is a tree of Folder, Device, and Tag nodes.
Viewing the Hierarchy
# Root node
root = opc.get_root_node()
# Specific nodes by path
nodes = opc.get_node_by_paths(["edgedevice/MyFolder", "edgedevice/MyFolder/MyDevice"])
# Export entire hierarchy as JSON
hierarchy = opc.export_as_json()Adding Nodes
# Folder
folder_id = opc.add_node_folder(
name="MyFolder",
parent_id="edgedevice",
worker_node_id_mode="hierarchical", # or "fixed"
)
# Device (under a folder)
device_id = opc.add_node_device(
name="MyDevice",
parent_id=folder_id,
)
# Tag (under a device)
tag = opc.add_node_tag(
name="Pressure",
parent_id=device_id,
data_type="uint32",
topic_name="plant/pump/01/pressure",
)worker_node_id_mode:
- "hierarchical" (default): node ID is auto-generated from the tree position
- "fixed": provide an explicit worker_node_id
Updating a Node
All fields must be specified:
opc.update_node_by_id(
node_id=folder_id,
parent_id="edgedevice",
name="RenamedFolder",
node_type="FOLDER",
worker_node_id="edgedevice.RenamedFolder",
worker_node_id_mode="fixed",
)For TAG nodes, also pass data_type and topic_name.
Removing a Node
opc.remove_single_node(node_id)Reset Hierarchy
opc.reset_manual_hierarchy() # removes manually created nodes
opc.reset_imported_hierarchy() # removes auto-imported nodes
opc.reset_all_hierarchy() # removes everythingImport from DeviceHub
Auto-import all DeviceHub tags as OPC nodes:
opc.import_all_dh_tags()Import / Export JSON
import json
# Export
hierarchy = opc.export_as_json()
with open("opc_hierarchy.json", "w") as f:
json.dump(hierarchy, f)
# Import (replaces current hierarchy)
with open("opc_hierarchy.json") as f:
opc.import_json(json.load(f))
License
Copyright (c) Litmus Automation Inc.