-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- wind_data added - power data added - some refactoring
- Loading branch information
1 parent
d3e6985
commit 7197b25
Showing
20 changed files
with
454 additions
and
211 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.