Skip to content

Commit

Permalink
Merge pull request #17 from Danielhiversen/slowtests
Browse files Browse the repository at this point in the history
Slowtests
  • Loading branch information
Daniel Høyer Iversen committed Mar 13, 2016
2 parents 301cc56 + 50b8509 commit eddd10b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
42 changes: 21 additions & 21 deletions RFXtrx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -355,13 +355,18 @@ def read(self, data=None):
sleep(1)
return res

def close(self):
""" close connection to rfxtrx device """
pass


###############################################################################
# RFXtrxTransport class
###############################################################################

class RFXtrxTransport(object):
""" Abstract superclass for all transport mechanisms """

# pylint: disable=attribute-defined-outside-init
@staticmethod
def parse(data):
Expand Down Expand Up @@ -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:
Expand All @@ -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':
Expand Down Expand Up @@ -447,16 +453,16 @@ def reset(self):

def close(self):
""" close connection to rfxtrx device """
self._stop = True
self._run_event.clear()
self.serial.close()


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 """
Expand Down Expand Up @@ -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:
Expand All @@ -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()


Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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='[email protected]',
Expand Down

0 comments on commit eddd10b

Please sign in to comment.