Skip to content
Snippets Groups Projects
Commit 5b86627f authored by Paulus Kruger's avatar Paulus Kruger
Browse files

Async lib

parent 6970006b
No related branches found
No related tags found
1 merge request!19Async lib
...@@ -3,8 +3,10 @@ ...@@ -3,8 +3,10 @@
import sys import sys
sys.path.insert(0, "..") sys.path.insert(0, "..")
import time import time
from opcua import ua, Server #from opcua import ua, Server
from asyncua.sync import ua, Server
from datetime import datetime; from datetime import datetime;
from queue import Queue
import logging import logging
#import Vars #import Vars
#import HWconf #import HWconf
...@@ -19,14 +21,30 @@ class SubHandler(object): ...@@ -19,14 +21,30 @@ class SubHandler(object):
""" """
Subscription Handler. To receive events from server for a subscription 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): 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) # print("Python: New data change event", node, val,data)
if not(running): return if not(running): return
vname,myvar,v,reader,opcvar=Vars_W[node.nodeid.Identifier] logging.info(str(("Datachange callback",node.nodeid.Identifier,data.monitored_item.Value.StatusCode)))
# val=(val if isinstance(val, list) else [val] ) if data.monitored_item.Value.StatusCode != ua.StatusCode(ua.StatusCodes.Good): return
logging.info(str(("Datachange callback",vname,val))) # logging.warning(str(("Python: New client data change event", node, val, data.monitored_item.Value.StatusCode)))
for r in reader: self.datachange_queue.put([node,val])
r.setvar(v,val)
# myvar2.Value.Value=val # myvar2.Value.Value=val
# myvar2.SourceTimestamp = datetime.utcnow() # myvar2.SourceTimestamp = datetime.utcnow()
...@@ -109,7 +127,7 @@ def InitServer(port=4840): ...@@ -109,7 +127,7 @@ def InitServer(port=4840):
# uri = "http://examples.freeopcua.github.io" # uri = "http://examples.freeopcua.github.io"
# idx = server.register_namespace(uri) # idx = server.register_namespace(uri)
objects = server.get_objects_node() objects = server.nodes.objects #server.get_objects_node()
# populating our address space # populating our address space
PCCobj = objects #.add_object(idx, "PCC") PCCobj = objects #.add_object(idx, "PCC")
...@@ -120,7 +138,7 @@ def InitServer(port=4840): ...@@ -120,7 +138,7 @@ def InitServer(port=4840):
logging.info("Start server"); logging.info("Start server");
server.start() server.start()
handler = SubHandler() handler = SubHandler()
sub = server.create_subscription(500, handler) sub = server.create_subscription(50, handler)
running=False; running=False;
logging.info("Add variables:") logging.info("Add variables:")
#P1.GetVarNames("",AddVar); #P1.GetVarNames("",AddVar);
...@@ -128,7 +146,7 @@ def InitServer(port=4840): ...@@ -128,7 +146,7 @@ def InitServer(port=4840):
# time.sleep(1) # time.sleep(1)
# running=True # running=True
# return Vars_R,Vars_W # return Vars_R,Vars_W
return return handler
#exit() #exit()
def start(): def start():
......
...@@ -41,7 +41,7 @@ signal.signal(signal.SIGINT, signal_handler) ...@@ -41,7 +41,7 @@ signal.signal(signal.SIGINT, signal_handler)
#Start i2c processes as soon as possible to have minimum duplication #Start i2c processes as soon as possible to have minimum duplication
logging.info("Start I2C processes") logging.info("Start I2C processes")
if True: if not(args.simulator):
logging.info("Make I2C lock") logging.info("Make I2C lock")
import multiprocessing as mp import multiprocessing as mp
lock=mp.Lock() lock=mp.Lock()
...@@ -62,7 +62,7 @@ for name in I2Cports: ...@@ -62,7 +62,7 @@ for name in I2Cports:
logging.info("Initialised OPC-UA Server") logging.info("Initialised OPC-UA Server")
configs=[] configs=[]
if not(args.test): if not(args.test):
opcuaserv.InitServer(port=args.port) handler=opcuaserv.InitServer(port=args.port)
logging.info("Load OPCUA variables & start i2c listing thread") logging.info("Load OPCUA variables & start i2c listing thread")
for i,name in enumerate(I2Cports): for i,name in enumerate(I2Cports):
RCU_I2C=I2Cclients[i] RCU_I2C=I2Cclients[i]
...@@ -87,7 +87,10 @@ if False: ...@@ -87,7 +87,10 @@ if False:
try: try:
while RunTimer: while RunTimer:
time.sleep(0.1); if not(args.test):
handler.check_datachange(0.1);
else:
time.sleep(0.1);
for c in configs: for c in configs:
c.Monitor(); c.Monitor();
finally: finally:
......
git+https://github.com/amaork/libi2c.git; platform_system == "linux" git+https://github.com/amaork/libi2c.git; platform_system == "linux"
opcua asyncua
numpy numpy
recordclass recordclass
pyyaml pyyaml
......
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
...@@ -3,7 +3,7 @@ import setuptools ...@@ -3,7 +3,7 @@ import setuptools
#with open('requirements.txt') as f: #with open('requirements.txt') as f:
# required = f.read().splitlines() # required = f.read().splitlines()
# Requires: setup.cfg # Requires: setup.cfg
setuptools.setup(install_requires=["opcua", setuptools.setup(install_requires=["asyncua",
"numpy", "numpy",
"recordclass", "recordclass",
"pyyaml", "pyyaml",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment