Skip to content

Commit

Permalink
Merge pull request #7 from RyuzakiKK/do_not_reuse_socket
Browse files Browse the repository at this point in the history
Do not reuse socket
  • Loading branch information
RyuzakiKK authored May 29, 2021
2 parents b908021 + 9770039 commit b2483d0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
12 changes: 8 additions & 4 deletions pyialarm/pyialarm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from setuptools import setup

__version__ = '1.5'
__version__ = '1.7'

setup(
name='pyialarm',
Expand Down

0 comments on commit b2483d0

Please sign in to comment.