forked from jjposio/DHT22-TemperatureLogger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DHT22logger.py
executable file
·146 lines (125 loc) · 5.96 KB
/
DHT22logger.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# Copyright (c) 2015
# Author: Janne Posio
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE
# This script is intended to use with Adafruit DHT22 temperature and humidity sensors
# and with sensor libraries that Adafruit provides for them. This script alone, without
# sensor/s to provide data for it doesn't really do anyting useful.
# For guidance how to create your own temperature logger that makes use of this script,
# Adafruit DHT22 sensors and raspberry pi, visit :
# http://www.instructables.com/id/Raspberry-PI-and-DHT22-temperature-and-humidity-lo/
#!/usr/bin/python2
#coding=utf-8
import sys
import urllib2
import logging, logging.handlers
from Debugger.Logger import Logger
from Utility.MailSender import MailSender
from Utility.WeeklyAverages import WeeklyAverages
from Database.DbActionController import DbController
from Configurations.ConfigHandler import ConfigHandler
from Sensors.SensorDataHandler import SensorDataHandler
def main():
# Create logger for debugging purposes
try:
Logger()
logger = logging.getLogger()
# Print to console if logger instantiation failed and terminate. Execution will fail anyway in next logging attempt
except Exception as e:
print('Logger initialization failed. Error:\n{0}\nTry adding write permission directly to root (DHT22-TemperatureLogger) folder with "sudo chmod -R 777"'.format(e))
sys.exit(0)
# First log entry to indicate execution has started
logger.info("DHT22logger execution started")
# Read configurations from config.json. If this fails, no need to run further -> terminate.
try:
configurationHandler = ConfigHandler()
configurations = configurationHandler.getFullConfiguration()
except Exception as e:
logger.error('Failed to get configurations:\n',exc_info=True)
sys.exit(0)
# Instantiate dbController with configurations
try:
dbControl = DbController(configurations)
except Exception as e:
logger.error("dbController instantiation failed:\n",exc_info=True)
sys.exit(0)
if configurations.get('mailConfig')[0]['sendMail'] == 'y':
# Instantiate mail sender
# If mail sender instantiation fails, mail warnings cannot be send. Logger to db should work though, so no need for terminating
try:
mailSender = MailSender(configurations, dbControl)
mailSenderAvailable = True
except Exception as e:
mailSenderAvailable = False
logger.error('MailSender instantiation failed:\n',exc_info=True)
else:
mailSender = False
mailSenderAvailable = False
# Instantiate sensorHandler and use it to read and persist readings
try:
SensorDataHandler(configurations,dbControl,mailSender).readAndStoreSensorReadings()
except Exception as e:
logger.error('Sensor data handling failed:\n',exc_info=True)
if mailSenderAvailable:
try:
mailSender.sendWarningEmail("Error with sensor data handling.\nError message: {0}".format(e.message))
except:
logger.error('Sending warning mail failed\n',exc_info=True)
# Weekly average temperatures - Used to check that rpi and connection is still alive
# Check if mail sended is available, if not, no need to continue
if mailSenderAvailable:
logger.info("Check if weekly averages need to be sended")
# Check if weekly average sending is enabled in configurations
if configurationHandler.isWeeklyAveragesConfigEnabled():
# Instantiate Weekly averages that handles configuration check etc.
averagesSender = WeeklyAverages(configurations,dbControl,mailSender)
# Go through configurations to check if connection check is enabled
try:
# Sending is enabled and it is time to send. Execute
averagesSender.performWeeklyAverageMailSending()
# Log exceptions raised and send warning email
except Exception as e:
logger.error('Failed to check weekly averages\n',exc_info=True)
try:
mailSender.sendWarningEmail("Failed to send weekly averages.\nError message: {0}\nCheck debug log from Raspberry for more information".format(e.message))
except Exception as e:
logger.error('Failed to send email\n',exc_info=True)
# SQL dump
#Check if sql dump is enabled in configurations and if it is time to do the dump
if configurationHandler.isBackupDumpConfigEnabled():
# Yes, SQL dump is needed
logger.info("Starting sql backup dump")
try:
# Call create sql backup dump
dbControl.createSqlBackupDump()
except Exception as e:
logger.error('Failed to create SQL backup dump')
if mailSenderAvailable:
logger.error('Exception in DbBackupControl\n',exc_info=True)
try:
mailSender.sendWarningEmail('SQL Backup dump failed. Check debug log from raspberrypi for information')
except Exception as e:
logger.error('Failed to send email:\n',exc_info=True)
logger.info("Sql backup dump finished")
try:
request = urllib2.Request("http://localhost/data-updated", headers={"Accept" : "text/html"})
contents = urllib2.urlopen(request).read()
logger.info("Weatherpi webhook called\n")
except Exception as e:
logger.info("Weatherpi webhook failed\n")
logger.info("DHT22logger execution finished\n")
if __name__ == "__main__":
main()