-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
73 lines (59 loc) · 2.29 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
from logging import basicConfig, getLogger, DEBUG, FileHandler, Formatter
from time import sleep
import slack_notifier
import datadog_notifier
from CCS811 import CCS811
class AirConditionMonitor:
CO2_LOWER_LIMIT = 400
CO2_HIGHER_LIMIT = 8192
LOG_FILE = '{script_dir}/logs/air_condition_monitor.log'.format(
script_dir = os.path.dirname(os.path.abspath(__file__))
)
def __init__(self):
self._ccs811 = CCS811()
self.slack_notifier = slack_notifier.SlackNotifier()
self.init_logger()
def init_logger(self):
self._logger = getLogger(self.__class__.__name__)
file_handler = FileHandler(self.LOG_FILE)
formatter = Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
file_handler.setFormatter(formatter)
self._logger.addHandler(file_handler)
self._logger.setLevel(DEBUG)
def status(self, co2):
if co2 < self.CO2_LOWER_LIMIT or co2 > self.CO2_HIGHER_LIMIT:
return self.CO2_STATUS_CONDITIONING
elif co2 < self.CO2_PPM_THRESHOLD_1:
return self.CO2_STATUS_LOW
elif co2 < self.CO2_PPM_THRESHOLD_2:
return self.CO2_STATUS_HIGH
else:
return self.CO2_STATUS_TOO_HIGH
def execute(self):
while True:
try:
if not self._ccs811.available():
sleep(1)
continue
if not self._ccs811.readData():
co2 = self._ccs811.geteCO2()
tvoc = self._ccs811.getTVOC()
if co2 < self.CO2_LOWER_LIMIT or co2 > self.CO2_HIGHER_LIMIT:
self._logger.debug("Under Conditioning...")
sleep(2)
continue
# self.slack_notifier.notify(co2, tvoc)
datadog_notifier.notify(co2, tvoc)
self._logger.info("CO2: {0}ppm, TVOC: {1}".format(co2, tvoc))
else:
self._logger.error('ERROR!')
except:
self._logger.error(sys.exc_info())
sleep(2)
if __name__ == '__main__':
air_condition_monitor = AirConditionMonitor()
air_condition_monitor.execute()