FreeOpcUa / python-opcua

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

Unable to subscribe to datachange when using Datavalue #1507

Closed venkatp-co closed 1 year ago

venkatp-co commented 1 year ago

Hi I'm unable to subscribe to datachange notification when using Datavalue, please find my server and client below.

from opcua import ua, uamethod, Server
import csv
import datetime
import json
import time

if __name__== "__main__":
    #Server setup

    server= Server()
    endpoint = "opc.tcp://127.0.0.1:4848"
    server.set_endpoint(endpoint)

    servername= "Softsensor_OPC_Server"
    server.set_server_name(servername)

    #Object Node and Variable Node Modeling

    root_node = server.get_root_node()
    object_node = server.get_objects_node()
    idx = server.register_namespace("OPCUA_SERVER")
    myobj = object_node.add_object(idx, "Variables")
    print(root_node, object_node, myobj)

    var_4413 = myobj.add_variable('ns=2;s=4413;','4413', "0", ua.VariantType.Float)

    server.start()    

    count = 1.1
    while True:
        current_timestamp = datetime.datetime.now()
        dv = ua.DataValue(ua.Variant(count, ua.VariantType.Float))
        dv.ServerTimestamp = current_timestamp
        dv.SourceTimestamp = current_timestamp
        var_4413.set_value(dv)
        count=count+1

MY Client

import time
from opcua import Client, ua, Node
from datetime import datetime
import requests
import json
import asyncio

#Subscription handler class, gets the Update when Value Changes

class SubscriptionHandler:
    def datachange_notification(self, node:Node, val, data):
        print(str(node), str(val))

client = Client("opc.tcp://127.0.0.1:4848")
client.connect()

myvar = client.get_node("ns=2;i=1").get_children()
print(myvar)
#Subscribing to Variable Node and Get update for Every Variable change

handler = SubscriptionHandler()
sub=client.create_subscription(500, handler)
handle = sub.subscribe_data_change(myvar)
time.sleep(0.1)