-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.py
106 lines (79 loc) · 3.25 KB
/
run.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
import os
import shutil
import threading
from flask import *
from config import *
import GaugeProjectApache.py.main
app = Flask(__name__)
class SVGBuildingThread(object):
""" Threading example class
The run() method will be started and it will run in the background
until the application exits.
"""
def __init__(self, interval=1):
""" Constructor
:type interval: int
:param interval: Check interval, in seconds
"""
self.interval = interval
thread = threading.Thread(target=self.run, args=())
thread.daemon = True # Daemonize thread
thread.start() # Start the execution
def run(self):
""" Method that runs forever """
GaugeProjectApache.py.main.main()
def main():
if os.path.isdir('./static/svg'):
shutil.rmtree('./static/svg')
os.mkdir('./static/svg')
example = SVGBuildingThread()
gauge_base_path = os.path.join(os.sep, 'static', 'svg')
@app.route(HOST_PREFIX + '/')
def index():
return redirect(HOST_PREFIX + "/kW")
#Add headers to both force latest IE rendering engine or Chrome Frame,
#and also to cache the rendered page for 10 minutes.
@app.after_request
def add_header(r):
r.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
r.headers["Pragma"] = "no-cache"
r.headers["Expires"] = "0"
r.headers['Cache-Control'] = 'public, max-age=0'
return r
@app.route(HOST_PREFIX + '/dollars')
def dollars():
return render_template('gaugePage.html', title="Cost (Reset Daily) | Electricity Statistics Dashboard",
gauge_path=os.path.join(gauge_base_path, 'dollars.svg'))
@app.route(HOST_PREFIX + '/documentation')
def documentation():
return render_template('documentation.html')
@app.route(HOST_PREFIX + '/kW')
def kW():
return render_template('gaugePage.html', title="Kilowatts (Live) | Electricity Statistics Dashboard",
gauge_path=os.path.join(gauge_base_path, 'kw.svg'))
@app.route(HOST_PREFIX + '/kWh')
def kWh():
return render_template('gaugePage.html',
title="Kilowatt-Hours (Reset Daily) | Electricity Statistics Dashboard",
gauge_path=os.path.join(gauge_base_path, 'kwh.svg'))
@app.route(HOST_PREFIX + '/kWhHourly')
def hourly():
return render_template('gaugePage.html', title="Kilowatt-Hours (Hourly) | Electricity Statistics Dashboard",
gauge_path=os.path.join(gauge_base_path, 'kWhHourly.svg'))
app.jinja_env.globals['host_prefix'] = HOST_PREFIX
app.static_url_path = '{0}/static'.format(HOST_PREFIX)
# remove old static map
url_map = app.url_map
try:
for rule in url_map.iter_rules('static'):
url_map._rules.remove(rule)
except ValueError:
# no static view was created yet
pass
# register new; the same view function is used
app.add_url_rule(app.static_url_path + '/<path:filename>', endpoint='static', view_func=app.send_static_file)
while not os.path.exists(os.path.join('static', 'svg', 'dollars.svg')):
pass
main()
if __name__ == '__main__':
app.run()