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 logging
import numpy as np
import os
import paho.mqtt.client as mqtt
import pickle
import sys
import time
from datetime import datetime, timezone
import numpy as np
import paho.mqtt.client as mqtt
from dateutil.parser import parse
from redis import Redis
@ -56,32 +55,21 @@ def publish(topic: str, msg):
def main():
redis = Redis(host='localhost', port=6379, db=0)
while True:
data = redis.get('glotec')
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')
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')
geojson = pickle.loads(data)
geojson = pickle.loads(data)
utc_hr = datetime.now(timezone.utc).hour
logging.info(f'Using hour {utc_hr}')
utc_hr = datetime.now(timezone.utc).hour
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)])
avg_tec = np.mean(glotec_map_ranged)
logging.info(f'Data timestamp: {parse(geojson["time_tag"]).isoformat()}')
latest = round(avg_tec, 1)
publish('glotec', latest)
del data
del geojson
del glotec_map_ranged
del avg_tec
del latest
gc.collect()
time.sleep(60)
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)
logging.info(f'Data timestamp: {parse(geojson["time_tag"]).isoformat()}')
latest = round(avg_tec, 1)
publish('glotec', latest)
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
ExecStart=/srv/ha-noaa-space-weather/venv/bin/python /srv/ha-noaa-space-weather/feeder/cache.py
SyslogIdentifier=space-weather-cache
Restart=on-failure
RestartSec=5s
Restart=always
[Install]
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
SyslogIdentifier=space-weather-mqtt
Restart=on-failure
RestartSec=5s
[Install]
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