-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Converting weather station fastapi service.
- Loading branch information
Showing
2 changed files
with
41 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,80 @@ | ||
import os | ||
from contextlib import asynccontextmanager, suppress | ||
|
||
from fastapi import FastAPI | ||
from fastapi_utils.tasks import repeat_every | ||
from panoptes.utils.config.client import get_config | ||
from serial.tools.list_ports import comports as get_comports | ||
|
||
from panoptes.pocs.sensor.weather import WeatherStation | ||
|
||
app = FastAPI() | ||
weather_station: WeatherStation | ||
conf = get_config('environment.weather', {}) | ||
app_objects = {} | ||
|
||
|
||
@app.on_event('startup') | ||
async def startup(): | ||
global weather_station | ||
global conf | ||
|
||
print(f'Weather config: {conf}') | ||
|
||
@asynccontextmanager | ||
async def lifespan(app: FastAPI): | ||
"""Context manager for the lifespan of the app. | ||
This will connect to the weather station and record | ||
readings at a regular interval. | ||
""" | ||
conf = get_config('environment.weather', {}) | ||
app_objects['conf'] = conf | ||
|
||
# Get list of possible ports for auto-detect or use the configured port. | ||
if conf.get('auto_detect', False) is True: | ||
ports = [p.device for p in get_comports()] | ||
else: | ||
ports = [conf['serial_port']] | ||
|
||
|
||
# Check the ioptron symlink and skip that port if it exists. | ||
ioptron_port = None | ||
with suppress(FileNotFoundError): | ||
ioptron_port = os.readlink('/dev/ioptron') | ||
|
||
# Try to connect to the weather station. | ||
for port in ports: | ||
if 'ttyUSB' not in port: | ||
continue | ||
|
||
|
||
if port == ioptron_port: | ||
continue | ||
|
||
conf['serial_port'] = port | ||
try: | ||
weather_station = WeatherStation(**conf) | ||
weather_station.logger.info(f'Weather station setup: {weather_station}') | ||
app_objects['weather_station'] = weather_station | ||
break | ||
except Exception as e: | ||
print(f'Could not connect to weather station on {port}: {e}') | ||
else: | ||
raise RuntimeError('Could not connect to weather station.') | ||
|
||
yield | ||
print('Shutting down weather station') | ||
|
||
|
||
app = FastAPI(lifespan=lifespan) | ||
|
||
|
||
@app.on_event('startup') | ||
@repeat_every(seconds=conf.get('capture_delay', 60), wait_first=True) | ||
@repeat_every(seconds=60, wait_first=True) | ||
def record_readings(): | ||
"""Record the current readings in the db.""" | ||
global weather_station | ||
weather_station = app_objects['weather_station'] | ||
reading = weather_station.record() | ||
print(f'Recorded weather reading: {reading}') | ||
weather_station.logger.debug(f'Recorded weather reading: {reading}') | ||
return reading | ||
|
||
|
||
@app.get('/status') | ||
async def status(): | ||
"""Returns the power board status.""" | ||
global weather_station | ||
return weather_station.status | ||
return app_objects['weather_station'].status | ||
|
||
|
||
@app.get('/config') | ||
async def get_config(): | ||
"""Returns the power board status.""" | ||
global weather_station | ||
return weather_station.weather_station.config | ||
return app_objects['weather_station'] |