FreeOpcUa / python-opcua

LGPL Pure Python OPC-UA Client and Server
http://freeopcua.github.io/
GNU Lesser General Public License v3.0
1.37k stars 661 forks source link

Subscription Timeout Against KepserverEX OPC-UA Server #892

Open jhoggood opened 5 years ago

jhoggood commented 5 years ago

Environment: Ubuntu 18.04 LTS, Python 2.7.15, Python-OPCUA Kepware KepserverEX 6.7.1046.0

Problem: This code below executes client.get_node(xxx) just fine. The client.create_subscription(xxxxxx) will only work repeatedly if you have the time.sleep() otherwise it fails as follows:

ubuntu@ip-172-31-28-53:~/ProcessDataCollector/Source$ python PDC_Main.py PDC_MAIN-I-MAIN, Logging WARN level enabled PDC_MAIN-I-MAIN, Client instantiated PDC_MAIN-I-MAIN, Client connected

PDC_MAIN-I-MAIN, Root: Node(TwoByteNodeId(i=84)) PDC_MAIN-I-MAIN, Root Name: QualifiedName(0:Root) PDC_MAIN-I-MAIN, Root Children: [Node(TwoByteNodeId(i=85)), Node(TwoByteNodeId(i=86)), Node(TwoByteNodeId(i=87))]

PDC_MAIN-I-MAIN, Objects: Node(TwoByteNodeId(i=85)) PDC_MAIN-I-MAIN, Child Objects: [Node(FourByteNodeId(i=2253)), Node(StringNodeId(ns=2;s=_AdvancedTags)), Node(StringNodeId(ns=2;s=_ConnectionSharing)), Node(StringNodeId(ns=2;s=_CustomAlarms)), Node(StringNodeId(ns=2;s=_DataLogger)), Node(StringNodeId(ns=2;s=_EFMExporter)), Node(StringNodeId(ns=2;s=_IDF_for_Splunk)), Node(StringNodeId(ns=2;s=_IoT_Gateway)), Node(StringNodeId(ns=2;s=_LocalHistorian)), Node(StringNodeId(ns=2;s=_Redundancy)), Node(StringNodeId(ns=2;s=_Scheduler)), Node(StringNodeId(ns=2;s=_SecurityPolicies)), Node(StringNodeId(ns=2;s=_SNMP Agent)), Node(StringNodeId(ns=2;s=_System)), Node(StringNodeId(ns=2;s=_ThingWorx)), Node(StringNodeId(ns=2;s=Channel1)), Node(StringNodeId(ns=2;s=Data Type Examples)), Node(StringNodeId(ns=2;s=Painting)), Node(StringNodeId(ns=2;s=Simulation Examples))]

PDC_MAIN-I-MAIN, Tag1 Name: Node(StringNodeId(ns=2;s=Painting.Oven.Fan)) PDC_MAIN-I-MAIN, Tag1 Value: 10.010052681

PDC_MAIN-I-MAIN, Subscription created

PDC_MAIN-I-SUBHAND, Data Change Event Handler: Node(StringNodeId(ns=2;s=Painting.Oven.Fan)) 10.010052681 PDC_MAIN-I-SUBHAND, Data Change Event Handler: Node(StringNodeId(ns=2;s=Painting.Oven.Fan)) 10.3241558075 PDC_MAIN-I-SUBHAND, Data Change Event Handler: Node(StringNodeId(ns=2;s=Painting.Oven.Fan)) 10.9410829544 PDC_MAIN-I-SUBHAND, Data Change Event Handler: Node(StringNodeId(ns=2;s=Painting.Oven.Fan)) 11.5643444061 PDC_MAIN-I-SUBHAND, Data Change Event Handler: Node(StringNodeId(ns=2;s=Painting.Oven.Fan)) 11.8836870193 PDC_MAIN-I-SUBHAND, Data Change Event Handler: Node(StringNodeId(ns=2;s=Painting.Oven.Fan)) 12.4966344833 PDC_MAIN-I-SUBHAND, Data Change Event Handler: Node(StringNodeId(ns=2;s=Painting.Oven.Fan)) 13.0901699066 PDC_MAIN-I-MAIN, Subscribe done nodehandle1: 1

