Skip to content

Commit

Permalink
Corrected iterative ability
Browse files Browse the repository at this point in the history
  • Loading branch information
douginoz committed Jul 21, 2024
1 parent 6483ab4 commit e6d08d0
Showing 1 changed file with 79 additions and 115 deletions.
194 changes: 79 additions & 115 deletions iceicedata/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,43 @@
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.")
sys.exit(0)

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
Expand All @@ -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:
Expand All @@ -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)
Expand Down Expand Up @@ -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()

0 comments on commit e6d08d0

Please sign in to comment.