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>
2. `pip install -r requirements.txt`
3. `sudo apt install p7zip-full redis-server`
4. `sudo systemctl enable --now redis-server`
3. `sudo apt-get install p7zip-full 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

View File

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

View File

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