Skip to content

Commit

Permalink
Massive update:
Browse files Browse the repository at this point in the history
- wind_data added
- power data added
- some refactoring
  • Loading branch information
wwakabobik committed Feb 3, 2021
1 parent d3e6985 commit 7197b25
Show file tree
Hide file tree
Showing 20 changed files with 454 additions and 211 deletions.
74 changes: 64 additions & 10 deletions home_server/app.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#!/usr/bin/env python3.7

from datetime import datetime
from time import time

from flask import Flask, jsonify, request, abort

from db.db import init_app
from db.weather_station import store_weather_data
from db.queries import store_weather_data
from pages.index import index_page
from pages.weather_station.dashboard import dashboard_page
from pages.weather_station.single_page import single_page
from pages.weather_station.single_page import single_weather_page, single_wind_page, single_power_page
from pages.weather_station.single_data_page import single_data_page
from pages.weather_station.compare_page import compare_page
from pages.weather_station.send_data import send_data
Expand All @@ -18,7 +19,7 @@


@app.route('/api/v1/add_weather_data', methods=['POST'])
def store_in_db():
def store_weather_data():
if not request.json:
abort(400)
timestamp = str(datetime.now())
Expand All @@ -28,6 +29,30 @@ def store_in_db():
return jsonify({'data': db_data}), 201


@app.route('/api/v1/add_power_data', methods=['POST'])
def store_power_data():
if not request.json:
abort(400)
timestamp = str(datetime.now())
unix_timestamp = int(time())
data = request.json.get('data', "")[:2]
db_data = f'"{timestamp}", {unix_timestamp}, {data}'
store_weather_data(db_data)
return jsonify({'data': db_data}), 201


@app.route('/api/v1/add_wind_data', methods=['POST'])
def store_wind_data():
if not request.json:
abort(400)
timestamp = str(datetime.now())
unix_timestamp = int(time())
data = request.json.get('data', "")[:2]
db_data = f'"{timestamp}", {unix_timestamp}, {data}'
store_weather_data(db_data)
return jsonify({'data': db_data}), 201


@app.route('/send_data')
def send_weather_data():
return send_data()
Expand All @@ -44,25 +69,54 @@ def dashboard():
return dashboard_page()


@app.route('/single_page')
def single():
@app.route('/single_weather_page')
def single_weather_page_via_url():
period = request.args.get('period')
param = request.args.get('param')
return single_weather_page(param=param, period=period)


@app.route('/single_wind_page')
def single_wind_page_via_url():
period = request.args.get('period')
param = request.args.get('param')
return single_weather_page(param=param, period=period)


@app.route('/single_power_page')
def single_power_page_via_url():
period = request.args.get('period')
param = request.args.get('param')
return single_page(param=param, period=period)
return single_weather_page(param=param, period=period)


@app.route('/single_weather_page', methods=['POST'])
def single_weather_page_via_post():
period = request.form['period']
param = request.form['param']
return single_weather_page(param=param, period=period)


@app.route('/single_wind_page', methods=['POST'])
def single_wind_page_via_post():
period = request.form['period']
param = request.form['param']
return single_wind_page(param=param, period=period)


@app.route('/single_page', methods=['POST'])
def single_via_post():
@app.route('/single_power_page', methods=['POST'])
def single_power_page_via_post():
period = request.form['period']
param = request.form['param']
return single_page(param=param, period=period)
return single_power_page(param=param, period=period)


@app.route('/single_data_page')
def single_data():
table = request.args.get('table')
period = request.args.get('period')
param = request.args.get('param')
return single_data_page(param=param, period=period)
return single_data_page(table=table, param=param, period=period)


@app.route('/compare_page', methods=['POST'])
Expand Down
3 changes: 0 additions & 3 deletions home_server/db/power_data.py

This file was deleted.

101 changes: 101 additions & 0 deletions home_server/db/queries.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
from db.db import get_db, close_db

data_update_period = 5


def store_weather_data(data):
connection = get_db()
sql = f'''INSERT INTO weather_data(ts,meas_type,temperature,humidity,pressure,dew_point) VALUES({data});'''
cur = connection.cursor()
cur.execute(sql)
connection.commit()
close_db()
return cur.lastrowid


def store_wind_data(data):
connection = get_db()
sql = f'''INSERT INTO wind_data(ts,unix_ts,
avg_rps,max_rps,min_rps,
avg_ms,max_ms,min_ms,
avg_kmh,max_kmh,min_knh,
avg_knots,max_knots,min_knots,
heading,heading_abbr) VALUES({data});'''
cur = connection.cursor()
cur.execute(sql)
connection.commit()
close_db()
return cur.lastrowid


def store_power_data(data):
connection = get_db()
sql = f'''INSERT INTO wind_data(ts,unix_ts,avg_voltage,avg_current,avg_power,avg_consumption) VALUES({data});'''
cur = connection.cursor()
cur.execute(sql)
connection.commit()
close_db()
return cur.lastrowid