PDC_MAIN-F-MAIN, Exception occurred PDC_MAIN-I-MAIN, Client disconnected WARNING:opcua.client.ua_client.Socket:ServiceFault from server received while waiting for publish response ERROR:concurrent.futures:exception calling callback for <Future at 0x7f6555e034d0 state=finished returned Buffer> Traceback (most recent call last): File "/home/ubuntu/.local/lib/python2.7/site-packages/concurrent/futures/_base.py", line 331, in _invoke_callbacks callback(self) File "/home/ubuntu/.local/lib/python2.7/site-packages/opcua/client/ua_client.py", line 469, in _call_publish_callback self._uasocket.check_answer(data, "while waiting for publish response") File "/home/ubuntu/.local/lib/python2.7/site-packages/opcua/client/ua_client.py", line 88, in check_answer hdr.ServiceResult.check() File "/home/ubuntu/.local/lib/python2.7/site-packages/opcua/ua/uatypes.py", line 233, in check raise UaStatusCodeError(self.value) BadSessionClosed: The session was closed by the client.(BadSessionClosed) WARNING:opcua.client.ua_client.Socket:ServiceFault from server received while waiting for publish response ERROR:concurrent.futures:exception calling callback for <Future at 0x7f6555e03250 state=finished returned Buffer> Traceback (most recent call last): File "/home/ubuntu/.local/lib/python2.7/site-packages/concurrent/futures/_base.py", line 331, in _invoke_callbacks callback(self) File "/home/ubuntu/.local/lib/python2.7/site-packages/opcua/client/ua_client.py", line 469, in _call_publish_callback self._uasocket.check_answer(data, "while waiting for publish response") File "/home/ubuntu/.local/lib/python2.7/site-packages/opcua/client/ua_client.py", line 88, in check_answer hdr.ServiceResult.check() File "/home/ubuntu/.local/lib/python2.7/site-packages/opcua/ua/uatypes.py", line 233, in check raise UaStatusCodeError(self.value) BadSessionClosed: The session was closed by the client.(BadSessionClosed) Traceback (most recent call last): File "PDC_Main.py", line 76, in eventhandle1 = subscription1.subscribe_events() File "/home/ubuntu/.local/lib/python2.7/site-packages/opcua/common/subscription.py", line 210, in subscribe_events return self._subscribe(sourcenode, ua.AttributeIds.EventNotifier, evfilter, queuesize=queuesize) File "/home/ubuntu/.local/lib/python2.7/site-packages/opcua/common/subscription.py", line 228, in _subscribe mids[0].check() File "/home/ubuntu/.local/lib/python2.7/site-packages/opcua/ua/uatypes.py", line 233, in check raise UaStatusCodeError(self.value) opcua.ua.uaerrors._auto.BadMonitoredItemFilterUnsupported: The server does not support the requested monitored item filter.(BadMonitoredItemFilterUnsupported)


Library Imports

import sys sys.path.insert(0, "..") import time import logging from opcua import Client, ua

from opcua import Subscription

OPC-UA Client event handlers

class SubHandler(object):

OPC-UA Server data change event handler

def datachange_notification(self, node, val, data):
    print "PDC_MAIN-I-SUBHAND, Data Change Event Handler: ", node, val

# OPC-UA Server miscellaneous event handler
def event_notification(self, event):
    print "PDC_MAIN-I-SUBHAND, Miscellaneous Event Handler: : ", event

Main routine

if name == "main":

Set Python logging level

logging.basicConfig(level=logging.WARN)
print "PDC_MAIN-I-MAIN, Logging WARN level enabled"

# Create OPC-UA Client object (x.x.x.x = KepserverEX target)
client = Client("opc.tcp://x.x.x.x:49320")
print "PDC_MAIN-I-MAIN, Client instantiated"

params = ua.CreateSubscriptionParameters()
params.RequestedPublishingInterval = 2000
params.RequestedLifetimeCount = 3000
params.RequestedMaxKeepAliveCount = 10000
params.MaxNotificationsPerPublish = 0
# params.MaxNotificationsPerPublish = 2147483647
params.PublishingEnabled = True
params.Priority = 0

try:
    # Connect the OPC-UA Client to the OPC-UA Server
    client.connect()
    print "PDC_MAIN-I-MAIN, Client connected"
    print ""

    # OPC-UA Server Root Node list
    root = client.get_root_node()
    print "PDC_MAIN-I-MAIN, Root: ", root
    print "PDC_MAIN-I-MAIN, Root Name: ", root.get_browse_name()
    print "PDC_MAIN-I-MAIN, Root Children: ", root.get_children()
    print ""

    # OPC-UA Server Child Objects list
    objects = client.get_objects_node()
    print "PDC_MAIN-I-MAIN, Objects: ", client.get_objects_node()
    print "PDC_MAIN-I-MAIN, Child Objects: ", objects.get_children()
    print ""

     # OPC-UA Server Node Name/Value (tag1 = Painting.Oven.Fan)
    tag1 = client.get_node("ns=2;s=Painting.Oven.Fan")
    print "PDC_MAIN-I-MAIN, Tag1 Name: ", format(tag1)
    print "PDC_MAIN-I-MAIN, Tag1 Value: ", format(tag1.get_value())
    print ""

    # Subscribe to a specific OPC-UA Server Node (tag1) or could be Events
    handler1 = SubHandler()
    subscription1 = client.create_subscription(500, handler1)
    print "PDC_MAIN-I-MAIN, Subscription created"
    print ""

    # Subscribe to a specific OPC-UA Server Node (tag1)
    nodehandle1 = subscription1.subscribe_data_change(tag1)
    time.sleep(10.0)
    print "PDC_MAIN-I-MAIN, Subscribe done nodehandle1: ", nodehandle1
    print ""

    # Subscribe to events from the OPC-UA Server
    eventhandle1 = subscription1.subscribe_events()
    print "PDC_MAIN-I-MAIN, Subscribe done eventhandle1: ", eventhandle1

    exit()

finally:
    # Disconnect the OPC-UA Client from the OPC-UA Server
    print "PDC_MAIN-F-MAIN, Exception occurred"
    print "PDC_MAIN-I-MAIN, Client disconnected"
    client.disconnect()
jhoggood commented 5 years ago

Turns out KepserverEX 6.7.1046.0 OPC-UA Server doesn't like the client.disconnect() operation for some reason.

jhoggood commented 5 years ago

Any suggestions on why client.disconnect() fails on KepserverEX would be appreciated. Also, I'm not sure I understand how this subscribe_data_change() operation is working either as even if I then execute a subscription1.unsubscribe(nodehandle1) in the try: block after the subscribe_data_change() operation my code continues to execute.