Browse Source

Timeout for current data implemented

master
Dirk Alders 4 years ago
parent
commit
26a4ed180f
1 changed files with 13 additions and 1 deletions
  1. 13
    1
      __init__.py

+ 13
- 1
__init__.py View File

1
+import logging
1
 import numbers
2
 import numbers
2
 import os
3
 import os
3
 import time
4
 import time
4
 
5
 
6
+logger_name = 'HELPERS'
7
+logger = logging.getLogger(logger_name)
8
+
5
 
9
 
6
 class continues_statistic(dict):
10
 class continues_statistic(dict):
7
     KEY_SINGLE_VALUE = '_'
11
     KEY_SINGLE_VALUE = '_'
88
 
92
 
89
 
93
 
90
 class data_collector(object):
94
 class data_collector(object):
91
-    def __init__(self, bufferlength):
95
+    def __init__(self, bufferlength, current_max_age=300.):
96
+        self.__current_max_age__ = current_max_age
92
         self.__current_data_rinbuffer__ = ringbuffer(length=3)
97
         self.__current_data_rinbuffer__ = ringbuffer(length=3)
93
         #
98
         #
99
+        self.__last_add_data__ = 0
94
         self.__continues_statistic__ = None
100
         self.__continues_statistic__ = None
95
         self.__yesterdays_statistic__ = None
101
         self.__yesterdays_statistic__ = None
96
         self.__measurement_day__ = None
102
         self.__measurement_day__ = None
114
 
120
 
115
 
121
 
116
     def add_data(self, **kwargs):
122
     def add_data(self, **kwargs):
123
+        logger.debug('Got data %s', repr(kwargs))
124
+        self.__last_add_data__ = time.time()
117
         self.__current_data_rinbuffer__.append(kwargs)
125
         self.__current_data_rinbuffer__.append(kwargs)
118
         self.__process_continues_statistic__(**kwargs)
126
         self.__process_continues_statistic__(**kwargs)
119
         self.__ringbuffer__.append(kwargs)
127
         self.__ringbuffer__.append(kwargs)
121
     @property
129
     @property
122
     def current(self):
130
     def current(self):
123
         if len(self.__current_data_rinbuffer__) == 0:
131
         if len(self.__current_data_rinbuffer__) == 0:
132
+            logger.info('No data available, yet.')
133
+            return None
134
+        elif  time.time() > self.__last_add_data__ + self.__current_max_age__:
135
+            logger.info('Data max age reached. No current data available.')
124
             return None
136
             return None
125
         elif  len(self.__current_data_rinbuffer__) < 3:
137
         elif  len(self.__current_data_rinbuffer__) < 3:
126
             return self.__current_data_rinbuffer__[-1]
138
             return self.__current_data_rinbuffer__[-1]

Loading…
Cancel
Save