get working

This commit is contained in:
Cyberes 2024-09-03 17:09:36 -06:00
parent aa472d98cf
commit 33dae67940
3 changed files with 40 additions and 49 deletions

View File

@ -4,8 +4,9 @@ This is an MQTT sensor to send NOAA space weather data to Home Assistant. Fetchi
1. Create an account at <https://urs.earthdata.nasa.gov> 1. Create an account at <https://urs.earthdata.nasa.gov>
2. `pip install -r requirements.txt` 2. `pip install -r requirements.txt`
3. `sudo apt install p7zip-full redis-server` 3. `sudo apt-get install p7zip-full redis-server`
4. `sudo systemctl enable --now redis-server` 4. `sudo apt-get install dvipng texlive-latex-extra texlive-fonts-recommended cm-super`
5. `sudo systemctl enable --now redis-server`
### Google Chrome ### Google Chrome

View File

@ -1,11 +1,8 @@
import logging import logging
import os import os
import pickle import pickle
import signal
import sys import sys
import threading
import time import time
import traceback
from datetime import datetime from datetime import datetime
from redis import Redis from redis import Redis
@ -22,28 +19,22 @@ if not CDDIS_USERNAME or not CDDIS_PASSWORD:
sys.exit(1) sys.exit(1)
def update_cache(): def main():
try: redis = Redis(host='localhost', port=6379, db=0)
redis = Redis(host='localhost', port=6379, db=0) redis.flushall()
redis.flushall() while True:
while True: utc_hr = datetime.utcnow().hour
utc_hr = datetime.utcnow().hour logging.info('Fetching latest IONEX data')
logging.info('Fetching latest IONEX data') logging.info(f'Using hour {utc_hr}')
logging.info(f'Using hour {utc_hr}') ionex_data = fetch_latest_ionex(CDDIS_USERNAME, CDDIS_PASSWORD)
ionex_data = fetch_latest_ionex(CDDIS_USERNAME, CDDIS_PASSWORD) parsed_data = []
parsed_data = [] for tecmap, epoch in get_tecmaps(ionex_data):
for tecmap, epoch in get_tecmaps(ionex_data): parsed_dt = parse_ionex_datetime(epoch)
parsed_dt = parse_ionex_datetime(epoch) parsed_data.append((tecmap, parsed_dt))
parsed_data.append((tecmap, parsed_dt)) redis.set('tecmap_data', pickle.dumps(parsed_data))
redis.set('tecmap_data', pickle.dumps(parsed_data)) logging.info('Scrape complete')
logging.info('Scrape complete') time.sleep(1800) # 30 minutes
time.sleep(1800) # 30 minutes
except:
logging.critical(traceback.format_exc())
os.kill(os.getpid(), signal.SIGKILL)
if __name__ == '__main__': if __name__ == '__main__':
threading.Thread(target=update_cache).start() main()
while True:
time.sleep(3600)

View File

@ -22,36 +22,35 @@ LON_RANGE_MAX = 180
def main(): def main():
redis = Redis(host='localhost', port=6379, db=0) redis = Redis(host='localhost', port=6379, db=0)
while True: utc_hr = datetime.utcnow().hour
utc_hr = datetime.utcnow().hour logging.info(f'Generating plot for hour {utc_hr}')
logging.info(f'Generating plot for hour {utc_hr}')
ionex_data: List = pickle.loads(redis.get('tecmap_data')) ionex_data: List = pickle.loads(redis.get('tecmap_data'))
while ionex_data is None: while ionex_data is None:
logging.warning('Redis has not been populated yet. Is cache.py running? Sleeping 10s...') logging.warning('Redis has not been populated yet. Is cache.py running? Sleeping 10s...')
time.sleep(10) time.sleep(10)
for tecmap, epoch in ionex_data: for tecmap, epoch in ionex_data:
if epoch.hour == utc_hr: if epoch.hour == utc_hr:
plt = plot_tec_map(tecmap, [float(LON_RANGE_MIN), float(LON_RANGE_MAX)], [float(LAT_RANGE_MIN), float(LAT_RANGE_MAX)], timestamp=epoch)[1] plt = plot_tec_map(tecmap, [float(LON_RANGE_MIN), float(LON_RANGE_MAX)], [float(LAT_RANGE_MIN), float(LAT_RANGE_MAX)], timestamp=epoch)[1]
buf = io.BytesIO()
plt.savefig(buf, format='png', bbox_inches='tight', pad_inches=0, dpi=110)
plt.close()
del plt
buf = io.BytesIO() buf.seek(0)
plt.savefig(buf, format='png', bbox_inches='tight', pad_inches=0, dpi=110) img = Image.open(buf)
plt.close() buf = io.BytesIO()
del plt img.save(buf, format='PNG')
buf.seek(0) redis.set('global_map', buf.getvalue())
img = Image.open(buf) buf.close()
buf = io.BytesIO() logging.info(f'Finished hour {utc_hr}')
img.save(buf, format='PNG')
redis.set('global_map', buf.getvalue())
buf.close()
if __name__ == '__main__': if __name__ == '__main__':
main() main()
schedule.every().hour.at(":00").do(main) schedule.every().hour.at(':00').do(main)
while True: while True:
schedule.run_pending() schedule.run_pending()
time.sleep(1) time.sleep(1)