diff --git a/modules/locationdata.py b/modules/locationdata.py index 635e2b6..f7a7639 100644 --- a/modules/locationdata.py +++ b/modules/locationdata.py @@ -7,7 +7,7 @@ import maidenhead as mh # pip install maidenhead import requests # pip install requests import bs4 as bs # pip install beautifulsoup4 import xml.dom.minidom -from modules.settings import * +from modules.log import * trap_list_location = ("whereami", "tide", "moon", "wx", "wxc", "wxa", "wxalert") @@ -16,6 +16,7 @@ def where_am_i(lat=0, lon=0): grid = mh.to_maiden(float(lat), float(lon)) if float(lat) == 0 and float(lon) == 0: + logger.error("Location: No GPS data, cant find where you are") return NO_DATA_NOGPS # initialize Nominatim API @@ -41,6 +42,7 @@ def where_am_i(lat=0, lon=0): def get_tide(lat=0, lon=0): station_id = "" if float(lat) == 0 and float(lon) == 0: + logger.error("Location:No GPS data, cant find where you are for tide") return NO_DATA_NOGPS station_lookup_url = "https://api.tidesandcurrents.noaa.gov/mdapi/prod/webapi/tidepredstations.json?lat=" + str(lat) + "&lon=" + str(lon) + "&radius=50" try: @@ -48,14 +50,17 @@ def get_tide(lat=0, lon=0): if station_data.ok: station_json = station_data.json() else: + logger.error("Location:Error fetching tide station table from NOAA") return ERROR_FETCHING_DATA if station_json['stationList'] == [] or station_json['stationList'] is None: + logger.error("Location:No tide station found") return ERROR_FETCHING_DATA station_id = station_json['stationList'][0]['stationId'] except (requests.exceptions.RequestException, json.JSONDecodeError): + logger.error("Location:Error fetching tide station table from NOAA") return ERROR_FETCHING_DATA station_url = "https://tidesandcurrents.noaa.gov/noaatidepredictions.html?id=" + station_id @@ -65,8 +70,10 @@ def get_tide(lat=0, lon=0): try: station_data = requests.get(station_url, timeout=urlTimeoutSeconds) if not station_data.ok: + logger.error("Location:Error fetching station data from NOAA") return ERROR_FETCHING_DATA except (requests.exceptions.RequestException): + logger.error("Location:Error fetching station data from NOAA") return ERROR_FETCHING_DATA # extract table class="table table-condensed" @@ -108,14 +115,17 @@ def get_weather(lat=0, lon=0, unit=0): try: weather_data = requests.get(weather_url, timeout=urlTimeoutSeconds) if not weather_data.ok: + logger.error("Location:Error fetching weather data from NOAA") return ERROR_FETCHING_DATA except (requests.exceptions.RequestException): + logger.error("Location:Error fetching weather data from NOAA") return ERROR_FETCHING_DATA soup = bs.BeautifulSoup(weather_data.text, 'html.parser') table = soup.find('div', id="detailed-forecast-body") if table is None: + logger.error("Location:Bad weather data from NOAA") return ERROR_FETCHING_DATA else: # get rows @@ -203,8 +213,10 @@ def getWeatherAlerts(lat=0, lon=0): try: alert_data = requests.get(alert_url, timeout=urlTimeoutSeconds) if not alert_data.ok: + logger.error("Location:Error fetching weather alerts from NOAA") return ERROR_FETCHING_DATA except (requests.exceptions.RequestException): + logger.error("Location:Error fetching weather alerts from NOAA") return ERROR_FETCHING_DATA alerts = "" @@ -236,6 +248,7 @@ def getActiveWeatherAlertsDetail(lat=0, lon=0): # get the latest details of weather alerts from NOAA alerts = "" if float(lat) == 0 and float(lon) == 0: + logger.error("Location:No GPS data, cant find where you are for weather alerts") return NO_DATA_NOGPS alert_url = "https://api.weather.gov/alerts/active.atom?point=" + str(lat) + "," + str(lon) @@ -244,8 +257,10 @@ def getActiveWeatherAlertsDetail(lat=0, lon=0): try: alert_data = requests.get(alert_url, timeout=urlTimeoutSeconds) if not alert_data.ok: + logger.error("Location:Error fetching weather alerts detailed from NOAA") return ERROR_FETCHING_DATA except (requests.exceptions.RequestException): + logger.error("Location:Error fetching weather alerts detailed from NOAA") return ERROR_FETCHING_DATA alerts = "" diff --git a/modules/radio.py b/modules/radio.py index 444f1ff..7268ef1 100644 --- a/modules/radio.py +++ b/modules/radio.py @@ -5,7 +5,7 @@ import socket import asyncio -from modules.settings import * +from modules.log import * def get_hamlib(msg="f"): try: @@ -13,7 +13,7 @@ def get_hamlib(msg="f"): rigControlSocket.settimeout(2) rigControlSocket.connect((rigControlServerAddress.split(":")[0],int(rigControlServerAddress.split(":")[1]))) except Exception as e: - print(f"\nSystem: Error connecting to rigctld: {e}") + logger.error(f"RadioMon: Error connecting to rigctld: {e}") return ERROR_FETCHING_DATA try: @@ -27,7 +27,7 @@ def get_hamlib(msg="f"): data = data.replace(b'\n',b'') return data.decode("utf-8").rstrip() except Exception as e: - print(f"\nSystem: Error fetching data from rigctld: {e}") + logger.error(f"RadioMon: Error fetching data from rigctld: {e}") return ERROR_FETCHING_DATA def get_freq_common_name(freq): @@ -140,6 +140,7 @@ async def signalWatcher(): signalStrength = int(get_sig_strength()) if signalStrength >= previousStrength and signalStrength > signalDetectionThreshold: message = f"Detected {get_freq_common_name(get_hamlib('f'))} active. S-Meter:{signalStrength}dBm" + logger.debug(f"RadioMon: {message}. Waiting for {signalHoldTime} seconds") previousStrength = signalStrength signalCycle = 0 await asyncio.sleep(signalHoldTime) diff --git a/modules/solarconditions.py b/modules/solarconditions.py index f697728..ec62909 100644 --- a/modules/solarconditions.py +++ b/modules/solarconditions.py @@ -7,7 +7,7 @@ import xml.dom.minidom from datetime import datetime import ephem # pip install pyephem from datetime import timedelta -from modules.settings import * +from modules.log import * trap_list_solarconditions = ("sun", "solar", "hfcond") @@ -19,9 +19,11 @@ def hf_band_conditions(): solarxml = xml.dom.minidom.parseString(band_cond.text) for i in solarxml.getElementsByTagName("band"): hf_cond += i.getAttribute("time")[0]+i.getAttribute("name") +"="+str(i.childNodes[0].data)+"\n" + hf_cond = hf_cond[:-1] # remove the last newline else: - hf_cond += ERROR_FETCHING_DATA - hf_cond = hf_cond[:-1] # remove the last newline + logger.error("Solar: Error fetching HF band conditions") + hf_cond = ERROR_FETCHING_DATA + return hf_cond def solar_conditions(): @@ -39,7 +41,8 @@ def solar_conditions(): signalnoise = i.getElementsByTagName("signalnoise")[0].childNodes[0].data solar_cond = "A-Index: " + solar_a_index + "\nK-Index: " + solar_k_index + "\nSunspots: " + sunspots + "\nX-Ray Flux: " + solar_xray + "\nSolar Flux: " + solar_flux + "\nSignal Noise: " + signalnoise else: - solar_cond += ERROR_FETCHING_DATA + logger.error("Solar: Error fetching solar conditions") + solar_cond = ERROR_FETCHING_DATA return solar_cond def drap_xray_conditions(): @@ -53,7 +56,8 @@ def drap_xray_conditions(): if x_filter in line: xray_flux = line.split(": ")[1] else: - xray_flux += ERROR_FETCHING_DATA + logger.error("Error fetching DRAP X-ray flux") + xray_flux = ERROR_FETCHING_DATA return xray_flux def get_sun(lat=0, lon=0): diff --git a/modules/wx_meteo.py b/modules/wx_meteo.py index 8e403b8..f89f1fb 100644 --- a/modules/wx_meteo.py +++ b/modules/wx_meteo.py @@ -32,22 +32,32 @@ def get_wx_meteo(lat=0, lon=0, unit=0): params["distance_unit"] = "mile" params["pressure_unit"] = "inHg" - responses = openmeteo.weather_api(url, params=params) + try: + # Fetch the weather data + responses = openmeteo.weather_api(url, params=params) + except Exception as e: + logger.error(f"Error fetching meteo weather data: {e}") + return ERROR_FETCHING_DATA - # Process first location. Add a for-loop for multiple locations or weather models - response = responses[0] - logger.debug(f"Got wx data from Open-Meteo in {response.Timezone()} {response.TimezoneAbbreviation()}") + # Check if we got a response + try: + # Process location + response = responses[0] + logger.debug(f"Got wx data from Open-Meteo in {response.Timezone()} {response.TimezoneAbbreviation()}") - # Process daily data. The order of variables needs to be the same as requested. - daily = response.Daily() - daily_weather_code = daily.Variables(0).ValuesAsNumpy() - daily_temperature_2m_max = daily.Variables(1).ValuesAsNumpy() - daily_temperature_2m_min = daily.Variables(2).ValuesAsNumpy() - daily_precipitation_hours = daily.Variables(3).ValuesAsNumpy() - daily_precipitation_probability_max = daily.Variables(4).ValuesAsNumpy() - daily_wind_speed_10m_max = daily.Variables(5).ValuesAsNumpy() - daily_wind_gusts_10m_max = daily.Variables(6).ValuesAsNumpy() - daily_wind_direction_10m_dominant = daily.Variables(7).ValuesAsNumpy() + # Process daily data. The order of variables needs to be the same as requested. + daily = response.Daily() + daily_weather_code = daily.Variables(0).ValuesAsNumpy() + daily_temperature_2m_max = daily.Variables(1).ValuesAsNumpy() + daily_temperature_2m_min = daily.Variables(2).ValuesAsNumpy() + daily_precipitation_hours = daily.Variables(3).ValuesAsNumpy() + daily_precipitation_probability_max = daily.Variables(4).ValuesAsNumpy() + daily_wind_speed_10m_max = daily.Variables(5).ValuesAsNumpy() + daily_wind_gusts_10m_max = daily.Variables(6).ValuesAsNumpy() + daily_wind_direction_10m_dominant = daily.Variables(7).ValuesAsNumpy() + except Exception as e: + logger.error(f"Error processing meteo weather data: {e}") + return ERROR_FETCHING_DATA # convert wind value to cardinal directions for value in daily_wind_direction_10m_dominant: