diff --git a/.gitignore b/.gitignore index e65021e..702cd46 100644 --- a/.gitignore +++ b/.gitignore @@ -173,3 +173,4 @@ cython_debug/ prot.csv helpertils/serial.py helpertils/test.py +helpertils/socker.py diff --git a/EHSConfig.py b/EHSConfig.py index 993ffd7..daeb034 100644 --- a/EHSConfig.py +++ b/EHSConfig.py @@ -25,6 +25,7 @@ class EHSConfig(): MQTT = None GENERAL = None SERIAL = None + TCP = None NASA_REPO = None LOGGING = {} @@ -77,7 +78,13 @@ class EHSConfig(): config = yaml.safe_load(file) self.MQTT = config.get('mqtt') self.GENERAL = config.get('general') - self.SERIAL = config.get('serial') + + if 'tcp' in config: + self.TCP = config.get('tcp') + + if 'serial' in config: + self.SERIAL = config.get('serial') + if 'logging' in config: self.LOGGING = config.get('logging') else: @@ -106,11 +113,25 @@ class EHSConfig(): if 'protocolFile' not in self.GENERAL: self.GENERAL['protocolFile'] = None - if 'device' not in self.SERIAL: - raise ConfigException(argument=self.SERIAL['device'], message="serial device config parameter is missing") - - if 'baudrate' not in self.SERIAL: - raise ConfigException(argument=self.SERIAL['baudrate'], message="serial baudrate config parameter is missing") + if self.SERIAL is None and self.TCP is None: + raise ConfigException(argument="", message="define tcp or serial config parms") + + if self.SERIAL is not None and self.TCP is not None: + raise ConfigException(argument="", message="you cannot define tcp and serial please define only one") + + if self.SERIAL is not None: + if 'device' not in self.SERIAL: + raise ConfigException(argument=self.SERIAL['device'], message="serial device config parameter is missing") + + if 'baudrate' not in self.SERIAL: + raise ConfigException(argument=self.SERIAL['baudrate'], message="serial baudrate config parameter is missing") + + if self.TCP is not None: + if 'ip' not in self.TCP: + raise ConfigException(argument=self.TCP['ip'], message="tcp ip config parameter is missing") + + if 'port' not in self.TCP: + raise ConfigException(argument=self.TCP['port'], message="tcp port config parameter is missing") if 'broker-url' not in self.MQTT: raise ConfigException(argument=self.MQTT['broker-url'], message="mqtt broker-url config parameter is missing") diff --git a/README.md b/README.md index 46b9e2e..f86a339 100644 --- a/README.md +++ b/README.md @@ -184,12 +184,21 @@ The `config.yml` file contains configuration settings for the EHS-Sentinel proje - Default: `False` ### Serial Connection Settings +cannot be defined with TCP parm... - **device**: The serial device URL. - Example: `/dev/ttyUSB0` - **baudrate**: The baud rate for the serial connection. - Example: `9600` +### TCP Settings +cannot be defined with SERIAL parm... + +- **ip**: The ip of rs485 to ETH Adapter. + - Example: `168.192.2.200` +- **port**: The port of rs485 to ETH Adapter. + - Example: `4196` + ### MQTT Broker Settings - **broker-url**: The URL of the MQTT broker. @@ -214,15 +223,18 @@ The `config.yml` file contains configuration settings for the EHS-Sentinel proje ```yaml general: nasaRepositoryFile: data/NasaRepository.yml - protocolFile: prot.csv +# protocolFile: prot.csv logging: deviceAdded: True messageNotFound: False packetNotFromIndoorOutdoor: False proccessedMessage: False -serial: - device: /dev/ttyUSB0 - baudrate: 9600 +#serial: +# device: /dev/ttyUSB0 +# baudrate: 9600 +tcp: + ip: 168.192.2.200 + port: 4196 mqtt: broker-url: 123.45.6.69 broker-port: 1883 @@ -267,6 +279,9 @@ if you want to see how many uniquie Messages have been collected in the Dumpfile # Changelog +### v0.2.2 - 2025-02-24 +- Support for rs485 to ETH Adapter, tcp options instead of serial port are possible now + ### v0.2.1 - 2025-02-22 - limit NASA_EHSSENTINEL_COP and NASA_EHSSENTINEL_TOTAL_COP to values between 0 and 20 diff --git a/data/config.yml b/data/config.yml index 26d4a0a..1a8b76e 100644 --- a/data/config.yml +++ b/data/config.yml @@ -8,6 +8,9 @@ logging: serial: device: /dev/ttyUSB0 baudrate: 9600 +tcp: + ip: 168.192.2.200 + port: 4196 mqtt: broker-url: 111.111.11.1 broker-port: 1883 diff --git a/startEHSSentinel.py b/startEHSSentinel.py index 5f53287..b070328 100644 --- a/startEHSSentinel.py +++ b/startEHSSentinel.py @@ -115,36 +115,31 @@ async def serial_connection(config, args): """ Asynchronously reads data from a serial connection and processes it. Args: - config (object): Configuration object containing serial connection parameters. + config (object): Configuration object containing serial or tcp connection parameters. args (object): Additional arguments for buffer processing. - This function establishes a serial connection using parameters from the config object, - reads data from the serial port until a specified delimiter (0x34) is encountered, + This function establishes a serial or tcp connection using parameters from the config object, + reads data from the serial port or tcp port until a specified delimiter (0x34) is encountered, and appends the received data to a buffer. It also starts an asynchronous task to process the buffer. - The serial connection is configured with the following parameters: - - Device URL: config.SERIAL['device'] - - Baudrate: config.SERIAL['baudrate'] - - Parity: Even - - Stopbits: One - - Bytesize: Eight - - RTS/CTS flow control: Enabled - - Timeout: 0 - The function runs an infinite loop to continuously read data from the serial port. + The function runs an infinite loop to continuously read data from the serial port/tcp port. """ buffer = [] loop = asyncio.get_running_loop() - reader, writer = await serial_asyncio.open_serial_connection( - loop=loop, - url=config.SERIAL['device'], - baudrate=config.SERIAL['baudrate'], - parity=serial.PARITY_EVEN, - stopbits=serial.STOPBITS_ONE, - bytesize=serial.EIGHTBITS, - rtscts=True, - timeout=1 - ) + if config.TCP is not None: + reader, writer = await asyncio.open_connection(config.TCP['ip'], config.TCP['port']) + else: + reader, writer = await serial_asyncio.open_serial_connection( + loop=loop, + url=config.SERIAL['device'], + baudrate=config.SERIAL['baudrate'], + parity=serial.PARITY_EVEN, + stopbits=serial.STOPBITS_ONE, + bytesize=serial.EIGHTBITS, + rtscts=True, + timeout=1 + ) await asyncio.gather( serial_read(reader, args, config),