def _read(self): if not self.cid: raise InvalidValueError(self, 'Connection lost to CARESS') if hasattr(self._caressObject, 'read_module'): # result = self._caressObject.read_module(0x80000000, self.cid) result, state, val = self._caressObject.read_module(0, self.cid) if result != CARESS.OK: raise CommunicationError( self, 'Could not read the CARESS module: %d' % self.cid) if hasattr(val, 'f'): return (state, val.f) return ( state, val.l, ) else: _ = () self.log.debug('read module: %d', self.cid) l, result = self._caressObject.read_module_orb(0, self.cid, _) self.log.debug('read_module: %d, %r', l, result) if l != 0: raise CommunicationError( self, 'Could not read the CARESS module: %d' % self.cid) if result[0].value() != self.cid: raise CommunicationError( self, 'Answer from wrong module!: %d %r' % (self.cid, result[0])) state = result[1].value() if state == OFF_LINE: raise NicosError(self, 'Module is off line!') if result[2].value() < 1: raise InvalidValueError(self, 'No position in data') return state, result[4].value()
def _com_return(self, result, info): # Check if the communication was successful response = result.status_code if response in self.status_code_msg: raise CommunicationError(self.status_code_msg.get(response)) elif response != 200: raise CommunicationError('Error while connecting to server!') self._setROParam('curstatus', (status.OK, '')) return result
def _init(self, cid): try: if session.sessiontype != POLLER: if hasattr(self._caressObject, 'init_system_orb'): if not CARESSDevice._caress_initialized: self.log.debug('initialize the CARESS absdev ' 'container') if self._caressObject.init_system_orb(0)[0] in \ (0, CARESS.OK): CARESSDevice._caress_initialized = True else: raise CommunicationError( self, 'could not ' 'initialize CARESS absdev' ' container') _config = self._normalized_config() res = self._caressObject.init_module(INIT_CONNECT, cid, _config) self.log.debug('INIT_CONNECT: %r', res) if res[0] in (0, CARESS.OK): if res[1] == OFF_LINE: res = self._caressObject.init_module( INIT_REINIT, cid, _config) else: res = self._caressObject.init_module(INIT_NORMAL, cid, _config) self.log.debug('init module (Connect): %r', res) if res[0] not in (0, CARESS.OK) or res[1] == OFF_LINE: raise NicosError( self, 'Could not initialize module! (%r) %d' % ((res, ), self._device_kind())) # res = self._caressObject.init_module(INIT_REINIT, cid, _config) # self.log.debug('Init module (Re-Init): %r', res) # if res not in[(0, ON_LINE), (CARESS.OK, ON_LINE)]: # self.log.error('Init module (Re-Init): %r (%d, %s)', # res, cid, self.config) if self._device_kind() == CORBA_DEVICE: if self.absdev: res = self._caressObject.char_loadblock_module_orb( 0, cid, 1, len(self.loadblock), 16, self.loadblock) else: val = CARESS.Value(ab=self.loadblock.encode()) res = self._caressObject.loadblock_module( 0, cid, 1, len(self.loadblock), val) # 16, val) self._initialized = True if not self._is_corba_device(): CARESSDevice._used_counter += 1 except CORBA.TRANSIENT as err: raise CommunicationError( self, 'could not init CARESS module %r (%d: %s)' % (err, cid, self.config)) from err
def call(self, method, args): payload = { 'method': method, 'params': list(args), 'jsonrpc': '2.0', 'id': 0, } try: response = requests.post(self.url, json=payload).json() if response['id'] != 0 or not response['jsonrpc']: raise CommunicationError('invalid reply') return response['result'] except Exception as err: raise CommunicationError('RPC call error: %s' % err) from err
def _com_raise(self, err, info): """Process the exception raised either by communication or _com_return. Should raise a NICOS exception. Default is to raise CommunicationError. """ raise CommunicationError(self, str(err))
def _caress_guard_nolog(self, function, *args): if not self._initialized or not self._caressObject: CARESSDevice.doInit(self, self._mode) # self._com_lock.aquire() try: return function(*args) except (CORBA.COMM_FAILURE, CORBA.TRANSIENT) as err: tries = self.comtries - 1 while True and tries > 0: self.log.warning('Remaining tries: %d', tries) session.delay(self.comdelay) if isinstance(err, CORBA.TRANSIENT): CARESSDevice.doShutdown(self) CARESSDevice.doInit(self, self._mode) session.delay(self.comdelay) try: return function(*args) except (CORBA.COMM_FAILURE, CORBA.TRANSIENT) as err: tries -= 1 raise CommunicationError( self, 'CARESS error: %s%r: %s' % (function.__name__, args, err)) from err finally: pass
def _readblock(self): if hasattr(self._caressObject, 'readblock_module') and \ hasattr(self._caressObject, 'readblock_params'): _type = CARESS.TypeLong _start = 0 _end = 0 try: result, _start, _end, _type = \ self._caress_guard(self._caressObject.readblock_params, READBLOCK_NORMAL, self.cid, _start, _end, _type) if result != CARESS.OK: raise CommunicationError( self, 'Could not read the CARESS ' 'module') result, _status, data = \ self._caress_guard(self._caressObject.readblock_module, READBLOCK_NORMAL, self.cid, _start, _end) if result != CARESS.OK: raise CommunicationError( self, 'Could not read the CARESS ' 'module') # self.log.warning('%r', data) return [self._width, self._height, 1] + data.al except CORBA.COMM_FAILURE as ex: raise CommunicationError( self, 'Could not read the CARESS ' 'module : %s' % (ex, )) else: _ = () result = self._caress_guard(self._caressObject.read_module_orb, 0, self.cid, _) self.log.debug('read_module: %r', result) if result[0] != 0: raise CommunicationError(self, 'Could not read the CARESS module') if result[1][0].value() != self.cid: raise NicosError( self, 'Answer from wrong module!: %d %r' % (self.cid, result[1][0])) if result[1][1].value() == OFF_LINE: raise NicosError(self, 'Module is off line!') if result[1][2].value() < 1: raise InvalidValueError(self, 'No position in data') return result[1][1].value(), result[1][4].value()
def doReset(self): self.log.info('Checking if camera script is ready!') try: msg = self._dev.communicate('ready?') if msg.strip() != 'ready!': raise CommunicationError( self, 'Camera script gave wrong ' 'answer - please check!') except NicosError: self.log.warning('Camera is not responding - please start ' 'tomography script on camera first!') raise
def _communicate(self, cmd): mnem = '%d%s' % (self.id, cmd) ret = self.communicate('s%s' % mnem) if ret and ret.startswith == ('g%s' % mnem): return ret[len(mnem):] if ret[2:4] == '@E': err = self._integer(ret[4:]) if err in errors: raise HardwareError(errors[err]) raise HardwareError('Hardware failure (Contact Dimetix)') raise CommunicationError("Did not get a valid answer from hardware " "for command '%s': '%s'" % (cmd, ret))
def _initObject(self): if not self._orb: raise ProgrammingError(self, 'Programmer forgot to call _initORB') obj = self._orb.resolve_initial_references('NameService') _root_context = obj._narrow(CosNaming.NamingContext) if not _root_context: raise CommunicationError( self, 'Failed to narrow the root naming' ' context') if self._is_corba_device(): try: tmp = self.objname.split('.') if self.objname else \ self.config.split()[2].split('.') if len(tmp) < 2: tmp.append('caress_object') self.log.debug('%r', tmp) obj = _root_context.resolve( [CosNaming.NameComponent(tmp[0], tmp[1])]) except CosNaming.NamingContext.NotFound as ex: raise ConfigurationError(self, 'Name not found: %s' % (ex, )) from ex self._caressObject = obj._narrow(CARESS.CORBADevice) else: try: self._caressObject = \ self._orb.string_to_object('corbaname::%s#%s.context/' 'caress.context/' 'server.context/absdev.object' % (self.nameserver, self.objname)) except CORBA.BAD_PARAM as ex: raise ConfigurationError(self, 'Name not found: %s' % (ex, )) from ex if CORBA.is_nil(self._caressObject): raise CommunicationError(self, 'Could not create a CARESS device') if hasattr(self._caressObject, 'init_module_orb'): self._caressObject.init_module = self._caressObject.init_module_orb
def _read_controller(self, keys): line = '_read_controller %s' % keys self.log.debug(line) try: data = requests.get(self.url, timeout=self.timeout).json() self.log.debug(data) except requests.Timeout as e: self.log.info(line) self.log.info('url %s' % self.url) self.log.info('err %s' % e) raise CommunicationError(self, 'HTTP Timeout failed') except Exception as e: self.log.info(line) self.log.info('url %s' % self.url) self.log.info('err %s' % e) raise ConfigurationError(self, 'HTTP request failed') res = {} for key in keys: res[key] = data[key] return res
def _init(self, cid): try: if not self._is_corba_device(): raise ConfigurationError( self, 'Must be configured as "CORBA_device" (ID=500)') _config = ' '.join(self.config.split()[3:]) self.log.debug('reduced config: %s', _config) res = self._caressObject.init_module(INIT_NORMAL, cid, _config) self.log.debug('init module (Connect): %r', res) if res not in [(0, ON_LINE), (CARESS.OK, ON_LINE)]: res = self._caressObject.init_module(INIT_REINIT, cid, _config) self.log.debug('init module (Re-Init): %r', res) if res not in[(0, ON_LINE), (CARESS.OK, ON_LINE)]: self.log.error('Init module (Re-Init): %r (%d, %s)', res, cid, _config) raise NicosError(self, 'Could not initialize module!') self._initialized = True except CORBA.TRANSIENT as err: raise CommunicationError( self, 'could not init CARESS module %r (%d: %s)' % ( err, cid, self.config)) from err
def _com_raise(self, err, info): """Overwrite to set status to not OK. """ self._statusok = False raise CommunicationError(self, str(err))