diff --git a/15_dhtsim_usb/python_sqlite/run_all.py b/15_dhtsim_usb/python_sqlite/run_all.py new file mode 100644 index 0000000..5939ef9 --- /dev/null +++ b/15_dhtsim_usb/python_sqlite/run_all.py @@ -0,0 +1,27 @@ +import subprocess +import sys +import os +import time + +SCRIPT_CAPTURE = "capture.py" +SCRIPT_SERVER = "server.py" + +def run_script_in_new_cmd(script_name): + try: + print(f"zkusim spustit {script_name}") + process = subprocess.Popen( + ["cmd", "/c", "start", "cmd", "/k", sys.executable, script_name], + creationflags=subprocess.CREATE_NEW_CONSOLE + ) + print(f"{script_name} by měl běžet v novém okně") + return process + except FileExistsError: + print(f"{script_name}404") + except Exception as e: + print(f"Nastala chyba: {e}") + +if __name__ == "__main__": + print("zkusim zapnout odposlech a server") + capture_process = run_script_in_new_cmd(SCRIPT_CAPTURE) + time.sleep(2) + server_process = run_script_in_new_cmd(SCRIPT_SERVER) diff --git a/15_dhtsim_usb/python_sqlite/server.py b/15_dhtsim_usb/python_sqlite/server.py new file mode 100644 index 0000000..02fba9f --- /dev/null +++ b/15_dhtsim_usb/python_sqlite/server.py @@ -0,0 +1,46 @@ +from flask import Flask, render_template, g +import sqlite3 +import os + +DB_FILE = "dht_data.sqlite" +TABLE_NAME = "readings" +BASE_DIR = os.path.dirname(os.path.abspath(__file__)) +DATABASE_PATH = os.path.join(BASE_DIR,DB_FILE) + +app = Flask(__name__) +app.config["DATABASE"] = DATABASE_PATH + +def get_db(): + if "db" not in g: + try: + g.db = sqlite3.connect( + app.config["DATABASE"], + detect_types=sqlite3.PARSE_DECLTYPES + ) + g.db.row_factory = sqlite3.Row + except sqlite3.Error as e: + print(f"Chyba při pripojeni db: {e}") + return g.db + +@app.teardown_appcontext +def close_db(): + db = g.pop("db",None) + if db is not None: + db.close() + +@app.route("/") +def index(): + data = [] + try: + db = get_db() + cursor = db.cursor() + cursor.execute(f"SELECT timestamp, temperature, humidity FROM {TABLE_NAME} ORDER BY timestamp DESC") + data = cursor.fetchall() + except sqlite3.Error as e: + print(f"Chyba při čtení z db: {e}") + return render_template("index.html", data=data) + +if __name__ == "__main__": + print(f"DB se očekává zde: {app.config['DATABASE']}") + print(f"Server bude dostupný na http://localhost:5000") + app.run(debug=True, host="0.0.0.0", port=5000) \ No newline at end of file diff --git a/15_dhtsim_usb/python_sqlite/templates/index.html b/15_dhtsim_usb/python_sqlite/templates/index.html new file mode 100644 index 0000000..3d7796c --- /dev/null +++ b/15_dhtsim_usb/python_sqlite/templates/index.html @@ -0,0 +1,30 @@ + + + + + + Document + + + +

VYPIST HODNOT

+ + + + + + + + + + {% for row in data %} + + + + + + {% endfor %} + +
casteplotavlhkost
{{ row["timestamp"] }} {{ row["temperature"] }} {{ row["humidity"] }}
+ + \ No newline at end of file diff --git a/15_dhtsim_usb/python_sqlite/win_zapnuti_webseveru.bat b/15_dhtsim_usb/python_sqlite/win_zapnuti_webseveru.bat new file mode 100644 index 0000000..37928df --- /dev/null +++ b/15_dhtsim_usb/python_sqlite/win_zapnuti_webseveru.bat @@ -0,0 +1,4 @@ +Echo zapnuti serveru; +.venv\Scripts\activate; +python server.py; +pause; \ No newline at end of file