浏览代码

Adaption to new logging

spike_filter
Dirk Alders 3 年前
父节点
当前提交
f8cb99aa08
共有 3 个文件被更改,包括 75 次插入7 次删除
  1. 6
    2
      __init__.py
  2. 10
    1
      bmp.py
  3. 59
    4
      dht.py

+ 6
- 2
__init__.py 查看文件

@@ -4,8 +4,12 @@ import time
4 4
 
5 5
 DEBUG = False
6 6
 
7
-logger_name = 'RPI_ENVSENS'
8
-logger = logging.getLogger(logger_name)
7
+try:
8
+    from config import APP_NAME as ROOT_LOGGER_NAME
9
+except ImportError:
10
+    ROOT_LOGGER_NAME = 'root'
11
+
12
+logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
9 13
 
10 14
 
11 15
 class background_task(object):

+ 10
- 1
bmp.py 查看文件

@@ -1,6 +1,15 @@
1
+import logging
1 2
 import time
2 3
 
3
-from . import logger, background_task
4
+try:
5
+    from config import APP_NAME as ROOT_LOGGER_NAME
6
+except ImportError:
7
+    ROOT_LOGGER_NAME = 'root'
8
+
9
+logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
10
+
11
+from . import background_task
12
+
4 13
 try:
5 14
     import smbus
6 15
 except ImportError:

+ 59
- 4
dht.py 查看文件

@@ -1,6 +1,15 @@
1
+import logging
1 2
 import time
2 3
 
3
-from . import logger, background_task
4
+try:
5
+    from config import APP_NAME as ROOT_LOGGER_NAME
6
+except ImportError:
7
+    ROOT_LOGGER_NAME = 'root'
8
+
9
+logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
10
+
11
+from . import background_task
12
+
4 13
 try:
5 14
     import adafruit_dht
6 15
 except ImportError:
@@ -18,9 +27,10 @@ class dht_22(background_task):
18 27
 
19 28
     def __init__(self, gpio, data_callback=None):
20 29
         self.__data_callback__ = data_callback
30
+        self.__monitor__ = dht_22_monitor(300)
21 31
         # Initial the dht device, with data pin connected to:
22 32
         if not DEBUG:
23
-            self.__dht_device__ = adafruit_dht.DHT22(gpio)
33
+            self.__dht_device__ = adafruit_dht.DHT22(gpio, use_pulseio=False)
24 34
         # Initialise background_task
25 35
         background_task.__init__(self)
26 36
 
@@ -28,8 +38,13 @@ class dht_22(background_task):
28 38
         data = self.__dht_data_transmission__()
29 39
         if data is not None:
30 40
             logger.debug('DHT-Communication: Successfully: %s', repr(data))
41
+            self.__monitor__.process(data)
31 42
             if self.__data_callback__ is not None:
32 43
                 self.__data_callback__(**data)
44
+            if self.__monitor__.status():
45
+                logger.warning("DHT measurement stopped caused by too many identical values!")
46
+                self.close()
47
+
33 48
 
34 49
     def __dht_data_transmission__(self):
35 50
         if not DEBUG:
@@ -42,11 +57,11 @@ class dht_22(background_task):
42 57
                     data[self.KEY_TIME] = int(time.time())
43 58
                     if data[self.KEY_TEMPERATURE] is not None and data[self.KEY_HUMIDITY] is not None:
44 59
                         return data
45
-                    time.sleep(2.0)
60
+                    time.sleep(self.RUN_SLEEP_TIME)
46 61
                 except RuntimeError as error:
47 62
                     # Errors happen fairly often, DHT's are hard to read, just keep going
48 63
                     logger.info('DHT-Communication: ' + error.args[0])
49
-                    time.sleep(0.1)
64
+                    time.sleep(2.0)
50 65
                 except Exception as error:
51 66
                     self.__dht_device__.exit()
52 67
                     raise error
@@ -56,3 +71,43 @@ class dht_22(background_task):
56 71
                 self.KEY_HUMIDITY: 1.7,
57 72
                 self.KEY_TIME: int(time.time()),
58 73
             }
74
+
75
+
76
+class dht_22_monitor(object):
77
+    def __init__(self, max_const_treshold=250):
78
+        self.__max_const_treshold__ = max_const_treshold
79
+        self.__fail__ = False
80
+        self.__max_const_measurements__ = 0
81
+        self.__init_statevars__()
82
+
83
+    def __init_statevars__(self):
84
+        self.__current_const_measurements__ = 0
85
+        self.__last_measurements__ = None
86
+
87
+    def process(self, data):
88
+        if self.__last_measurements__ is None:
89
+            self.__last_measurements__ = data
90
+        else:
91
+            data_is_equal = True
92
+            for key in [dht_22.KEY_HUMIDITY, dht_22.KEY_TEMPERATURE]:
93
+                if data[key] != self.__last_measurements__[key]:
94
+                    data_is_equal = False
95
+            if data_is_equal:
96
+                self.__current_const_measurements__ += 1
97
+                if self.__current_const_measurements__ > self.__max_const_measurements__:
98
+                    self.__max_const_measurements__ = self.__current_const_measurements__
99
+            else:
100
+                if self.__current_const_measurements__ >= self.__max_const_measurements__:
101
+                    logger.warning('%d identical data measurements identified!', self.__max_const_measurements__)
102
+                self.__init_statevars__()
103
+        if self.__max_const_measurements__ > self.__max_const_treshold__:
104
+            if not self.__fail__:
105
+                logger.warning("DHT measurement values are suspicious constant!")
106
+            self.__fail__ = True
107
+
108
+    def status(self):
109
+        if self.__fail__:
110
+            self.__fail__ = False
111
+            self.__max_const_measurements__ = 0
112
+            return True
113
+        return False

正在加载...
取消
保存