4 changed files with 107 additions and 0 deletions
@ -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) |
@ -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) |
@ -0,0 +1,30 @@ |
|||||
|
<!DOCTYPE html> |
||||
|
<html lang="en"> |
||||
|
<head> |
||||
|
<meta charset="UTF-8"> |
||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0"> |
||||
|
<title>Document</title> |
||||
|
</head> |
||||
|
<body> |
||||
|
|
||||
|
<h1>VYPIST HODNOT</h1> |
||||
|
<table> |
||||
|
<thead> |
||||
|
<tr> |
||||
|
<th>cas</th> |
||||
|
<th>teplota</th> |
||||
|
<th>vlhkost</th> |
||||
|
</tr> |
||||
|
</thead> |
||||
|
<tbody> |
||||
|
{% for row in data %} |
||||
|
<tr> |
||||
|
<td> {{ row["timestamp"] }}</td> |
||||
|
<td> {{ row["temperature"] }}</td> |
||||
|
<td> {{ row["humidity"] }}</td> |
||||
|
</tr> |
||||
|
{% endfor %} |
||||
|
</tbody> |
||||
|
</table> |
||||
|
</body> |
||||
|
</html> |
@ -0,0 +1,4 @@ |
|||||
|
Echo zapnuti serveru; |
||||
|
.venv\Scripts\activate; |
||||
|
python server.py; |
||||
|
pause; |
Loading…
Reference in new issue