Exemplo n.º 1
0
 def rpm_callback(self, array_value):
     #if debug: print 'batch array callback entry: %s' % (self,)
     now = time.time()
     try:
         answer = {}
         if isinstance(array_value, Exception):  #if we enter with an exception, disperse it to all the array elements
             for id in self.ids:
                 answer[id] = Result(array_value, now)
             return answer
         if isinstance(array_value,Result):
             if isinstance(array_value.value,Exception):
                 for id in self.ids:
                     answer[id] = Result(array_value.value, now)
                 return answer
         for id in self.ids:
             nr = self.map[id]
             try:
                 if nr.is_array():                   
                     answer[id] = Result(array_value.value, array_value.timestamp) #should just be one of these
                 else:
                     answer[id] = Result(array_value.value[nr.index], array_value.timestamp)
             except Exception, e:
                 answer[id] = Result(e, now)
         if debug: print 'Batch Callback Time:%s %s' % (self.array_node.as_node_url(), time.time() - self.last_get_time,) 
         return answer
Exemplo n.º 2
0
 def get_result(self, skipCache=0, **keywords):
     result = Result()
     result.timestamp = time.time()
     _module_lock.acquire()
     try:
         if self.value is None:
             if not self.buffer is None: #only affects _Register classes
                 #convert the buffer bytes to our value
                 self.convert_buffer_to_value()
         result.value = self.value
         return result
     finally:
         _module_lock.release()
Exemplo n.º 3
0
 def get_result(self, skipCache=0, **keywords):
     result = Result()
     result.timestamp = time.time()
     _module_lock.acquire()
     try:
         if self.value is None:
             if not self.buffer is None:  #only affects _Register classes
                 #convert the buffer bytes to our value
                 self.convert_buffer_to_value()
         result.value = self.value
         return result
     finally:
         _module_lock.release()
Exemplo n.º 4
0
class BatchArray(Batch):
    def _init_props(self):
        nr = self.map[self.ids[0]]
        self.array_node = nr
        if hasattr(nr, 'is_array') and nr.is_array(): return
        if hasattr(nr, 'is_array_element_of') and nr.is_array_element_of():
            self.array_node = nr.is_array_element_of()
        #survive this even if array_node is bogus, catch it in the get

    def get(self, manager, **keywords):
        if debug: print 'Start of Batch:%s' % (self.array_node.as_node_url())
        now = time.time()
        self.last_get_time = now
        answer = {}
        if not self.array_node.is_array():
            #mark all ids bad and throw exception
            for id in self.ids:
                nr = self.map[id]
                nr.set_batch_manager(None) #turn off rpm for the offending property
            raise EBadBatch('bacnet', self.array_node.name, 'array node is NOT an array')

        if keywords.has_key('callback'):
            keywords['callback'].callback(self.rpm_callback)
            keywords['T_OUT'] = self.timeout
        try:            
            array_value = self.array_node.get_result(1, **keywords)
            if isinstance(array_value, Callback):
                return array_value #if we are in callback mode
        except Exception, e:
            if debug: msglog.exception()
            for id in self.ids:
                answer[id] = Result(e, now)
            return answer
        
        if isinstance(array_value,Result):
            if isinstance(array_value.value,Exception):
                for id in self.ids:
                    answer[id] = Result(array_value.value, now)
                return answer
      
        for id in self.ids:
            nr = self.map[id]
            try:
                if nr.is_array():
                    answer[id] = Result(array_value.value, array_value.timestamp) #should just be one of these
                else:
                    answer[id] = Result(array_value.value[nr.index], array_value.timestamp)
            except Exception, e:
                answer[id] = Result(e, now)
Exemplo n.º 5
0
 def get_rp(self, prop, timeout=3.0):
     if debug: print 'reading a property'
     now = time.time()
     result = Result()
     result.timestamp = now
     result.cached = 1
     try:
         if debug: print self.device, prop.property_tuple
         r = read_property_g3(self.device, prop.property_tuple, timeout)
         v = r.property_value
         self.v = v
         prop.decode(v)
     except BACnetError, e:
         prop._value = e
         result.timestamp = now - prop.ttl
Exemplo n.º 6
0
 def get_result(self, skipCache=0):
     cache = self.cache
     cache.lock.acquire()
     try:
         if skipCache or (cache.response is None) or \
            ((cache.timestamp + cache.ttl) < time()):
             cached = 0
             cache._refresh()
         else:
             cached = 1
         result = Result()
         result.timestamp = cache.timestamp
         result.cached = cached
         result.value = apply(
             self.read,
             [cache.response, self.offset, cache.start, self.orders])
     finally:
         cache.lock.release()
     return result
