frappy() indicates also if frappy server is not running

+ improvements in the envlist creation
This commit is contained in:
2022-08-25 14:15:39 +02:00
parent cbbec5647c
commit 7b076458bc
2 changed files with 27 additions and 21 deletions

View File

@ -26,6 +26,7 @@ from glob import glob
from configparser import ConfigParser from configparser import ConfigParser
from nicos import session, config from nicos import session, config
from nicos.core import status
from nicos.utils import printTable from nicos.utils import printTable
from nicos.commands import helparglist, usercommand from nicos.commands import helparglist, usercommand
from nicos.commands.basic import AddSetup, CreateAllDevices, CreateDevice from nicos.commands.basic import AddSetup, CreateAllDevices, CreateDevice
@ -98,17 +99,16 @@ def frappy_start(**services):
cfginfo = services.get(service) cfginfo = services.get(service)
if cfginfo is None: if cfginfo is None:
if not secnode: if not secnode:
continue continue
cfginfo = secnode() or '' cfginfo = secnode() or ''
all_cfg[service] = cfginfo
else: else:
if cfginfo: if cfginfo:
new_cfg.append((service, secnode, cfginfo)) new_cfg.append((service, secnode, cfginfo))
else: else:
remove_cfg.append(secnode) remove_cfg.append(secnode)
all_cfg[service] = cfginfo
if secnode: if secnode:
secnode('') secnode('')
all_cfg[service] = secnode.get_info()
# check cfg is not used twice # check cfg is not used twice
for cfg in cfginfo.split(','): for cfg in cfginfo.split(','):
@ -126,6 +126,7 @@ def frappy_start(**services):
AddSetup('frappy_' + service) AddSetup('frappy_' + service)
secnode = session.devices[nodename] secnode = session.devices[nodename]
secnode(cfginfo) secnode(cfginfo)
all_cfg[service] = secnode.get_info()
CreateDevice(nodename) CreateDevice(nodename)
cleanup_defunct() cleanup_defunct()
CreateAllDevices() CreateAllDevices()

View File

@ -133,13 +133,8 @@ class FrappyConfig(Device):
meaning_name, importance = meaning meaning_name, importance = meaning
sample_devices.setdefault(meaning_name, []).append((importance, devname)) sample_devices.setdefault(meaning_name, []).append((importance, devname))
envlist = list(session.experiment.envlist) newenv = {} # to be added to envlist (dict [devname] of aliasname)
prev_env_alias = {} # dict [devname] of aliasname of previous aliases in envlist to_remove = set() # items to be removed from previous envlist, if present
for devname in envlist:
dev = session.device.get(devname)
if isinstance(dev, DeviceAlias):
prev_env_alias[devname] = dev.alias
newenv = {}
for meaning in self.meanings: for meaning in self.meanings:
info = getattr(self, meaning) info = getattr(self, meaning)
aliasnames = info.get('alias') aliasnames = info.get('alias')
@ -162,26 +157,23 @@ class FrappyConfig(Device):
session.dynamic_devices[aliasname] = 'frappy' # assign to frappy setup session.dynamic_devices[aliasname] = 'frappy' # assign to frappy setup
aliasdev = previous_aliases.pop(aliasname, None) aliasdev = previous_aliases.pop(aliasname, None)
if aliasdev: if aliasdev:
session.log.debug('change alias %r -> %r', aliasname, devname) if aliasdev.alias != devname:
session.log.debug('change alias %r -> %r', aliasname, devname)
else: else:
session.log.debug('create alias %r -> %r', aliasname, devname) session.log.debug('create alias %r -> %r', aliasname, devname)
aliasdev = session.createDevice(aliasname, recreate=True, explicit=True) aliasdev = session.createDevice(aliasname, recreate=True, explicit=True)
aliasdev.alias = devname aliasdev.alias = devname
# only the first item of aliasnames is added to the envlist # only the first item of aliasnames is added to the envlist
aliasname = aliasnames[0] aliasname = aliasnames[0]
if devname in envlist: to_remove.add(devname)
envlist.remove(devname) to_remove.add(aliasname)
else:
prev_alias = prev_env_alias[devname]
if prev_alias in envlist:
envlist.remove(prev_alias)
if devname not in newenv and info.get('envlist', True): if devname not in newenv and info.get('envlist', True):
# example: when 'temperature' and 'temperature_regulation' are the # example: when 'temperature' and 'temperature_regulation' are the
# same device, the first one is kept # same device, the first one is kept
newenv[devname] = aliasname newenv[devname] = aliasname
break break
else: else:
envlist = [k for k in envlist if k not in aliasnames] to_remove.union(aliasnames)
for aliasname in previous_aliases: for aliasname in previous_aliases:
session.destroyDevice(aliasname) session.destroyDevice(aliasname)
@ -190,10 +182,14 @@ class FrappyConfig(Device):
applyAliasConfig() # for other aliases applyAliasConfig() # for other aliases
envlist.extend(newenv.values()) envlist = [k for k in session.experiment.envlist if k not in to_remove] + list(newenv.values())
if set(envlist) != set(session.experiment.envlist): if envlist != session.experiment.envlist:
removed = set(session.experiment.envlist).difference(envlist)
session.experiment.setEnvironment(envlist) session.experiment.setEnvironment(envlist)
session.log.info('changed environment to: %s', ', '.join(session.experiment.envlist)) if removed:
session.log.info('removed %s from environment', ', '.join(removed))
if newenv:
session.log.info('added %s to environment', ', '.join(newenv.values()))
class FrappyNode(SecNodeDevice, Moveable): class FrappyNode(SecNodeDevice, Moveable):
@ -313,3 +309,12 @@ class FrappyNode(SecNodeDevice, Moveable):
if self._cache: if self._cache:
self._cache.put(self, 'value', cfg) self._cache.put(self, 'value', cfg)
self._setROParam('target', cfg) self._setROParam('target', cfg)
def get_info(self):
result = self.doRead() or ''
code, text = self.status()
if code == status.OK:
return result
if (code, text) == (status.ERROR, 'reconnecting'):
return '%s (frappy not running)' % result
return '%s (%s)' % (result, text)