示例#1
0
def main(args=None):
    """
    see PyTangoArchiving.check.USAGE
    """
    try:
        import argparse
        parser = argparse.ArgumentParser() #usage=USAGE)
        parser.add_argument('schema')
        #parser.add_argument('--period',type=int)
        parser.add_argument('--tref',type=str,default='-43200',
            help = 'min epoch considered ok')
        parser.add_argument('--action',type=str,default='check',
            help = 'start|restart|save|check')
        parser.add_argument('--export',help = 'json|pickle',default='json')
        parser.add_argument('--values',type=str,
            help = 'values file, will be loaded by load_schema_values()')
        
        try:
            args = dict(parser.parse_args().__dict__)
        except:
            sys.exit(-1)
        
        #if not args:
            #args = {}
            #assert sys.argv[2:]
            #args = fn.sysargs_to_dict(defaults=('schema','period','ti',
                #'values','action','folder'))
        #print(args)

        if args.get('action') == 'start':
            print('Call Start() for %s devices' % sys.argv[1])
            pta.api(args['schema']).start_devices(force=True)
            print('done')
            
        if args.get('action') == 'restart':
            print('Restart %s servers' % sys.argv[1])
            pta.api(args['schema']).start_servers(restart=True)
            print('done') 
            
        if args.get('action') == 'save':
            save_schema_values(args['schema'],
                    filename=args.get('filename',''),
                    folder=args.get('folder',''))
        else:
            try:
                args.pop('action')
                args = dict((k,v) for k,v in args.items() 
                            if k and v not in (False,[]))
                print(args)
                r = check_db_schema(**args)
            except:
                print(fn.except2str())

    except SystemExit:
        pass
    except:
        print fn.except2str()
示例#2
0
    def add_periodic_attributes(self, attributes, periods, wait=3.):
        """
        attributes must be a list, periods a number, list or dict
        """
        attributes = sorted(
            parse_tango_model(a, fqdn=True).fullname.lower()
            for a in attributes)
        if fn.isNumber(periods):
            periods = dict((a, periods) for a in attributes)
        elif fn.isSequence(periods):
            periods = dict(zip(attributes, periods))

        devs = fn.defaultdict(list)
        [devs[fn.tango.get_dev_name(a)].append(a) for a in attributes]
        done = []

        for dev, attrs in devs.items():
            archiver = self.get_next_periodic_archiver(attrexp=dev + '/*')
            for attribute in attrs:
                try:
                    period = periods[attribute]
                    self.info('add_periodic_attribute(%s,%s,%s)' %
                              (attribute, period, archiver))
                    self.add_periodic_attribute(attribute,
                                                period=period,
                                                archiver=archiver,
                                                wait=wait)
                    done.append((attribute, period, archiver))
                except:
                    self.warning(fn.except2str())

        return done
示例#3
0
 def Test(self, argin):
     # PROTECTED REGION ID(SchemaManager.Test) ENABLED START #
     try:
         v = str(eval(argin, {'self': self}))
     except:
         v = fn.except2str()
     return
示例#4
0
    def trigger_callbacks(self, regs=None):
        """
        regs = list of addresses that changed
        """
        if not self.callbacks:
            return
        for key, cb in self.callbacks.items():
            try:
                rs, push, org = regs, False, cb
                if fn.isSequence(cb):
                    rs = regs and [r for r in regs if r in cb]
                    if not regs or rs or (len(cb) == 1
                                          and fn.isCallable(cb[0])):
                        cb = cb[-1]
                    else:
                        continue
                msg = ('%s: %s.trigger_callbacks(%s,%s): %s:%s' %
                       (fn.time2str(), self.name, fn.shortstr(
                           regs, 40), rs, key, org))
                if self.plc_obj is not None:
                    self.plc_obj.debug(msg)
                else:
                    print(msg)
                if fn.isCallable(cb):
                    cb(key)  #,push=push)
                else:
                    cb = getattr(cb, 'push_event',
                                 getattr(cb, 'event_received', None))
                    cb and cb(key)

                fn.wait(1.e-4)

            except Exception as e:
                print(fn.except2str())
                print('%s.callback(%s,%s): %s' % (self, key, cb, e))