Exemplo n.º 7
0
 def get_result(self, skipCache=0):
     cache = self.cache
     cache.lock.acquire()
     try:
         if skipCache or (cache.response is None) or \
            ((cache.timestamp + cache.ttl) < time()):
             cached = 0
             cache._refresh()
         else:
             cached = 1
         result = Result()
         result.timestamp = cache.timestamp
         result.cached = cached
         result.value = apply(self.read, [cache.response,
                                          self.offset,
                                          cache.start,
                                          self.orders])
     finally:
         cache.lock.release()
     return result
Exemplo n.º 8
0
    def update_cache(self, prop):
        if debug: print 'update cache: ' #, prop, prop.ttl
        #age_group = self.find_age_group_for(prop)
        rpm = device_accepts_rpm(self.device)
        if debug: print 'does device RPM? :', str(rpm)
        if rpm:
            if debug: print 'device accepts rpm'
            age_group = self.predicted_next_nodes_to_expire(prop)
            keys = age_group.keys()
            properties = []
            for x in keys:  #go through this to guarentee having the same order in the results as the properties
                properties.append(age_group[x])
                if debug: print '    rpm: ', x

            if debug: print 'RPM for :', self.device, properties
            r = read_property_multiple_g3(self.device, properties)
            if debug: print ' RPM result: ', r
    
            now = time.time()
            for x in keys:
                result = Result()
                result.timestamp = now
                result.cached = 1
                v = r.pop(0)
                if debug: print ' RPM decode: ', v #, x
                try:
                    pv = v.list_of_results[0].property_value
                    x.decode(pv)
                    result.value = x._value
                except sequence.ETagMissing, e:
                    result.value = sequence.ETagMissing('ETagMissing():\n ' + \
                                                        str(e))
                if debug: print '         add to cache: ', result.value
                self.cache[x] = result
                x.last_result = result
Exemplo n.º 9
0
 def get(self, manager, **keywords):
     if debug: print 'Start of Batch:%s' % (self.properties)
     now = time.time()
     self.last_get_time = now
     answer = {}  #return a dictionary of ids and results
     cache = self.cache
     cache.lock.acquire()
     try:
         try:
             if (cache.response is None) or \
                ((cache.timestamp + cache.ttl) < time.time()):
                 cached = 0
                 cache._refresh()
             else:
                 cached = 1
             for id in self.ids:
                 ion = self.map[id]
                 result = Result()
                 result.timestamp = cache.timestamp
                 result.cached = cached
                 result.value = apply(
                     ion.read,
                     [cache.response, ion.offset, cache.start, ion.orders])
                 answer[id] = result
             self._error_counter = 0
         except ETimeout, e:
             return e  #subscription manager will re-raise the timeout, not cause for bad batch
         except:
             msglog.exception()
             self._error_counter += 1
             if self._error_counter > 15:  #catch run away thrashing
                 for id in self.ids:
                     nr = self.map[id]
                     nr.set_batch_manager(
                         None)  #none of these points can batch
                 raise EBadBatch('bacnet', self, 'error threshold exceeded')
Exemplo n.º 10
0
 def get(self, manager, **keywords):
     if debug: print 'Start of Batch:%s' % (self.properties)
     now = time.time()
     self.last_get_time = now
     answer = {} #return a dictionary of ids and results
     cache = self.cache
     cache.lock.acquire()
     try:
         try:
             if (cache.response is None) or \
                ((cache.timestamp + cache.ttl) < time.time()):
                 cached = 0
                 cache._refresh()
             else:
                 cached = 1
             for id in self.ids:
                 ion = self.map[id]
                 result = Result()
                 result.timestamp = cache.timestamp
                 result.cached = cached
                 result.value = apply(ion.read, [cache.response,
                                                  ion.offset,
                                                  cache.start,
                                                  ion.orders])
                 answer[id]=result
             self._error_counter = 0
         except ETimeout, e:
             return e #subscription manager will re-raise the timeout, not cause for bad batch
         except:
             msglog.exception()
             self._error_counter += 1
             if self._error_counter > 15: #catch run away thrashing
                 for id in self.ids:
                     nr = self.map[id]
                     nr.set_batch_manager(None) #none of these points can batch
                 raise EBadBatch('bacnet', self, 'error threshold exceeded')
