-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgraphing.py
115 lines (99 loc) · 3.54 KB
/
graphing.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
'''
Created on 23 Nov 2015
@author: steph
'''
import datetime
import time
from database import DbUtils
DB = DbUtils()
class MakeGraph():
def __init__(self):
#self.createGraph()
pass
def createGraph(self, roomName):
roomSplit = roomName.split('?')
cleanName = roomSplit[1].replace('%20', ' ')
graphPeriod = int(roomSplit[2])
currentTime = time.time() - 86400 * graphPeriod
tempData = DB.getTemps(cleanName, currentTime)
boilerData = DB.getAllBoiler()
pageText = []
pageText.append(self.html_Top())
pageText.append(self.html_Data(tempData, boilerData))
pageText.append(self.html_Options(cleanName))
pageText.append(self.html_Chart())
pageText.append(self.html_Body())
html_text = ''.join(pageText)
f = open('graph.html', 'w')
f.write(html_text)
f.close()
def html_Top(self):
html_text = """<html>
<head>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawVisualization);
"""
return html_text
def html_Data(self, tempData, boilerData):
pageText = []
pageText.append("""function drawVisualization() {
// Some raw data (not necessarily accurate)
var data = google.visualization.arrayToDataTable([
['Time', 'SetPoint', 'Temperature', 'Boiler', 'Valve %'],
""")
lastTemp = 0.0
for lines in tempData:
tempTime = lines[2]
setPoint = lines[3]
realTemp = float(lines[4])
valvePos = lines[5] / 10.0
# Exclude Zero Temperature values
if realTemp > 0.0:
lastTemp = realTemp
if realTemp == 0.0 and lastTemp > 1.0:
realTemp = 'null'
# Find if boiler is on at temperature time
for states in reversed(boilerData):
if tempTime >= states[1]:
boilerOn = states[2] * 12
break
timeString = (datetime.datetime.fromtimestamp(float(tempTime)).strftime('%d %H:%M'))
pageText.append("""['{}', {}, {}, {}, {}],
""".format(timeString,setPoint,realTemp,boilerOn,valvePos))
pageText.append("""]);
""")
html_text = ''.join(pageText)
return html_text
def html_Options(self, roomName):
html_text = """
var options = {{
title : 'Temperature of {}',
seriesType: 'line',
interpolateNulls: true,
series: {{
2: {{type: 'area'}},
3: {{type: 'area'}}
}}
}};
""".format(roomName)
return html_text
def html_Chart(self):
html_text = """
var chart = new google.visualization.ComboChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
</head>
"""
return html_text
def html_Body(self):
html_text = """
<body>
<div id="chart_div" style="width: 100%; height: 600px;"></div>
<a href="/index.html"><button type="button">Main UI</button></a>
</body>
</html>
"""
return html_text