diff --git a/pypcc/opcuaserv/opcuaserv.py b/pypcc/opcuaserv/opcuaserv.py index 6c284cdf5561a0bdf1c90fbce13aa1842fce0b6f..e9644075d18d6b105be25a141ffdaa08b68398f0 100644 --- a/pypcc/opcuaserv/opcuaserv.py +++ b/pypcc/opcuaserv/opcuaserv.py @@ -3,8 +3,10 @@ import sys sys.path.insert(0, "..") import time -from opcua import ua, Server +#from opcua import ua, Server +from asyncua.sync import ua, Server from datetime import datetime; +from queue import Queue import logging #import Vars #import HWconf @@ -19,14 +21,30 @@ class SubHandler(object): """ Subscription Handler. To receive events from server for a subscription """ + def __init__(self): + self.datachange_queue=Queue() + def check_datachange(self,timeout): + while True: + try: + node,val=self.datachange_queue.get(timeout=timeout) + except: + break + nodeid=node.nodeid.Identifier + vname,myvar,v,reader,opcvar=Vars_W[nodeid] + # val=(val if isinstance(val, list) else [val] ) + logging.info(str(("Datachange callback",nodeid,vname,val))) + node.set_value(ua.DataValue(val, ua.StatusCode(ua.StatusCodes.GoodCompletesAsynchronously))) + for r in reader: + r.setvar(v,val) + def datachange_notification(self, node, val, data): +# NOTE: OPC variables can not be updates in the datachange_notification when using asyncua.sync!! So we put them in a queue. # print("Python: New data change event", node, val,data) if not(running): return - vname,myvar,v,reader,opcvar=Vars_W[node.nodeid.Identifier] -# val=(val if isinstance(val, list) else [val] ) - logging.info(str(("Datachange callback",vname,val))) - for r in reader: - r.setvar(v,val) + logging.info(str(("Datachange callback",node.nodeid.Identifier,data.monitored_item.Value.StatusCode))) + if data.monitored_item.Value.StatusCode != ua.StatusCode(ua.StatusCodes.Good): return +# logging.warning(str(("Python: New client data change event", node, val, data.monitored_item.Value.StatusCode))) + self.datachange_queue.put([node,val]) # myvar2.Value.Value=val # myvar2.SourceTimestamp = datetime.utcnow() @@ -109,7 +127,7 @@ def InitServer(port=4840): # uri = "http://examples.freeopcua.github.io" # idx = server.register_namespace(uri) - objects = server.get_objects_node() + objects = server.nodes.objects #server.get_objects_node() # populating our address space PCCobj = objects #.add_object(idx, "PCC") @@ -120,7 +138,7 @@ def InitServer(port=4840): logging.info("Start server"); server.start() handler = SubHandler() - sub = server.create_subscription(500, handler) + sub = server.create_subscription(50, handler) running=False; logging.info("Add variables:") #P1.GetVarNames("",AddVar); @@ -128,7 +146,7 @@ def InitServer(port=4840): # time.sleep(1) # running=True # return Vars_R,Vars_W - return + return handler #exit() def start(): diff --git a/pypcc/pypcc.py b/pypcc/pypcc.py index b23c71f88b0b913ec1347163c1e48b6e282a60fd..9d5cb779faa8255bfa55d6854f55ce70e3390879 100755 --- a/pypcc/pypcc.py +++ b/pypcc/pypcc.py @@ -41,7 +41,7 @@ signal.signal(signal.SIGINT, signal_handler) #Start i2c processes as soon as possible to have minimum duplication logging.info("Start I2C processes") -if True: +if not(args.simulator): logging.info("Make I2C lock") import multiprocessing as mp lock=mp.Lock() @@ -62,7 +62,7 @@ for name in I2Cports: logging.info("Initialised OPC-UA Server") configs=[] if not(args.test): - opcuaserv.InitServer(port=args.port) + handler=opcuaserv.InitServer(port=args.port) logging.info("Load OPCUA variables & start i2c listing thread") for i,name in enumerate(I2Cports): RCU_I2C=I2Cclients[i] @@ -87,7 +87,10 @@ if False: try: while RunTimer: - time.sleep(0.1); + if not(args.test): + handler.check_datachange(0.1); + else: + time.sleep(0.1); for c in configs: c.Monitor(); finally: diff --git a/requirements.txt b/requirements.txt index 01abf7a7b876ab2a4800472ecaaa12b277ad9057..eb9df2a6f5849b36e70cf04ae089584d4a49ebc4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,5 @@ git+https://github.com/amaork/libi2c.git; platform_system == "linux" -opcua +asyncua numpy recordclass pyyaml diff --git a/scripts/opcua_list.py b/scripts/opcua_list.py new file mode 100644 index 0000000000000000000000000000000000000000..f73bd3fe973aaa08edb5b1f3d040f649407ba3c2 --- /dev/null +++ b/scripts/opcua_list.py @@ -0,0 +1,30 @@ +from test_common import * +connect() + +names=[ + "RECVTR_I2C_error","RECVTR_translator_busy", + "RCU_PCB_ID","RCU_PCB_version","RCU_PCB_number", + "RCU_PWR_ANT_on","RCU_PWR_DIGITAL_on","RCU_PWR_ANALOG_on", + "RCU_attenuator_dB","RCU_band_select","RCU_ADC_locked", + "RCU_TEMP","RCU_PWR_1V8","RCU_PWR_2V5","RCU_PWR_3V3","RCU_PWR_ANT_VIN","RCU_PWR_ANT_VOUT","RCU_PWR_ANT_IOUT", + "RCU_DTH_on","RCU_DTH_freq","RCU_DTH_PWR" +] +for name in names: + att=get_value(name+"_R") + try: + print(name,att[:15]) + except: + print(name,[att]) + +#RCU=[0]; +#RCU=[0,1,2,3]; +#Att=[15,15,15] +#RCU=[0,1,2,3,16,17,18,19]; +#Att=[4,4,4] +#Att=[5,5,5] + + +#setAntmask(RCU) + + +disconnect() \ No newline at end of file diff --git a/setup.py b/setup.py index 498798131df030826dc4bc9d002d9211dec33551..25adcbf6c8ede44d4565fb729756ec288db654f7 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ import setuptools #with open('requirements.txt') as f: # required = f.read().splitlines() # Requires: setup.cfg -setuptools.setup(install_requires=["opcua", +setuptools.setup(install_requires=["asyncua", "numpy", "recordclass", "pyyaml",