#!/usr/bin/env python3 """ Test client for signal generator Connects to Unix socket and prints incoming signals """ import socket import sys import json from datetime import datetime def listen_signals(socket_path="/tmp/signals.sock"): """Connect and listen for signals""" print(f"Connecting to {socket_path}...") try: sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) sock.connect(socket_path) print("Connected! Listening for signals...\n") buffer = "" while True: chunk = sock.recv(4096).decode("utf-8") if not chunk: print("Connection closed by server") break buffer += chunk # Process complete messages (newline-delimited) while "\n" in buffer: line, buffer = buffer.split("\n", 1) if line.strip(): try: signal = json.loads(line) print_signal(signal) except json.JSONDecodeError as e: print(f"Invalid JSON: {e}") sock.close() except FileNotFoundError: print(f"Error: Socket not found at {socket_path}") print("Is the signal generator running?") return 1 except ConnectionRefusedError: print(f"Error: Connection refused at {socket_path}") return 1 except KeyboardInterrupt: print("\nDisconnecting...") return 0 except Exception as e: print(f"Error: {e}") return 1 def print_signal(signal): """Pretty print a signal""" timestamp = datetime.fromisoformat(signal["generated_at"]) # Color coding color = "\033[92m" if signal["signal"] == "BUY" else "\033[91m" reset = "\033[0m" print(f"{color}{'=' * 70}{reset}") print( f"{color}[{timestamp.strftime('%Y-%m-%d %H:%M:%S')}] " f"{signal['signal']} SIGNAL{reset}" ) print(f"Timeframe: {signal['timeframe']}") print(f"Price: ${signal['price']:.2f}") print(f"Confidence: {signal['confidence']:.1%}") print(f"Personality: {signal['personality']}") print(f"Reasons:") for reason in signal["reasons"]: print(f" • {reason}") print(f"{color}{'=' * 70}{reset}\n") if __name__ == "__main__": socket_path = sys.argv[1] if len(sys.argv) > 1 else "/tmp/signals.sock" sys.exit(listen_signals(socket_path))