Improved code structure
This commit is contained in:
parent
20f8f64f25
commit
feefd55e20
4 changed files with 24 additions and 27 deletions
51
src/main.py
51
src/main.py
|
@ -2,11 +2,11 @@ import os
|
||||||
from fastapi import FastAPI, HTTPException
|
from fastapi import FastAPI, HTTPException
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import requests
|
import requests
|
||||||
from history import get_recent_entries
|
from handler.history import get_recent_entries
|
||||||
from matrix import MatrixDisplay
|
from handler.matrix import MatrixDisplay
|
||||||
from fastapi.middleware.cors import CORSMiddleware
|
from fastapi.middleware.cors import CORSMiddleware
|
||||||
import asyncio
|
import asyncio
|
||||||
from climate import Dht22Sensor
|
from handler.climate import Dht22Sensor
|
||||||
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
|
|
||||||
|
@ -14,7 +14,7 @@ origins = [
|
||||||
"http://localhost",
|
"http://localhost",
|
||||||
"http://localhost:8000",
|
"http://localhost:8000",
|
||||||
"http://raspberrypi",
|
"http://raspberrypi",
|
||||||
"http://192.168.178.84"
|
"http://192.168.178.84",
|
||||||
]
|
]
|
||||||
|
|
||||||
app.add_middleware(
|
app.add_middleware(
|
||||||
|
@ -32,24 +32,28 @@ climate_log_file = "./climate.csv"
|
||||||
matrix_display = MatrixDisplay()
|
matrix_display = MatrixDisplay()
|
||||||
dht22_sensor = Dht22Sensor(dht22_pin)
|
dht22_sensor = Dht22Sensor(dht22_pin)
|
||||||
|
|
||||||
|
|
||||||
# Start background service to log temperature and humidity every minute
|
# Start background service to log temperature and humidity every minute
|
||||||
async def log_temperature():
|
async def log_temperature():
|
||||||
# If file does not exist, create it and write header
|
# If file does not exist, create it and write header
|
||||||
if not os.path.isfile(climate_log_file):
|
if not os.path.isfile(climate_log_file):
|
||||||
with open(climate_log_file, "w") as f:
|
with open(climate_log_file, "w") as f:
|
||||||
f.write("timestamp,temperature,humidity\n")
|
f.write("timestamp,temperature,humidity\n")
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
measurements = dht22_sensor.read()
|
measurements = dht22_sensor.read()
|
||||||
if measurements is not None:
|
if measurements is not None:
|
||||||
with open(climate_log_file, "a") as f:
|
with open(climate_log_file, "a") as f:
|
||||||
f.write("{},{},{}\n".format(
|
f.write(
|
||||||
datetime.now().isoformat(),
|
"{},{},{}\n".format(
|
||||||
measurements["temperature"],
|
datetime.now().isoformat(),
|
||||||
measurements["humidity"]
|
measurements["temperature"],
|
||||||
))
|
measurements["humidity"],
|
||||||
|
)
|
||||||
|
)
|
||||||
await asyncio.sleep(60)
|
await asyncio.sleep(60)
|
||||||
|
|
||||||
|
|
||||||
async def display_time():
|
async def display_time():
|
||||||
while should_run_time_loop:
|
while should_run_time_loop:
|
||||||
try:
|
try:
|
||||||
|
@ -64,12 +68,10 @@ async def display_time():
|
||||||
await asyncio.sleep(seconds_until_next_minute)
|
await asyncio.sleep(seconds_until_next_minute)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
asyncio.create_task(display_time())
|
asyncio.create_task(display_time())
|
||||||
asyncio.create_task(log_temperature())
|
asyncio.create_task(log_temperature())
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@app.post("/time")
|
@app.post("/time")
|
||||||
async def start_time_loop():
|
async def start_time_loop():
|
||||||
global should_run_time_loop
|
global should_run_time_loop
|
||||||
|
@ -86,25 +88,22 @@ async def turn_off():
|
||||||
return {"message": "Display turned off"}
|
return {"message": "Display turned off"}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@app.post("/temperature")
|
@app.post("/temperature")
|
||||||
async def temperature():
|
async def temperature():
|
||||||
measurements = dht22_sensor.get_last_read()
|
measurements = dht22_sensor.get_last_read()
|
||||||
if measurements is None:
|
if measurements is None:
|
||||||
return {"message": "Failed to read temperature"}
|
return {"message": "Failed to read temperature"}
|
||||||
|
|
||||||
|
|
||||||
global should_run_time_loop
|
global should_run_time_loop
|
||||||
was_clock_runnign = should_run_time_loop
|
was_clock_runnign = should_run_time_loop
|
||||||
should_run_time_loop = False
|
should_run_time_loop = False
|
||||||
|
|
||||||
|
|
||||||
matrix_display.show_text("{0:0.1f}*C".format(measurements["temperature"]))
|
matrix_display.show_text("{0:0.1f}*C".format(measurements["temperature"]))
|
||||||
|
|
||||||
if was_clock_runnign:
|
if was_clock_runnign:
|
||||||
should_run_time_loop = True
|
should_run_time_loop = True
|
||||||
asyncio.create_task(display_time())
|
asyncio.create_task(display_time())
|
||||||
|
|
||||||
return measurements
|
return measurements
|
||||||
|
|
||||||
|
|
||||||
|
@ -113,19 +112,17 @@ async def humidity():
|
||||||
measurements = dht22_sensor.get_last_read()
|
measurements = dht22_sensor.get_last_read()
|
||||||
if measurements is None:
|
if measurements is None:
|
||||||
return {"message": "Failed to read humidity"}
|
return {"message": "Failed to read humidity"}
|
||||||
|
|
||||||
|
|
||||||
global should_run_time_loop
|
global should_run_time_loop
|
||||||
was_clock_runnign = should_run_time_loop
|
was_clock_runnign = should_run_time_loop
|
||||||
should_run_time_loop = False
|
should_run_time_loop = False
|
||||||
|
|
||||||
|
|
||||||
matrix_display.show_text("{0:0.1f}%".format(measurements["humidity"]))
|
matrix_display.show_text("{0:0.1f}%".format(measurements["humidity"]))
|
||||||
|
|
||||||
if was_clock_runnign:
|
if was_clock_runnign:
|
||||||
should_run_time_loop = True
|
should_run_time_loop = True
|
||||||
asyncio.create_task(display_time())
|
asyncio.create_task(display_time())
|
||||||
|
|
||||||
return measurements
|
return measurements
|
||||||
|
|
||||||
|
|
||||||
|
@ -140,9 +137,9 @@ async def flash(count: int = 1):
|
||||||
global should_run_time_loop
|
global should_run_time_loop
|
||||||
was_clock_runnign = should_run_time_loop
|
was_clock_runnign = should_run_time_loop
|
||||||
should_run_time_loop = False
|
should_run_time_loop = False
|
||||||
|
|
||||||
matrix_display.flash(count)
|
matrix_display.flash(count)
|
||||||
|
|
||||||
if was_clock_runnign:
|
if was_clock_runnign:
|
||||||
should_run_time_loop = True
|
should_run_time_loop = True
|
||||||
asyncio.create_task(display_time())
|
asyncio.create_task(display_time())
|
||||||
|
|
Loading…
Reference in a new issue