From d7eaff80b60e490c651f0595239534b5adbff69c Mon Sep 17 00:00:00 2001 From: Ludovico de Nittis Date: Sat, 22 May 2021 15:23:47 +0200 Subject: [PATCH 1/2] Do not reuse the same socket for multiple requests It has been reported that sometimes the alarm system replies are not what we expected them to be. To trigger this bug you need to create an IAlarm instance and call "get_status()" in loop. Then with the official iAlarm application change the alarm status from disarmed to armed. At this point "get_status()" will start to complain that it doesn't understand the alarm system replies. By using a fresh new socket connection it seems like to be way safer and this bug doesn't happen to present itself anymore. --- pyialarm/pyialarm.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/pyialarm/pyialarm.py b/pyialarm/pyialarm.py index 3970ec2..7ff66ea 100644 --- a/pyialarm/pyialarm.py +++ b/pyialarm/pyialarm.py @@ -38,7 +38,6 @@ def __init__(self, host, port=18034): """ self.host = host self.port = port - self.seq = 0 self.sock = None def ensure_connection_is_open(self) -> None: @@ -55,6 +54,10 @@ def ensure_connection_is_open(self) -> None: self.sock.close() raise ConnectionError('Connection to the alarm system failed') from err + def _close_connection(self) -> None: + if self.sock and self.sock.fileno() != 1: + self.sock.close() + def _send_request_list(self, xpath, command, offset=0, partial_list=None): if offset > 0: command['Offset'] = 'S32,0,0|%d' % offset @@ -73,12 +76,15 @@ def _send_request_list(self, xpath, command, offset=0, partial_list=None): if total > offset: # Continue getting elements increasing the offset self._send_request_list(xpath, command, offset, partial_list) + + self._close_connection() return partial_list def _send_request(self, xpath, command) -> dict: root_dict = self._create_root_dict(xpath, command) self._send_dict(root_dict) response = self._receive() + self._close_connection() return self._clean_response_dict(response, xpath) def get_mac(self) -> str: @@ -164,9 +170,7 @@ def _send_dict(self, root_dict) -> None: self.ensure_connection_is_open() - self.seq += 1 - msg = b'@ieM%04d%04d0000%s%04d' % (len(xml), self.seq, self._xor(xml), - self.seq) + msg = b'@ieM%04d00010000%s0001' % (len(xml), self._xor(xml)) self.sock.send(msg) def _receive(self): From 97700397a40209fa6633fa59c1529cd987749be0 Mon Sep 17 00:00:00 2001 From: Ludovico de Nittis Date: Sat, 22 May 2021 15:25:15 +0200 Subject: [PATCH 2/2] Release version 1.7 --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 5c978a3..1405a2c 100644 --- a/setup.py +++ b/setup.py @@ -1,6 +1,6 @@ from setuptools import setup -__version__ = '1.5' +__version__ = '1.7' setup( name='pyialarm',