示例#5
0
    def trigger_callbacks(self, regs=None):
        """
        regs = list of addresses that changed
        """
        if not self.callbacks:
            return
        for key, cb in self.callbacks.items():
            try:
                push = False
                if fun.isSequence(cb):
                    if not regs or any(r in cb for r in regs):
                        #print('%s: %s.trigger(%s): callback(%s):%s' %
                        #(fun.time2str(),self,key,cb,regs))
                        cb, push = cb[-1], True
                    else:
                        continue
                if fun.isCallable(cb):
                    cb(key, push=push)
                else:
                    cb = getattr(cb, 'push_event',
                                 getattr(cb, 'event_received', None))
                    cb and cb(key)

                fandango.wait(1.e-4)

            except Exception as e:
                print(fandango.except2str())
                print('%s.callback(%s): %s' % (self, cb, e))
 def Test(self, argin):
     # PROTECTED REGION ID(SchemaManager.Test) ENABLED START #
     try:
         v = str(eval(argin,{'self':self}))
     except:
         v = fn.except2str()
     return 
示例#7
0
 def get_periodic_attributes(self):
     self.periodic_attributes = {}
     for v in self.get_periodic_archivers_attributes().values():
         for k, p in v.items():
             try:
                 p = [s.lower() for s in p.split(';') if 'period' in s][0]
                 self.periodic_attributes[k.lower()] = int(p.split('=')[-1])
             except:
                 print(fn.except2str())
     return self.periodic_attributes
def get_table_partitions(api, table, description=''):
    """
    DUPLICATED BY pta.dbs.get_partitions_from_query !!!
    """
    if fn.isString(api): api = pta.api(api)
    if not description: description = get_table_description(api, table)
    rows = [l for l in description.split('\n') if 'partition' in l.lower()]
    f = rows[0].split()[-1]
    data = (f, [])
    for i, l in enumerate(rows[1:]):
        try:
            l, n = l.split(), i and rows[i].split() or [0] * 6
            data[-1].append((l[1], n[5], l[5]))
        except:
            print(fn.except2str())
            print(i, l, n)
    return (data)
def get_table_partitions(api,table,description=''):
    """
    DUPLICATED BY pta.dbs.get_partitions_from_query !!!
    """
    if fn.isString(api): api = pta.api(api)
    if not description: description = get_table_description(api,table)
    rows = [l for l in description.split('\n') if 'partition' in l.lower()]
    f = rows[0].split()[-1]
    data = (f,[])
    for i,l in enumerate(rows[1:]):
        try:
            l,n = l.split(),i and rows[i].split() or [0]*6
            data[-1].append((l[1],n[5],l[5]))
        except:
            print(fn.except2str())
            print(i,l,n)
    return(data)
    def UpdateAttributes(self):
        # PROTECTED REGION ID(SchemaManager.UpdateAttributes) ENABLED START #
        try:
            self.info_stream('UpdateAttributes()')
            self.attributes = sorted(self.api.keys())
            self.attr_on = sorted(self.api.get_archived_attributes())
            self.attr_off = [a for a in self.attributes 
                             if a not in self.attr_on]
            self.info_stream('pushing_events')
            for a in ['AttributeList','AttributeOnList','AttributeOffList']:
                self.push_change_event(a,getattr(self,'read_%s'%a)())

            if self.Threaded:
                [self.threadDict.append(a,value=[]) for a in self.attr_on]
                
            self.state_machine()
            self.info_stream(self.get_status())
        except:
            self.error_stream(fn.except2str())
示例#11
0
    def UpdateAttributes(self):
        # PROTECTED REGION ID(SchemaManager.UpdateAttributes) ENABLED START #
        try:
            self.info_stream('UpdateAttributes()')
            self.attributes = sorted(self.api.keys())
            self.attr_on = sorted(self.api.get_archived_attributes())
            self.attr_off = [
                a for a in self.attributes if a not in self.attr_on
            ]
            self.info_stream('pushing_events')
            for a in ['AttributeList', 'AttributeOnList', 'AttributeOffList']:
                self.push_change_event(a, getattr(self, 'read_%s' % a)())

            if self.Threaded:
                [self.threadDict.append(a, value=[]) for a in self.attr_on]

            self.state_machine()
            self.info_stream(self.get_status())
        except:
            self.error_stream(fn.except2str())
示例#12
0
def delete_tdb_table_data(table, date):
    tdb = pta.api('tdb')
    tdb.db.setLogLevel('debug')
    if table in tdb:
        table = tdb[table].table
    try:
        v = tdb.db.Query('select time from %s order by time limit 1' % table)
        print('first value at %s' % str(v))
    except:
        tdb.db.Query('repair table ' + table)
        v = tdb.db.Query('select time from %s order by time limit 1' % table)
        print('first value at %s' % str(v))
    try:
        tdb.db.Query("delete from %s where time < '%s'" % (table, date))
        tdb.db.Query('optimize table ' + table)
        tdb.db.Query('repair table ' + table)
        v = tdb.db.Query('select time from %s order by time limit 1' % table)
        print(v)
        return True
    except:
        print(fn.except2str())
        print(table, 'failed')
        return False
