From a8641d94a6e33e3f62af52c5af38ed378a0bdc9e Mon Sep 17 00:00:00 2001 From: simon Date: Fri, 4 Jun 2021 18:25:09 +0700 Subject: [PATCH] updating input function for new db layout --- backend/flask/app/db_connect.py | 13 +++++++------ postgres/setup.sql | 14 ++++++++++---- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/backend/flask/app/db_connect.py b/backend/flask/app/db_connect.py index 4ae4823..7a4984a 100644 --- a/backend/flask/app/db_connect.py +++ b/backend/flask/app/db_connect.py @@ -46,21 +46,22 @@ def db_insert(config, json_dict): weather_icon = json_dict['weather_icon'] wind_speed = json_dict['wind_speed'] wind_direction = json_dict['wind_direction'] + sensor_id = json_dict['sensor_id'] # connect conn, cur = db_connect(config) # insert aqi cur.execute("INSERT INTO aqi \ - (epoch_time, time_stamp, uptime, pm25, pm10, aqi_value, aqi_category) \ - VALUES (%s, %s, %s, %s, %s, %s, %s)", - (epoch_time, time_stamp, uptime, pm25, pm10, aqi_value, aqi_category) + (epoch_time, sensor_id, time_stamp, uptime, pm25, pm10, aqi_value, aqi_category) \ + VALUES (%s, %s, %s, %s, %s, %s, %s, %s)", + (epoch_time, sensor_id, time_stamp, uptime, pm25, pm10, aqi_value, aqi_category) ) # insert weather cur.execute("INSERT INTO weather \ - (epoch_time, time_stamp, temperature, pressure, humidity, \ + (epoch_time, sensor_id, time_stamp, temperature, pressure, humidity, \ wind_speed, wind_direction, weather_name, weather_icon) \ - VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)", - (epoch_time, time_stamp, temperature, pressure, humidity, + VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)", + (epoch_time, sensor_id, time_stamp, temperature, pressure, humidity, wind_speed, wind_direction, weather_name, weather_icon) ) diff --git a/postgres/setup.sql b/postgres/setup.sql index 55d34a5..c9e64d7 100644 --- a/postgres/setup.sql +++ b/postgres/setup.sql @@ -1,6 +1,8 @@ -- create aqi table CREATE TABLE aqi ( - epoch_time INT NOT NULL PRIMARY KEY, + id SERIAL NOT NULL PRIMARY KEY, + epoch_time INT NOT NULL, + sensor_id SMALLINT NOT NULL, time_stamp VARCHAR(20) NOT NULL, uptime INT NOT NULL, pm25 FLOAT4 NOT NULL, @@ -12,6 +14,7 @@ CREATE TABLE aqi ( -- example aqi insert INSERT INTO aqi ( epoch_time, + sensor_id, time_stamp, uptime, pm25, @@ -19,13 +22,15 @@ INSERT INTO aqi ( aqi_value, aqi_category ) VALUES ( - 1613648178, '2021-02-18 18:36:18', 206728, 20.4, 22.8, 67.0, 'Moderate' + 1613648178, 1, '2021-02-18 18:36:18', 206728, 20.4, 22.8, 67.0, 'Moderate' ); -- create weather table CREATE TABLE weather ( - epoch_time INT NOT NULL PRIMARY KEY, + id SERIAL NOT NULL PRIMARY KEY, + epoch_time INT NOT NULL, + sensor_id SMALLINT NOT NULL, time_stamp VARCHAR(20) NOT NULL, temperature FLOAT4 NOT NULL, pressure FLOAT4 NOT NULL, @@ -39,6 +44,7 @@ CREATE TABLE weather ( -- example weather insert INSERT INTO weather ( epoch_time, + sensor_id, time_stamp, temperature, pressure, @@ -48,6 +54,6 @@ INSERT INTO weather ( weather_name, weather_icon ) VALUES ( - 1613648178, '2021-02-18 18:36:18', 27.32, 982.41, + 1613648178, 1, '2021-02-18 18:36:18', 27.32, 982.41, 39.62598, 3.09, 40, 'Clouds', '04n' );