def GetCurrentQueries(self):
     print("In "+self.get_name()+"::GetCurrentQueries()")
     
     #self.get_device_properties()
     #if not self.is_command_polled('state'):
     #self.poll_command('state',3000)
     try:
       pending = []
       for s in self.PeriodicQueries:
         s = s.split(',')
         a,t = s[0],max((float(s[-1]),self.ExpireTime))
         if a not in self.AttrData or self.AttrData[a][0]<(fn.now()-t):
           if a in self.AttrData: 
             print('%s data is %s seconds old'%(a,fn.now()-self.AttrData[a][0]))
           pending.append(s[:3])
           
       if pending: 
         self.set_state(PyTango.DevState.RUNNING)
         print('Executing %d scheduled queries:\n%s'%(len(pending),'\n'.join(map(str,pending))))
         for p in pending:
           self.GetAttDataBetweenDates(p)
       else: 
         self.set_state(PyTango.DevState.ON)
         
     except:
       self.set_state(PyTango.DevState.FAULT)
       self.set_status(traceback.format_exc())
       print(self.get_status())
       
     return self.PeriodicQueries
Example #2
0
    def reader_hook(self, attribute, values):
        """This method will be executed by the ReaderProcess to process the queried data."""
        try:
            print('>' * 80)
            print(time.ctime() + ' In reader_hook(%s,[%d])' %
                  (attribute, len(values)))

            MAXDIM = 1024 * 1024 * 1024
            #First create the attributes
            epoch, data, aname = [], [], attribute.replace('/', '__')
            values = decimate_values(values)
            [(epoch.append(v[0]), data.append(v[1])) for v in values]
            writable = PyTango.AttrWriteType.READ

            #Adding time attribute
            m, atformat, dims = None, PyTango.SpectrumAttr, [MAXDIM]
            for d in data:
                if d is not None:
                    if fn.isSequence(d):
                        atformat, dims = PyTango.ImageAttr, [MAXDIM, MAXDIM]
                        m = d[0]
                    else:
                        m = d
                    break

            attype = PyTango.DevDouble if (
                fn.isNumber(m) or fn.isBool(m)) else PyTango.DevString
            self.add_attribute(
                PyTango.ImageAttr(aname, attype, writable, MAXDIM, MAXDIM),
                self.read_dyn_attr, None, self.is_dyn_attr_allowed)

            self.add_attribute(
                PyTango.SpectrumAttr(aname + '_t', PyTango.DevDouble, writable,
                                     MAXDIM), self.read_dyn_attr, None,
                self.is_dyn_attr_allowed)

            self.add_attribute(
                PyTango.SpectrumAttr(aname + '_d', PyTango.DevString, writable,
                                     MAXDIM), self.read_dyn_attr, None,
                self.is_dyn_attr_allowed)

            self.add_attribute(atformat(aname + '_r', attype, writable,
                                        *dims), self.read_dyn_attr, None,
                               self.is_dyn_attr_allowed)

            #Then add the data to Cache values, so IsDataReady will return True
            t = fn.now()
            self.RemoveCachedAttribute(attribute)
            self.AttrData[attribute] = (t, atformat, attype, values)
            print('Done: %s,%s,%s,%s,%d' %
                  (attribute, t, atformat, attype, len(values)))
        except:
            print(traceback.format_exc())
Example #3
0
 def update(self,value,date,quality,t=None):
     self.value=value
     self.date=date
     self.quality=quality
     self.updated = t or fun.now()
     try: 
         if value is not None and not hasattr(value,'__len__') and not isinstance(value,Exception):
             if self.max_peak[0] is None or self.value>self.max_peak[0]:
                 self.max_peak = (value,date)
             if self.min_peak[0] is None or self.value<self.min_peak[0]:
                 self.min_peak = (value,date)
     except: pass
