def updateCommandStatus(self): if self.detector_command is None: return cmd_alive = self.detector_command.is_alive() if cmd_alive is False: try: cmd_name = self.detector_command.getCommandName() ret_value = self.detector_command.getCommandAnswer() if cmd_name == 'check_connections': try: ans = ret_value['value'] ret_value = "" for conn in ans: ret_value += '{name}={up},'.format(**conn) except: ret_value = "cannot parse result" else: ret_value = str(ret_value) self.cmd_answer[cmd_name] = ret_value log.log( 2, "command %s finished execution. answer is: %s" % (cmd_name, ret_value)) except BaseException as e: log.log(1, "error update command: %s" % str(e))
def run_command(self, cmdname, url): log.log(2, "command %s - (%s) is running" % (self.cmd_name, cmdname)) headers = {} headers['Content-type'] = self.mimeType self._connection.request('PUT', url, body=self.data, headers=headers) response = self._connection.getresponse() status = response.status data = response.read() if status == 200: data_l = json.loads(data) if isinstance(data_l, dict) and cmdname in ['arm', 'trigger']: log.log(2, "answer to %s is : %s" % (cmdname, str(data_l))) if 'sequence_id' in data_l.keys(): answer = data_l['sequence id'] else: log.log( 3, "answer to %s contains keys: %s" % (cmdname, data_l.keys())) answer = data_l else: answer = data_l log.log(2, "response from %s command : %s" % (cmdname, str(answer))) return answer else: log.log( 1, "cannot get response from %s command - status: %s" % (cmdname, status)) return None
def setVerbose(self, verbose): """ Switch verbose mode on and off. Args: verbose: bool value """ self._verbose = bool(verbose) log.log(2, "DEigerClient verbose level set to %s" % (verbose))
def __init__(self, host, port, url, command, data, mimeType): log.log(2, "creating command thread for command %s" % command) self._connection = http.HTTPConnection(host, port, timeout=self.commandTimeout) self.url = url self.cmd_name = command self.data = data self.mimeType = mimeType threading.Thread.__init__(self) self.answer = None
def set_number_trigger(self, value): headers = {} headers['Content-type'] = 'application/json; charset=utf-8' data = json.dumps({'value': value}) log.log(3, "setting ntrigger to %s" % value) self._connection.request('PUT', self.ntrigger_url, body=data, headers=headers) response = self._connection.getresponse() log.log(3, "setting ntrigger returns %s " % str(response))
def __init__(self, host='127.0.0.1', port=80, verbose=False, urlPrefix=None, user=None): """ Create a client object to talk to the EIGER API. Args: host: hostname of the detector computer port: port usually 80 (http) verbose: bool value urlPrefix: String prepended to the urls. Should be None. Added for future convenience. user: "******". Should be None. Added for future convenience. """ super(DEigerClient, self).__init__() self._host = host self._port = port self._version = Version self._verbose = verbose self._urlPrefix = "" self._user = None self._connectionTimeout = MAX_TIMEOUT log.log( 2, "DEigerClient created - host: %s / port: %s (verbose=%s)" % (self._host, self._port, self._verbose)) #pingret = os.system("ping -c 1 -w 1 -W 10 %s > /dev/null 2>&1" % self._host) #if pingret != 0: #raise BaseException("Cannot access DCU") try: self._connection = http.HTTPConnection( self._host, self._port, timeout=self._connectionTimeout) except BaseException as exc: log.log(1, "Exception while connecting to Eiger DCU %s" % str(exc)) raise (e) self._serializer = None self.detector_command = None self.cmd_answer = {} self.read_thread = None self.read_value = {} self.setUrlPrefix(urlPrefix) self.setUser(user)
def startAcquisition(self, need_arm=True, trigger_mode='ints', ntrigger=1): if self.isExecutingCommand(): log.log(1, "cannot start acquisition while a command is running") return False arm_url = self._url('detector', 'command', 'arm') trigger_url = self._url('detector', 'command', 'trigger') ntrigger_url = self._url('detector', 'config', 'ntrigger') self.detector_command = StartAcquisitionThread(self._host, self._port, \ arm_url, trigger_url, ntrigger_url) self.detector_command.set_arm_needed(need_arm) self.detector_command.set_trigger_mode(trigger_mode) self.detector_command.set_ntrigger(ntrigger) self.startDetectorCommand() return True
def run(self): req = urlopen(self.url, timeout=self.commandTimeout) try: with open(self.target_path, 'wb') as fp: log.log(2, 'writing ' + self.target_path) shutil.copyfileobj(req, fp, 512 * 1024) assert os.access(self.target_path, os.R_OK) return "ok" except: import traceback log.log(1, 'cannot write ' + self.target_path) log.log(1, traceback.format_exc()) return ".error."
def _log(self, *args): if self._verbose: log.log(2, ' '.join([str(elem) for elem in args]))
def run(self): try: if self.trigger_mode in ['ints', 'inte']: if self.arm_needed: if self.ntrigger == 1: log.log(2, "programming big trigger no") self.set_ntrigger(TRIGGER_NO_MAX) elif self.ntrigger == TRIGGER_NO_MAX: log.log(3, "ntrigger is big") else: log.log(2, "programming ntrigger to %s" % self.ntrigger) self.set_ntrigger(self.ntrigger) aret = self.run_command("arm", self.arm_url) log.log(2, "arm command finished. returned: %s" % aret) if aret is None: return self.error() else: aret = None tret = self.run_command("trigger", self.trigger_url) log.log(2, "trigger command finished. returned: %s" % aret) if tret is not None: if aret is None: self.answer = tret log.log(2, "startacq finished. returning t - %s " % tret) return else: log.log(2, "startacq finished. returning a - %s " % aret) self.answer = aret return else: # external trigger aret = self.run_command("arm", self.arm_url) log.log(2, "arm command finished. returned: %s" % aret) self.set_ntrigger(self.ntrigger) if aret is None: return self.error() except BaseException as exc: import traceback log.log(1, "error starting acquisition %s" % traceback.format_exc()) return self.error()