make the mqtt feeder a timer since we're getting memory leaks or something

This commit is contained in:
Cyberes 2024-12-05 15:52:18 -07:00
parent 765f04978f
commit 86626495c2
5 changed files with 24 additions and 44 deletions

View File

@ -1,13 +1,12 @@
import gc import gc
import logging import logging
import numpy as np
import os import os
import paho.mqtt.client as mqtt
import pickle import pickle
import sys import sys
import time import time
from datetime import datetime, timezone from datetime import datetime, timezone
import numpy as np
import paho.mqtt.client as mqtt
from dateutil.parser import parse from dateutil.parser import parse
from redis import Redis from redis import Redis
@ -56,32 +55,21 @@ def publish(topic: str, msg):
def main(): def main():
redis = Redis(host='localhost', port=6379, db=0) redis = Redis(host='localhost', port=6379, db=0)
data = redis.get('glotec')
while True: while data is None:
logging.warning('Redis has not been populated yet. Is cache.py running? Sleeping 10s...')
time.sleep(10)
data = redis.get('glotec') data = redis.get('glotec')
while data is None: geojson = pickle.loads(data)
logging.warning('Redis has not been populated yet. Is cache.py running? Sleeping 10s...')
time.sleep(10)
data = redis.get('glotec')
geojson = pickle.loads(data)
utc_hr = datetime.now(timezone.utc).hour utc_hr = datetime.now(timezone.utc).hour
logging.info(f'Using hour {utc_hr}') logging.info(f'Using hour {utc_hr}')
glotec_map_ranged, _ = plot_glotec_map(geojson, [float(LON_RANGE_MIN), float(LON_RANGE_MAX)], [float(LAT_RANGE_MIN), float(LAT_RANGE_MAX)]) glotec_map_ranged, _ = plot_glotec_map(geojson, [float(LON_RANGE_MIN), float(LON_RANGE_MAX)], [float(LAT_RANGE_MIN), float(LAT_RANGE_MAX)])
avg_tec = np.mean(glotec_map_ranged) avg_tec = np.mean(glotec_map_ranged)
logging.info(f'Data timestamp: {parse(geojson["time_tag"]).isoformat()}') logging.info(f'Data timestamp: {parse(geojson["time_tag"]).isoformat()}')
latest = round(avg_tec, 1) latest = round(avg_tec, 1)
publish('glotec', latest) publish('glotec', latest)
del data
del geojson
del glotec_map_ranged
del avg_tec
del latest
gc.collect()
time.sleep(60)
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -1,15 +0,0 @@
#!/bin/bash
SOURCE=${BASH_SOURCE[0]}
while [ -L "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
DIR=$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )
SOURCE=$(readlink "$SOURCE")
[[ $SOURCE != /* ]] && SOURCE=$DIR/$SOURCE # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
DIR=$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )
cp "$DIR"/*.service /etc/systemd/system
systemctl daemon-reload
systemctl enable --now *.service
systemctl restart *.service
systemctl status *.service

View File

@ -8,8 +8,7 @@ User=homeassistant
EnvironmentFile=/etc/secrets/space-weather EnvironmentFile=/etc/secrets/space-weather
ExecStart=/srv/ha-noaa-space-weather/venv/bin/python /srv/ha-noaa-space-weather/feeder/cache.py ExecStart=/srv/ha-noaa-space-weather/venv/bin/python /srv/ha-noaa-space-weather/feeder/cache.py
SyslogIdentifier=space-weather-cache SyslogIdentifier=space-weather-cache
Restart=on-failure Restart=always
RestartSec=5s
[Install] [Install]
WantedBy=multi-user.target WantedBy=multi-user.target

View File

@ -9,7 +9,6 @@ EnvironmentFile=/etc/secrets/space-weather
ExecStart=/srv/ha-noaa-space-weather/venv/bin/python /srv/ha-noaa-space-weather/feeder/mqtt.py ExecStart=/srv/ha-noaa-space-weather/venv/bin/python /srv/ha-noaa-space-weather/feeder/mqtt.py
SyslogIdentifier=space-weather-mqtt SyslogIdentifier=space-weather-mqtt
Restart=on-failure Restart=on-failure
RestartSec=5s
[Install] [Install]
WantedBy=multi-user.target WantedBy=multi-user.target

View File

@ -0,0 +1,9 @@
[Unit]
Description=Space Weather MQTT timer
[Timer]
OnCalendar=hourly
Persistent=true
[Install]
WantedBy=timers.target