示例#13
0
def delete_tdb_table_data(table, date):
    tdb = pta.api('tdb')
    tdb.db.setLogLevel('debug')
    if table in tdb:
        table = tdb[table].table
    try:
        v = tdb.db.Query('select time from %s order by time limit 1' % table)
        print('first value at %s' % str(v))
    except:
        tdb.db.Query('repair table '+table)
        v = tdb.db.Query('select time from %s order by time limit 1' % table)
        print('first value at %s' % str(v))        
    try:
        tdb.db.Query("delete from %s where time < '%s'" % (table, date))
        tdb.db.Query('optimize table '+table)
        tdb.db.Query('repair table '+table)
        v = tdb.db.Query('select time from %s order by time limit 1' % table)        
        print(v)
        return True
    except:
        print(fn.except2str())
        print(table,'failed')
        return False
示例#14
0
    def eventReceived(self, evt_src, evt_type, evt_value):
        try:
            debug = 'debug' in str(evt_src).lower()
            now = fandango.time2str()
            evtype = str(TaurusEventType.reverseLookup[evt_type])
            evvalue = getattr(evt_value, 'value', None)
            if debug:
                print '\n'
                #trace('%s: In AlarmRow(%s).eventReceived(%s,%s,%s)'%(fandango.time2str(),self.alarm.tag,evt_src,evtype,evvalue),clean=True)
            disabled, acknowledged, quality, value = self.alarmDisabled, self.alarmAcknowledged, self.quality, bool(
                self.alarm.active)
            if self.qtparent and getattr(self.qtparent, 'api', None):
                self.alarm = self.qtparent.api[
                    self.tag]  #Using common api object

            #Ignoring Config Events
            if evt_type == TaurusEventType.Config:
                if debug:
                    trace('%s: AlarmRow(%s).eventReceived(CONFIG): %s' %
                          (now, self.alarm.tag, str(evt_value)[:20]),
                          clean=True)
                return
            #Filtering Error Events
            elif evt_type == TaurusEventType.Error or not hasattr(
                    evt_value, 'value'):
                error = True
                self.errors += 1
                if self.errors >= self.MAX_ERRORS:
                    self.alarm.active, self.quality = None, PyTango.AttrQuality.ATTR_INVALID
                if not self.errors % self.MAX_ERRORS:
                    if 'EventConsumer' not in str(evt_value):
                        trace('%s: AlarmRow(%s).eventReceived(ERROR): %s' % (
                            now, self.alarm.tag, 'ERRORS=%s!:\n\t%s' %
                            (self.errors, fandango.except2str(evt_value, 80))),
                              clean=True)
                    #if self.value is None: taurus.Attribute(self.model).changePollingPeriod(5*REFRESH_TIME)
                    if not self.changed and self.errors == self.MAX_ERRORS or 'Exception' not in self.status:
                        print '%s : %s.emitValueChanged(ERROR!)' % (
                            now, self.alarm.tag)
                        print 'ERROR: %s(%s)' % (type(evt_value),
                                                 clean_str(evt_value))
                        self.qtparent.emitValueChanged()
                        self.changed = True  #This flag is set here, and set to False after updating row style
                        self.updateStyle(
                            event=True, error=fandango.except2str(evt_value)
                        )  #It seems necessary to update the row text, color and icon
                else:
                    if debug:
                        trace('In AlarmRow(%s).eventReceived(%s,%s,%d/%d)' %
                              (self.alarm.tag, evt_src, evtype, self.errors,
                               self.MAX_ERRORS),
                              clean=True)
                    pass
            #Change Events
            elif evt_type == TaurusEventType.Change or evt_type == TaurusEventType.Periodic:
                self.errors = 0

                # Refresh period not changed as these lines slows down a lot!!
                #ta = taurus.Attribute(self.model)
                #if self.value is None: ta.changePollingPeriod(5*REFRESH_TIME)
                #elif ta.getPollingPeriod()!=REFRESH_TIME: ta.changePollingPeriod(REFRESH_TIME)

                disabled = self.get_disabled()
                acknowledged = self.get_acknowledged()
                if str(self.model).endswith('/ActiveAlarms'):
                    value, quality = any(
                        s.startswith(self.alarm.tag + ':') for s in (
                            evt_value.value or [])), self.alarm.get_quality()
                else:
                    value, quality = evt_value.value, evt_value.quality

                if debug:
                    trace('In AlarmRow(%s).eventReceived(%s,%s,%s)' %
                          (self.alarm.tag, evt_src,
                           str(TaurusEventType.reverseLookup[evt_type]),
                           evvalue),
                          clean=True)
                if debug:
                    trace('\t%s (%s), dis:%s, ack:%s' %
                          (value, quality, disabled, acknowledged))

                if value != bool(
                        self.alarm.active
                ) or quality != self.quality or disabled != self.alarmDisabled or acknowledged != self.alarmAcknowledged:
                    if not self.changed:
                        #print '%s : %s.emitValueChanged(%s)'%(fandango.time2str(),self.alarm.tag,value)
                        self.qtparent.emitValueChanged()
                    self.changed = True  #This flag is set here, and set to False after updating row style

                self.alarmDisabled = disabled
                self.alarmAcknowledged = acknowledged
                self.quality = quality
                self.alarm.active = getAlarmTimestamp(
                    self.alarm) if value else 0

                self.updateStyle(event=True, error=False)
            else:
                print '\tUnknown event type?!? %s' % evt_type
        except:
            try:
                print 'Exception in eventReceived(%s,...): \n%s' % (
                    evt_src, fandango.log.except2str())
            except:
                print 'eventReceived(...)!' * 80 + '\n' + traceback.format_exc(
                )
        if debug: print '\n'
    def UpdateValues(self):
        # PROTECTED REGION ID(SchemaManager.UpdateValues) ENABLED START #
        try:

            t0 = t1 = fn.now()
            self.info_stream('UpdateValues()')
            
            if (self.ValuesFile or '').strip():
                self.info_stream('Load values from: %s ...' % self.ValuesFile)
                if self.ValuesFile.endswith('json'):
                    self.values = fn.json2dict(self.ValuesFile)
                else:
                    with open(self.ValuesFile) as f:
                        self.values = pickle.load(f)
                        
                self.values = dict((a,self.get_last_value(a,v)) 
                                   for a,v in self.values.items())
                t1 = max(v[0] for v in self.values.values() if v)
                t1 = min((t1,fn.now()))
                self.info_stream('reference time is %s' % fn.time2str(t1))

            elif self.Threaded:
                self.info_stream('Loading values from thread cache ...')
                self.values = dict((a,v) for a,v in self.threadDict.items()
                    if self.threadDict._updates.get(a,0))
            else:
                self.info_stream('Loading values from db ...')
                self.values = self.api.load_last_values(self.attr_on)

            self.info_stream('Updating %d values: %s' % (
                len(self.values),str(len(self.values) 
                                     and self.values.items()[0])))
            self.attr_ok = []
            self.attr_nok = []
            self.attr_lost = []
            self.attr_err = []
            for a,v in sorted(self.values.items()):
                try:
                    a = fn.tango.get_full_name(a)
                    if self.Threaded:
                        t1 = self.threadDict._updates.get(a,0)
                    self.check_attribute_ok(a,v,t=t1)
                except Exception as e:
                    self.attr_err.append(a)
                    traceback.print_exc()
                    m = str("%s: %s: %s" % (a, str(v), str(e)))
                    #self.error_stream(m)
                    print('*'*80)
                    print(fn.time2str()+' '+self.get_name()+'.ERROR!:'+m)
                    fn.wait(1e-6)
                    
            for a in ['AttributeValues','AttributeOkList','AttributeNokList',
                    'AttributeWrongList','AttributeLostList',
                    'AttributeNoevList','AttributeStalledList']:
                self.push_change_event(a,getattr(self,'read_%s'%a)())
                
            self.update_time = fn.now()
            self.state_machine()
            self.info_stream(self.get_status())
            self.info_stream('UpdateValues() took %f seconds' % (fn.now()-t0))
            
        except Exception as e:
            traceback.print_exc()
            self.error_stream(fn.except2str())
            raise e            
