Skip to content

Commit

Permalink
Converting weather station fastapi service.
Browse files Browse the repository at this point in the history
  • Loading branch information
wtgee committed May 24, 2024
1 parent ff72ac5 commit aae84ac
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 20 deletions.
5 changes: 4 additions & 1 deletion src/panoptes/pocs/utils/service/power.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ class RelayCommand(BaseModel):

@asynccontextmanager
async def lifespan(app: FastAPI):
"""Context manager for the lifespan of the app."""
"""Context manager for the lifespan of the app.
This will connect to the power board and record readings at a regular interval.
"""
conf: dict = get_config('environment.power', {})
power_board = PowerBoard(**conf)
power_board.logger.info(f'Power board setup: {power_board}')
Expand Down
56 changes: 37 additions & 19 deletions src/panoptes/pocs/utils/service/weather.py
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']

0 comments on commit aae84ac

Please sign in to comment.