Eh, cleanup, pep8 and stuff
This commit is contained in:
@@ -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,19 +24,8 @@ 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
|
# basic config for the server, the SSL part is questionable...
|
||||||
serve us data). These imports are static files in the same directory. Each
|
server_config = {
|
||||||
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...
|
|
||||||
server_config={
|
|
||||||
'server.socket_host': '0.0.0.0',
|
'server.socket_host': '0.0.0.0',
|
||||||
'server.socket_port': 443,
|
'server.socket_port': 443,
|
||||||
'server.ssl_module': 'builtin',
|
'server.ssl_module': 'builtin',
|
||||||
|
|||||||
101
web/weather.py
101
web/weather.py
@@ -8,7 +8,7 @@ import json
|
|||||||
|
|
||||||
# Universal variables
|
# Universal variables
|
||||||
_SCRIPT_PATH = os.path.dirname(sys.argv[0])
|
_SCRIPT_PATH = os.path.dirname(sys.argv[0])
|
||||||
_STATIC_DIR = '/templates/' # Needs to have trailing and leading slash '/'
|
_STATIC_DIR = '/templates/' # Needs to have trailing and leading slash '/'
|
||||||
|
|
||||||
influx_host = 'localhost'
|
influx_host = 'localhost'
|
||||||
influx_port = 8086
|
influx_port = 8086
|
||||||
@@ -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,18 +42,19 @@ 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):
|
||||||
header = read_html('header')
|
header = read_html('header')
|
||||||
menu_raw = read_html('top_menu')
|
menu_raw = read_html('top_menu')
|
||||||
menu = menu_raw.format(energy = '', weather = 'active', status = '')
|
menu = menu_raw.format(energy='', weather='active', status='')
|
||||||
body = self.body()
|
body = self.body()
|
||||||
footer = read_html('footer')
|
footer = read_html('footer')
|
||||||
result = header\
|
result = header\
|
||||||
+ menu\
|
+ menu\
|
||||||
+ body\
|
+ body\
|
||||||
+ footer
|
+ footer
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def winddirName(self, direction):
|
def winddirName(self, direction):
|
||||||
@@ -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]
|
||||||
@@ -157,7 +159,7 @@ class WeatherInfo(object):
|
|||||||
|
|
||||||
# Put the time to a uhman readable format, strip nanosecs
|
# Put the time to a uhman readable format, strip nanosecs
|
||||||
time_stamp = time.strptime(result_windspeed['time'].split('.')[0],
|
time_stamp = time.strptime(result_windspeed['time'].split('.')[0],
|
||||||
"%Y-%m-%dT%H:%M:%S")
|
"%Y-%m-%dT%H:%M:%S")
|
||||||
result = {}
|
result = {}
|
||||||
|
|
||||||
# Construct the result to return
|
# Construct the result to return
|
||||||
@@ -173,28 +175,31 @@ 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(
|
||||||
timestamp = time.strftime("%d.%m.%Y %H:%M:%S",
|
timestamp=time.strftime("%d.%m.%Y %H:%M:%S",
|
||||||
current_weather['time']),
|
current_weather['time']),
|
||||||
w_speed_icon = fs_wind_icon,
|
w_speed_icon=fs_wind_icon,
|
||||||
w_speed_km = current_weather['speed'],
|
w_speed_km=current_weather['speed'],
|
||||||
w_speed_ms = round(current_weather['speed'] / 3.6, 1),
|
w_speed_ms=round(current_weather['speed'] / 3.6, 1),
|
||||||
w_gust_icon = fs_windgust_icon,
|
w_gust_icon=fs_windgust_icon,
|
||||||
w_gust_km = current_weather['windgust'],
|
w_gust_km=current_weather['windgust'],
|
||||||
w_gust_ms = round(current_weather['windgust'] / 3.6, 1),
|
w_gust_ms=round(current_weather['windgust'] / 3.6, 1),
|
||||||
w_dir_icon = fs_winddirection_icon,
|
w_dir_icon=fs_winddirection_icon,
|
||||||
w_dir_name = self.winddirName(current_weather['direction']),
|
w_dir_name=self.winddirName(current_weather['direction']),
|
||||||
w_dir_deg = current_weather['direction'],
|
w_dir_deg=current_weather['direction'],
|
||||||
out_temp_icon = fs_out_temperature_icon,
|
out_temp_icon=fs_out_temperature_icon,
|
||||||
out_temp = current_weather['temp_ext'],
|
out_temp=current_weather['temp_ext'],
|
||||||
in_temp_icon = fs_in_temperature_icon,
|
in_temp_icon=fs_in_temperature_icon,
|
||||||
in_temp = current_weather['temp_int'],
|
in_temp=current_weather['temp_int'],
|
||||||
pressure_icon = fs_pressure_icon,
|
pressure_icon=fs_pressure_icon,
|
||||||
pressure = current_weather['pressure'],
|
pressure=current_weather['pressure'],
|
||||||
raw_pressure = current_weather['pressure_raw']
|
raw_pressure=current_weather['pressure_raw']
|
||||||
)
|
)
|
||||||
return admin_html
|
return admin_html
|
||||||
|
|||||||
Reference in New Issue
Block a user