From 8c7887afb853b5961e888f6c1320b19cb350128b Mon Sep 17 00:00:00 2001 From: Daniel Date: Sat, 12 Mar 2016 18:06:37 +0100 Subject: [PATCH 1/2] Fix closing thread --- RFXtrx/__init__.py | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/RFXtrx/__init__.py b/RFXtrx/__init__.py index 99d1a08..3ef36ee 100644 --- a/RFXtrx/__init__.py +++ b/RFXtrx/__init__.py @@ -23,8 +23,8 @@ # pylint: disable=R0903 from __future__ import print_function -from threading import Thread from time import sleep +import threading import serial from . import lowlevel @@ -355,6 +355,10 @@ def read(self, data=None): sleep(1) return res + def close(self): + """ close connection to rfxtrx device """ + pass + ############################################################################### # RFXtrxTransport class @@ -362,6 +366,7 @@ def read(self, data=None): class RFXtrxTransport(object): """ Abstract superclass for all transport mechanisms """ + # pylint: disable=attribute-defined-outside-init @staticmethod def parse(data): @@ -399,7 +404,8 @@ class PySerialTransport(RFXtrxTransport): def __init__(self, port, debug=False): self.debug = debug - self._stop = False + self._run_event = threading.Event() + self._run_event.set() try: self.serial = serial.Serial(port, 38400, timeout=0.1) except serial.serialutil.SerialException: @@ -412,7 +418,7 @@ def __init__(self, port, debug=False): def receive_blocking(self): """ Wait until a packet is received and return with an RFXtrxEvent """ - while not self._stop: + while self._run_event.is_set(): data = self.serial.read() if len(data) > 0: if data == '\x00': @@ -447,7 +453,7 @@ def reset(self): def close(self): """ close connection to rfxtrx device """ - self._stop = True + self._run_event.clear() self.serial.close() @@ -455,8 +461,8 @@ class DummyTransport(RFXtrxTransport): """ Dummy transport for testing purposes """ def __init__(self, device="", debug=True): - self.debug = debug self.device = device + self.debug = debug def receive(self, data=None): """ Emulate a receive by parsing the given data """ @@ -485,37 +491,31 @@ class DummyTransport2(PySerialTransport): def __init__(self, device="", debug=True): self.serial = _dummySerial(device, 38400, timeout=0.1) self.debug = debug - self._stop = False - - def close(self): - """ close connection to rfxtrx device """ - self._stop = True + self._run_event = threading.Event() + self._run_event.set() class Connect(object): """ The main class for rfxcom-py. Has methods for sensors. """ - + # pylint: disable=too-many-instance-attributes def __init__(self, device, event_callback=None, debug=False, transport_protocol=PySerialTransport): + self._run_event = threading.Event() + self._run_event.set() self._sensors = {} self._event_callback = event_callback - self.transport = None - self.transport_protocol = transport_protocol - self._shutdown = False - self._thread = Thread(target=self._connect, args=(device, debug)) + self.transport = transport_protocol(device, debug) + self._thread = threading.Thread(target=self._connect) self._thread.setDaemon(True) self._thread.start() - while not self.transport: - sleep(0.1) - def _connect(self, device, debug): + def _connect(self): """Connect """ - self.transport = self.transport_protocol(device, debug) self.transport.reset() - while not self._shutdown: + while self._run_event.is_set(): event = self.transport.receive_blocking() if isinstance(event, RFXtrxEvent): if self._event_callback: @@ -531,8 +531,8 @@ def sensors(self): def close_connection(self): """ Close connection to rfxtrx device """ + self._run_event.clear() self.transport.close() - self._shutdown = True self._thread.join() From 50b8509ba95c7f69412e51e9ab2e22e658181aa5 Mon Sep 17 00:00:00 2001 From: Daniel Date: Sun, 13 Mar 2016 19:58:03 +0100 Subject: [PATCH 2/2] version 0.6.5 --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index eff5c4e..32610a8 100644 --- a/setup.py +++ b/setup.py @@ -26,7 +26,7 @@ name = 'pyRFXtrx', packages = ['RFXtrx'], install_requires=['pyserial>=2.7'], - version = '0.6', + version = '0.6.5', description = 'a library to communicate with the RFXtrx family of devices', author='Edwin Woudt', author_email='edwin@woudt.nl',