def get_user_filename(self): """ Returns a filename given the current run number and title. If device is simulated do not attempt to get run number or title from channel access """ if self.simulated: filename = "LSICORR_IOC_test_user_save.dat" else: run_number = ChannelAccess.caget(RUNNUMBER_PV.format(pv_prefix=self.pv_prefix)) print_and_log("run number = {}".format(run_number)) timestamp = datetime.now().strftime("%Y-%m-%dT%H_%M_%S") experiment_name = self.get_converted_pv_value(Records.EXPERIMENTNAME.name) if experiment_name == "": # No name supplied, use run title experiment_name = ChannelAccess.caget(TITLE_PV.format(pv_prefix=self.pv_prefix)) filename = "{run_number}_{experiment_name}_{timestamp}.dat".format( run_number=run_number, experiment_name=remove_non_ascii(experiment_name), timestamp=timestamp ) # Update last used filename PV full_filename = os.path.join(self.user_filepath, filename) self.update_pv_and_write_to_device(Records.OUTPUTFILE.name, full_filename) return full_filename
def process_ca_loop(): logger.info("Reflectometry Server processing requests") while True: try: SERVER.process(0.1) ChannelAccess.poll() except Exception as err: break
def stop_ioc(self, prefix, ioc): """Stops the specified IOC. Args: prefix (string): The prefix for the instrument ioc (string): The name of the IOC """ print_and_log("Stopping IOC %s" % ioc) ChannelAccess.caput(self.generate_prefix(prefix, ioc) + ":STOP", 1)
def restart_ioc(self, prefix, ioc): """Restarts the specified IOC. Args: prefix (string): The prefix for the instrument ioc (string): The name of the IOC """ print_and_log("Restarting IOC %s" % ioc) ChannelAccess.caput(self.generate_prefix(prefix, ioc) + ":RESTART", 1)
def start_ioc(self, prefix, ioc): """Starts the specified IOC Args: prefix (string): The prefix of the instrument the IOC is being run on ioc (string): The name of the IOC to start """ print_and_log("Starting IOC %s" % ioc) ChannelAccess.caput(self.generate_prefix(prefix, ioc) + ":START", 1)
def toggle_autorestart(self, prefix, ioc): """Toggles the auto-restart property. Args: prefix (string): The prefix for the instrument ioc (string): The name of the IOC """ # Check IOC is running, otherwise command is ignored print_and_log("Toggling auto-restart for IOC %s" % ioc) ChannelAccess.caput(self.generate_prefix(prefix, ioc) + ":TOGGLE", 1)
def _reload(self): print_and_log("Reloading gateway") try: # Have to wait after put as the gateway does not do completion callbacks (it is not an IOC) ChannelAccess.caput(self._prefix + "newAsFlag", 1, False) while ChannelAccess.caget(self._prefix + "newAsFlag") == 1: time.sleep(1) print_and_log("Gateway reloaded") except Exception as err: print_and_log("Problem with reloading the gateway %s" % err)
def _set_motor_moving_pv(self, value): """ Set/clear the motor is moving pv to indicate we are calculating the readback value Args: value: value to set it to. 1 is moving, 0 not moving """ try: ChannelAccess.caput(MOTOR_MOVING_PV, value, safe_not_quick=False) except Exception as e: STATUS_MANAGER.update_error_log( "Failed to set motor moving pv: {}".format(e), e) STATUS_MANAGER.update_active_problems( ProblemInfo("Failed to update motor moving pv", "pv_wrapper", Severity.MAJOR_ALARM))
def get_archive_filename(self): """ Returns a filename which the archive data file will be saved with """ timestamp = datetime.now().strftime("%Y-%m-%dT%H_%M_%S") run_number = ChannelAccess.caget( RUNNUMBER_PV.format(pv_prefix=self.pv_prefix)) instrument = ChannelAccess.caget( INSTNAME_PV.format(pv_prefix=self.pv_prefix)) filename = "{instrument}{run_number}_DLS_{timestamp}.txt".format( instrument=instrument, run_number=run_number, timestamp=timestamp) full_filename = os.path.join(DATA_DIR, filename) print_and_log("filename1: {}".format(full_filename)) return full_filename
def get_iocs(self): """ Get a list of IOCs from DatabaseServer. Returns: list : A list of IOC names """ try: rawjson = dehex_and_decompress(ChannelAccess.caget(self._blockserver_prefix + "IOCS")) return json.loads(rawjson).keys() except: return list()
def exists(self): """Checks the gateway exists by querying on of the PVs. Returns: bool : Whether the gateway is running and is accessible """ val = ChannelAccess.caget(self._prefix + "pvtotal") if val is None: return False else: return True
def get_archive_filename(self): """ Returns a filename which the archive data file will be saved with. If device is simulated do not attempt to get instrument name or run number from channel access. If simulated save file in user directory instead of usual data directory. """ if self.simulated: full_filename = os.path.join(self.user_filepath, "LSICORR_IOC_test_archive_save.dat") else: timestamp = datetime.now().strftime("%Y-%m-%dT%H_%M_%S") run_number = ChannelAccess.caget(RUNNUMBER_PV.format(pv_prefix=self.pv_prefix)) instrument = ChannelAccess.caget(INSTNAME_PV.format(pv_prefix=self.pv_prefix)) filename = "{instrument}{run_number}_DLS_{timestamp}.txt".format(instrument=instrument, run_number=run_number, timestamp=timestamp) full_filename = os.path.join(DATA_DIR, filename) return full_filename
def get_ioc_status(self, prefix, ioc): """Gets the status of the specified IOC. Args: prefix (string): The prefix for the instrument ioc (string): The name of the IOC Returns: string : The status """ ans = ChannelAccess.caget(self.generate_prefix(prefix, ioc) + ":STATUS", as_string=True) if ans is None: raise Exception("Could not find IOC (%s)" % self.generate_prefix(prefix, ioc)) return ans.upper()
def get_ioc_status(self, prefix, ioc): """Gets the status of the specified IOC Args: prefix (string): The prefix of the instrument the IOC is being run on ioc (string): The name of the IOC Returns: string : The status of the requested IOC """ pv = self.generate_prefix(prefix, ioc) + ":STATUS" ans = ChannelAccess.caget(pv, as_string=True) if ans is None: raise Exception("Could not find IOC (%s)" % pv) return ans.upper()
def get_autorestart(self, prefix, ioc): """Gets the current auto-restart setting of the specified IOC. Args: prefix (string): The prefix for the instrument ioc (string): The name of the IOC Returns: bool : Whether auto-restart is enabled """ ans = ChannelAccess.caget(self.generate_prefix(prefix, ioc) + ":AUTORESTART", as_string=True) if ans is None: raise Exception("Could not find IOC (%s)" % self.generate_prefix(prefix, ioc)) elif ans == "On": return True elif ans == "Off": return False else: raise Exception("Could not get auto-restart property for IOC (%s)" % self.generate_prefix(prefix, ioc))
def _check_PV_compressed_hex(self, pv_name, description, help_text="", allowed_values=None): full_pv = "{prefix}{pv_name}".format(prefix=self._prefix, pv_name=pv_name) value = ca.caget(full_pv) if value is None: LOG.error("{description}: Fail PV can not be read".format(description=description)) LOG.error(" {help}".format(help=help_text)) return False try: uncompressed_val = zlib.decompress(value.decode("hex")) except Exception as ex: #TODO: untried LOG.error("{full_pv}={value}".format(full_pv=full_pv, value=value)) LOG.error("{description}: Fail to decompress PV with error - {ex}".format(ex=ex)) return False if allowed_values is not None and uncompressed_val not in allowed_values: LOG.error("{full_pv}={value}".format(full_pv=full_pv, value=uncompressed_val)) LOG.error("{description}: Fail PV has invalid value must be one of {allowed_values}".format( description=description, allowed_values=allowed_values)) LOG.error(" {help}".format(help=help_text)) return False return True
# The PV address list pv_address = "CS:INSTLIST" # instrument list values to set (uses utility to return the dictionary but you can use a dictionary directly) instruments_list = [ inst_dictionary("LARMOR"), inst_dictionary("ALF"), inst_dictionary("DEMO"), inst_dictionary("IMAT"), inst_dictionary("MUONFE", hostname_prefix="NDE"), inst_dictionary("ZOOM"), inst_dictionary("IRIS") ] new_value = json.dumps(instruments_list) new_value_compressed = compress_and_hex(new_value) ca.caput(pv_address, str(new_value_compressed), True) result_compr = ca.caget(pv_address, True) result = dehex_and_decompress(result_compr) print result if result != new_value: print "Warning! Entered value does not match new value." print "Entered value: " + new_value print "Actual value: " + result else: print "Success! The PV now reads: {0}".format(result)