示例#16
0
def check_db_schema(schema, attributes = None, values = None,
                    tref = -12*3600, n = 1, filters = '*', export = 'json',
                    restart = False, subscribe = False):
    """
    tref is the time that is considered updated (e.g. now()-86400)
    n is used to consider multiple values
    
    attrs: all attributes in db
    on: archived
    off: in db but not currently archived
    ok: updated   
    
    known error causes (attrs not lost but not updated):
    
    nok: attributes are not currently readable
    noevs: attributes not sending events
    novals: attributes never recorded a value
    stall: not updated, but current value matches archiving
    lost: not updated, and values doesn't match with current
    """
    
    t0 = fn.now()
    if hasattr(schema,'schema'):
        api,schema = schema,api.schema
    else:
        api = pta.api(schema)

    r = fn.Struct(api=api,schema=schema)    
    if isString(tref): 
        tref = fn.str2time(tref)
    r.tref = fn.now()+tref if tref < 0 else tref
    r.attrs = [a for a in (attributes or api.get_attributes())
                if fn.clmatch(filters,a)]
    print('check_db_schema(%s,attrs[%s],tref="%s",export as %s)' 
          % (schema,len(r.attrs),fn.time2str(r.tref),export))
    
    if restart and schema!='hdbpc':
        archs = [a for a in api.get_archivers() if not fn.check_device(a)]
        if archs:
            try:
                print('Restarting archivers: %s' % str(archs))
                astor = fn.Astor(archs)
                astor.stop_servers()
                astor.start_servers()
            except:
                traceback.print_exc()
        
        stopped = api.get_stopped_attributes()
        print('Restarting %d stopped attributes' % len(stopped))
        api.restart_attributes(stopped)
    
    r.on = [a for a in api.get_archived_attributes() if a in r.attrs]
    r.off = [a for a in r.attrs if a not in r.on]
    
    r.archs = fn.defaultdict(list)
    r.pers = fn.defaultdict(list)
    r.values = load_schema_values(api,r.on,values,n,tref=tref)
    
    if schema in ('tdb','hdb'):
        [r.archs[api[k].archiver].append(k) for k in r.on]
    else:
        r.rvals = r.values
        r.freq, r.values = {}, {}
        for k,v in r.rvals.items():
            try:
                if n > 1:
                    v = v[0] if isSequence(v) and len(v) else v
                    r.values[k] = v[0] if isSequence(v) and len(v) else v
                    r.freq[k] = v and float(len(v))/abs(v[0][0]-v[-1][0])
                else:
                    r.values[k] = v
            except Exception as e:
                print(k,v)
                print(fn.except2str())
                
        for k in api.get_archivers():
            r.archs[k] = api.get_archiver_attributes(k)
        for k in api.get_periodic_archivers():
            r.pers[k] = api.get_periodic_archivers_attributes(k)

    # Get all updated attributes
    r.ok = [a for a,v in r.values.items() if v and v[0] > r.tref]
    # Try to read not-updated attributes
    r.check = dict((a,fn.check_attribute(a)
                    ) for a in r.on if a not in r.ok)
    #r.novals = [a for a,v in r.values.items() if not v]
    r.nok, r.stall, r.noevs, r.lost, r.novals, r.evs, r.rem = [],[],[],[],[],{},[]
    # Method to compare numpy values
    
    for a,v in r.check.items():
        state = check_archived_attribute(a, v, default=CheckState.LOST, 
            cache=r, tref=r.tref, 
            check_events = subscribe and not api.is_periodic_archived(a))
        {
            #CheckState.ON : r.on,
            #CheckState.OFF : r.off,
            CheckState.OK : r.ok, #Shouldn't be any ok in check list               
            CheckState.NO_READ : r.nok,
            CheckState.STALL : r.stall,
            CheckState.NO_EVENTS : r.noevs,
            CheckState.LOST : r.lost,
            CheckState.UNK : r.novals,
         }[state].append(a)
                
    # SUMMARY
    r.summary = schema +'\n'
    r.summary += ','.join(
        """on: archived
        off: not archived
        ok: updated   
        nok: not readable
        noevs: no events
        novals: no values
        stall: not changing
        lost: not updated
        """.split('\n'))+'\n'
    
    getline = lambda k,v,l: '\t%s:\t:%d\t(%s)' % (k,len(v),l)
    
    r.summary += '\n\t%s:\t:%d\tok+stall: %2.1f %%' % (
        'attrs',len(r.attrs),
        (100.*(len(r.ok)+len(r.stall))/(len(r.on) or 1e12)))
    r.summary += '\n\t%s/%s:\t:%d/%d' % (
        'on','off',len(r.on),len(r.off))
    #if r.off > 20: r.summary+=' !!!'
    r.summary += '\n\t%s/%s:\t:%d/%d' % (
        'ok','nok',len(r.ok),len(r.nok))
    if len(r.nok) > 10: 
        r.summary+=' !!!'
    r.summary += '\n\t%s/%s:\t:%d/%d' % (
        'noevs','novals',len(r.noevs),len(r.novals))
    if len(r.novals) > 1: 
        r.summary+=' !!!'
    r.summary += '\n\t%s/%s:\t:%d/%d' % (
        'lost','stall',len(r.lost),len(r.stall))
    if len(r.lost) > 1: 
        r.summary+=' !!!'
    r.summary += '\n'
        
    r.archivers = dict.fromkeys(api.get_archivers())
    for d in sorted(r.archivers):
        r.archivers[d] = api.get_archiver_attributes(d)
        novals = [a for a in r.archivers[d] if a in r.novals]   
        lost = [a for a in r.archivers[d] if a in r.lost]
        if (len(novals)+len(lost)) > 2:
            r.summary += ('\n%s (all/novals/lost): %s/%s/%s' 
                % (d,len(r.archivers[d]),len(novals),len(lost)))
            
    if hasattr(api,'get_periodic_archivers'):
        r.periodics = dict.fromkeys(api.get_periodic_archivers())
        for d in sorted(r.periodics):
            r.periodics[d] = api.get_periodic_archiver_attributes(d)
            novals = [a for a in r.periodics[d] if a in r.novals]
            lost = [a for a in r.periodics[d] if a in r.lost]
            if len(novals)+len(lost) > 2:
                r.summary += ('\n%s (all/novals/lost): %s/%s/%s' % 
                    (d,len(r.periodics[d]),len(novals),len(lost)))
        
        r.perattrs = [a for a in r.on if a in api.get_periodic_attributes()]
        r.notper = [a for a in r.on if a not in r.perattrs]
        
        
    r.summary += '\nfinished in %d seconds\n\n'%(fn.now()-t0)
    print(r.summary)
    
    if restart:
        try:
            retries = r.lost+r.novals+r.nok
            print('restarting %d attributes' % len(retries))
            api.restart_attributes(retries)
        except:
            traceback.print_exc()
    
    if export is not None:
        if export is True:
            export = 'txt'
        for x in (export.split(',') if isString(export) else export):
            if x in ('json','pck','pickle','txt'):
                x = '/tmp/%s.%s' % (schema,x)
            print('Saving %s file with keys:\n%s' % (x,r.keys()))
            if 'json' in x:
                fn.dict2json(r.dict(),x)
            else:
                f = open(x,'w')
                if 'pck' in x or 'pickle' in x:
                    pickle.dump(r.dict(),f)
                else:
                    f.write(fn.dict2str(r.dict()))
                f.close()     
                
    for k,v in r.items():
        if fn.isSequence(v):
            r[k] = sorted(v)
                
    return r
