def register_devices(device_class_name, server_name, server_instance_name, devices_list=None): if devices_list is None: print """No device(s) to register for server: {}, server_instance: {}, class: {} """.format(server_name, server_instance_name, device_class_name) return elif not isinstance(devices_list, list): devices_list = [devices_list] svr_name = server_name + '/' + server_instance_name for device_name in devices_list: print """Attempting to register TANGO device {} class: {} server: {}.""".format(device_name, device_class_name, svr_name) try: fantango.add_new_device(svr_name, device_class_name, device_name) except PyTango.DevError as deverr: logging.error("FAILED to register device {} {}". format(device_name, deverr)) print """Failed to register device {} due to an error with the database. """.format(device_name) devices_not_registered.append("{}({})".format(device_name, svr_name)) else: print """Successfully registered device {} in the database. """.format(device_name) registered_devices.append(device_name+"({})".format(svr_name))
def configure(): global tangodb, alarms, receiver, sender fn.log.info('MAIL_TEST: CONFIGURE '+'>'*40) ft.add_new_device('PyAlarm/test_mail','PyAlarm','test/pyalarm/sendmail') ft.add_new_device('PyAlarm/test_mail','PyAlarm','test/pyalarm/smtpmail') alarms.load() alarms.edit_phonebook('%TEST_MAIL',receiver) alarms.add('TEST_MAIL', 'test/pyalarm/sendmail', formula = '1', description = 'Testing alarm sending using sendmail', receivers = '%TEST_MAIL') alarms.put_db_properties('test/pyalarm/sendmail',{ 'MailMethod':'mail', 'FromAddress': sender, #'*****@*****.**', 'PollingPeriod':3,'AlarmThreshold':1, 'StartupDelay':0, 'Enabled':'1'}) alarms.add('TEST_SMTP', 'test/pyalarm/smtpmail', formula = '1', description = 'Testing alarm sending using smtplib', receivers = '%TEST_MAIL') alarms.put_db_properties('test/pyalarm/smtpmail',{ 'MailMethod':'smtp:%s'%smtp_host, 'FromAddress': sender, #'*****@*****.**', 'PollingPeriod':3,'AlarmThreshold':1, 'StartupDelay':0, 'Enabled':'1'})
def configure(self, servers, tags, phonebook): """ servers['PyAlarm/test_tg']['test/pyalarm/tg'].update({ 'TGConfig': sender, }) phonebook = { '%TESTER':receiver } tags['TEST_TG'] = { 'device': 'test/pyalarm/tg', 'formula': '1', 'description': 'Testing alarm sending using telegram', 'receivers': '%TESTER', } """ self.log('CONFIGURE ' + '>' * 40) self.servers = servers self.tags = tags self.phonebook = phonebook for s, devs in servers.items(): for d, props in devs.items(): ft.add_new_device('PyAlarm/' + s, 'PyAlarm', d) self.alarms.load() [self.alarms.edit_phonebook(k, v) for k, v in phonebook.items()] for a, props in tags.items(): self.alarms.add(a, **props) for s, devs in servers.items(): for d, props in devs.items(): dct = PanicTester.defaults.copy() dct.update(props) self.alarms.put_db_properties(d, dct)
def create(s=server, d=device, p=w): try: s, d = str(s.text()), str(d.text()) if '/' not in s: s = 'PyAlarm/%s' % s import fandango.tango as ft ft.add_new_device(s, 'PyAlarm', d) print('%s - %s: created!' % (s, d)) except: traceback.print_exc() self.api.load() p.close()
def load(instance, devices=[df, sf]): """ load .json files into simulated devices """ done = [] devices = fn.toList(devices) for dd in devices: if os.path.isfile(dd): df, dd = dd, file2dev(dd) else: df, dd = dev2file(dd), dd data = json.load(open(df)) if data['dev_class'] == 'PyAlarm': props = data['properties'] props = dict((str(k), map(str, v)) for k, v in props.items()) assert not ft.get_matching_devices(dd), Exception( 'Device %s Already Exists!!!' % dd) ft.add_new_device('PyAlarm/' + instance, 'PyAlarm', dd) ft.put_device_property(dd, props) else: vals = dict( (str(k), v['value']) for k, v in data['attributes'].items()) dynattrs = [] for k, v in sorted(vals.items()): if k.lower() in ('state', 'status'): continue t = type(v).__name__ if t == 'unicode': t = 'str' v = str(v) if t != 'str' else "'%s'" % v dynattrs.append('%s = %s(%s)' % (k, t, v)) assert not ft.get_matching_devices(dd), Exception( 'Device %s Already Exists!!!' % dd) ft.add_new_device('PyAttributeProcessor/' + instance, 'PyAttributeProcessor', dd) ft.put_device_property(dd, 'DynamicAttributes', dynattrs) done.append(dd) return done
def load(tango_host, instance, devices, replace={}, overwrite=False, def_class='SimulatorDS'): """ the tango_host variable must match with the current tango_host; it is used to avoid accidental loading load .json files into simulated devices; one .json file per device devices may be a list of devices, a list of files or a dictionary {device:filename} """ assert tango_host == fn.get_tango_host() done = [] if isMapping(devices): filenames = devices else: devices = fn.toList(devices) filenames = {} for dd in devices: if os.path.isfile(dd): df, dd = dd, file2dev(dd) else: df, dd = dev2file(dd), dd filenames[dd] = df for dd, df in filenames.items(): exists = ft.get_matching_devices(dd) if exists and not overwrite: raise Exception('Device %s Already Exists!!!' % dd) data = json.load(open(df)) props = data['properties'] props = dict((str(k), map(str, v)) for k, v in props.items()) for r, rr in replace.items(): dd = clsub(r, rr, dd) for p, pp in props.items(): for i, l in enumerate(pp): props[p][i] = clsub(r, rr, l) if overwrite: props['polled_attr'] = [] props['polled_cmd'] = [] if data['dev_class'] == 'PyAlarm': if not exists: ft.add_new_device('PyAlarm/' + instance, 'PyAlarm', dd) props['AlarmReceivers'] = [] ft.put_device_property(dd, props) else: if not exists: ft.add_new_device(def_class + '/' + instance, def_class, dd) ft.put_device_property(dd, props) #if data['dev_class'] not in ('PySignalSimulator','PyAttributeProcessor','PyStateComposer','CopyCatDS'): vals = dict((str(k), v['value'] if v else 0) for k, v in data['attributes'].items()) dynattrs = [] attrprops = fn.dicts.defaultdict(dict) for k, v in sorted(vals.items()): if k.lower() in ('state', 'status'): continue if v is None: continue t = type(v).__name__ if t == 'unicode': t = 'str' v = str(v) if t != 'str' else "'%s'" % v dynattrs.append( #'%s = %s(%s) #%s'%( '%s = %s(VAR(%s,default=%s,WRITE=True)) #%s' % (k, k, t, v, data['attributes'][k]['data_type'])) attrprops[dd][k] = dict((p, data['attributes'][k].get(p, '')) for p in ('format', 'label', 'unit', 'min_alarm', 'max_alarm')) ft.put_device_property(dd, 'DynamicAttributes', dynattrs) try: ft.get_database().put_device_attribute_property( dd, dict((k, v) for k, v in attrprops[dd].items() if v)) except: fn.time.sleep(3.) done.append(dd) return done
def load(tango_host,instance,devices,replace={},overwrite=False): """ the tango_host variable must match with the current tango_host; it is used to avoid accidental loading load .json files into simulated devices devices may be a list of devices, a list of files or a dictionary {device:filename} """ assert tango_host == fn.get_tango_host() done = [] if isMapping(devices): filenames = devices else: devices = fn.toList(devices) filenames = {} for dd in devices: if os.path.isfile(dd): df,dd = dd,file2dev(dd) else: df,dd = dev2file(dd),dd filenames[dd] = df for dd,df in filenames.items(): exists = ft.get_matching_devices(dd) if exists and not overwrite: raise Exception('Device %s Already Exists!!!'%dd) data = json.load(open(df)) props = data['properties'] props = dict((str(k),map(str,v)) for k,v in props.items()) for r,rr in replace.items(): dd = clsub(r,rr,dd) for p,pp in props.items(): for i,l in enumerate(pp): props[p][i] = clsub(r,rr,l) if overwrite: props['polled_attr'] = [] props['polled_cmd'] = [] if data['dev_class'] == 'PyAlarm': if not exists: ft.add_new_device('PyAlarm/'+instance,'PyAlarm',dd) props['AlarmReceivers'] = [] ft.put_device_property(dd,props) else: if not exists: ft.add_new_device('PyAttributeProcessor/'+instance,'PyAttributeProcessor',dd) ft.put_device_property(dd,props) #if data['dev_class'] not in ('PySignalSimulator','PyAttributeProcessor','PyStateComposer','CopyCatDS'): vals = dict((str(k),v['value'] if v else 0) for k,v in data['attributes'].items()) dynattrs = [] attrprops = fn.dicts.defaultdict(dict) for k,v in sorted(vals.items()): if k.lower() in ('state','status'): continue if v is None: continue t = type(v).__name__ if t == 'unicode': t = 'str' v = str(v) if t!='str' else "'%s'"%v dynattrs.append( #'%s = %s(%s) #%s'%( '%s = %s(VAR(%s,default=%s,WRITE=True)) #%s'%(k, k,t,v,data['attributes'][k]['data_type'])) attrprops[dd][k] = dict((p,data['attributes'][k].get(p,'')) for p in ('format','label','unit','min_alarm','max_alarm')) ft.put_device_property(dd,'DynamicAttributes',dynattrs) try: ft.get_database().put_device_attribute_property(dd, dict((k,v) for k,v in attrprops[dd].items() if v)) except: fn.time.sleep(3.) done.append(dd) return done
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')