import paramiko, time, json

k = paramiko.Ed25519Key.from_private_key_file(r'C:\Users\fabia\.ssh\id_ed25519')
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect('192.168.178.150', username='fabian', pkey=k, timeout=10)
print("Connected. Monitoring sampling progress...")

for i in range(40):  # up to 10 minutes
    # Check log for sampling progress
    _, o, _ = c.exec_command('tail -5 /tmp/comfyui.log 2>/dev/null')
    log = o.read().decode(errors='replace').strip()
    
    # Check GPU temp
    _, o, _ = c.exec_command('cat /sys/class/drm/card0/device/hwmon/hwmon*/temp1_input 2>/dev/null')
    temp = o.read().decode().strip()
    temp_c = int(temp) // 1000 if temp.isdigit() else '?'
    
    # Check GPU usage
    _, o, _ = c.exec_command('cat /sys/class/drm/card0/device/gpu_busy_percent 2>/dev/null')
    gpu_pct = o.read().decode().strip()
    
    # Check output dir for generated images
    _, o, _ = c.exec_command('ls ~/ComfyUI/output/*.png 2>/dev/null')
    files = o.read().decode().strip()
    
    # Check queue
    _, o, _ = c.exec_command('curl -s http://127.0.0.1:8188/queue 2>/dev/null')
    queue_raw = o.read().decode().strip()
    
    # Parse last log line for progress
    last_line = log.split('\n')[-1] if log else ''
    print(f"[{i*15:>3}s] GPU:{gpu_pct}% temp:{temp_c}C | {last_line[-120:]}")
    
    if files:
        print(f"\n*** IMAGE GENERATED! ***")
        print(f"Files: {files}")
        # Get last 10 lines of log for timing info
        _, o, _ = c.exec_command('tail -10 /tmp/comfyui.log 2>/dev/null')
        print(o.read().decode(errors='replace'))
        break
    
    try:
        qdata = json.loads(queue_raw)
        running = len(qdata.get('queue_running', []))
        pending = len(qdata.get('queue_pending', []))
        if running == 0 and pending == 0 and i > 3:
            print("\nQueue empty - job finished or failed. Last 30 log lines:")
            _, o, _ = c.exec_command('tail -30 /tmp/comfyui.log 2>/dev/null')
            print(o.read().decode(errors='replace'))
            break
    except:
        pass
    
    time.sleep(15)

c.close()
print("Monitor done.")
