diff --git a/pypcc/opcuaserv/yamlreader.py b/pypcc/opcuaserv/yamlreader.py index d82ef4fc857bd5fca8372bfd9daa5a851c12873d..f4debf26a51c71aa0b62452827a6c1aaba2d3130 100644 --- a/pypcc/opcuaserv/yamlreader.py +++ b/pypcc/opcuaserv/yamlreader.py @@ -134,6 +134,8 @@ class yamlreader(yamlconfig): self.statusid=self.getvarid(self.basename+"_translator_busy"); self.monitorvarid=self.getvarid(self.basename+"_monitor_rate"); self.statusOPC=None + self.lastSend=time.localtime() + self.lastRecv=time.localtime() def SetBusy(self): if self.statusid is None: return @@ -224,6 +226,7 @@ class yamlreader(yamlconfig): mask=v.get('maskOPC',None); mask=mask.get_value() if (mask!=None) else []; self.SetBusy() + self.lastSend=time.localtime() self.server.callmethod(id1,mask) def CallInit(self): @@ -248,6 +251,7 @@ class yamlreader(yamlconfig): logging.info(str(("setvar ",v['name'],data2,mask))); if data2 is None: return self.SetBusy() + self.lastSend=time.localtime() self.server.setvar(id1,data2,mask) def getvar(self): @@ -258,6 +262,7 @@ class yamlreader(yamlconfig): # try: item=self.server.readdata() if item is None: break; + self.lastRecv=time.localtime() id1,data,mask=item; logging.debug(str(("**getvar",id1,data,mask))); if len(data)==0: continue; @@ -290,18 +295,20 @@ class yamlreader(yamlconfig): # if v['name']=='UNB2_FPGA_POL_ERAM_IOUT': logging.warn(str((data3,data2,mask,var1.get_value(),[hex(d) for d in data]))); if len(data3)==1: data3=data3[0]; var1.set_value(data3,datatype); - + def watchdog(self): + print(self.lastRecv,self.lastSend,self.lastSend-self.lastRecv) + return self.lastSend-self.lastRecv; def Monitor(self): - if self.monitorvar is None: return; + if self.monitorvar is None: return T1=self.monitorvar.get_value()*10; - if T1<=0: return; + if T1<=0: return self.timecount+=1; # if self.statusOPC is None: self.statusOPC=self.conf['variables'][self.statusid].get('OPCR') while self.timecount>=T1: if self.GetBusy(): logging.info("Busy, try monitor again in 1s") self.timecount=T1-10; #busy, so try again in 1s - return; + return if not(self.statusOPC is None) and self.statusOPC.get_value(): return; if self.server.QoutLength()>0: return; v=self.conf['variables'][self.monitorvarcnt]; @@ -309,10 +316,10 @@ class yamlreader(yamlconfig): mask=(v['maskOPC'].get_value() if v.get('maskOPC') else []) # print("monitor",v['name'],mask) # self.SetBusy() + self.lastSend=time.localtime() self.server.readvar(self.monitorvarcnt,mask=mask) self.monitorvarcnt+=1; if self.monitorvarcnt>=len(self.conf['variables']): self.monitorvarcnt=0; self.timecount=0; - # self.OPCset(self.statusid,[1],[]) diff --git a/pypcc/pypcc.py b/pypcc/pypcc.py index 45b48e0b93308ed6097f8cf4ac738a4fcf24d9a5..ba4cecbf282c79acdae319e942ff3ea73111ea2b 100755 --- a/pypcc/pypcc.py +++ b/pypcc/pypcc.py @@ -54,17 +54,18 @@ else: #I2Cports=['UNB2','RCU','CLK'] #I2Cports=['RCU'] I2Cports=[x for x in args.config.split(',')] -threads=[] +I2C_process=[] I2Cclients=[] for name in I2Cports: - RCU_I2C=i2client.i2client(name=name) + RCU_I2C=i2client.i2client(name=name) if not(args.simulator): - thread1=i2cthread.start(*RCU_I2C.GetInfo(),lock=lock) - threads.append(thread1) + thread1=i2cthread.start(*RCU_I2C.GetInfo(),lock=lock) #Start a new I2C process + I2C_process.append(thread1) I2Cclients.append(RCU_I2C) #Initialise OPCUA server and load variables logging.info("Initialised OPC-UA Server") configs=[] +I2C_threads=[] if not(args.test): handler=opcuaserv.InitServer(port=args.port) logging.info("Load OPCUA variables & start i2c listing thread") @@ -79,7 +80,7 @@ if not(args.test): thread2=threading.Thread(target=RCU_conf.getvar); #Thread on OPC-UA side of pipe thread2.start() - threads.append(thread2) + I2C_threads.append(thread2) time.sleep(1) logging.info("Start OPC-UA server") opcuaserv.start() @@ -97,7 +98,18 @@ try: else: time.sleep(0.1); for c in configs: - c.Monitor(); + if c.Monitor(): continue + if not(args.simulator) and not(args.test): + for i,c in enumerate(configs): + timeout=c.watchdog() + if timeout>60: + logging.warning("Restarting I2C process!") + I2C_process[i].terminate() + RCU_I2C=I2Cclients[i]; + thread1=i2cthread.start(*RCU_I2C.GetInfo(),lock=lock) #Start a new I2C process + I2C_process[i]=thread1 + configs[i].CallInit() + finally: if not(args.test): logging.info("Stop OPC-UA server")