示例#17
0
    def UpdateValues(self):
        # PROTECTED REGION ID(SchemaManager.UpdateValues) ENABLED START #
        try:

            t0 = t1 = fn.now()
            self.info_stream('UpdateValues()')

            if (self.ValuesFile or '').strip():
                self.info_stream('Load values from: %s ...' % self.ValuesFile)
                if self.ValuesFile.endswith('json'):
                    self.values = fn.json2dict(self.ValuesFile)
                else:
                    with open(self.ValuesFile) as f:
                        self.values = pickle.load(f)

                self.values = dict((a, self.get_last_value(a, v))
                                   for a, v in self.values.items())
                t1 = max(v[0] for v in self.values.values() if v)
                t1 = min((t1, fn.now()))
                self.info_stream('reference time is %s' % fn.time2str(t1))

            elif self.Threaded:
                self.info_stream('Loading values from thread cache ...')
                self.values = dict((a, v) for a, v in self.threadDict.items()
                                   if self.threadDict._updates.get(a, 0))
            else:
                self.info_stream('Loading values from db ...')
                self.values = self.api.load_last_values(self.attr_on)

            self.info_stream(
                'Updating %d values: %s' %
                (len(self.values),
                 str(len(self.values) and self.values.items()[0])))
            self.attr_ok = []
            self.attr_nok = []
            self.attr_lost = []
            self.attr_err = []
            for a, v in sorted(self.values.items()):
                try:
                    a = fn.tango.get_full_name(a)
                    if self.Threaded:
                        t1 = self.threadDict._updates.get(a, 0)
                    self.check_attribute_ok(a, v, t=t1)
                except Exception as e:
                    self.attr_err.append(a)
                    traceback.print_exc()
                    m = str("%s: %s: %s" % (a, str(v), str(e)))
                    #self.error_stream(m)
                    print('*' * 80)
                    print(fn.time2str() + ' ' + self.get_name() + '.ERROR!:' +
                          m)
                    fn.wait(1e-6)

            for a in [
                    'AttributeValues', 'AttributeOkList', 'AttributeNokList',
                    'AttributeWrongList', 'AttributeLostList',
                    'AttributeNoevList', 'AttributeStalledList'
            ]:
                self.push_change_event(a, getattr(self, 'read_%s' % a)())

            self.update_time = fn.now()
            self.state_machine()
            self.info_stream(self.get_status())
            self.info_stream('UpdateValues() took %f seconds' %
                             (fn.now() - t0))

        except Exception as e:
            traceback.print_exc()
            self.error_stream(fn.except2str())
            raise e
