-
Notifications
You must be signed in to change notification settings - Fork 0
/
createReport.py
116 lines (97 loc) · 3.92 KB
/
createReport.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
from database import Database
import logging
import json
import csv
import re
logging.basicConfig(level=logging.DEBUG)
class Report:
def getReportName(self):
validfilename = False
print("What would you like to name the report file?")
print("Enter a name only, the file extension will be added")
while validfilename is False:
filename = input()
# check file name contains no spaces or special characters
regex = re.compile(' [@_!#$%^&*()<>?/\|}{~:] ')
if regex.search(filename) is None and (' ' in filename) is False:
reportfilename = filename + ".csv"
validfilename = True
return reportfilename
else:
print("That filename is not valid, Please try again:")
print("New file name: ")
validfilename = False
def __init__(self):
self.configFilename = 'config.json'
self.reportFilename = self.getReportName()
self.databaseName = 'VirtualSenseHat.db'
self.database = Database(self.databaseName)
self.reportData = [["Date", "Status"]]
def initRange(self):
with open(self.configFilename, "r") as file:
self.range = json.load(file)
self.minTemp = self.range["min_temperature"]
self.maxTemp = self.range["max_temperature"]
self.minHumidity = self.range["min_humidity"]
self.maxHumidity = self.range["max_humidity"]
def evaluateStatus(self, value, unit, min=None, max=None):
if unit == "*C":
item = "temperature"
else:
item = "humidity"
if max is None:
if value < min:
different = round(min - value, 2)
status = "{}{} below minimum {} (Minimum is {} {})".format(
str(different), unit, item, min, unit)
else:
return ""
if min is None:
if value > max:
different = round(value - max, 2)
status = "{}{} above maximum {} (Maximum is {} {})".format(
str(different), unit, item, max, unit)
else:
return ""
return status
def computeReportData(self):
maxDevTemperature, maxDevHumidity = self.database.getMaxMinDataPerDay()
for date in maxDevTemperature.keys():
temperatureStatus = []
humidityStatus = []
reportRow = [date]
temperatureStatus.append(
self.evaluateStatus(
maxDevTemperature[date][0], "*C", max=self.maxTemp))
temperatureStatus.append(
self.evaluateStatus(
maxDevTemperature[date][1], "*C", min=self.minTemp))
humidityStatus.append(
self.evaluateStatus(
maxDevHumidity[date][0], "%", max=self.maxHumidity))
humidityStatus.append(
self.evaluateStatus(
maxDevHumidity[date][1], "%", min=self.minHumidity))
if temperatureStatus != ["", ""] or humidityStatus != ["", ""]:
shortStatus = "BAD"
else:
shortStatus = "OK"
reportRow.append(shortStatus)
reportRow += [
status for status in temperatureStatus if status != ""]
reportRow += [
status for status in humidityStatus if status != ""]
self.reportData.append(reportRow)
def writeFile(self):
with open(self.reportFilename, 'w', newline='') as csvFile:
writer = csv.writer(csvFile)
writer.writerows(self.reportData)
csvFile.close()
logging.debug("All report data is written to {}".format(
self.reportFilename))
def generateReport(self):
self.initRange()
self.computeReportData()
self.writeFile()
report = Report()
report.generateReport()