Eh, cleanup, pep8 and stuff

This commit is contained in:
Milan Toman
2018-07-15 01:02:05 +02:00
parent 1f5d741d18
commit b2a368e14d
2 changed files with 64 additions and 61 deletions

View File

@@ -1,10 +1,19 @@
#!/usr/bin/python3 #!/usr/bin/python3
"""Our cherrypy server, it all starts here """Our cherrypy server, it all starts here
Let's look at the statically linked stuff as well (i.e. the files that will
serve us data). These imports are static files in the same directory. Each
a spearate application with it's own face and behavior
""" """
import os import os
import cherrypy import cherrypy
import OpenSSL import OpenSSL
import sys import sys
# UI for weather ./weather.py
import weather
# CSV "API" for graphs, or individual pulling ./dynamic.py
import dynamic
# UI for RasPi / davis status ./status.py
import status
PATH = os.path.abspath(os.path.dirname(__file__)) PATH = os.path.abspath(os.path.dirname(__file__))
@@ -15,17 +24,6 @@ sys.path.append(_SCRIPT_PATH)
# Certificates reside here # Certificates reside here
_CERT_PATH = _SCRIPT_PATH + '/.cert' _CERT_PATH = _SCRIPT_PATH + '/.cert'
"""Let's look at the statically linked stuff (i.e. the files that will
serve us data). These imports are static files in the same directory. Each
a spearate application with it's own face and behavior
"""
# UI for weather ./weather.py
import weather
# CSV "API" for graphs, or individual pulling ./dynamic.py
import dynamic
# UI for RasPi / davis status ./status.py
import status
# basic config for the server, the SSL part is questionable... # basic config for the server, the SSL part is questionable...
server_config = { server_config = {
'server.socket_host': '0.0.0.0', 'server.socket_host': '0.0.0.0',

View File

@@ -25,6 +25,7 @@ fs_out_temperature_icon = '../static/img/outside_temp_icon.png'
fs_windgust_icon = '../static/img/wind_gust_icon.png' fs_windgust_icon = '../static/img/wind_gust_icon.png'
fs_winddirection_icon = '../static/img/wind_direction_icon.png' fs_winddirection_icon = '../static/img/wind_direction_icon.png'
# Functions # Functions
def read_html(filename): def read_html(filename):
read_path = _SCRIPT_PATH + _STATIC_DIR + filename + '.html' read_path = _SCRIPT_PATH + _STATIC_DIR + filename + '.html'
@@ -41,6 +42,7 @@ body_start = '<body>'
body_close = '</body>' body_close = '</body>'
html_close = '</html>' html_close = '</html>'
class WeatherInfo(object): class WeatherInfo(object):
@cherrypy.expose @cherrypy.expose
def index(self): def index(self):
@@ -60,9 +62,7 @@ class WeatherInfo(object):
Function to get energy readings from InfluxDB. Function to get energy readings from InfluxDB.
returns: returns:
dict(): {"last": str(last_entry), str(): The linguistic representation of wind direction
"details": list(detailed_table),
"watthours": str(watthours)}
''' '''
if 10.5 <= direction < 34.5: if 10.5 <= direction < 34.5:
result = "NNE" result = "NNE"
@@ -95,25 +95,23 @@ class WeatherInfo(object):
elif 346.5 <= direction <= 359.9 or 0 < direction < 10.5: elif 346.5 <= direction <= 359.9 or 0 < direction < 10.5:
result = "N" result = "N"
elif direction == 0: elif direction == 0:
result = "ERROR, windvan broken" # it is being said that 0 means error, not sure.
result = "ERROR, windvane broken"
else: else:
result = "NaN" result = "NaN"
return result return result
def LastKnownState(self): def LastKnownState(self):
''' """
Function to get energy readings from InfluxDB. Returns a dict full of weather data.
:param self:
returns: """
dict(): {"last": str(last_entry),
"details": list(detailed_table),
"watthours": str(watthours)}
'''
influx_weather_client = influxdb.client.InfluxDBClient( influx_weather_client = influxdb.client.InfluxDBClient(
influx_host, influx_port, influx_user, influx_pwd, influx_weather_db influx_host,
) influx_port,
influx_status_client = influxdb.client.InfluxDBClient( influx_user,
influx_host, influx_port, influx_user, influx_pwd, influx_status_db influx_pwd,
influx_weather_db
) )
# General query # General query
@@ -124,13 +122,18 @@ class WeatherInfo(object):
wind_speed_q = "{} '{}' {}".format(query1, "speed", query2) wind_speed_q = "{} '{}' {}".format(query1, "speed", query2)
wind_direction_q = "{} '{}' {}".format(query1, "direction", query2) wind_direction_q = "{} '{}' {}".format(query1, "direction", query2)
wind_gust_q = "{} '{}' {}".format(query1, "windgust", query2) wind_gust_q = "{} '{}' {}".format(query1, "windgust", query2)
hum_ext_q = "SELECT time, humidity FROM temphumi WHERE type = 'external' {}".format(query2) hum_ext_q = "SELECT time, humidity FROM temphumi\
hum_int_q = "SELECT time, humidity FROM temphumi WHERE type = 'internal' {}".format(query2) WHERE type = 'external' {}".format(query2)
press_q = "SELECT time, pressure FROM temphumi WHERE type = 'adjusted' {}".format(query2) hum_int_q = "SELECT time, humidity FROM temphumi\
presr_q = "SELECT time, pressure FROM temphumi WHERE type = 'raw' {}".format(query2) WHERE type = 'internal' {}".format(query2)
t_ext_q = "SELECT time, temperature FROM temphumi WHERE type = 'external' {}".format(query2) press_q = "SELECT time, pressure FROM temphumi\
t_int_q = "SELECT time, temperature FROM temphumi WHERE type = 'internal' {}".format(query2) WHERE type = 'adjusted' {}".format(query2)
presr_q = "SELECT time, pressure FROM temphumi\
WHERE type = 'raw' {}".format(query2)
t_ext_q = "SELECT time, temperature FROM temphumi\
WHERE type = 'external' {}".format(query2)
t_int_q = "SELECT time, temperature FROM temphumi\
WHERE type = 'internal' {}".format(query2)
# the actual query to DB # the actual query to DB
wind_speed = influx_weather_client.query(wind_speed_q) wind_speed = influx_weather_client.query(wind_speed_q)
@@ -143,7 +146,6 @@ class WeatherInfo(object):
t_ext = influx_weather_client.query(t_ext_q) t_ext = influx_weather_client.query(t_ext_q)
t_int = influx_weather_client.query(t_int_q) t_int = influx_weather_client.query(t_int_q)
# returned is a list, in this case, we just need one value [0] # returned is a list, in this case, we just need one value [0]
result_windspeed = [speed for speed in wind_speed][0][0] result_windspeed = [speed for speed in wind_speed][0][0]
result_winddir = [direction for direction in wind_direction][0][0] result_winddir = [direction for direction in wind_direction][0][0]
@@ -173,8 +175,11 @@ class WeatherInfo(object):
result.update({"temp_int": round(result_t_int['temperature'], 1)}) result.update({"temp_int": round(result_t_int['temperature'], 1)})
return result return result
def body(self): def body(self):
"""
A fully formated body of a HTML document, taken from ./templates/
:param self:
"""
admin_preformat = read_html('weather_admin') admin_preformat = read_html('weather_admin')
current_weather = self.LastKnownState() current_weather = self.LastKnownState()
admin_html = admin_preformat.format( admin_html = admin_preformat.format(