Example #4
0
    def read_dyn_attr(self, attr):

        try:
            #attr.set_value(1.0)
            aname, values = attr.get_name(), []
            attribute = self.tag2attr(aname)
            print time.ctime() + 'In read_dyn_attr(%s)' % aname
            print(self.counter)

            try:
                req, atformat, attype, data = self.AttrData[attribute]
            except Exception, e:
                print('Unable to read %s: key = %s ; cache = %s' %
                      (attr, attribute, self.AttrData.keys()))
                traceback.print_exc()
                raise e

            conv = self.bool2float if attype is PyTango.DevBoolean \
            else (float if attype is PyTango.DevDouble
                else str)

            if aname.endswith('_r'):
                if atformat is PyTango.SpectrumAttr:
                    values = [conv(v[1] or 0.) for v in data]
                else:
                    values = [map(conv, v[1]) for v in data]
                if values:
                    print time.ctime(
                    ) + 'In read_dyn_attr(%s): %s[%d]:%s...%s' % (
                        aname, type(
                            values[0]), len(values), values[0], values[-1])
                else:
                    print '\tno values'
                attr.set_value(values, len(values))

            elif aname.endswith('_l'):
                print('%s: %s' % (aname, data))
                if data[-1:]:
                    value = conv(data[-1][1])
                    date = float(data[-1][0] or 0.)
                    q = ft.AttrQuality.ATTR_VALID
                else:
                    value = None
                    date = fn.now()
                    q = ft.AttrQuality.ATTR_INVALID

                print(
                    time.ctime() + 'In read_dyn_attr(%s): (%s,%s,%s)' %
                    (aname, value, date, q))
                attr.set_value_date_quality((value or 0.), date, q)

            elif aname.endswith('_w'):
                if atformat is PyTango.SpectrumAttr:
                    values = [conv(v[2] or 0.) for v in data]
                else:
                    values = [map(conv, v[2]) for v in data]
                if values:
                    print time.ctime(
                    ) + 'In read_dyn_attr(%s): %s[%d]:%s...%s' % (
                        aname, type(
                            values[0]), len(values), values[0], values[-1])
                else:
                    print '\tno values'
                attr.set_value(values, len(values))

            elif aname.endswith('_t'):
                values = [float(v[0] or 0.) for v in data]
                if values:
                    print time.ctime(
                    ) + 'In read_dyn_attr(%s): %s[%d]:%s...%s' % (
                        aname, type(
                            values[0]), len(values), values[0], values[-1])
                else:
                    print '\tno values'
                attr.set_value(values, len(values))

            elif aname.endswith('_d'):
                values = [fn.time2str(float(v[0] or 0.)) for v in data]
                if values:
                    print time.ctime(
                    ) + 'In read_dyn_attr(%s): %s[%d]:%s...%s' % (
                        aname, type(
                            values[0]), len(values), values[0], values[-1])
                else:
                    print '\tno values'
                attr.set_value(values, len(values))

            elif aname.endswith('_ld'):
                lv = [fn.time2str(float(v[0] or 0.)) for v in data[-1:]]
                if lv:
                    print(
                        time.ctime() + 'In read_dyn_attr(%s): %s[%d]:%s...%s' %
                        (aname, type(lv[0]), len(lv), lv[0], lv[-1]))
                else:
                    print '\tno values'
                attr.set_value(lv[-1])

            else:
                if atformat == PyTango.SpectrumAttr:
                    if attype == PyTango.DevString:
                        values = [(fn.time2str(d[0]), str(d[1])) for d in data]
                    else:
                        values = [(d[0], conv(d[1])) for d in data]
                else:
                    if attype is PyTango.DevString:
                        values = [[fn.time2str(d[0])] + map(str, d[1])
                                  for d in data]
                    else:
                        values = [[d[0]] + map(conv, d[1]) for d in data]

                if values:
                    print time.ctime(
                    ) + 'In read_dyn_attr(%s): %s[%d]:%s...%s' % (
                        aname, type(
                            values[0]), len(values), values[0], values[-1])
                else:
                    print '\tno values'
                attr.set_value(values, len(values))

            print '\treturned %d values' % len(values)
 def RemoveCachedAttributes(self):
     print "In ", self.get_name(), "::RemoveCachedAttributes()"
     #    Add your own code here
     remove = [a for a,v in self.AttrData.items() if v[0]<fn.now()-self.ExpireTime]
     for a in self.AttrData.keys()[:]:
         self.RemoveCachedAttribute(a)
    def reader_hook(self,attribute,values):
        """This method will be executed by the ReaderProcess to process the queried data.""" 
        try:
            print('>'*80)
            print(time.ctime()+' In reader_hook(%s,[%d])'
                  %(attribute,len(values)))
            self.counter-=1
            print(self.counter)
            
            MAXDIM = 1024*1024*1024
            #First create the attributes
            epoch,data,aname = [],[],attribute.replace('/','__')
            values = decimate_values(values)
            [(epoch.append(v[0]),data.append(v[1])) for v in values]
            writable = PyTango.AttrWriteType.READ

            #Adding time attribute
            m,atformat,dims = None,PyTango.SpectrumAttr,[MAXDIM]
            for d in data:
              if d is not None:
                if fn.isSequence(d):
                  atformat,dims = PyTango.ImageAttr,[MAXDIM,MAXDIM]
                  m = d[0]
                else:
                  m = d
                break

            attype = PyTango.DevDouble if (fn.isNumber(m) or fn.isBool(m)) else PyTango.DevString
            self.add_attribute(
                PyTango.ImageAttr(aname,attype,writable,MAXDIM,MAXDIM),
                self.read_dyn_attr,None,self.is_dyn_attr_allowed)
            
            self.add_attribute(
                PyTango.SpectrumAttr(aname+'_t',PyTango.DevDouble, writable,MAXDIM),
                self.read_dyn_attr,None,self.is_dyn_attr_allowed)

            self.add_attribute(
                PyTango.SpectrumAttr(aname+'_d',PyTango.DevString, writable,MAXDIM),
                self.read_dyn_attr,None,self.is_dyn_attr_allowed)
            
            #ARRAY
            self.add_attribute(atformat(aname+'_r',attype, writable,*dims),
                               self.read_dyn_attr,None,self.is_dyn_attr_allowed)
            
            #LAST VALUE
            self.add_attribute(PyTango.Attr(aname+'_l',attype,PyTango.AttrWriteType.READ),
                               self.read_dyn_attr,None,self.is_dyn_attr_allowed)   
            
            #LAST DATE
            self.add_attribute(
                PyTango.Attr(aname+'_ld',PyTango.DevString,PyTango.AttrWriteType.READ),
                               self.read_dyn_attr,None,self.is_dyn_attr_allowed)              
            
            self.add_attribute(
                PyTango.Attr(aname+'_rg',PyTango.DevString,PyTango.AttrWriteType.READ),
                               self.read_dyn_attr,None,self.is_dyn_attr_allowed)                 
            
            #Then add the data to Cache values, so IsDataReady will return True
            t = fn.now()
            self.RemoveCachedAttribute(attribute)
            self.AttrData[attribute] = (t,atformat,attype,values)
            print('Done: %s,%s,%s,%s,%d'%(attribute,t,atformat,attype,len(values)))
        except:
            print(traceback.format_exc())