# Script to read initial conditions for LED start mask. # The script is triggered by "1", then it will be excecuted only at the beginning of opi launch. # Orlando Soto, March 2014. from org.csstudio.opibuilder.scriptUtil import PVUtil, ConsoleUtil from org.csstudio.utility.pv import PVFactory from java.lang import Thread, Runnable from BCAL_LEDControl_parameters import delay_time # Waiting time to retry. pref = widget.getMacroValue("pref") # Getting the macro value for "pref". The prefix for the IOC. base = 1 # Base channel. ########## Class to handle the connection ####### class PVconnect(Runnable): # Class inherit from Runnable, this allow to run it in threads. def __init__(self,pvr,pvw): self.pvr = pvr # PV object for reading self.pvw = pvw # PV object for writing return def run(self): pvw = self.pvw pvr = self.pvr pvw.start() # Starting the PV. pvr.start() # Starting the PV. cnt = 0 # Retrying counter. status = pvw.isConnected() and pvr.isConnected() # Getting connected status. while ( not status) and (cnt<5) : # Retry if is not connected and retry counter is below 5 cnt += 1 Thread.sleep(delay_time) # Wait delay_time for retry. status = pvw.isConnected() and pvr.isConnected() # Get new connected status. if cnt == 5: # If time out is reached. ConsoleUtil.writeInfo("Error: Timeout to connect with PVs %s and %s --- %d (ms)" % (pvr.getName(), pvr.getName() ,cnt*delay_time)) pvw.setValue(PVUtil.getDouble(pvr)) # Retrieving value and writing in the corresponding PV. pvw.stop() pvr.stop() #ConsoleUtil.writeInfo( "child says: thread: %d %s in cnt: %d running " % (Thread.currentThread().getId(), pv.getValue(),cnt)) return ################################################# class runInThread(Runnable): # Class to run the script on a thread. def run(self): for i in range(32): pvrName = str(pref) + ":" + str(i+base) + "_start_mask_r" # PV to be read from. pvwName = str(pref) + ":" + str(i+base) + "_start_mask_w" # PV to be written in. con = PVconnect(PVFactory.createPV(pvrName),PVFactory.createPV(pvwName)) # Instantiating a new PVconnect object. thread =Thread(con) # Creating a new thread to handle the connection. thread.start() # Connecting proccess variable. th = Thread(runInThread()) # Creating the thread. th.start() # Running the thread.