def restart_attributes(self, attributes=None, timewait=0.5): if attributes is None: attributes = self.get_attributes_not_updated() todo = [] for a in attributes: a = self.is_attribute_archived(a) if a: todo.append(a) else: self.warning('%s is not archived!' % a) devs = dict(fn.kmap(self.get_attribute_archiver, todo)) for a, d in fn.randomize(sorted(devs.items())): if not fn.check_device(d): self.start_devices('(.*/)?' + d, do_restart=True) else: dp = fn.get_device(d, keep=True) dp.AttributeStop(a) fn.wait(timewait) fn.wait(10. * timewait) for a, d in devs.items(): dp = fn.get_device(d, keep=True) dp.AttributeStart(a) fn.wait(timewait) print('%d attributes restarted' % len(attributes))
def start_archiving_for_attributes(attrs,*args,**kwargs): """ from start_archiving(self,attribute,archiver,period=0, rel_event=None,per_event=0,abs_event=None, code_event=False, ttl=None, start=False): See HDBpp.add_attribute.__doc__ for a full description of arguments """ archs = get_archivers_for_attributes(attrs) dbs = get_hdbpp_databases(archs.keys()) done = [] if not args and not kwargs: kwargs['code_event'] = True for db,devs in dbs.items(): api = PyTangoArchiving.Schemas.getApi(db) devs = [d for d in devs if d in archs] for d in devs: ts = archs[d] print('Launching %d attributes in %s.%s'%(len(ts),db,d)) for t in ts: api.start_archiving(t,d,*args,**kwargs) done.append(fn.tango.get_full_name(t)) if not kwargs.get('start'): fn.get_device(d).Start() if len(done)!=len(attrs): print('No hdbpp database match for: %s' % str( [a for a in attrs if fn.tango.get_full_name(a) not in done])) return done
def find(self, device, mask='', N=0, strict=False): """ Method for finding devices or files find(device): will return devices or folders matching the given string find(device,file): will return files matching the given argument for the given devices """ if not strict and '*' not in device: device = '*' + device + '*' m = [d for d in self.get_all_devices() if fn.clmatch(device, d)] if not m: for d in self.values(): try: if fn.clmatch(device, d.SaveFolder): m.append(d.name()) if N and N >= len(m): return m except: pass if m and not mask: return m if not strict and '*' not in mask: mask = '*' + mask + '*' r = [] for d in m: l = fn.get_device(d).ListFiles(mask) for f in l: r.append(d + ':' + f) if N and N >= len(r): return r return r
def start_devices(self, regexp='*', force=False, do_init=False, do_restart=False): #devs = fn.tango.get_class_devices('HdbEventSubscriber') devs = self.get_archivers() if regexp: devs = fn.filtersmart(devs, regexp) off = sorted(set(d for d in devs if not fn.check_device(d))) if off and do_restart: print('Restarting %s Archiving Servers ...' % self.db_name) astor = fn.Astor() astor.load_from_devs_list(list(off)) astor.stop_servers() fn.wait(3.) astor.start_servers() fn.wait(3.) for d in devs: try: dp = fn.get_device(d) if do_init: dp.init() if force or dp.attributenumber != dp.attributestartednumber: off.append(d) print('%s.Start()' % d) dp.start() except Exception, e: self.warning('start_archivers(%s) failed: %s' % (d, e))
def start_devices(self,regexp = '*', force = False, do_init = False, do_restart = False): #devs = fn.tango.get_class_devices('HdbEventSubscriber') devs = self.get_archivers() if regexp: devs = fn.filtersmart(devs,regexp) off = sorted(set(d for d in devs if not fn.check_device(d))) if off and do_restart: print('Restarting %s Archiving Servers ...'%self.db_name) astor = fn.Astor() astor.load_from_devs_list(list(off)) astor.stop_servers() fn.wait(3.) astor.start_servers() fn.wait(3.) for d in devs: try: dp = fn.get_device(d, keep=True) if do_init: dp.init() if force or dp.attributenumber != dp.attributestartednumber: off.append(d) print('%s.Start()' % d) dp.start() except Exception,e: self.warning('start_archivers(%s) failed: %s' % (d,e))
def set_push_events(filein, period=3000, diff=1e-5): print('set_push_events(%s,%s,%s)' % (filein, period, diff)) devs = fandango.get_matching_devices(filein) if devs: devs = dict( (d, fandango.Struct( {'attrs': fandango.get_device(d).get_attribute_list()})) for d in devs) else: devs = pickle.load(open(filein)) for d, t in sorted(devs.items()): print('Setting events (%s,%s) for %s' % (period, diff, d)) dp = PyTango.DeviceProxy(d) for a in t.attrs: dp.poll_attribute(a, int(period)) if period > 0: ac = dp.get_attribute_config(a) cei = PyTango.ChangeEventInfo() cei.rel_change = str(diff) ac.events.ch_event = cei try: dp.set_attribute_config(ac) except: pass print('done')
def get_device_timeout(device,timeout,tries=1,exported=[], proxy=None,attr='ActiveAlarms',trace=True): exported = exported or fd.tango.get_all_devices(exported=True) device = device.lower() proxy = proxy or fd.get_device(device) t = 0 if device not in exported: if trace: print('%s is not exported!'%d) return -1 else: for i in range(tries): try: proxy.set_timeout_millis(int(timeout)) t0 = now() for a in proxy.get_attribute_list(): if clmatch(attr,a): proxy.read_attribute(a) time.sleep(1e-5) t1 = now()-t0 except Exception,e: if trace: print('%s failed!: %s'%(device,fd.excepts.exc2str(e))) #traceback.print_exc() t = END_OF_TIME break t += t1/float(tries)
def __init__(self,device,filename=''): self.proxy = fandango.get_device(device) self.commands = copy_cmd_list(self.proxy) self.attributes = copy_attr_list(self.proxy) self.filename = filename self.data = [] self.mode = self.BYPASS self.load()
def get_device_eval_times(proxy, timeout=10000., total=False): if isString(proxy): proxy = fd.get_device(proxy) vals = evaluate_formula(proxy, 'SELF.EvalTimes', timeout) if total: if len(vals): vals = sum(vals.values()) else: vals = -1 return vals
def get_archiver_errors(self, archiver): try: dp = fn.get_device(archiver, keep=True) al = dp.AttributeList or [] er = dp.AttributeErrorList or [] return dict((a, e) for a, e in zip(al, er) if e) except: print('Unable to get %s errors' % archiver) return {}
def get_periodic_archiver_errors(self, archiver): try: dp = fn.get_device(archiver, keep=True) #al = dp.AttributeList er = dp.AttributesErrorList return er or [] #dict((a,e) for a,e in zip(al,er) if e) except: print('Unable to get %s errors' % archiver) traceback.print_exc() return {}
def add_attributes(self,attributes,*args,**kwargs): """ Call add_attribute sequentially with a 1s pause between calls :param start: True by default, will force Start() in related archivers See add_attribute? for more help on arguments """ try: start = kwargs.get('start',True) for a in attributes: kwargs['start'] = False #Avoid recursive start self.add_attribute(a,*args,**kwargs) time.sleep(3.) if start: archs = set(map(self.get_attribute_archiver,attributes)) for h in archs: self.info('%s.Start()' % h) fn.get_device(h, keep=True).Start() except Exception,e: print('add_attribute(%s) failed!: %s'%(a,traceback.print_exc()))
def add_attributes(self, attributes, *args, **kwargs): """ Call add_attribute sequentially with a 1s pause between calls :param start: True by default, will force Start() in related archivers See add_attribute? for more help on arguments """ try: start = kwargs.get('start', True) for a in attributes: kwargs['start'] = False #Avoid recursive start self.add_attribute(a, *args, **kwargs) time.sleep(3.) if start: archs = set(map(self.get_attribute_archiver, attributes)) for h in archs: self.info('%s.Start()' % h) fn.get_device(h).Start() except Exception, e: print('add_attribute(%s) failed!: %s' % (a, traceback.print_exc()))
def add_attributes(self, attributes, *args, **kwargs): """ Call add_attribute sequentially with a 1s pause between calls :param start: True by default, will force Start() in related archivers See add_attribute? for more help on arguments """ try: attributes = sorted(attributes) start = kwargs.get('start', True) devs = fn.defaultdict(list) [devs[fn.tango.get_dev_name(a)].append(a) for a in attributes] for dev, attrs in devs.items(): arch = self.get_next_archiver(attrexp=dev + '/*') for a in attrs: kwargs['start'] = False #Avoid recursive start try: kwargs['clear'] = False self.add_attribute(a, archiver=arch, *args, **kwargs) except: self.warning('add_attribute(%s) failed!\n%s' % (a, traceback.format_exc())) time.sleep(3.) self.clear_caches() if start: self.get_archivers_attributes() archs = set(map(self.get_attribute_archiver, attributes)) for h in archs: try: if h: self.info('%s.Start()' % h) fn.get_device(h, keep=True).Start() except: traceback.print_exc() except Exception, e: self.error('add_attributes(%s) failed!: %s' % (attributes, traceback.print_exc()))
def restart_attribute(self,attr, d=''): try: d = self.get_attribute_archiver(attr) print('%s.restart_attribute(%s)' % (d,attr)) dp = fn.get_device(d, keep=True) if not fn.check_device(dp): self.start_devices('(.*/)?'+d,do_restart=True) dp.AttributeStop(attr) fn.wait(.1) dp.AttributeStart(attr) except: print('%s.AttributeStart(%s) failed!'%(d,attr))
def get_value_formatter(attribute): formatter = str try: #ai = fandango.tango.get_attribute_config(attribute) d, a = attribute.rsplit('/', 1) ai = fandango.get_device(d).get_attribute_config(a) #print(attribute,':',ai) if ai.data_type is fandango.tango.CmdArgType.DevState: formatter = state_formatter #else: #print(repr(ai.datatype)) except: traceback.print_exc() return formatter
def get_value_formatter(attribute): formatter = str try: #ai = fandango.tango.get_attribute_config(attribute) d,a = attribute.rsplit('/',1) ai = fandango.get_device(d).get_attribute_config(a) #print(attribute,':',ai) if ai.data_type is fandango.tango.CmdArgType.DevState: formatter = state_formatter #else: #print(repr(ai.datatype)) except: traceback.print_exc() return formatter
def restart_attribute(self, attr, d=''): try: d = self.get_attribute_archiver(attr) print('%s.restart_attribute(%s)' % (d, attr)) dp = fn.get_device(d, keep=True) if not fn.check_device(dp): self.start_devices('(.*/)?' + d, do_restart=True) dp.AttributeStop(attr) fn.wait(.1) dp.AttributeStart(attr) except: print('%s.AttributeStart(%s) failed!' % (d, attr))
def get_stopped_attributes(self, errors=False): r = [] for d in self.get_subscribers(): try: dp = fn.get_device(d, keep=True) l = dp.AttributeStoppedList if l: r.extend(l) if errors: self.debug('adding %s error list' % d) r.extend(self.get_archiver_errors(d).keys()) except: self.warning('%s not running!' % d) r.extend(self.get_archiver_attributes(d, from_db=True)) return r
def stop_periodic_archiving(self, attribute): try: attribute = parse_tango_model(attribute, fqdn=True).fullname.lower() arch = self.get_periodic_attribute_archiver(attribute) if not arch: self.warning('%s is not archived!' % attribute) else: self.info('Removing %s from %s' % (attribute, arch)) dp = fn.get_device(archiver) v = dp.AttributeRemove([attribute, str(int(float(period)))]) dp.UpdateAttributeList() fn.wait(wait) return v except: self.warning('stop_periodic_archiving(%s) failed!' % (attribute, traceback.format_exc()))
def restart_periodic_archiving(self, attribute): try: attribute = parse_tango_model(attribute, fqdn=True).fullname.lower() arch = self.get_periodic_attribute_archiver(attribute) if not arch: self.warning('%s is not archived!' % attribute) else: self.info('Restarting %s at %s' % (attribute, arch)) dp = fn.get_device(archiver) v = dp.AttributeStop(attribute) dp.ResetErrorAttributes() fn.wait(wait) v = dp.AttributeStart(attribute) return v except: self.warning('restart_periodic_archiving(%s) failed!' % (attribute, traceback.format_exc()))
def inspect_device(device): dp = fn.get_device(device) attrs = eval(dp.evaluateformula('FORMULAS')) mapping = eval(dp.evaluateformula('Mapping')) mapped = [a for a,f in attrs.items() if any (m in f for m in mapping)] polled = fn.tango.get_polled_attrs(device) polled = [a for a in mapped if a.lower() in polled] rd = PyTangoArchiving.Reader() arch = filter(rd.is_attribute_archived,[device+'/'+a for a in mapped]) used = set(map(str.lower,polled+arch)) p = sum(int(l.split(',')[-1]) for l in mapping.values()) return dp.MemUsage,len(mapped),len(polled),len(arch),len(used),p
def restart_attribute(self, attr, d=''): """ execute AttributeStop/Start on subscriber device """ try: a = self.is_attribute_archived(attr) if not a: raise Exception('%s is not archived!' % attr) attr = a d = self.get_attribute_archiver(attr) print('%s.restart_attribute(%s)' % (d, attr)) dp = fn.get_device(d, keep=True) if not fn.check_device(dp): self.start_devices('(.*/)?' + d, do_restart=True) dp.AttributeStop(attr) fn.wait(10.) dp.AttributeStart(attr) except: print('%s.AttributeStart(%s) failed!' % (d, attr))
def add_periodic_attribute(self, attribute, period, archiver=None, wait=3., force=False): if not force and period < 500: raise Exception('periods below 500 ms are not allowed!') attribute = parse_tango_model(attribute, fqdn=True).fullname.lower() arch = self.get_periodic_attribute_archiver(attribute) if arch: print('%s is already archived by %s!' % (attribute, arch)) p = self.get_periodic_attribute_period(attribute) if p == period: return False else: archiver = arch archiver = archiver or self.get_next_periodic_archiver( attrexp=fn.tango.get_dev_name(attribute) + '/*') if not self.is_attribute_archived(attribute): self.info('Attribute %s does not exist in %s database, adding it' % (attribute, self.db_name)) subs = [d for d in self.get_subscribers() if 'null' in d.lower()] self.add_attribute(attribute, archiver=(subs[0] if subs else None), code_event=True, context='SERVICE') self.info('%s.AttributeAdd(%s,%s)' % (archiver, attribute, period)) dp = fn.get_device(archiver, keep=True) dp.set_timeout_millis(30000) v = dp.AttributeAdd([attribute, str(int(float(period)))]) fn.wait(wait) return v
def set_push_events(filein, period=3000, diff=1e-5): print('set_push_events(%s,%s,%s)' % (filein, period, diff)) devs = fd.get_matching_devices(filein) for d in devs[:]: if not check_device(d): q = raw_input('Unable to configure events for %s, ' 'do you wish to continue?' % d).lower() if 'y' not in q: return devs.remove(d) if devs: devs = dict( (d, fd.Struct({'attrs': fd.get_device(d).get_attribute_list()})) for d in devs) else: devs = pickle.load(open(filein)) for d, t in sorted(devs.items()): print('Setting events (%s,%s) for %s' % (period, diff, d)) try: dp = PyTango.DeviceProxy(d) for a in t.attrs: dp.poll_attribute(a, int(period)) if period > 0: ac = dp.get_attribute_config(a) cei = PyTango.ChangeEventInfo() cei.rel_change = str(diff) ac.events.ch_event = cei try: dp.set_attribute_config(ac) except: pass except: q = raw_input('Unable to configure events for %s, ' 'do you wish to continue?' % d) if 'y' not in q.lower(): break print('done')
def convert_properties(device,write=False): dp = fn.get_device(device) prop = fn.get_device_property(device,'DynamicAttributes') r = '([^\ ]*).*[=]IeeeFloat.*AnalogRealsREAD\[([0-9]+)[:].*\].*\)(.*)' for j,p in enumerate(prop): m = re.match(r,p) if m: a,i,k = m.groups() n = '%s = IeeeFloat(AnalogRealsREAD,%s)%s'%(a,i,k) print(p) pv = dp.EvaluateFormula(p.split('=')[-1]) nv = dp.EvaluateFormula(n.split('=')[-1]) print(n,pv,nv) prop[j] = n print('') print('\n'.join(prop)) if write: fn.put_device_property(device,'DynamicAttributes',prop) dp.updateDynamicAttributes()
def get_attr_descriptions(device): dp = fandango.get_device(device) aql = fandango.retry(dp.attribute_list_query) return dict((c.name,c) for c in aql)
def main_test(): print msg try: msg=""" #Create the test device #Launch it; take snapshot of memory usage NOTE: This testing is not capable of testing email/SMS sending. This will have to be human-tested defining a test receiver: Basic steps: * Create a simulator with attributes suitable for testing. * Start the simulators * Ensure that all alarms conditions are not enabled reseting attribute values. <pre><code class="python">""" if check_step(0): tango.add_new_device('PySignalSimulator/test-alarms','PySignalSimulator','test/test/alarms-test') tango.put_device_property('test/test/alarms-test',{ 'DynamicAttributes':map(str.strip, """#ALARM TESTING A=READ and VAR('VALUE1') or WRITE and VAR('VALUE1',VALUE) B=DevDouble(READ and VAR('B') or WRITE and VAR('B',VALUE)) S=DevDouble(READ and VAR('B')*sin(t%3.14) or WRITE and VAR('B',VALUE)) D=DevLong(READ and PROPERTY('DATA',True) or WRITE and WPROPERTY('DATA',VALUE)) C = DevLong(READ and VAR('C') or WRITE and VAR('C',VALUE)) T=t""".split('\n')), 'DynamicStates': 'STATE=C #INT to STATE conversion' }) fandango.Astor().start_servers('PySignalSimulator/test-alarms') time.sleep(10.) simulator = fandango.get_device('test/test/alarms-test') [simulator.write_attribute(a,0) for a in 'ABSDC'] msg="""</pre> * Create 2 PyAlarm instances, to check attribute-based and alarm/group based alarms * Setup the time variables that will manage the alarm cycle <pre><code class="python">""" alarms = panic.api() if check_step(1): tango.add_new_device('PyAlarm/test-alarms','PyAlarm','test/alarms/alarms-test') tango.add_new_device('PyAlarm/test-group','PyAlarm','test/alarms/alarms-group') threshold = 3 polling = 5 autoreset = 60 alarmdevs = ['test/alarms/alarms-test','test/alarms/alarms-group'] props = { 'Enabled':'15', 'AlarmThreshold':threshold, 'AlertOnRecovery':'email', 'PollingPeriod':polling, 'Reminder':0, 'AutoReset':autoreset, 'RethrowState':True, 'RethrowAttribute':False, 'IgnoreExceptions':True, 'UseSnap':True, 'CreateNewContexts':True, 'MaxMessagesPerAlarm':20, 'FromAddress':'*****@*****.**', 'LogLevel':'DEBUG', 'SMSConfig':':', 'StartupDelay':0, 'EvalTimeout':500, 'UseProcess':False, 'UseTaurus':False, } [tango.put_device_property(d,props) for d in alarmdevs] N,msg=2,gb+"* Start the PyAlarm devices"+ge if check_step(N): receiver = "[email protected],SMS:+3400000000" fandango.Astor().start_servers('PyAlarm/test-alarms') fandango.Astor().start_servers('PyAlarm/test-group') time.sleep(15.) N,msg=3,gb+"* create simple and group Alarms to inspect."+ge if check_step(N): alarms.add(tag='TEST_A',formula='test/test/alarms-test/A',device='test/alarms/alarms-test', receivers=receiver,overwrite=True) alarms.add(tag='TEST_DELTA',device='test/alarms/alarms-test',receivers=receiver,overwrite=True, formula = 'not -5<test/sim/test-00/S.delta<5 and ( test/sim/test-00/S, test/sim/test-00/S.delta )') alarms.add(tag='TEST_STATE',formula='test/test/alarms-test/State not in (OFF,UNKNOWN,FAULT,ALARM)', device='test/alarms/alarms-test',receivers=receiver,overwrite=True) alarms.add(tag='TEST_GROUP1',formula='any([d>0 for d in FIND(test/alarms/*/TEST_[ABC].delta)]) and FIND(test/alarms/*/TEST_[ABC])', device='test/alarms/alarms-group',receivers=receiver,overwrite=True) alarms.add(tag='TEST_GROUP2',formula='GROUP(TEST_[ABC])', device='test/alarms/alarms-group',receivers=receiver,overwrite=True) N,msg=4,gb+""" Test steps: * Enable an alarm condition in the simulated A attribute. * Alarm should be enabled after 1+AlarmThreshold*PollingPeriod time (and not before) * Group alarm should be enabled after 1+AlarmThreshold*PollingPeriod """+ge if check_step(N): pass N,msg=5,gb+""" * Disable alarm condition * Alarm should enter in recovery state after AlarmThreshold*PollingPeriod. * Alarm should AutoReset after AutoReset period. * Group should reset after AutoReset period. ###############################################################################"""+ge if check_step(N): pass N,msg=6,gb+""" * Testing properties ** RethrowAttribute ... None/NaN ** RethrowState ... True/False ** Enabled ... time or formula or both ###############################################################################"""+ge if check_step(N): pass msg=ge except: print traceback.format_exc() N,msg = -1,"""Stopping all servers ...""" check_step(N) fandango.Astor().stop_servers('PySignalSimulator/test-alarms') fandango.Astor().stop_servers('PyAlarm/test-alarms') fandango.Astor().stop_servers('PyAlarm/test-group')
def add_attribute(self, attribute, archiver=None, period=0, rel_event=None, per_event=None, abs_event=None, code_event=False, ttl=None, start=False, use_freq=True, clear=False, context='ALWAYS'): """ set _event arguments to -1 to ignore them and not modify the database code_event will be set to True if no other event is setup """ attribute = parse_tango_model(attribute, fqdn=True).fullname if archiver: archiver = fn.tango.get_full_name(archiver, fqdn=True) archiver = archiver or self.get_next_archiver( use_freq=use_freq, attrexp=fn.tango.get_dev_name(attribute) + '/*') self.warning('add_attribute(%s, %s) to %s' % (attribute, archiver, self.db_name)) config = get_attribute_config(attribute) #if 'spectrum' in str(config.data_format).lower(): #raise Exception('Arrays not supported yet!') data_type = str(PyTango.CmdArgType.values[config.data_type]) if not self.manager: return False try: d = self.get_manager() d.lock() print('SetAttributeName: %s' % attribute) d.write_attribute('SetAttributeName', attribute) time.sleep(0.2) if period > 0: d.write_attribute('SetPollingPeriod', period) if per_event not in (None, -1, 0): d.write_attribute('SetPeriodEvent', per_event) if not any((abs_event, rel_event, code_event)): code_event = True if abs_event not in (None, -1, 0): print('SetAbsoluteEvent: %s' % abs_event) d.write_attribute('SetAbsoluteEvent', abs_event) if rel_event not in (None, -1, 0): d.write_attribute('SetRelativeEvent', rel_event) if ttl not in (None, -1): d.write_attribute('SetTTL', ttl) d.write_attribute('SetCodePushedEvent', code_event) d.write_attribute('SetStrategy', context) d.write_attribute('SetArchiver', archiver) time.sleep(.2) d.AttributeAdd() if start: try: arch = archiver # self.get_attribute_archiver(attribute) self.info('%s.Start()' % (arch)) fn.get_device(arch, keep=True).Start() except: traceback.print_exc() if clear: self.clear_caches() except Exception, e: if 'already archived' not in str(e).lower(): self.error('add_attribute(%s,%s,%s): %s' % (attribute, archiver, period, traceback.format_exc().replace('\n', ''))) else: self.warning('%s already archived!' % attribute) return False
def get_cmd_descriptions(device): return dict((c.cmd_name,c) for c in fandango.get_device(device).command_list_query())
def get_archiver_errors(self,archiver): dp = fn.get_device(archiver,keep=True) al = dp.AttributeList er = dp.AttributeErrorList return dict((a,e) for a,e in zip(al,er) if e)
#!/usr/bin/env python import fandango as fn devs = fn.tango.get_class_devices('HdbEventSubscriber') for d in devs: try: if not fn.check_device(d): fn.Astor(d).stop_servers() fn.Astor(d).start_servers() else: # Wait to next iteration before setting polling dp = fn.get_device(d) dp.poll_command('start', 1200000) print(d, 'done') except: print(fn.getLastException())
#!/usr/bin/env python import fandango as fn devs = fn.tango.get_class_devices('HdbEventSubscriber') for d in devs: try: if not fn.check_device(d): fn.Astor(d).stop_servers() fn.Astor(d).start_servers() else: # Wait to next iteration before setting polling dp = fn.get_device(d) dp.poll_command('start',1200000) print(d,'done') except: print(fn.getLastException())
def get_panic_report(api=None, timeout=3000., tries=3, devfilter='*', attr='ActiveAlarms', trace=False): """ The key of the results: key.count('/') == 0 : alarm == 1: server == 2: device == 4: summary """ if api is None: api = panic.api() elif isString(api): api = panic.api(api) if not len(api.servers): if devfilter == '*': api.servers.load_by_name('PyAlarm/*') if isString(devfilter): api.servers.load_by_name(devfilter) else: [api.servers.load_by_name(d) for d in devfilter] alldevs = fd.tango.get_all_devices(exported=True) result = fd.dicts.defaultdict(dict) result['//alarms//'] = api.alarms.keys() result['//devices//'] = api.devices.keys() result['//servers//'] = api.servers.keys() off = [] hung = [] slow = [] aslow = [] errors = {} for s, ss in api.servers.items(): admin = fd.parse_tango_model(ss.get_admin_name())['device'] if admin.lower() not in alldevs: off.append(admin) result[s]['devices'] = ss.get_classes()['PyAlarm'] for d in result[s]['devices']: if isSequence(devfilter): if d not in devfilter: continue elif not clsearch(devfilter, d): continue if admin in off: t = END_OF_TIME off.append(d) else: proxy = fd.get_device(d) t = get_device_timeout(d, timeout, tries, alldevs, proxy, trace=False, attr=attr) if t == END_OF_TIME: hung.append(d) if t * 1e3 > timeout: slow.append(d) result[d]['timeout'] = t result[d]['attrs'] = 0 result[d]['alarms'] = len(api.devices[d].alarms) polling = float(api.devices[d].config['PollingPeriod']) result[d]['polling'] = polling try: evals = get_device_eval_times(proxy, timeout) teval = sum(evals.values()) ratio = teval / float(polling) result[d]['eval'], result[d]['ratio'] = teval, ratio except: evals = {} result[d]['eval'], result[d]['ratio'] = -1, -1 for a, aa in api.devices[d].alarms.items(): attrs = api.parse_attributes(aa['formula']) result[d]['attrs'] += len(attrs) result[a]['device'] = d result[a]['attrs'] = attrs result[a]['timeout'] = evals.get(a, -1) if result[a]['timeout'] > polling / result[d]['alarms']: aslow.append(a) result['//off//'] = off result['//bloat//'] = [ k for k in result if k.count('/') == 2 and result[k].get('ratio', -1) >= 1. ] result['//hung//'] = hung result['//slow_devices//'] = slow result['//slow_alarms//'] = aslow #print('off devices: %s\n'%(off)) #print('hung devices: %s\n'%(hung)) #print('slow devices: %s\n'%(slow)) #print('bloat devices: %s\n'%([k for k in result #if '/' in k and result[k].get('ratio',-1)>=1.])) #print('slow alarms: %s\n'%aslow) return result
def create_simulators(filein, instance='', path='', domains={}, server='', tango_host='', filters='', override=True): #domains = {'wr/rf':'test/rf'} path = path or os.path.abspath(os.path.dirname(filein)) + '/' print('create_simulators:' + str( (filein, instance, path, domains, tango_host))) files = fd.listdir(path) if not any(f.endswith('_attributes.txt') for f in files): q = raw_input('Property files do not exist yet,\n' 'Do you want to generate them? (y/n)') if q.lower().startswith('y'): cur = os.path.abspath(os.curdir) os.chdir(path) generate_class_properties(filein) os.chdir(cur) ## CHECK IS MANDATORY, YOU SHOULD EXPORT AND SIMULATE IN DIFFERENT HOSTS assert tango_host and tango_host in str(fd.tango.get_tango_host()),\ 'Tango Host (%s!=%s) does not match!'%(tango_host,fd.tango.get_tango_host()) devs, org = {}, pickle.load( open(filein if '/' in filein else path + filein)) done = [] all_devs = fd.get_all_devices() print('>' * 80) if not filters: print('%d devices in %s: %s' % (len(org), filein, sorted(org.keys()))) filters = raw_input('Enter a filter for device names: [*/*/*]').lower() for d, t in org.items(): k = ('/'.join( d.split('/')[-3:])).lower() #Removing tango host from the name for a, b in domains.items(): if k.startswith(a): k = k.replace(a, b) if not filters or fd.matchCl(filters, d) or fd.matchCl( filters, org[d].dev_class): devs[k] = t if override is not False: dds = [ d for d in devs if ('/'.join(d.split('/')[-3:])).lower() in all_devs ] if dds: print('%d devices already exist: %s' % (len(dds), sorted(dds))) override = raw_input( 'Do you want to override existing properties? (y/n)').lower( ).startswith('y') else: override = False if not instance: instance = raw_input( 'Enter your instance name for the simulated server (use "instance-" to use multiple instances):' ) elif '/' in instance: server, instance = instance.split('/') keepclass = 'y' in raw_input('Keep original Class names?').lower() if keepclass: server = 'SimulatorDS' elif not server: server = raw_input( 'Enter your server name (SimulatorDS/DynamicDS): [SimulatorDS]') \ or 'SimulatorDS' print('>' * 80) for d, t in sorted(devs.items()): t.dev_class = t.dev_class or d.split('/')[-1] if t.dev_class == 'PyStateComposer': klass = t.dev_class elif keepclass: klass = t.dev_class + '_sim' else: klass = 'SimulatorDS' instance_temp = '%s%s' % (instance, t.dev_class) if '-' in instance else instance print('%s/%s:%s , "%s" => %s ' % (server, instance_temp, d, t.dev_class, klass)) its_new = ('/'.join(('dserver', server, instance_temp)) ).lower() not in all_devs or d.lower() not in all_devs if its_new or override: print('writing ... %s(%s)' % (type(t), d)) fd.tango.add_new_device('%s/%s' % (server, instance_temp), klass, d) for p, v in t.props.items(): if not p.startswith( '__' ): #p not in ('DynamicCommands','DynamicStates','LoadFromFile','DevicesList') and fd.tango.put_device_property(d, p, v) #Overriding Dynamic* properties try: fd.tango.put_device_property( d, 'LoadFromFile', path + '%s_attributes.txt' % t.dev_class) fd.tango.put_device_property( d, 'DynamicAttributes', filter( bool, map( str.strip, open(path + '%s_attributes.txt' % t.dev_class).readlines()))) fd.tango.put_device_property( d, 'DynamicCommands', filter( bool, map( str.strip, open(path + '%s_commands.txt' % t.dev_class).readlines()))) fd.tango.put_device_property( d, 'DynamicStates', filter( bool, map( str.strip, open(path + '%s_states.txt' % t.dev_class).readlines()))) except: print('Unable to configure %s(%s) properties ' % (d, t.dev_class)) #traceback.print_exc() fd.tango.put_device_property(d, 'OFFSET', random.randint(0, len(devs))) done.append(d) exported = fd.get_all_devices(exported=True) update = [d for d in done if d in exported] print('Updating %d Devices ...' % len(update)) for d in update: if fd.check_device(d): print('Updating %s ...' % d) try: fd.get_device(d).updateDynamicAttributes() except Exception, e: print(e) else: print('%s failed!' % d) time.sleep(.2)
def add_attribute(self,attribute,archiver,period=0, rel_event=None,per_event=None,abs_event=None, code_event=False, ttl=None, start=False): """ set _event arguments to -1 to ignore them and not modify the database """ attribute = parse_tango_model(attribute,fqdn=True).fullname self.info('add_attribute(%s)'%attribute) config = get_attribute_config(attribute) #if 'spectrum' in str(config.data_format).lower(): #raise Exception('Arrays not supported yet!') data_type = str(PyTango.CmdArgType.values[config.data_type]) if not self.manager: return False try: d = self.get_manager() d.lock() print('SetAttributeName: %s'%attribute) d.write_attribute('SetAttributeName',attribute) time.sleep(0.2) if period>0: d.write_attribute('SetPollingPeriod',period) if per_event not in (None,-1): d.write_attribute('SetPeriodEvent',per_event) if not any((abs_event,rel_event,code_event)): if re.search("short|long",data_type.lower()): abs_event = 1 elif not re.search("bool|string",data_type.lower()): rel_event = 1e-2 if abs_event not in (None,-1): print('SetAbsoluteEvent: %s'%abs_event) d.write_attribute('SetAbsoluteEvent',abs_event) if rel_event not in (None,-1): d.write_attribute('SetRelativeEvent',rel_event) if ttl not in (None,-1): d.write_attribute('SetTTL',ttl) d.write_attribute('SetCodePushedEvent',code_event) d.write_attribute('SetArchiver',archiver) time.sleep(.2) d.AttributeAdd() if start: try: arch = archiver # self.get_attribute_archiver(attribute) self.info('%s.Start()' % (arch)) fn.get_device(arch, keep=True).Start() except: traceback.print_exc() except Exception,e: if 'already archived' not in str(e).lower(): self.error('add_attribute(%s,%s,%s): %s' %(attribute,archiver,period, traceback.format_exc().replace('\n',''))) else: self.warning('%s already archived!' % attribute) return False
def add_attribute(self, attribute, archiver, period=0, rel_event=None, per_event=300000, abs_event=None, code_event=False, ttl=None, start=False): """ set _event arguments to -1 to ignore them and not modify the database """ attribute = parse_tango_model(attribute, fqdn=True).fullname self.info('add_attribute(%s)' % attribute) config = get_attribute_config(attribute) #if 'spectrum' in str(config.data_format).lower(): #raise Exception('Arrays not supported yet!') data_type = str(PyTango.CmdArgType.values[config.data_type]) if not self.manager: return False try: d = self.get_manager() d.lock() print('SetAttributeName: %s' % attribute) d.write_attribute('SetAttributeName', attribute) time.sleep(0.2) if period > 0: d.write_attribute('SetPollingPeriod', period) if per_event not in (None, -1): d.write_attribute('SetPeriodEvent', per_event) if not any((abs_event, rel_event)): if re.search("short|long", data_type.lower()): abs_event = 1 elif not re.search("bool|string", data_type.lower()): rel_event = 1e-2 if abs_event not in (None, -1): print('SetAbsoluteEvent: %s' % abs_event) d.write_attribute('SetAbsoluteEvent', abs_event) if rel_event not in (None, -1): d.write_attribute('SetRelativeEvent', rel_event) if ttl not in (None, -1): d.write_attribute('SetTTL', ttl) d.write_attribute('SetCodePushedEvent', code_event) d.write_attribute('SetArchiver', archiver) time.sleep(.2) d.AttributeAdd() if start: try: arch = archiver # self.get_attribute_archiver(attribute) self.warning('%s.Start()' % (arch)) fn.get_device(arch).Start() except: traceback.print_exc() except Exception, e: if 'already archived' not in str(e).lower(): self.error('add_attribute(%s,%s,%s): %s' % (attribute, archiver, period, traceback.format_exc().replace('\n', ''))) else: self.warning('%s already archived!' % attribute) return False