Newer
Older
smart-home / testowe / check_status2.py
import argparse

# Path to the XML file
xml_file = "ibvunit/ibvunit.xml"
dok_file = "dok.txt"

def parse_arguments():
    parser = argparse.ArgumentParser(description="Controller output monitoring script.")
    parser.add_argument('--all', action='store_true', help='Show all controller outputs.')
    parser.add_argument('--show-active-output', action='store_true', help='Show only active outputs.')
    parser.add_argument('--show-id', type=int, help='Show outputs for a specific controller ID.')
    return parser.parse_args()

def read_dok_file(dok_file):
    output_descriptions = {}
    controllers = {}

    with open(dok_file, 'r') as f:
        lines = f.readlines()
        current_id = None
        for line in lines:
            line = line.strip()
            if line.startswith("id."):
                current_id = line.split('.')[1]
                controllers[current_id] = {'active_outputs': [], 'name': f'Controller {current_id}'}
            elif current_id and "=" in line:
                key, value = line.split("=")
                output_key = key.strip()
                output_descriptions[output_key] = value.strip()
                # Check if the output key is an active output for the current controller
                if output_key.startswith('output.do.'):
                    controllers[current_id]['active_outputs'].append(output_key)

    return output_descriptions, controllers

def main():
    args = parse_arguments()

    # Read output descriptions from dok.txt and create controllers dictionary
    output_descriptions, controllers = read_dok_file(dok_file)

    if not (args.all or args.show_active_output):
        print("Error: You must provide either --all or --show-active-output.")
        return

    for address, controller in controllers.items():
        if args.show_id is not None and int(address) != args.show_id:
            continue

        if args.all:
            print(f"{controller['name']}:")
            for output in controller['active_outputs']:
                description = output_descriptions.get(output, "Unknown output")
                print(f"{output} name: {description}")

        if args.show_active_output:
            active_outputs = controller['active_outputs']
            if active_outputs:
                for line in active_outputs:
                    output_identifier = line.split('.')[-1]  # This should give us do.x

                    # Look for the description in the loaded mappings
                    full_output_name = f"rs.0.id.{address}.output.{output_identifier}"
                    description = output_descriptions.get(full_output_name, "Unknown output")

                    # Debug: Print the full output name and the corresponding description key
                    print(f"Checking for output: {full_output_name} (Key: {output_identifier})")

                    # Check if the output is present in the descriptions and get the name
                    output_description_key = f"output.do.{output_identifier}"
                    print(f"Checking for description with key: {output_description_key}")  # Debug line
                    if output_description_key in output_descriptions:
                        description = output_descriptions[output_description_key]

                    # Change output to include the full output key
                    print(f"Controller id.{address} output.do.{output_identifier} - {description}")
            else:
                print(f"Controller id.{address} has no active outputs.")

if __name__ == "__main__":
    main()