Exemplo n.º 11
0
 def rpm_callback(self, rars):  #called all the way from the TSM callback upon completion
     try:
         now = time.time()
         if isinstance(rars, Exception):
             raise rars
         self.total_rpm_error_counter = 0 #one good read resets thrashing counter
         answer = {}
         for ps in self.properties: #[(obj type, instance, (pids,)),]
             rar = rars.pop(0)
             lrs = rar.list_of_results
             for p in ps[2]: #pids tuple
                 id = None
                 nr = None
                 try:
                     lr = lrs.pop(0)
                     pe = lr.property_access_error
                     self.pe = pe #take this out
                     pv = lr.property_value
                     self.pv = pv #same here
                     id = self.prop2id[ps[:2] + (p,)]
                     nr = self.map[id]
                     value = None
                     if not isinstance(pv, OPTIONAL):
                         value = nr.decoder().decode(pv) #use bacnet_property to decode
                     elif not isinstance(pe, OPTIONAL):
                         if debug: msglog.log('bacnet', nr.as_node_url(), str(pe))
                         value = BACnetRpmRarError('batch', nr.as_node_url(), str(pe))
                     else:
                         return EUnreachableCode()
                 except Exception, e:
                     if debug: 
                         msglog.exception()
                         msglog.log('bacnet', nr.as_node_url(), 'exception doing rpm response list')
                     value = e
                 if id:
                     if nr.hasattr('is_binary_pv'):
                         if nr.is_binary_pv():
                             value = int(value)
                     elif nr.hasattr('_is_binary_type'):
                         if nr._is_binary_type():
                             value = int(value)
                     answer[id] = Result(value, now)                        
         #print 'Answer:%s' % (str(answer),)
         if debug: print 'Batch Callback Time:%s %s' % (self.properties, time.time() - self.last_get_time,) 
         return answer
Exemplo n.º 12
0
    def get(self, manager, **keywords):
        if debug: print 'Start of Batch:%s' % (self.array_node.as_node_url())
        now = time.time()
        self.last_get_time = now
        answer = {}
        if not self.array_node.is_array():
            #mark all ids bad and throw exception
            for id in self.ids:
                nr = self.map[id]
                nr.set_batch_manager(None) #turn off rpm for the offending property
            raise EBadBatch('bacnet', self.array_node.name, 'array node is NOT an array')

        if keywords.has_key('callback'):
            keywords['callback'].callback(self.rpm_callback)
            keywords['T_OUT'] = self.timeout
        try:            
            array_value = self.array_node.get_result(1, **keywords)
            if isinstance(array_value, Callback):
                return array_value #if we are in callback mode
        except Exception, e:
            if debug: msglog.exception()
            for id in self.ids:
                answer[id] = Result(e, now)
            return answer
Exemplo n.º 13
0
                     value = BACnetRpmRarError('batch', nr.as_node_url(), str(pe))
                 else:
                     raise EUnreachableCode
             except Exception, e:
                 if debug: 
                     msglog.exception()
                     msglog.log('bacnet', nr.name, 'exception doing rpm response list')
                 value = e
             if id:
                 if nr.hasattr('is_binary_pv'):
                     if nr.is_binary_pv():
                         value = int(value)
                 elif nr.hasattr('_is_binary_type'):
                     if nr._is_binary_type():
                         value = int(value)
                 answer[id] = Result(value, now)
     #print 'Answer:%s' % (str(answer),)
     if debug: print 'Batch Time:%s %s' % (self.properties, time.time() - self.last_get_time,) 
     return answer
 def rpm_callback(self, rars):  #called all the way from the TSM callback upon completion
     try:
         now = time.time()
         if isinstance(rars, Exception):
             raise rars
         self.total_rpm_error_counter = 0 #one good read resets thrashing counter
         answer = {}
         for ps in self.properties: #[(obj type, instance, (pids,)),]
             rar = rars.pop(0)
             lrs = rar.list_of_results
             for p in ps[2]: #pids tuple
                 id = None
Exemplo n.º 14
0
 def get_result(self, skipCache=0, **keywords):
     result = Result()
     result.value = len(self.children_nodes())
     result.timestamp = time.time()
     return result
Exemplo n.º 15
0
 def get_result(self,skipCache=0, **keywords):
     result = Result()
     result.value = len(self.children_nodes())
     result.timestamp = time.time()
     return result
Exemplo n.º 16
0
 def __init__(self):
     Result.__init__(self)
     self.node = None