Newer
Older
smart-home / testowe / check_status.py
#!/usr/bin/env python3

import sys
import subprocess
import xml.etree.ElementTree as ET

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

# Function to display help

def show_help():
    print()
    print("Options:")
    print("  --show_active_output    Display only controllers with active outputs")
    print("  --all                   Display full information about controllers")
    print("  --show-id <id>          Display outputs only from the specified controller")
    print("                          (Works only with --all or --show_active_output)")
    print("                          --all --show-id 35")
    print("                          --show_active_output--show-id 35")
    print("  --help                  Display this help message")
    sys.exit(1)

# Function to run system commands and return the output
def run_command(command):
    result = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    return result.stdout.decode('utf-8')

# Parse the XML file and get Address values from the <Devices> section
def get_addresses_from_xml(xml_file):
    tree = ET.parse(xml_file)
    root = tree.getroot()

    # Namespace must be included for the XML
    namespace = {'ns': 'http://www.insbud.net/ibvunit'}

    # Extract all addresses from the Devices section
    addresses = []
    for device in root.findall('.//ns:Devices/ns:Device', namespace):
        addresses.append(device.get('Address'))

    return addresses

# Main logic
def main():
    show_active_output = False
    show_all = False
    specific_id = None

    # Check arguments
    if len(sys.argv) == 1:
        show_help()

    i = 1
    while i < len(sys.argv):
        arg = sys.argv[i]
        if arg == "--show_active_output":
            show_active_output = True
        elif arg == "--all":
            show_all = True
        elif arg == "--show-id":
            if i + 1 < len(sys.argv):  # Check if id is provided
                specific_id = sys.argv[i + 1]
                i += 1  # Skip to the next argument
            else:
                print("Error: You must provide an identifier after --show-id.")
                show_help()
        elif arg == "--help":
            show_help()
        else:
            print(f"Unknown option: {arg}")
            show_help()
        i += 1  # Move to the next argument

    # Validate argument combinations
    if specific_id is not None and not (show_active_output or show_all):
        print("Error: --show-id works only with --all or --show_active_output.")
        show_help()

    # Get addresses from the XML file
    addresses = get_addresses_from_xml(xml_file)

    # For each address, execute the corresponding command
    for address in addresses:
        # Check if the identifier matches
        if specific_id is not None and specific_id != address:
            continue  # Skip other identifiers

        output = run_command(f'./ibls -a 127.0.0.1 --pretty -p 2001 -c "get(rs.0.id.{address}.out;);"')

        # If --all option is provided, display full output without active output details
        if show_all:
            print(f"Controller id.{address}:")
            print(output)

        # If --show_active_output option is provided, display only active outputs
        if show_active_output:
            active_outputs = [line for line in output.splitlines() if 'do.' in line and '= 1' in line]

            if active_outputs:
                for line in active_outputs:
                    # Extract the full output name, e.g., do.x
                    output_name = line.split('=')[0].strip()
                    output_name = output_name.split('.')[-2] + '.' + output_name.split('.')[-1]  # do.x
                    print(f"Active output: {output_name}")
            else:
                print(f"Controller id.{address} has no active outputs.")

if __name__ == "__main__":
    main()