示例#18
0
            s = '%s status is %s, updated at %s' % (
                self.schema, ns, fn.time2str(self.update_time))
            for a in ('ArchiverOnList', 'ArchiverOffList', 'AttributeList',
                      'AttributeOnList', 'AttributeOffList', 'AttributeOkList',
                      'AttributeNokList', 'AttributeLostList',
                      'AttributeWrongList', 'AttributeNoevList',
                      'AttributeStalledList'):
                try:
                    v = str(len(getattr(self, 'read_%s' % a)()))
                except Exception, e:
                    v = str(e)
                s += '\n%s = %s' % (a, v)
            self.set_status(s)
            self.push_change_event('Status')
        except:
            self.error_stream(fn.except2str())

    def check_attribute_ok(self, a, v, t=0):
        """
        arguments are attribute name and last value from db, plus ref. time
        """
        r = check_attribute_value(a)
        rv = getattr(r, 'value', None)
        if isinstance(rv, (type(None), Exception)):
            # Attribute not readable
            self.attr_nok.append(a)
        elif self.is_hpp and not check_attribute_events(a):
            self.attr_nevs.append(a)
        else:
            if v is None or fn.isSequence(v) and not len(v):
                # Attribute has no values in DB
示例#19
0
文件: row.py 项目: sergirubio/panic
 def eventReceived(self,evt_src,evt_type,evt_value):
     try:
         debug = 'debug' in str(evt_src).lower()
         now = fandango.time2str()
         evtype = str(TaurusEventType.reverseLookup[evt_type])
         evvalue = getattr(evt_value,'value',None)
         if debug: 
             print '\n'
             #trace('%s: In AlarmRow(%s).eventReceived(%s,%s,%s)'%(fandango.time2str(),self.alarm.tag,evt_src,evtype,evvalue),clean=True)
         disabled,acknowledged,quality,value = self.alarmDisabled,self.alarmAcknowledged,self.quality,bool(self.alarm.active)
         if self.qtparent and getattr(self.qtparent,'api',None): self.alarm = self.qtparent.api[self.tag] #Using common api object
         
         #Ignoring Config Events
         if evt_type==TaurusEventType.Config:
             if debug: trace('%s: AlarmRow(%s).eventReceived(CONFIG): %s' % (now,self.alarm.tag,str(evt_value)[:20]),clean=True)
             return
         #Filtering Error Events
         elif evt_type==TaurusEventType.Error or not hasattr(evt_value,'value'):
             error = True
             self.errors+=1
             if self.errors>=self.MAX_ERRORS: 
                 self.alarm.active,self.quality = None,PyTango.AttrQuality.ATTR_INVALID
             if not self.errors%self.MAX_ERRORS:
                 if 'EventConsumer' not in str(evt_value): 
                     trace('%s: AlarmRow(%s).eventReceived(ERROR): %s' %(now,self.alarm.tag,'ERRORS=%s!:\n\t%s'%(self.errors,fandango.except2str(evt_value,80))),clean=True)
                 #if self.value is None: taurus.Attribute(self.model).changePollingPeriod(5*REFRESH_TIME)
                 if not self.changed and self.errors==self.MAX_ERRORS or 'Exception' not in self.status: 
                     print '%s : %s.emitValueChanged(ERROR!)'%(now,self.alarm.tag)
                     print 'ERROR: %s(%s)' % (type(evt_value),clean_str(evt_value))
                     self.qtparent.emitValueChanged()
                     self.changed = True #This flag is set here, and set to False after updating row style
                     self.updateStyle(event=True,error=fandango.except2str(evt_value)) #It seems necessary to update the row text, color and icon
             else: 
                 if debug: trace('In AlarmRow(%s).eventReceived(%s,%s,%d/%d)' % (self.alarm.tag,evt_src,evtype,self.errors,self.MAX_ERRORS),clean=True)
                 pass
         #Change Events
         elif evt_type==TaurusEventType.Change or evt_type==TaurusEventType.Periodic:
             self.errors = 0
             
             # Refresh period not changed as these lines slows down a lot!!
             #ta = taurus.Attribute(self.model)
             #if self.value is None: ta.changePollingPeriod(5*REFRESH_TIME)
             #elif ta.getPollingPeriod()!=REFRESH_TIME: ta.changePollingPeriod(REFRESH_TIME)
             
             disabled = self.get_disabled()
             acknowledged = self.get_acknowledged()
             if str(self.model).endswith('/ActiveAlarms'):
                 value,quality = any(s.startswith(self.alarm.tag+':') for s in (evt_value.value or [])),self.alarm.get_quality()
             else:
                 value,quality = evt_value.value,evt_value.quality
             
             if debug: trace('In AlarmRow(%s).eventReceived(%s,%s,%s)' % (self.alarm.tag,evt_src,str(TaurusEventType.reverseLookup[evt_type]),evvalue),clean=True)
             if debug: trace('\t%s (%s), dis:%s, ack:%s'%(value,quality,disabled,acknowledged))
             
             if  value!=bool(self.alarm.active) or quality!=self.quality or disabled!=self.alarmDisabled or acknowledged!=self.alarmAcknowledged:
                 if not self.changed: 
                     #print '%s : %s.emitValueChanged(%s)'%(fandango.time2str(),self.alarm.tag,value)
                     self.qtparent.emitValueChanged()
                 self.changed = True #This flag is set here, and set to False after updating row style
             
             self.alarmDisabled = disabled
             self.alarmAcknowledged = acknowledged
             self.quality = quality
             self.alarm.active = getAlarmTimestamp(self.alarm) if value else 0
             
             self.updateStyle(event=True,error=False)
         else: 
             print '\tUnknown event type?!? %s' % evt_type
     except:
         try: print 'Exception in eventReceived(%s,...): \n%s' %(evt_src,fandango.log.except2str())
         except : print 'eventReceived(...)!'*80+'\n'+traceback.format_exc()
