-
Notifications
You must be signed in to change notification settings - Fork 0
/
webServer.py
executable file
·59 lines (48 loc) · 1.37 KB
/
webServer.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
#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-
"""
Home-web
~~~~~~~~
:copyright: (c) 2013 by Aurélien Chabot <aurelien@chabot.fr>
:license: LGPLv3, see COPYING for more details.
"""
try:
import sys
import json
import urllib2
from flask import Flask, request, session, g, redirect, url_for, abort, render_template, flash
except ImportError as error:
print 'ImportError: ', str(error)
exit(1)
try:
sys.path.insert(0, '../rest/')
sys.path.insert(0, '/usr/local/bin/')
from restClientLib import get_nodes, set_switch, update_sensor, update_switch
except ImportError as error:
print 'Custom py ImportError: ', str(error)
exit(1)
app = Flask(__name__)
# Server config
if len(sys.argv) > 1:
config = sys.argv[1]
else:
config = "servers.json"
try:
fd = open(config)
servers = json.load(fd)
except:
print("Invalid configuration file")
exit(1)
@app.route('/')
def index():
switches,sensors = get_nodes(servers)
app.logger.info("switches : %s", switches)
app.logger.info("sensors : %s", sensors)
for switch in switches:
update_switch(switch)
app.logger.info("name : %s, status: %d", switch["name"], switch["status"])
for sensor in sensors:
update_sensor(sensor)
app.logger.info("name : %s, value: %.1f", sensor["name"], sensor["value"])
return render_template('index.html', switches=switches, sensors=sensors)
app.run(host="0.0.0.0", port=80, debug=True)