Merge branch 'master' of ssh://gitea.setlock.xyz:2222/shaun/garden
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -138,3 +138,5 @@ dmypy.json
|
|||||||
# Cython debug symbols
|
# Cython debug symbols
|
||||||
cython_debug/
|
cython_debug/
|
||||||
|
|
||||||
|
# Secrets
|
||||||
|
.creds*
|
||||||
|
|||||||
73
db_tools/db_writer.py
Normal file
73
db_tools/db_writer.py
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
#! env/bin/python3
|
||||||
|
|
||||||
|
# import the psycopg2 database adapter for PostgreSQL
|
||||||
|
import psycopg2
|
||||||
|
from psycopg2.extras import Json
|
||||||
|
import json
|
||||||
|
import sys
|
||||||
|
|
||||||
|
def connect_db(db: str,host: str,user: str,passwd: str):
|
||||||
|
try:
|
||||||
|
# declare a new PostgreSQL connection object
|
||||||
|
conn = psycopg2.connect(
|
||||||
|
dbname = db,
|
||||||
|
user = user,
|
||||||
|
host = host,
|
||||||
|
password = passwd,
|
||||||
|
# attempt to connect for 3 seconds then raise exception
|
||||||
|
connect_timeout = 3
|
||||||
|
)
|
||||||
|
|
||||||
|
except (Exception, psycopg2.Error) as err:
|
||||||
|
#print ("\npsycopg2 connect error:", err)
|
||||||
|
conn = None
|
||||||
|
return conn
|
||||||
|
|
||||||
|
def get_db_creds(file: str):
|
||||||
|
with open(file) as cred_file:
|
||||||
|
creds = json.load(cred_file)
|
||||||
|
return creds
|
||||||
|
|
||||||
|
def insert_data(conn, data):
|
||||||
|
|
||||||
|
# insert a new vendor into the vendors table
|
||||||
|
sql = """
|
||||||
|
INSERT INTO
|
||||||
|
air(datetime, temperature, humidity)
|
||||||
|
VALUES
|
||||||
|
(%s, %s, %s)
|
||||||
|
"""
|
||||||
|
|
||||||
|
try:
|
||||||
|
# open cursor on our db connection
|
||||||
|
cur = conn.cursor()
|
||||||
|
|
||||||
|
# execute the INSERT statement
|
||||||
|
data = (data["datetime"], data["temperature"], data["humidity"])
|
||||||
|
cur.execute(sql,data)
|
||||||
|
|
||||||
|
# commit the changes to the database
|
||||||
|
conn.commit()
|
||||||
|
|
||||||
|
# close communication with the database
|
||||||
|
cur.close()
|
||||||
|
|
||||||
|
except (Exception, psycopg2.DatabaseError) as error:
|
||||||
|
print(error)
|
||||||
|
|
||||||
|
finally:
|
||||||
|
if conn is not None:
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
data = {
|
||||||
|
"datetime": "2021-10-23 01:58:08.205911",
|
||||||
|
"temperature": "73.4",
|
||||||
|
"humidity": "49.2"
|
||||||
|
}
|
||||||
|
|
||||||
|
creds = get_db_creds("./.creds.json")
|
||||||
|
|
||||||
|
conn = connect_db(creds["db"], creds["host"], creds["user"], creds["passwd"])
|
||||||
|
|
||||||
|
insert_data(conn, data)
|
||||||
13
db_tools/requirements.txt
Normal file
13
db_tools/requirements.txt
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
black==21.9b0
|
||||||
|
click==8.0.3
|
||||||
|
importlib-metadata==4.8.1
|
||||||
|
mypy-extensions==0.4.3
|
||||||
|
pathspec==0.9.0
|
||||||
|
platformdirs==2.4.0
|
||||||
|
psycopg2-binary==2.9.1
|
||||||
|
regex==2021.10.8
|
||||||
|
RPi.GPIO==0.7.0
|
||||||
|
tomli==1.2.1
|
||||||
|
typed-ast==1.4.3
|
||||||
|
typing-extensions==3.10.0.2
|
||||||
|
zipp==3.6.0
|
||||||
Reference in New Issue
Block a user