def get_one_measurement(table, param, offset, meas_type):
connection = get_db()
criteria = f'WHERE meas_type = \'{meas_type}\'' if meas_type else ''
sql = f''' SELECT {param} FROM {table} {criteria} ORDER BY RowId DESC LIMIT 2 OFFSET {offset}; '''
cur = connection.cursor()
cur.execute(sql)
row = cur.fetchone()
if row:
retval = dict(zip(row.keys(), row))[param]
else:
retval = 0
return retval


def get_last_measurement_pack(table, offset, meas_type=None):
connection = get_db()
criteria = f'WHERE meas_type = \'{meas_type}\'' if meas_type else ''
sql = f''' SELECT * FROM {table} {criteria} ORDER BY RowId DESC LIMIT 2 OFFSET {offset}; '''
cur = connection.cursor()
cur.execute(sql)
row = cur.fetchone()
if row:
retval = dict(zip(row.keys(), row))
else:
retval = 0
return retval


def get_one_last_average_measurement(table, param, period, meas_type=None):
connection = get_db()
criteria = f'WHERE meas_type = \'{meas_type}\'' if meas_type else ''
number = str(2 + (period / data_update_period))
sql = f''' avg({param}) from (SELECT {param} FROM {table} {criteria} ORDER BY RowId DESC LIMIT {number}); '''
cur = connection.cursor()
cur.execute(sql)
row = cur.fetchone()
if cur.rowcount > 0:
retval = dict(zip(row.keys(), row))[param]
else:
retval = 0
return retval


def get_last_series_measurement(table, param, period, meas_type=None):
connection = get_db()
criteria = f'WHERE meas_type = \'{meas_type}\'' if meas_type else ''
number = int(period / data_update_period)
sql = f''' SELECT ts, {param} FROM {table} {criteria} ORDER BY RowId DESC LIMIT {number}; '''
cur = connection.cursor()
cur.execute(sql)

columns = [column[0] for column in cur.description]
results = []
for row in cur.fetchall():
results.append(dict(zip(columns, row)))
x = []
y = []
for result in results:
x.append(result['ts'])
y.append(result[param])
return x, y
46 changes: 39 additions & 7 deletions home_server/db/schema.sql
Original file line number Diff line number Diff line change
@@ -1,11 +1,43 @@
DROP TABLE IF EXISTS weather_data;
DROP TABLE IF EXISTS wind_data;
DROP TABLE IF EXISTS power_data;

CREATE TABLE weather_data (
id INTEGER PRIMARY KEY AUTOINCREMENT,
ts TIMESTAMP NOT NULL,
meas_type INTEGER NOT NULL,
temperature DOUBLE,
humidity DOUBLE,
pressure DOUBLE,
dew_point DOUBLE
id INTEGER PRIMARY KEY AUTOINCREMENT,
ts TIMESTAMP NOT NULL,
meas_type INTEGER NOT NULL,
temperature DOUBLE,
humidity DOUBLE,
pressure DOUBLE,
dew_point DOUBLE
);

CREATE TABLE wind_data (
id INTEGER PRIMARY KEY AUTOINCREMENT,
ts TIMESTAMP NOT NULL,
unix_tx INTEGER NOT NULL,
avg_rps DOUBLE,
max_rps DOUBLE,
min_rps DOUBLE,
avg_ms DOUBLE,
max_ms DOUBLE,
min_ms DOUBLE,
avg_kmh DOUBLE,
max_kmh DOUBLE,
min_knh DOUBLE,
avg_knots DOUBLE,
max_knots DOUBLE,
min_knots DOUBLE,
heading DOUBLE,
heading_abbr VARCHAR
);

CREATE TABLE power_data (
id INTEGER PRIMARY KEY AUTOINCREMENT,
ts TIMESTAMP NOT NULL,
unix_tx INTEGER NOT NULL,
avg_voltage DOUBLE,
avg_current DOUBLE,
avg_power DOUBLE,
avg_consumption DOUBLE
);
76 changes: 0 additions & 76 deletions home_server/db/weather_station.py

This file was deleted.

3 changes: 0 additions & 3 deletions home_server/db/wind_station.py

This file was deleted.

6 changes: 3 additions & 3 deletions home_server/pages/weather_station/compare_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
from pages.weather_station.tools import generate_scatter


def compare_page(param1, param2, period):
def compare_page(table1, table2, param1, param2, period):
if param1 is None:
param1 = "temperature_in"
if param2 is None:
param2 = "temperature_in"
if period is None:
period = "day"
offline_fig1 = generate_scatter(param=param1, period=period, width=600, height=600)
offline_fig2 = generate_scatter(param=param2, period=period, width=600, height=600)
offline_fig1 = generate_scatter(table1=table1, param=param1, period=period, width=600, height=600)
offline_fig2 = generate_scatter(table2=table2, param=param2, period=period, width=600, height=600)
return render_template("weather_station/compare_page.html",
param1=param1,
param2=param2,
Expand Down
Loading

0 comments on commit 7197b25

Please sign in to comment.