Eh, cleanup, pep8 and stuff
This commit is contained in:
@@ -1,10 +1,19 @@
|
||||
#!/usr/bin/python3
|
||||
"""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 cherrypy
|
||||
import OpenSSL
|
||||
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__))
|
||||
|
||||
@@ -15,19 +24,8 @@ sys.path.append(_SCRIPT_PATH)
|
||||
# Certificates reside here
|
||||
_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...
|
||||
server_config={
|
||||
# basic config for the server, the SSL part is questionable...
|
||||
server_config = {
|
||||
'server.socket_host': '0.0.0.0',
|
||||
'server.socket_port': 443,
|
||||
'server.ssl_module': 'builtin',
|
||||
|
||||
101
web/weather.py
101
web/weather.py
@@ -8,7 +8,7 @@ import json
|
||||
|
||||
# Universal variables
|
||||
_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_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_winddirection_icon = '../static/img/wind_direction_icon.png'
|
||||
|
||||
|
||||
# Functions
|
||||
def read_html(filename):
|
||||
read_path = _SCRIPT_PATH + _STATIC_DIR + filename + '.html'
|
||||
@@ -41,18 +42,19 @@ body_start = '<body>'
|
||||
body_close = '</body>'
|
||||
html_close = '</html>'
|
||||
|
||||
|
||||
class WeatherInfo(object):
|
||||
@cherrypy.expose
|
||||
def index(self):
|
||||
header = read_html('header')
|
||||
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()
|
||||
footer = read_html('footer')
|
||||
result = header\
|
||||
+ menu\
|
||||
+ body\
|
||||
+ footer
|
||||
+ menu\
|
||||
+ body\
|
||||
+ footer
|
||||
return result
|
||||
|
||||
def winddirName(self, direction):
|
||||
@@ -60,9 +62,7 @@ class WeatherInfo(object):
|
||||
Function to get energy readings from InfluxDB.
|
||||
|
||||
returns:
|
||||
dict(): {"last": str(last_entry),
|
||||
"details": list(detailed_table),
|
||||
"watthours": str(watthours)}
|
||||
str(): The linguistic representation of wind direction
|
||||
'''
|
||||
if 10.5 <= direction < 34.5:
|
||||
result = "NNE"
|
||||
@@ -95,25 +95,23 @@ class WeatherInfo(object):
|
||||
elif 346.5 <= direction <= 359.9 or 0 < direction < 10.5:
|
||||
result = "N"
|
||||
elif direction == 0:
|
||||
result = "ERROR, windvan broken"
|
||||
# it is being said that 0 means error, not sure.
|
||||
result = "ERROR, windvane broken"
|
||||
else:
|
||||
result = "NaN"
|
||||
return result
|
||||
|
||||
def LastKnownState(self):
|
||||
'''
|
||||
Function to get energy readings from InfluxDB.
|
||||
|
||||
returns:
|
||||
dict(): {"last": str(last_entry),
|
||||
"details": list(detailed_table),
|
||||
"watthours": str(watthours)}
|
||||
'''
|
||||
"""
|
||||
Returns a dict full of weather data.
|
||||
:param self:
|
||||
"""
|
||||
influx_weather_client = influxdb.client.InfluxDBClient(
|
||||
influx_host, influx_port, influx_user, influx_pwd, influx_weather_db
|
||||
)
|
||||
influx_status_client = influxdb.client.InfluxDBClient(
|
||||
influx_host, influx_port, influx_user, influx_pwd, influx_status_db
|
||||
influx_host,
|
||||
influx_port,
|
||||
influx_user,
|
||||
influx_pwd,
|
||||
influx_weather_db
|
||||
)
|
||||
|
||||
# General query
|
||||
@@ -124,13 +122,18 @@ class WeatherInfo(object):
|
||||
wind_speed_q = "{} '{}' {}".format(query1, "speed", query2)
|
||||
wind_direction_q = "{} '{}' {}".format(query1, "direction", query2)
|
||||
wind_gust_q = "{} '{}' {}".format(query1, "windgust", query2)
|
||||
hum_ext_q = "SELECT time, humidity FROM temphumi WHERE type = 'external' {}".format(query2)
|
||||
hum_int_q = "SELECT time, humidity FROM temphumi WHERE type = 'internal' {}".format(query2)
|
||||
press_q = "SELECT time, pressure FROM temphumi 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)
|
||||
|
||||
hum_ext_q = "SELECT time, humidity FROM temphumi\
|
||||
WHERE type = 'external' {}".format(query2)
|
||||
hum_int_q = "SELECT time, humidity FROM temphumi\
|
||||
WHERE type = 'internal' {}".format(query2)
|
||||
press_q = "SELECT time, pressure FROM temphumi\
|
||||
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
|
||||
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_int = influx_weather_client.query(t_int_q)
|
||||
|
||||
|
||||
# returned is a list, in this case, we just need one value [0]
|
||||
result_windspeed = [speed for speed in wind_speed][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
|
||||
time_stamp = time.strptime(result_windspeed['time'].split('.')[0],
|
||||
"%Y-%m-%dT%H:%M:%S")
|
||||
"%Y-%m-%dT%H:%M:%S")
|
||||
result = {}
|
||||
|
||||
# Construct the result to return
|
||||
@@ -173,28 +175,31 @@ class WeatherInfo(object):
|
||||
result.update({"temp_int": round(result_t_int['temperature'], 1)})
|
||||
return result
|
||||
|
||||
|
||||
def body(self):
|
||||
"""
|
||||
A fully formated body of a HTML document, taken from ./templates/
|
||||
:param self:
|
||||
"""
|
||||
admin_preformat = read_html('weather_admin')
|
||||
current_weather = self.LastKnownState()
|
||||
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']),
|
||||
w_speed_icon = fs_wind_icon,
|
||||
w_speed_km = current_weather['speed'],
|
||||
w_speed_ms = round(current_weather['speed'] / 3.6, 1),
|
||||
w_gust_icon = fs_windgust_icon,
|
||||
w_gust_km = current_weather['windgust'],
|
||||
w_gust_ms = round(current_weather['windgust'] / 3.6, 1),
|
||||
w_dir_icon = fs_winddirection_icon,
|
||||
w_dir_name = self.winddirName(current_weather['direction']),
|
||||
w_dir_deg = current_weather['direction'],
|
||||
out_temp_icon = fs_out_temperature_icon,
|
||||
out_temp = current_weather['temp_ext'],
|
||||
in_temp_icon = fs_in_temperature_icon,
|
||||
in_temp = current_weather['temp_int'],
|
||||
pressure_icon = fs_pressure_icon,
|
||||
pressure = current_weather['pressure'],
|
||||
raw_pressure = current_weather['pressure_raw']
|
||||
w_speed_icon=fs_wind_icon,
|
||||
w_speed_km=current_weather['speed'],
|
||||
w_speed_ms=round(current_weather['speed'] / 3.6, 1),
|
||||
w_gust_icon=fs_windgust_icon,
|
||||
w_gust_km=current_weather['windgust'],
|
||||
w_gust_ms=round(current_weather['windgust'] / 3.6, 1),
|
||||
w_dir_icon=fs_winddirection_icon,
|
||||
w_dir_name=self.winddirName(current_weather['direction']),
|
||||
w_dir_deg=current_weather['direction'],
|
||||
out_temp_icon=fs_out_temperature_icon,
|
||||
out_temp=current_weather['temp_ext'],
|
||||
in_temp_icon=fs_in_temperature_icon,
|
||||
in_temp=current_weather['temp_int'],
|
||||
pressure_icon=fs_pressure_icon,
|
||||
pressure=current_weather['pressure'],
|
||||
raw_pressure=current_weather['pressure_raw']
|
||||
)
|
||||
return admin_html
|
||||
|
||||
Reference in New Issue
Block a user