Algorithm 1 Cortex API updates in Python |
1: Define class ConnectionManager 2: Define initialisation 3: Set active_connections as an empty list of WebSockets 4: 5: Define method Broadcast 6: If the time since last_sent exceeds 3 s 7: Send a text message to all active_connections 8: Update last_sent_time 9: Log message sent 10: Else 11: Log message sending skipped due to cooldown 12: 13: Define WebSocket Endpoint “/ws” 14: Connect incoming WebSocket 15: Continuously receive text from WebSocket 16: On exception, log and disconnect WebSocket 17: 18: Define Background Task to Listen for Commands 19: Continuously 20: Retrieve data from the Cortex instance 21: If valid command data received with sufficient power 22: Process and broadcast command 23: Handle exceptions with error logging and retry with delays 24: 25: Define Command Processing 26: Depending on the command 27: Set descriptions for recognised commands 28: Convert command and description to JSON 29: Broadcast JSON message 30: 31: Define Application Startup Event 32: Initialise and configure the Cortex instance 33: Connect and authorise with Cortex 34: Create a session and subscribe to the mental commands data stream 35: Start background task for listening to commands |