From 0cfe4a39ed54f449ef5ff34ba90eedc4add31e76 Mon Sep 17 00:00:00 2001 From: SpudGunMan Date: Tue, 28 Oct 2025 22:14:34 -0700 Subject: [PATCH] refactor --- config.template | 2 +- mesh_bot.py | 19 ++++++++++++++++--- modules/locationdata.py | 12 +----------- modules/system.py | 4 ++++ modules/test_bot.py | 2 +- test_xtide.py => modules/test_xtide.py | 0 modules/xtide.md | 12 ++++++++++++ modules/xtide.py | 6 +++--- requirements.txt | 3 +-- 9 files changed, 39 insertions(+), 21 deletions(-) rename test_xtide.py => modules/test_xtide.py (100%) diff --git a/config.template b/config.template index 91f448e..fdb563b 100644 --- a/config.template +++ b/config.template @@ -211,7 +211,7 @@ NOAAalertCount = 2 # use Open-Meteo API for weather data not NOAA useful for non US locations UseMeteoWxAPI = False -# Global Tide Prediction using tidepredict (for non-US locations) +# Global Tide Prediction using tidepredict (for non-US locations or offline use) # When enabled, uses tidepredict library for global tide predictions instead of NOAA API # tidepredict uses University of Hawaii's Research Quality Dataset for worldwide coverage useTidePredict = False diff --git a/mesh_bot.py b/mesh_bot.py index 6c72645..61d9bf8 100755 --- a/mesh_bot.py +++ b/mesh_bot.py @@ -1428,10 +1428,21 @@ def handle_repeaterQuery(message_from_id, deviceID, channel_number): return "Repeater lookup not enabled" def handle_tide(message_from_id, deviceID, channel_number, vox=False): - if vox: - return get_NOAAtide(str(my_settings.latitudeValue), str(my_settings.longitudeValue)) + # Check if tidepredict (xtide) is enabled location = get_node_location(message_from_id, deviceID, channel_number) - return get_NOAAtide(str(location[0]), str(location[1])) + lat = str(location[0]) + lon = str(location[1]) + if lat == "0.0" or lon == "0.0": + lat = str(my_settings.latitudeValue) + lon = str(my_settings.longitudeValue) + + if my_settings.useTidePredict: + logger.debug("System: Location: Using tidepredict") + return xtide.get_tide_predictions(lat, lon) + else: + # Fallback to NOAA tide data + logger.debug("System: Location: Using NOAA") + return get_NOAAtide(str(location[0]), str(location[1])) def handle_moon(message_from_id, deviceID, channel_number, vox=False): if vox: @@ -1553,6 +1564,8 @@ def handle_boot(mesh=True): if my_settings.coastalEnabled: logger.debug("System: Coastal Forecast and Tide Enabled!") + if my_settings.useTidePredict: + logger.debug("System: Using Local TidePredict for Tide Data") if games_enabled: logger.debug("System: Games Enabled!") diff --git a/modules/locationdata.py b/modules/locationdata.py index 499697a..e456f83 100644 --- a/modules/locationdata.py +++ b/modules/locationdata.py @@ -175,17 +175,7 @@ def getArtSciRepeaters(lat=0, lon=0): return msg def get_NOAAtide(lat=0, lon=0): - # Check if tidepredict (xtide) is enabled - if my_settings.useTidePredict: - try: - from modules import xtide - if xtide.is_enabled(): - logger.debug("Location: Using tidepredict for global tide data") - return xtide.get_tide_predictions(lat, lon) - except Exception as e: - logger.warning(f"Location: Failed to use tidepredict, falling back to NOAA: {e}") - - # Original NOAA implementation + # get tide data from NOAA for lat/lon station_id = "" location = lat,lon if float(lat) == 0 and float(lon) == 0: diff --git a/modules/system.py b/modules/system.py index 25a56fd..13165cb 100644 --- a/modules/system.py +++ b/modules/system.py @@ -125,6 +125,10 @@ if coastalEnabled: from modules.locationdata import * # from the spudgunman/meshing-around repo trap_list = trap_list + ("mwx","tide",) help_message = help_message + ", mwx, tide" +if useTidePredict: + from modules import xtide + trap_list = trap_list + ("tide",) + help_message = help_message + ", tide" # BBS Configuration if bbs_enabled: diff --git a/modules/test_bot.py b/modules/test_bot.py index 8e6f188..3d3a341 100644 --- a/modules/test_bot.py +++ b/modules/test_bot.py @@ -28,7 +28,7 @@ if os.path.isfile(checkall_path): # List of module names to exclude -exclude = ['test_bot','udp', 'system', 'log', 'gpio', 'web',] +exclude = ['test_bot','udp', 'system', 'log', 'gpio', 'web','test_xtide',] available_modules = [ m.name for m in pkgutil.iter_modules([modules_path]) if m.name not in exclude] diff --git a/test_xtide.py b/modules/test_xtide.py similarity index 100% rename from test_xtide.py rename to modules/test_xtide.py diff --git a/modules/xtide.md b/modules/xtide.md index 9585add..c274d0e 100644 --- a/modules/xtide.md +++ b/modules/xtide.md @@ -94,6 +94,18 @@ The module couldn't find a nearby station. This may happen if: - The station database hasn't been initialized - Network issues prevented loading the station list +Tide Station Map +[https://uhslc.soest.hawaii.edu/network/](https://uhslc.soest.hawaii.edu/network/) +- click on Tide Guages +- Find yourself on the map +- Locate the closest Gauge and its name (typically the city name) + +To manually download data for the station first location the needed station id +- `python -m tidepredict -l "Port Angeles"` finds a station +- `python -m tidepredict -l "Port Angeles" -genharm` downloads that datafile + + + ## Data Source Tide predictions are based on harmonic analysis of historical tide data from: diff --git a/modules/xtide.py b/modules/xtide.py index 9612329..1624de7 100644 --- a/modules/xtide.py +++ b/modules/xtide.py @@ -13,7 +13,7 @@ try: TIDEPREDICT_AVAILABLE = True except ImportError: TIDEPREDICT_AVAILABLE = False - logger.warning("xtide: tidepredict module not installed. Install with: pip install tidepredict") + logger.error("xtide: tidepredict module not installed. Install with: pip install tidepredict") def get_nearest_station(lat, lon): """ @@ -114,7 +114,7 @@ def get_tide_predictions(lat=0, lon=0, days=1): - Formatted string with tide predictions or error message """ if not TIDEPREDICT_AVAILABLE: - return "tidepredict library not installed" + return "module not installed, see logs for more ⚓️" if float(lat) == 0 and float(lon) == 0: return "No GPS data for tide prediction" @@ -132,7 +132,7 @@ def get_tide_predictions(lat=0, lon=0, days=1): # Check if harmonic data exists for this station if station_code not in station_dict: - logger.warning(f"xtide: No harmonic data for {station_name}.") + logger.warning(f"xtide: No harmonic data. python -m tidepredict -l \"{station_name}\" -genharm") return f"Tide data not available for {station_name}. Station database may need initialization." # Reconstruct tide model diff --git a/requirements.txt b/requirements.txt index 5b86b75..1b70d42 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,5 +7,4 @@ maidenhead beautifulsoup4 dadjokes geopy -schedule -tidepredict \ No newline at end of file +schedule \ No newline at end of file