Added temperature reading and saving to db.
This commit is contained in:
parent
6d282b6e83
commit
1e5bc7ba47
20
main.py
20
main.py
|
@ -1,20 +1,40 @@
|
|||
import schedule
|
||||
from typing import List
|
||||
|
||||
import uvicorn
|
||||
from fastapi import Depends, FastAPI, HTTPException, status
|
||||
from fastapi.security import OAuth2PasswordRequestForm
|
||||
from fastapi_utils.tasks import repeat_every
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from secure.authenticate import oauth2_scheme, get_token
|
||||
import config
|
||||
from db import crud, models, schemas
|
||||
from db.database import engine, get_db
|
||||
from readings.scheduler import clear_temp_task, init_temp_task
|
||||
|
||||
|
||||
models.Base.metadata.create_all(bind=engine)
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
@app.on_event('startup')
|
||||
def startup_event():
|
||||
init_temp_task()
|
||||
schedule.run_all()
|
||||
|
||||
|
||||
@app.on_event('startup')
|
||||
@repeat_every(seconds=60)
|
||||
def run_pending():
|
||||
schedule.run_pending()
|
||||
|
||||
|
||||
@app.on_event('shutdown')
|
||||
def close_event():
|
||||
clear_temp_task()
|
||||
|
||||
|
||||
@app.post('/token')
|
||||
def login(form_data: OAuth2PasswordRequestForm = Depends(),
|
||||
db: Session = Depends(get_db)):
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
import schedule
|
||||
|
||||
from .temperature import save_temperature
|
||||
|
||||
|
||||
def init_temp_task():
|
||||
schedule.every(5).minutes.do(save_temperature).tag('5_min_temp')
|
||||
|
||||
|
||||
def clear_temp_task():
|
||||
schedule.clear('5_min_temp')
|
|
@ -0,0 +1,51 @@
|
|||
import time
|
||||
import json
|
||||
import os
|
||||
import subprocess
|
||||
|
||||
from db.crud import create_owner_reading, get_owner_by_name
|
||||
from db.database import get_db
|
||||
from db.schemas import ReadingIn
|
||||
|
||||
commands = (
|
||||
# Manjaro
|
||||
# ['sensors', '-j'],
|
||||
|
||||
# Pi
|
||||
# ['vcgencmd', 'measure_temp'],
|
||||
['cat', '/sys/class/thermal/thermal_zone0/temp'],
|
||||
)
|
||||
db = next(get_db())
|
||||
|
||||
|
||||
def read_temperature() -> float:
|
||||
temp = None
|
||||
for command in commands:
|
||||
# print(f'{command=}')
|
||||
try:
|
||||
proc = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)
|
||||
stdout = proc.stdout.decode('utf-8')
|
||||
# print(f'{stdout=}')
|
||||
# stderr = proc.stderr.decode('utf-8')
|
||||
# print(f'{stderr=}')
|
||||
# output = json.loads(stdout)
|
||||
# temp = round(output['nvme-pci-0500']['Composite']['temp1_input'], 1)
|
||||
temp = round(float(stdout) / 1000, 1)
|
||||
except (FileNotFoundError, ValueError):
|
||||
pass # print('no command found:', command)
|
||||
except json.decoder.JSONDecodeError as e:
|
||||
pass # print(f'JSON error: {e}')
|
||||
if temp:
|
||||
return temp
|
||||
else:
|
||||
raise Exception('Couldn\'t get temperature.')
|
||||
|
||||
|
||||
def save_temperature():
|
||||
host = os.uname().nodename
|
||||
owner = get_owner_by_name(db, host)
|
||||
if not owner:
|
||||
raise Exception(f'Couldn\'t find owner {host}')
|
||||
now = time.strftime('%Y-%m-%d %H:%M:%S')
|
||||
reading = ReadingIn(timestamp=now, temperature=read_temperature())
|
||||
create_owner_reading(db, reading, owner.id)
|
|
@ -1,6 +1,11 @@
|
|||
uvicorn~=0.15.0
|
||||
fastapi~=0.70.0
|
||||
fastapi-utils~=0.2.1
|
||||
python-dotenv~=0.19.2
|
||||
SQLAlchemy~=1.4.27
|
||||
pydantic~=1.8.2
|
||||
passlib~=1.7.4
|
||||
passlib~=1.7.4
|
||||
python-jose~=3.3.0
|
||||
python-multipart~=0.0.5
|
||||
bcrypt~=3.2.0
|
||||
schedule~=1.1.0
|
Loading…
Reference in New Issue