update
This commit is contained in:
85
sqlite/create_db.py
Normal file
85
sqlite/create_db.py
Normal file
@@ -0,0 +1,85 @@
|
||||
'''
|
||||
____ ___ _ _ _
|
||||
/ ___| / _ \| | (_) |_ ___
|
||||
\___ \| | | | | | | __/ _ \
|
||||
___) | |_| | |___| | || __/
|
||||
|____/ \__\_\_____|_|\__\___|
|
||||
|
||||
Script to create a sqlite database
|
||||
/usr/bin/python3 /var/www/moduleair_pro_4g/sqlite/create_db.py
|
||||
|
||||
in case of readonly error:
|
||||
sudo chmod 777 /var/www/moduleair_pro_4g/sqlite/sensors.db
|
||||
'''
|
||||
|
||||
import sqlite3
|
||||
|
||||
# Connect to (or create if not existent) the database
|
||||
conn = sqlite3.connect("/var/www/moduleair_pro_4g/sqlite/sensors.db")
|
||||
cursor = conn.cursor()
|
||||
|
||||
# Create a table timer
|
||||
cursor.execute("""
|
||||
CREATE TABLE IF NOT EXISTS timestamp_table (
|
||||
id INTEGER PRIMARY KEY CHECK (id = 1), -- Enforce single row by using fixed ID
|
||||
last_updated DATETIME NOT NULL
|
||||
)
|
||||
""")
|
||||
cursor.execute("""
|
||||
INSERT INTO timestamp_table (id, last_updated) VALUES (1, CURRENT_TIMESTAMP);
|
||||
""")
|
||||
|
||||
|
||||
# Create a table NPM
|
||||
cursor.execute("""
|
||||
CREATE TABLE IF NOT EXISTS data_NPM (
|
||||
timestamp TEXT,
|
||||
PM1 REAL,
|
||||
PM25 REAL,
|
||||
PM10 REAL,
|
||||
temp_npm REAL,
|
||||
hum_npm REAL
|
||||
)
|
||||
""")
|
||||
|
||||
# Create a table BME280
|
||||
cursor.execute("""
|
||||
CREATE TABLE IF NOT EXISTS data_BME280 (
|
||||
timestamp TEXT,
|
||||
temperature REAL,
|
||||
humidity REAL,
|
||||
pressure REAL
|
||||
)
|
||||
""")
|
||||
|
||||
# Create a table cairsens
|
||||
cursor.execute("""
|
||||
CREATE TABLE IF NOT EXISTS data_envea (
|
||||
timestamp TEXT,
|
||||
no2 REAL,
|
||||
h2s REAL,
|
||||
nh3 REAL,
|
||||
co REAL,
|
||||
o3 REAL
|
||||
)
|
||||
""")
|
||||
|
||||
# Create a table NPM_5ch
|
||||
cursor.execute("""
|
||||
CREATE TABLE IF NOT EXISTS data_NPM_5channels (
|
||||
timestamp TEXT,
|
||||
PM_ch1 INTEGER,
|
||||
PM_ch2 INTEGER,
|
||||
PM_ch3 INTEGER,
|
||||
PM_ch4 INTEGER,
|
||||
PM_ch5 INTEGER
|
||||
)
|
||||
""")
|
||||
|
||||
|
||||
|
||||
# Commit and close the connection
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
print("Database and table created successfully!")
|
||||
71
sqlite/flush_old_data.py
Normal file
71
sqlite/flush_old_data.py
Normal file
@@ -0,0 +1,71 @@
|
||||
'''
|
||||
____ ___ _ _ _
|
||||
/ ___| / _ \| | (_) |_ ___
|
||||
\___ \| | | | | | | __/ _ \
|
||||
___) | |_| | |___| | || __/
|
||||
|____/ \__\_\_____|_|\__\___|
|
||||
|
||||
Script to flush (delete) data from a sqlite database
|
||||
|
||||
/usr/bin/python3 /var/www/moduleair_pro_4g/sqlite/flush_old_data.py
|
||||
|
||||
Available table are
|
||||
|
||||
data_NPM
|
||||
data_NPM_5channels
|
||||
data_BME280
|
||||
data_envea
|
||||
timestamp_table
|
||||
|
||||
'''
|
||||
|
||||
import sqlite3
|
||||
import datetime
|
||||
|
||||
|
||||
# Connect to the SQLite database
|
||||
conn = sqlite3.connect("/var/www/moduleair_pro_4g/sqlite/sensors.db")
|
||||
cursor = conn.cursor()
|
||||
|
||||
#GET RTC TIME from SQlite
|
||||
cursor.execute("SELECT * FROM timestamp_table LIMIT 1")
|
||||
row = cursor.fetchone() # Get the first (and only) row
|
||||
|
||||
if row:
|
||||
rtc_time_str = row[1] # Assuming timestamp is stored as TEXT (YYYY-MM-DD HH:MM:SS)
|
||||
print(f"[INFO] Last recorded timestamp: {rtc_time_str}")
|
||||
|
||||
# Convert last_updated to a datetime object
|
||||
last_updated = datetime.datetime.strptime(rtc_time_str, "%Y-%m-%d %H:%M:%S")
|
||||
|
||||
# Calculate the cutoff date (3 months before last_updated)
|
||||
cutoff_date = last_updated - datetime.timedelta(days=60)
|
||||
cutoff_date_str = cutoff_date.strftime("%Y-%m-%d %H:%M:%S")
|
||||
|
||||
print(f"[INFO] Deleting records older than: {cutoff_date_str}")
|
||||
|
||||
# List of tables to delete old data from
|
||||
tables_to_clean = ["data_NPM", "data_NPM_5channels", "data_BME280", "data_envea"]
|
||||
|
||||
# Loop through each table and delete old data
|
||||
for table in tables_to_clean:
|
||||
delete_query = f"DELETE FROM {table} WHERE timestamp < ?"
|
||||
cursor.execute(delete_query, (cutoff_date_str,))
|
||||
print(f"[INFO] Deleted old records from {table}")
|
||||
|
||||
# **Commit changes before running VACUUM**
|
||||
conn.commit()
|
||||
print("[INFO] Changes committed successfully!")
|
||||
|
||||
# Now it's safe to run VACUUM
|
||||
print("[INFO] Running VACUUM to optimize database space...")
|
||||
cursor.execute("VACUUM")
|
||||
|
||||
print("[SUCCESS] Old data flushed successfully!")
|
||||
|
||||
else:
|
||||
print("[ERROR] No timestamp found in timestamp_table.")
|
||||
|
||||
|
||||
# Close the database connection
|
||||
conn.close()
|
||||
52
sqlite/read.py
Normal file
52
sqlite/read.py
Normal file
@@ -0,0 +1,52 @@
|
||||
'''
|
||||
____ ___ _ _ _
|
||||
/ ___| / _ \| | (_) |_ ___
|
||||
\___ \| | | | | | | __/ _ \
|
||||
___) | |_| | |___| | || __/
|
||||
|____/ \__\_\_____|_|\__\___|
|
||||
|
||||
Script to read data from a sqlite database
|
||||
/usr/bin/python3 /var/www/moduleair_pro_4g/sqlite/read.py data_NPM 10
|
||||
|
||||
Available table are
|
||||
data_NPM
|
||||
data_NPM_5channels
|
||||
data_BME280
|
||||
data_envea
|
||||
timestamp_table
|
||||
|
||||
'''
|
||||
|
||||
import sqlite3
|
||||
import sys
|
||||
parameter = sys.argv[1:] # Exclude the script name
|
||||
#print("Parameters received:")
|
||||
table_name=parameter[0]
|
||||
limit_num=parameter[1]
|
||||
|
||||
# Connect to the SQLite database
|
||||
conn = sqlite3.connect("/var/www/moduleair_pro_4g/sqlite/sensors.db")
|
||||
cursor = conn.cursor()
|
||||
|
||||
# Retrieve the last 10 sensor readings
|
||||
#cursor.execute("SELECT * FROM data_NPM ORDER BY timestamp DESC LIMIT 10")
|
||||
#cursor.execute("SELECT * FROM data_BME280 ORDER BY timestamp DESC LIMIT 10")
|
||||
#cursor.execute("SELECT * FROM timestamp_table")
|
||||
if table_name == "timestamp_table":
|
||||
cursor.execute("SELECT * FROM timestamp_table")
|
||||
|
||||
else:
|
||||
query = f"SELECT * FROM {table_name} ORDER BY timestamp DESC LIMIT ?"
|
||||
cursor.execute(query, (limit_num,))
|
||||
|
||||
|
||||
rows = cursor.fetchall()
|
||||
rows.reverse() # Reverse the order in Python (to get ascending order)
|
||||
|
||||
|
||||
# Display the results
|
||||
for row in rows:
|
||||
print(row)
|
||||
|
||||
# Close the database connection
|
||||
conn.close()
|
||||
59
sqlite/read_select_date.py
Normal file
59
sqlite/read_select_date.py
Normal file
@@ -0,0 +1,59 @@
|
||||
'''
|
||||
____ ___ _ _ _
|
||||
/ ___| / _ \| | (_) |_ ___
|
||||
\___ \| | | | | | | __/ _ \
|
||||
___) | |_| | |___| | || __/
|
||||
|____/ \__\_\_____|_|\__\___|
|
||||
|
||||
Script to read data from a sqlite database using start date and end date
|
||||
|
||||
/usr/bin/python3 /var/www/moduleair_pro_4g/sqlite/read_select_date.py data_NPM 2025-02-09 2025-02-11
|
||||
|
||||
Available table are
|
||||
data_NPM
|
||||
data_NPM_5channels
|
||||
data_BME280
|
||||
data_envea
|
||||
timestamp_table
|
||||
|
||||
'''
|
||||
|
||||
import sqlite3
|
||||
import sys
|
||||
|
||||
parameter = sys.argv[1:] # Exclude the script name
|
||||
#print("Parameters received:")
|
||||
table_name=parameter[0]
|
||||
start_date=parameter[1]
|
||||
end_date=parameter[2]
|
||||
|
||||
# Convert to full timestamp range
|
||||
start_timestamp = f"{start_date} 00:00:00"
|
||||
end_timestamp = f"{end_date} 23:59:59"
|
||||
|
||||
# Connect to the SQLite database
|
||||
conn = sqlite3.connect("/var/www/moduleair_pro_4g/sqlite/sensors.db")
|
||||
cursor = conn.cursor()
|
||||
|
||||
# Retrieve the last 10 sensor readings
|
||||
#cursor.execute("SELECT * FROM data_NPM ORDER BY timestamp DESC LIMIT 10")
|
||||
#cursor.execute("SELECT * FROM data_BME280 ORDER BY timestamp DESC LIMIT 10")
|
||||
#cursor.execute("SELECT * FROM timestamp_table")
|
||||
if table_name == "timestamp_table":
|
||||
cursor.execute("SELECT * FROM timestamp_table")
|
||||
|
||||
else:
|
||||
query = f"SELECT * FROM {table_name} WHERE timestamp BETWEEN ? AND ? ORDER BY timestamp ASC"
|
||||
cursor.execute(query, (start_timestamp, end_timestamp))
|
||||
|
||||
|
||||
rows = cursor.fetchall()
|
||||
rows.reverse() # Reverse the order in Python (to get ascending order)
|
||||
|
||||
|
||||
# Display the results
|
||||
for row in rows:
|
||||
print(row)
|
||||
|
||||
# Close the database connection
|
||||
conn.close()
|
||||
33
sqlite/write.py
Normal file
33
sqlite/write.py
Normal file
@@ -0,0 +1,33 @@
|
||||
'''
|
||||
____ ___ _ _ _
|
||||
/ ___| / _ \| | (_) |_ ___
|
||||
\___ \| | | | | | | __/ _ \
|
||||
___) | |_| | |___| | || __/
|
||||
|____/ \__\_\_____|_|\__\___|
|
||||
|
||||
Script to write data to a sqlite database
|
||||
/usr/bin/python3 /var/www/moduleair_pro_4g/sqlite/write.py
|
||||
|
||||
'''
|
||||
|
||||
import sqlite3
|
||||
|
||||
# Connect to the SQLite database
|
||||
conn = sqlite3.connect("/var/www/moduleair_pro_4g/sqlite/sensors.db")
|
||||
cursor = conn.cursor()
|
||||
|
||||
# Insert a sample temperature reading
|
||||
timestamp = "2025-10-11"
|
||||
PM1 = 25.3
|
||||
PM25 = 18.3
|
||||
PM10 = 9.3
|
||||
|
||||
cursor.execute('''
|
||||
INSERT INTO data (timestamp, PM1, PM25, PM10) VALUES (?,?,?,?,?)'''
|
||||
, (timestamp,PM1,PM25,PM10))
|
||||
|
||||
# Commit and close the connection
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
print("Sensor data saved successfully!")
|
||||
Reference in New Issue
Block a user