示例#20
0
if __name__ == '__main__':

    args = {}
    try:
        assert sys.argv[2:]
        args = fn.sysargs_to_dict(defaults=('schema', 'period', 'ti', 'values',
                                            'action'))
        print(args)

        if args.get('action') == 'start':
            print('Call Start() for %s devices' % sys.argv[1])
            pta.api(args['schema']).start_devices(force=True)
            print('done')
        if args.get('action') == 'restart':
            print('Restart %s servers' % sys.argv[1])
            pta.api(args['schema']).start_servers(restart=True)
            print('done')
        if args.get('action') == 'save':
            save_schema_values(args['schema'])
        else:
            try:
                args = dict((k, v) for k, v in args.items()
                            if k and v not in (False, []))
                r = check_archiving_schema(**args)
            except:
                print(fn.except2str())

    except:
        print fn.except2str()
        print(USAGE)
             
         s = '%s status is %s, updated at %s' % (
             self.schema, ns, fn.time2str(self.update_time))
         for a in ('ArchiverOnList','ArchiverOffList','AttributeList',
                 'AttributeOnList','AttributeOffList','AttributeOkList',
                 'AttributeNokList','AttributeLostList','AttributeWrongList',
                 'AttributeNoevList','AttributeStalledList'):
             try:
                 v = str(len(getattr(self,'read_%s'%a)()))
             except Exception,e:
                 v = str(e)
             s+='\n%s = %s' % (a,v)
         self.set_status(s)        
         self.push_change_event('Status')
     except:
         self.error_stream(fn.except2str())
 
 def check_attribute_ok(self,a,v,t=0):
     """
     arguments are attribute name and last value from db, plus ref. time
     """
     r = check_attribute_value(a)
     rv = getattr(r,'value',None)
     if isinstance(rv,(type(None),Exception)):
         # Attribute not readable
         self.attr_nok.append(a)
     elif self.is_hpp and not check_attribute_events(a):
         self.attr_nevs.append(a)
     else:
         if v is None or fn.isSequence(v) and not len(v):
             # Attribute has no values in DB