initial commit
This commit is contained in:
106
web/status.py
Executable file
106
web/status.py
Executable file
@@ -0,0 +1,106 @@
|
||||
#!/usr/bin/python3
|
||||
import os
|
||||
import sys
|
||||
import cherrypy
|
||||
import influxdb
|
||||
import time
|
||||
import json
|
||||
|
||||
# Universal variables
|
||||
_SCRIPT_PATH = os.path.dirname(sys.argv[0])
|
||||
_STATIC_DIR = '/templates/' # Needs to have trailing and leading slash '/'
|
||||
|
||||
influx_host = 'localhost'
|
||||
influx_port = 8086
|
||||
influx_user = 'pi'
|
||||
influx_pwd = 'freedavis'
|
||||
influx_status_db = 'status'
|
||||
|
||||
# Icons
|
||||
fs_sol_icon = '../static/img/iss_solar_icon.png'
|
||||
fs_cap_icon = '../static/img/iss_capacitor_icon.png'
|
||||
|
||||
# Functions
|
||||
def read_html(filename):
|
||||
read_path = _SCRIPT_PATH + _STATIC_DIR + filename + '.html'
|
||||
try:
|
||||
with open(read_path, 'r') as handle:
|
||||
return handle.read()
|
||||
except:
|
||||
return """<div>ERROR!</div>""" + read_path
|
||||
|
||||
# Set default structure
|
||||
|
||||
html_start = '<html>'
|
||||
body_start = '<body>'
|
||||
body_close = '</body>'
|
||||
html_close = '</html>'
|
||||
|
||||
class StatusInfo(object):
|
||||
@cherrypy.expose
|
||||
def index(self):
|
||||
header = read_html('header')
|
||||
menu_raw = read_html('top_menu')
|
||||
menu = menu_raw.format(energy = '', weather = '', status = 'active')
|
||||
body = self.body()
|
||||
footer = read_html('footer')
|
||||
result = header\
|
||||
+ menu\
|
||||
+ body\
|
||||
+ footer
|
||||
return result
|
||||
|
||||
def LastKnownState(self):
|
||||
'''
|
||||
Function to get energy readings from InfluxDB.
|
||||
|
||||
returns:
|
||||
dict(): {"time": str(time_stamp),
|
||||
"solar": solar,
|
||||
"cap": cap}
|
||||
'''
|
||||
|
||||
influx_status_client = influxdb.client.InfluxDBClient(
|
||||
influx_host, influx_port, influx_user, influx_pwd, influx_status_db
|
||||
)
|
||||
|
||||
# General query
|
||||
query1 = "SELECT time, voltage FROM iss WHERE type = "
|
||||
query2 = "ORDER BY time DESC LIMIT 1"
|
||||
|
||||
# now parse the tag in the middle of the two q from above
|
||||
solar_q = "{} '{}' {}".format(query1, "solar", query2)
|
||||
cap_q = "{} '{}' {}".format(query1, "capcaitor", query2)
|
||||
|
||||
# the actual query to DB
|
||||
solar = influx_status_client.query(solar_q)
|
||||
cap = influx_status_client.query(cap_q)
|
||||
|
||||
# returned is a list, in this case, we just need one value [0]
|
||||
result_solar = [sol for sol in solar][0][0]
|
||||
result_cap = [cap_ for cap_ in cap][0][0]
|
||||
|
||||
# Put the time to a uhman readable format, strip nanosecs
|
||||
time_stamp = time.strptime(result_solar['time'].split('.')[0],
|
||||
"%Y-%m-%dT%H:%M:%S")
|
||||
result = {}
|
||||
|
||||
# Construct the result to return
|
||||
result.update({"time": time_stamp})
|
||||
result.update({"solar": round(result_solar['voltage'], 1)})
|
||||
result.update({"cap": round(result_cap['voltage'], 1)})
|
||||
return result
|
||||
|
||||
|
||||
def body(self):
|
||||
admin_preformat = read_html('status_admin')
|
||||
current_status = self.LastKnownState()
|
||||
admin_html = admin_preformat.format(
|
||||
timestamp = time.strftime("%d.%m.%Y %H:%M:%S",
|
||||
current_status['time']),
|
||||
sol_icon = fs_sol_icon,
|
||||
cap_icon = fs_cap_icon,
|
||||
sol_value = round(current_status['solar'] / 100, 2),
|
||||
cap_value = current_status['cap']
|
||||
)
|
||||
return admin_html
|
||||
Reference in New Issue
Block a user