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

Loading…
Cancel
Save