def _doFunc(self, arg): """Execute command: TODO: Update comments""" if (self.parent.enable.value() is not True): return try: # Convert arg if arg is None: arg = self._default else: arg = self.parseDisp(arg) ret = self._functionWrap(function=self._function, root=self.root, dev=self.parent, cmd=self, arg=arg) # Set arg to local variable if not a remote variable if self._arg and not isinstance(self, RemoteCommand): self._default = arg self._queueUpdate() return ret except Exception as e: pr.logException(self._log, e) raise e
def _worker(self): while True: ent = self._queue.get() # Done if ent is None: return if self._conn is not None: try: # Variable if ent[0] is not None: ins = self._varTable.insert().values( path=ent[0], enum=str(ent[1].enum), disp=ent[1].disp, value=ent[1].valueDisp, severity=ent[1].severity, status=ent[1].status) # Syslog else: ins = self._logTable.insert().values( name=ent[1]['name'], message=ent[1]['message'], exception=ent[1]['exception'], levelName=ent[1]['levelName'], levelNumber=ent[1]['levelNumber']) self._conn.execute(ins) except Exception as e: self._conn = None pr.logException(self._log, e) self._log.error("Lost database connection to {}".format( self._url))
def set(self, value, *, index=-1, write=True, verify=True, check=True): """ Set the value and write to hardware if applicable Writes to hardware are blocking if check=True, otherwise non-blocking. A verify will be performed according to self.verify if verify=True A verify will not be performed if verify=False An error will result in a logged exception. """ try: # Set value to block self._set(value, index) if write: self._parent.writeBlocks(force=True, recurse=False, variable=self, index=index) if verify: self._parent.verifyBlocks(recurse=False, variable=self) if check: self._parent.checkBlocks(recurse=False, variable=self) except Exception as e: pr.logException(self._log, e) self._log.error( "Error setting value '{}' to variable '{}' with type {}. Exception={}" .format(value, self.path, self.typeStr, e)) raise e
def set(self, value, *, index=-1, write=True, verify=True, check=True): """ Set the value and write to hardware if applicable Writes to hardware are blocking. An error will result in a logged exception. """ self._log.debug("{}.set({})".format(self, value)) try: # Set value to block self._block.set(self, value, index) if write: self._parent.writeBlocks(force=True, recurse=False, variable=self, index=index) self._parent.verifyBlocks(recurse=False, variable=self) self._parent.checkBlocks(recurse=False, variable=self) except Exception as e: pr.logException(self._log, e) self._log.error( "Error setting value '{}' to variable '{}' with type {}. Exception={}" .format(value, self.path, self.typeStr, e)) raise e
def genDisp(self, value): try: if isinstance(value, np.ndarray): with np.printoptions(formatter={'all': self.disp.format}, threshold=sys.maxsize): ret = np.array2string(value, separator=', ') return ret elif self.disp == 'enum': if value in self.enum: return self.enum[value] else: self._log.warning( "Invalid enum value {} in variable '{}'".format( value, self.path)) return f'INVALID: {value}' else: return self.disp.format(value) except Exception as e: pr.logException(self._log, e) self._log.error( f"Error generating disp for value {value} with type {type(value)} in variable {self.path}" ) raise e
def _run(self): self.Running.set(True) try: self._process() except Exception as e: pr.logException(self._log, e) self.Message.setDisp("Stopped after error!") self.Running.set(False)
def get(self, read=True, index=-1, check=True): try: return self._linkedGetWrap(function=self._linkedGet, dev=self.parent, var=self, read=read, index=index, check=check) except Exception as e: pr.logException(self._log, e) self._log.error("Error getting link variable '{}'".format( self.path)) raise e
def get(self, *, index=-1, read=True): try: if read: pr.startTransaction(self._block, type=rogue.interfaces.memory.Read, forceWr=False, checkEach=True, variable=self, index=index) return self._get(index) except Exception as e: pr.logException(self._log, e) raise e
def set(self, value, *, write=True, index=-1, verify=True, check=True): try: self._linkedSetWrap(function=self._linkedSet, dev=self.parent, var=self, value=value, write=write, index=index, verify=verify, check=check) except Exception as e: pr.logException(self._log, e) self._log.error("Error setting link variable '{}'".format( self.path)) raise e
def set(self, value, *, index=-1, write=True): self._log.debug("{}.set({})".format(self, value)) try: self._set(value, index) if write: pr.startTransaction(self._block, type=rogue.interfaces.memory.Write, forceWr=True, checkEach=True, variable=self, index=index) except Exception as e: pr.logException(self._log, e) raise e
def write(self, *, verify=True, check=True): """ Force a write of the variable. Hardware write is blocking if check=True. A verify will be performed according to self.verify if verify=True A verify will not be performed if verify=False An error will result in a logged exception """ try: self._parent.writeBlocks(force=True, recurse=False, variable=self) if verify: self._parent.verifyBlocks(recurse=False, variable=self) if check: self._parent.checkBlocks(recurse=False, variable=self) except Exception as e: pr.logException(self._log, e) raise e
def get(self, *, index=-1, read=True, check=True): """ Return the value after performing a read from hardware if applicable. Hardware read is blocking. An error will result in a logged exception. Listeners will be informed of the update. """ try: if read: self._parent.readBlocks(recurse=False, variable=self, index=index) if check: self._parent.checkBlocks(recurse=False, variable=self) return self._block.get(self, index) except Exception as e: pr.logException(self._log, e) self._log.error("Error reading value from variable '{}'".format( self.path)) raise e
def post(self, value, *, index=-1): """ Set the value and write to hardware if applicable using a posted write. This method does not call through parent.writeBlocks(), but rather calls on self._block directly. """ self._log.debug("{}.post({})".format(self, value)) try: self._block.set(self, value, index) pr.startTransaction(self._block, type=rim.Post, forceWr=False, checkEach=True, variable=self, index=index) except Exception as e: pr.logException(self._log, e) self._log.error( "Error posting value '{}' to variable '{}' with type {}". format(value, self.path, self.typeStr)) raise e
def _updateWorker(self): self._log.info("Starting update thread") strm = {} zmq = {} while True: uvars = self._updateQueue.get() # Done if uvars is None: self._log.info("Stopping update thread") self._updateQueue.task_done() return # Process list elif len(uvars) > 0: self._log.debug( F'Process update group. Length={len(uvars)}. Entry={list(uvars.keys())[0]}' ) for p, v in uvars.items(): try: val = v._doUpdate() # Add to stream if self._slaveCount() != 0 and v.filterByGroup( self._streamIncGroups, self._streamExcGroups): strm[p] = val # Add to zmq publish if not v.inGroup('NoServe'): zmq[p] = val # Call listener functions, with self._varListenLock: for func in self._varListeners: func(p, val) # Log to database if self._sqlLog is not None and v.filterByGroup( self._sqlIncGroups, self._sqlExcGroups): self._sqlLog.logVariable(p, val) except Exception as e: if v == self.SystemLog: print( "------- Error Executing Syslog Listeners -------" ) print("Error: {}".format(e)) print( "------------------------------------------------" ) else: pr.logException(self._log, e) self._log.debug( F"Done update group. Length={len(uvars)}. Entry={list(uvars.keys())[0]}" ) # Generate yaml stream try: if len(strm) > 0: self._sendYamlFrame(pr.dataToYaml(strm)) strm = {} except Exception as e: pr.logException(self._log, e) # Send over zmq link if self._zmqServer is not None: self._zmqServer._publish(pickle.dumps(zmq)) zmq = {} # Set done self._updateQueue.task_done()
def _poll(self): """Run by the poll thread""" while True: if self.empty() or self.paused(): # Sleep until woken with self._condLock: self._condLock.wait() if self._run is False: self._log.info("PollQueue thread exiting") return else: continue else: # Sleep until the top entry is ready to be polled # Or a new entry is added by updatePollInterval now = datetime.datetime.now() readTime = self.peek().readTime waitTime = (readTime - now).total_seconds() with self._condLock: self._log.debug(f'Poll thread sleeping for {waitTime}') self._condLock.wait(waitTime) self._log.debug(f'Global reference count: {sys.getrefcount(None)}') with self._condLock: # Stop the thread if someone set run to False if self._run is False: self._log.info("PollQueue thread exiting") return # Wait for block count to be zero while self.blockCount > 0: self._condLock.wait() # Start update capture with self._root.updateGroup(): # Pop all timed out entries from the queue now = datetime.datetime.now() blockEntries = [] for entry in self._expiredEntries(now): self._log.debug(f'Polling Block {entry.block.path}') blockEntries.append(entry) try: pr.startTransaction( entry.block, type=rogue.interfaces.memory.Read) except Exception as e: pr.logException(self._log, e) # Update the entry with new read time entry.readTime = now + entry.interval entry.count = next(self._counter) # Push the updated entry back into the queue heapq.heappush(self._pq, entry) for entry in blockEntries: try: pr.checkTransaction(entry.block) except Exception as e: pr.logException(self._log, e)