From e6d08d04e014ce35e12438f0c7dc4b85ea4715b2 Mon Sep 17 00:00:00 2001 From: douginoz Date: Sun, 21 Jul 2024 00:58:25 -0700 Subject: [PATCH] Corrected iterative ability --- iceicedata/main.py | 194 ++++++++++++++++++--------------------------- 1 file changed, 79 insertions(+), 115 deletions(-) diff --git a/iceicedata/main.py b/iceicedata/main.py index f4a814d18..740519120 100644 --- a/iceicedata/main.py +++ b/iceicedata/main.py @@ -13,7 +13,7 @@ from config_loader import load_config, validate_config from database import create_database, insert_data_into_database, check_database_integrity -VERSION = "1.3.0" # Updated version +VERSION = "1.3.1" # Updated version def signal_handler(sig, frame): print("\nProgram terminated by user.") @@ -21,6 +21,35 @@ def signal_handler(sig, frame): signal.signal(signal.SIGINT, signal_handler) +def validate_repeat(repeat): + import re + match = re.match(r'^(\d+)([md])$', repeat) + if not match: + print("Error: Invalid repeat format. Use '5m' for minutes or '1d' for days.") + sys.exit(1) + value, unit = match.groups() + value = int(value) + if unit == 'm' and value < 5: + print("Error: The repeat delay must be at least 5 minutes.") + sys.exit(1) + if unit == 'd' and value < 1: + print("Error: The repeat delay must be at least 1 day.") + sys.exit(1) + return value, unit + +def validate_station_id(station_id): + try: + station_id = station_id.strip().lstrip('0') + if not station_id.isdigit(): + raise ValueError + station_id = int(station_id) + if not (1 <= station_id <= 999999): + raise ValueError + return station_id + except ValueError: + print("Error: Invalid station ID. Please enter an integer between 1 and 999999.") + sys.exit(1) + def main(): final_url = None # Load configuration @@ -34,7 +63,6 @@ def main(): args = parse_arguments(config) # Setup logging - import logging log_level = logging.INFO log_format = '%(asctime)s - %(levelname)s - %(message)s' if args.debug is not None: @@ -56,38 +84,9 @@ def main(): print(f"Version: {VERSION}") sys.exit(0) - def validate_repeat(repeat): - import re - match = re.match(r'^(\d+)([md])$', repeat) - if not match: - print("Error: Invalid repeat format. Use '5m' for minutes or '1d' for days.") - sys.exit(1) - value, unit = match.groups() - value = int(value) - if unit == 'm' and value < 5: - print("Error: The repeat delay must be at least 5 minutes.") - sys.exit(1) - if unit == 'd' and value < 1: - print("Error: The repeat delay must be at least 1 day.") - sys.exit(1) - return value, unit - if args.repeat: repeat_value, repeat_unit = validate_repeat(args.repeat) - def validate_station_id(station_id): - try: - station_id = station_id.strip().lstrip('0') - if not station_id.isdigit(): - raise ValueError - station_id = int(station_id) - if not (1 <= station_id <= 999999): - raise ValueError - return station_id - except ValueError: - print("Error: Invalid station ID. Please enter an integer between 1 and 999999.") - sys.exit(1) - if args.setup_mqtt: logger.debug("Setting up MQTT configuration.") save_mqtt_config(config_file) @@ -134,100 +133,65 @@ def validate_station_id(station_id): station_id = validate_station_id(args.station_id) final_url = f"https://tempestwx.com/map/{station_id}" # Construct the URL using the station ID - print(f"Looking for station {station_id} -", end='', flush=True) - logger.debug("Processing data for URL: %s", final_url) - try: - # Updated to receive attribute_descriptions - data, wind_data, station_name, attribute_descriptions = process_data(final_url) - except Exception as e: - print(f"Error: Failed to process the data from the URL: {e}") - sys.exit(1) - logger.debug("Data processing completed. Data: %s", data) - logger.debug("Attribute descriptions: %s", attribute_descriptions) - logger.debug("Checking if data is None.") - if data is None: - print("Data is None.") - print("Failed to process the data from the URL.") - return - print(f" found. Station Name: {station_name}", end='') - if args.repeat: - print(f"; Retrieving data every {args.repeat} minutes.", end='') - print() # Move to the next line after the message - - if data is None: - print("Failed to process the data from the URL.") - return - - output_data(data, wind_data, json_file=args.json, output_file=args.output, stdout=True) - - station_identifier = f"{station_id} - {station_name}" - - logger.debug("Checking if MQTT option is provided for sending data.") - if args.mqtt or args.windrose: - if not config: - print(f"Error: Configuration file '{config_file}' not found or invalid. Please use the '-S' option to set up a new configuration or provide an existing configuration file with the '-m' option.") - sys.exit(1) - - if args.mqtt: - send_mqtt_data(data, config, f"{config['mqtt_root']}{station_identifier}") - - if args.windrose: - if not config.get('mqtt_windrose_root'): - print("Windrose root topic is not set in the configuration file. Please add it to the configuration file and try again.") - else: - windrose_data = {"wind_speed": wind_data.get("wind_speed"), "wind_direction": wind_data.get("wind_direction")} - send_mqtt_data(windrose_data, config, f"{config['mqtt_windrose_root']}{station_identifier}") - - logger.debug("Checking if database option is provided.") if args.database is not None: database_file = args.database create_database(database_file) check_database_integrity(database_file) - # Updated to use attribute_descriptions from process_data - logger.debug("Inserting data into database. Attribute descriptions: %s", attribute_descriptions) - insert_data_into_database(database_file, data, attribute_descriptions) - - while args.repeat is not None: - if repeat_unit == 'm': - time.sleep(repeat_value * 60) - elif repeat_unit == 'd': - time.sleep(repeat_value * 86400) - - try: - # Updated to receive attribute_descriptions - data, wind_data, station_name, attribute_descriptions = process_data(final_url, skip_initial=True) - except Exception as e: - print(f"Error: Failed to process the data from the URL: {e}") - sys.exit(1) - - if data is None: - print("Failed to process the data from the URL.") - return + while True: + print(f"Looking for station {station_id} -", end='', flush=True) + logger.debug("Processing data for URL: %s", final_url) + try: + data, wind_data, station_name, attribute_descriptions = process_data(final_url) + except Exception as e: + print(f"Error: Failed to process the data from the URL: {e}") + sys.exit(1) - station_identifier = f"{station_id} - {station_name}" - # Updated to use attribute_descriptions from process_data - logger.debug("Inserting data into database (repeat). Attribute descriptions: %s", attribute_descriptions) + logger.debug("Data processing completed. Data: %s", data) + logger.debug("Attribute descriptions: %s", attribute_descriptions) + logger.debug("Checking if data is None.") + if data is None: + print("Data is None.") + print("Failed to process the data from the URL.") + return + + print(f" found. Station Name: {station_name}", end='') + if args.repeat: + print(f"; Retrieving data every {args.repeat}.", end='') + print() # Move to the next line after the message + + output_data(data, wind_data, json_file=args.json, output_file=args.output, stdout=True) + + station_identifier = f"{station_id} - {station_name}" + + logger.debug("Checking if MQTT option is provided for sending data.") + if args.mqtt or args.windrose: + if args.mqtt: + send_mqtt_data(data, config, f"{config['mqtt_root']}{station_identifier}") + + if args.windrose: + if not config.get('mqtt_windrose_root'): + print("Windrose root topic is not set in the configuration file. Please add it to the configuration file and try again.") + else: + windrose_data = {"wind_speed": wind_data.get("wind_speed"), "wind_direction": wind_data.get("wind_direction")} + send_mqtt_data(windrose_data, config, f"{config['mqtt_windrose_root']}{station_identifier}") + + logger.debug("Checking if database option is provided.") + if args.database is not None: + logger.debug("Inserting data into database. Attribute descriptions: %s", attribute_descriptions) insert_data_into_database(database_file, data, attribute_descriptions) + if args.json or args.output: + output_data(data, wind_data, json_file=args.json, output_file=args.output, stdout=False) - if args.json or args.output: - output_data(data, wind_data, json_file=args.json, output_file=args.output, stdout=False) + if not args.repeat: + break - if args.mqtt or args.windrose: - if not config: - print(f"Error: Configuration file '{config_file}' not found or invalid. Please use the '-S' option to set up a new configuration or provide an existing configuration file with the '-m' option.") - sys.exit(1) - - if args.mqtt: - send_mqtt_data(data, config, f"{config['mqtt_root']}{station_identifier}") - - if args.windrose: - if not config.get('mqtt_windrose_root'): - print("Windrose root topic is not set in the configuration file. Please add it to the configuration file and try again.") - else: - windrose_data = {"wind_speed": wind_data.get("wind_speed"), "wind_direction": wind_data.get("wind_direction")} - send_mqtt_data(windrose_data, config, f"{config['mqtt_windrose_root']}{station_identifier}") + # Wait before next iteration + if repeat_unit == 'm': + time.sleep(repeat_value * 60) + elif repeat_unit == 'd': + time.sleep(repeat_value * 86400) if __name__ == "__main__": main() \ No newline at end of file