2023-12-18 00:21:37 +01:00
|
|
|
import asyncio
|
2023-12-24 15:07:26 +01:00
|
|
|
import logging
|
2023-12-23 19:19:17 +01:00
|
|
|
from typing import Optional
|
2023-12-18 00:21:37 +01:00
|
|
|
|
2023-12-23 20:28:35 +01:00
|
|
|
from fastapi import FastAPI
|
2023-10-11 02:06:38 +02:00
|
|
|
from fastapi.middleware.cors import CORSMiddleware
|
2023-10-11 01:34:34 +02:00
|
|
|
|
2024-03-03 16:15:04 +01:00
|
|
|
from actions import (
|
|
|
|
climate_sensor,
|
|
|
|
display_time,
|
|
|
|
log_temperature,
|
|
|
|
matrix_display,
|
|
|
|
display_pattern,
|
|
|
|
)
|
2023-12-18 00:21:37 +01:00
|
|
|
from config import climate_log_file
|
|
|
|
from handler.action_queue import ActionQueue
|
|
|
|
from handler.history import get_recent_entries
|
|
|
|
|
2023-12-24 15:07:26 +01:00
|
|
|
logging.getLogger().setLevel(logging.INFO)
|
2023-12-23 18:57:55 +01:00
|
|
|
|
2024-03-03 18:44:03 +01:00
|
|
|
|
|
|
|
# Start services
|
|
|
|
asyncio.create_task(log_temperature())
|
2024-01-30 20:43:25 +01:00
|
|
|
queue = ActionQueue(matrix_display.show_current_time)
|
2023-10-11 01:34:34 +02:00
|
|
|
app = FastAPI()
|
|
|
|
|
2024-03-03 18:44:03 +01:00
|
|
|
|
2023-10-11 02:06:38 +02:00
|
|
|
origins = [
|
|
|
|
"http://localhost",
|
|
|
|
"http://localhost:8000",
|
|
|
|
"http://raspberrypi",
|
2023-12-22 16:32:08 +01:00
|
|
|
"http://192.168.178.84:8000",
|
2023-12-17 21:04:39 +01:00
|
|
|
"http://192.168.178.84",
|
2023-10-11 02:06:38 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
app.add_middleware(
|
|
|
|
CORSMiddleware,
|
|
|
|
allow_origins=origins,
|
|
|
|
allow_credentials=True,
|
|
|
|
allow_methods=["*"],
|
|
|
|
allow_headers=["*"],
|
|
|
|
)
|
|
|
|
|
2023-10-11 01:48:50 +02:00
|
|
|
|
2023-10-11 01:34:34 +02:00
|
|
|
@app.post("/time")
|
|
|
|
async def start_time_loop():
|
2023-12-18 00:21:37 +01:00
|
|
|
await queue.set_idle_action(display_time)
|
2023-10-11 01:34:34 +02:00
|
|
|
return {"message": "Time loop started"}
|
|
|
|
|
|
|
|
|
2023-12-23 19:19:17 +01:00
|
|
|
@app.post("/full")
|
|
|
|
async def turn_full():
|
|
|
|
await queue.set_idle_action(matrix_display.turn_full)
|
|
|
|
return {"message": "Full screen turned on"}
|
|
|
|
|
|
|
|
|
2023-10-11 01:36:27 +02:00
|
|
|
@app.post("/off")
|
|
|
|
async def turn_off():
|
2023-12-18 00:21:37 +01:00
|
|
|
await queue.set_idle_action(matrix_display.turn_off)
|
2023-10-11 01:36:27 +02:00
|
|
|
return {"message": "Display turned off"}
|
|
|
|
|
|
|
|
|
2023-10-17 00:10:23 +02:00
|
|
|
@app.post("/temperature")
|
|
|
|
async def temperature():
|
2023-12-24 15:07:26 +01:00
|
|
|
measurements = climate_sensor.get_last_read()
|
2023-10-17 00:10:23 +02:00
|
|
|
if measurements is None:
|
|
|
|
return {"message": "Failed to read temperature"}
|
2023-12-17 21:04:39 +01:00
|
|
|
|
2023-12-18 00:21:37 +01:00
|
|
|
await queue.add_action_to_queue(
|
|
|
|
matrix_display.show_text, "{0:0.1f}*C".format(measurements["temperature"])
|
|
|
|
)
|
2023-12-17 21:04:39 +01:00
|
|
|
|
2023-10-17 00:10:23 +02:00
|
|
|
return measurements
|
|
|
|
|
|
|
|
|
|
|
|
@app.post("/humidity")
|
|
|
|
async def humidity():
|
2023-12-24 15:07:26 +01:00
|
|
|
measurements = climate_sensor.get_last_read()
|
2023-10-17 00:10:23 +02:00
|
|
|
if measurements is None:
|
|
|
|
return {"message": "Failed to read humidity"}
|
2023-12-17 21:04:39 +01:00
|
|
|
|
2023-12-18 00:21:37 +01:00
|
|
|
await queue.add_action_to_queue(
|
|
|
|
matrix_display.show_text, "{0:0.1f}%".format(measurements["humidity"])
|
|
|
|
)
|
2023-12-17 21:04:39 +01:00
|
|
|
|
2023-10-17 00:10:23 +02:00
|
|
|
return measurements
|
|
|
|
|
|
|
|
|
2023-10-25 17:44:45 +02:00
|
|
|
@app.post("/history")
|
|
|
|
async def history():
|
|
|
|
day_entry_count = 24 * 60
|
2023-10-25 17:45:45 +02:00
|
|
|
return get_recent_entries(climate_log_file, day_entry_count)
|
2023-10-25 17:44:45 +02:00
|
|
|
|
|
|
|
|
2023-10-11 01:36:27 +02:00
|
|
|
@app.post("/flash")
|
2023-12-23 20:28:35 +01:00
|
|
|
async def flash(count: int = 1, contrast: Optional[int] = None):
|
|
|
|
await queue.add_action_to_queue(
|
|
|
|
matrix_display.flash, count=count, contrast=contrast
|
|
|
|
)
|
2023-10-11 01:36:27 +02:00
|
|
|
return {"message": "Display flashed"}
|
|
|
|
|
|
|
|
|
2024-03-03 15:58:12 +01:00
|
|
|
@app.post("/pattern")
|
2024-03-03 18:57:53 +01:00
|
|
|
async def flash(pattern: str = "01", step_ms: int = 500):
|
2024-03-03 17:32:10 +01:00
|
|
|
await queue.set_idle_action(display_pattern, pattern=pattern, step_ms=step_ms)
|
2024-03-03 15:58:12 +01:00
|
|
|
return {"message": "Activated pattern."}
|
|
|
|
|
|
|
|
|
2023-12-23 19:19:17 +01:00
|
|
|
@app.post("/contrast")
|
|
|
|
async def contrast(contrast: Optional[int] = None):
|
|
|
|
if contrast:
|
|
|
|
matrix_display.set_contrast(contrast)
|
|
|
|
|
|
|
|
return {"contrast": matrix_display.contrast}
|
|
|
|
|
|
|
|
|
2023-10-11 01:34:34 +02:00
|
|
|
@app.post("/message")
|
|
|
|
async def display_message(body: dict):
|
|
|
|
message_text = body.get("message")
|
2023-12-18 00:21:37 +01:00
|
|
|
await queue.add_action_to_queue(matrix_display.show_text, message_text)
|
2023-10-11 01:34:34 +02:00
|
|
|
return {"message": "Message displayed"}
|