> How to use TCP? The Default Parser module is a default TCP parser designed to handle incoming TCP messages, parse commands, and log the results. It is part of the SPX Graphics Controller system and is used to process specific TCP commands sent to the server. --- ## Features - Reads incoming TCP data as UTF-8 strings. - Parses commands in the format `SPX\COMMAND\ID\\`. - Supports the following commands: - `PLAY` - `STOP` - `NEXT` - Extracts the command and object ID from the message. - Logs the received message and parsed results. - Handles unrecognized message formats and parsing errors gracefully. ## Supported Command Format The module expects TCP messages to follow this format: ``` SPX\COMMAND\ID\\ ``` Where: - `SPX\` is the fixed prefix. - `COMMAND` is one of the supported commands (`PLAY`, `STOP`, `NEXT`). - `ID` is an alphanumeric identifier (e.g., `69314c6859b4b67c2ef0a43c`). - `\\` indicates the end of the message. ### Example Messages - `SPX\PLAY\69314c6859b4b67c2ef0a43c\\` - `SPX\STOP\1234567890\\` - `SPX\NEXT\abcdef123456\\` ## API ### `parse(data)` Parses the incoming TCP message and extracts the command and object ID. ### Parameters - `data` (Buffer): The raw TCP data received by the server. ### Returns - `{ type: 'command', command: string, objectId: string }` if the message is successfully parsed. - `{ type: 'error', message: string }` if the message format is unrecognized or an error occurs during parsing. ### Example Usage ```javascript const parser = require('./tcpParserDefault'); const data = Buffer.from('SPX\\PLAY\\69314c6859b4b67c2ef0a43c\\\\'); const result = parser.parse(data); if (result.type === 'command') { console.log(`Command: ${result.command}, Object ID: ${result.objectId}`); } else { console.error(`Error: ${result.message}`); } ``` ## Logging The module uses the `logger` utility to log messages at different levels: - `info`: Logs received messages and parsed results. - `warn`: Logs unrecognized message formats. - `error`: Logs errors that occur during parsing. ## Error Handling The module handles errors gracefully by: - Returning an error object with a descriptive message if the message format is unrecognized. - Catching and logging exceptions that occur during parsing. ## File Location This module is located at: ``` ASSETS/ExtraFunctions/tcpParserDefault.js ``` ## Dependencies - `logger`: Utility for logging messages. ## Notes - Ensure that the incoming TCP messages conform to the expected format to avoid parsing errors. - This module is designed to be used as the default parser for TCP messages in the SPX Graphics Controller system. --- ## Read Next - [[Documentation/Control Interfaces/REST/Overview of SPX API|REST API Overview]] - HTTP-based API - [[Documentation/Control Interfaces/External Control|External Control]] - Hardware controller integration - [[Documentation/Server/Configurations|Server Configurations]] - TCP settings - [[Documentation/Control Interfaces/Overview|Control Interfaces Overview]] - All control options