Rewrite to reconnect handler v2
This commit is contained in:
@@ -13,6 +13,8 @@ import globalData as gd
|
||||
|
||||
new_value_event = asyncio.Event() # signals when a new value arrives
|
||||
|
||||
session_id = 0
|
||||
|
||||
class SubHandler:
|
||||
"""
|
||||
Subscription Handler. To receive events from server for a subscription
|
||||
@@ -64,25 +66,9 @@ class SubHandler:
|
||||
"""
|
||||
print("status_notification %s", status)
|
||||
|
||||
# def load_node_ids():
|
||||
# if not gc.NODE_IDS_FILE_PATH.exists():
|
||||
# return []
|
||||
|
||||
# with open(gc.NODE_IDS_FILE_PATH, "r", encoding="utf-8") as f:
|
||||
# node_ids = [line.strip() for line in f if line.strip() and not line.startswith("#")]
|
||||
|
||||
# if not node_ids:
|
||||
# raise ValueError(f"No node IDs found in {gc.NODE_IDS_FILE_PATH}")
|
||||
# return node_ids
|
||||
|
||||
# async def createSubscriptions():
|
||||
|
||||
# subscription = await gd.opc_ua_client.create_subscription(250, SubHandler(gd.queue))
|
||||
# node_ids = load_node_ids()
|
||||
# nodes = [gd.opc_ua_client.get_node(f"{nid}") for nid in node_ids]
|
||||
# await subscription.subscribe_data_change(nodes)
|
||||
|
||||
async def connection_watcher2():
|
||||
|
||||
await initOPCUAConnection()
|
||||
|
||||
connected = False
|
||||
@@ -93,35 +79,32 @@ async def connection_watcher2():
|
||||
await gd.opc_ua_client.connect()
|
||||
connected = True
|
||||
except Exception:
|
||||
print(f"Reconnecting in {gc.RECONNECT_INTERVAL} seconds")
|
||||
await asyncio.sleep(gc.RECONNECT_INTERVAL)
|
||||
|
||||
|
||||
# Create subscriptions
|
||||
gd.subscription_handler = await gd.opc_ua_client.create_subscription(250, SubHandler(gd.queue, gd.event_queue))
|
||||
|
||||
# Create an event subscription
|
||||
event_logger_node = gd.opc_ua_client.get_node("ns=8;s=eventlogger")
|
||||
await gd.subscription_handler.subscribe_events(event_logger_node, evtypes=[ua.ObjectIds.BaseEventType, "ns=5;i=4000"])
|
||||
|
||||
# Periodically check for connection
|
||||
state = 0
|
||||
while True:
|
||||
match (state):
|
||||
case 0:
|
||||
try:
|
||||
await gd.opc_ua_client.connect_sessionless()
|
||||
await gd.opc_ua_client.create_session()
|
||||
except Exception:
|
||||
|
||||
|
||||
await asyncio.sleep(gc.CHECK_INTERVAL)
|
||||
try:
|
||||
await gd.opc_ua_client.check_connection()
|
||||
except ua.uaerrors._auto.BadConnectionClosed:
|
||||
connected = False
|
||||
print(f"Reconnecting in {gc.RECONNECT_INTERVAL} seconds")
|
||||
await asyncio.sleep(gc.RECONNECT_INTERVAL)
|
||||
|
||||
if connected:
|
||||
await createSubscriptions()
|
||||
|
||||
# Periodically check for connection
|
||||
while True:
|
||||
if connected:
|
||||
try:
|
||||
await gd.opc_ua_client.check_connection()
|
||||
except ua.uaerrors._auto.BadConnectionClosed:
|
||||
connected = False
|
||||
gd.opc_ua_client.disconnect_sessionless()
|
||||
print(f"Reconnecting in {gc.RECONNECT_INTERVAL} seconds")
|
||||
await asyncio.sleep(gc.RECONNECT_INTERVAL)
|
||||
else:
|
||||
try:
|
||||
await gd.opc_ua_client.connect_sessionless()
|
||||
result = await gd.opc_ua_client.uaclient.activate_session()
|
||||
print(result)
|
||||
connected = True
|
||||
except Exception:
|
||||
print(f"Reconnecting in {gc.RECONNECT_INTERVAL} seconds")
|
||||
await asyncio.sleep(gc.RECONNECT_INTERVAL)
|
||||
|
||||
|
||||
|
||||
@@ -182,10 +165,29 @@ async def initOPCUAConnection():
|
||||
gd.opc_ua_client = Client(url=gc.OPC_UA_URL)
|
||||
|
||||
gd.opc_ua_client.application_uri = gc.CLIENT_APP_URI
|
||||
|
||||
result = await gd.opc_ua_client.get_endpoints()
|
||||
print(result)
|
||||
|
||||
gd.opc_ua_client.set_user(gc.OPC_UA_USER)
|
||||
gd.opc_ua_client.set_password(gc.OPC_UA_PW)
|
||||
await gd.opc_ua_client.set_security(
|
||||
SecurityPolicyBasic256Sha256,
|
||||
certificate=str(gc.CERT),
|
||||
private_key=str(gc.PRIVATE_KEY)
|
||||
)
|
||||
gd.opc_ua_client.set_user(gc.OPC_UA_USER)
|
||||
gd.opc_ua_client.set_password(gc.OPC_UA_PW)
|
||||
|
||||
|
||||
|
||||
async def createSubscriptions():
|
||||
sub_param = ua.CreateSubscriptionParameters
|
||||
sub_param.RequestedPublishingInterval = gd.PUBLISH_INTERVAL
|
||||
sub_param.RequestedMaxKeepAliveCount = 5
|
||||
sub_param.RequestedLifetimeCount = 10
|
||||
sub_param.MaxNotificationsPerPublish = 0
|
||||
|
||||
gd.subscription_handler = await gd.opc_ua_client.create_subscription(sub_param, SubHandler(gd.queue, gd.event_queue))
|
||||
|
||||
# Create an event subscription
|
||||
event_logger_node = gd.opc_ua_client.get_node(gc.EVENT_LOGGER_NODE_ID)
|
||||
await gd.subscription_handler.subscribe_events(event_logger_node, evtypes=[ua.ObjectIds.BaseEventType, gc.TC_EVENT_DATATYPE_NODE_ID])
|
||||
|
||||
Reference in New Issue
Block a user