Skip to content

Commit

Permalink
Merge pull request #8 from TimOrme/config_poll_interval
Browse files Browse the repository at this point in the history
Fix poll interval
  • Loading branch information
TimOrme authored Mar 22, 2023
2 parents 3c5b3b6 + 4194253 commit 704da06
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions aqimon/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,27 +86,32 @@ async def database_disconnect():


@app.on_event("startup")
@repeat_every(seconds=5)
async def read_from_device() -> None:
"""Background cron task to read from the device."""
config = get_config_from_env()
database = get_database(config)
reader = get_reader(config)

try:
result: AqiRead = await reader.read()
event_time = datetime.now()
epa_aqi_pm25 = aqi_common.calculate_epa_aqi(result.pmtwofive)
await add_entry(
dbconn=database,
event_time=event_time,
epa_aqi_pm25=epa_aqi_pm25,
raw_pm25=result.pmtwofive,
raw_pm10=result.pmten,
)
await clean_old(dbconn=database, retention_minutes=config.retention_minutes)
except Exception as e:
log.exception("Failed to retrieve data from reader", e)
async def read_function():
try:
result: AqiRead = await reader.read()
event_time = datetime.now()
epa_aqi_pm25 = aqi_common.calculate_epa_aqi(result.pmtwofive)
await add_entry(
dbconn=database,
event_time=event_time,
epa_aqi_pm25=epa_aqi_pm25,
raw_pm25=result.pmtwofive,
raw_pm10=result.pmten,
)
await clean_old(dbconn=database, retention_minutes=config.retention_minutes)
except Exception as e:
log.exception("Failed to retrieve data from reader", e)

# Note that we leverage the @repeat_every decorator here, but as a regular function call. This allows us to
# use a non-global config object to specify the poll frequency
repeater = repeat_every(seconds=config.poll_frequency_sec)
await repeater(read_function)()


def convert_all_to_view_dict(results):
Expand Down

0 comments on commit 704da06

Please sign in to comment.