Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding network to rfxtrx [see PR] #84

Closed
barrystaes opened this issue Jul 9, 2019 · 4 comments
Closed

Adding network to rfxtrx [see PR] #84

barrystaes opened this issue Jul 9, 2019 · 4 comments

Comments

@barrystaes
Copy link

I never use Python but would like to give this a try..
Is adding network support to pyRFXtrx feasible, if i implement a Network alternative for the PySerialTransport class? Looks like the code already accounts for swapping this, or am i misinterpreting what a transporter does here.

Source:

pyRFXtrx/RFXtrx/__init__.py

Lines 613 to 685 in df50186

###############################################################################
# PySerialTransport class
###############################################################################
class PySerialTransport(RFXtrxTransport):
""" Implementation of a transport using PySerial """
def __init__(self, port, debug=False):
self.debug = debug
self.port = port
self.serial = None
self._run_event = threading.Event()
self._run_event.set()
self.connect()
def connect(self):
""" Open a serial connexion """
try:
self.serial = serial.Serial(self.port, 38400, timeout=0.1)
except serial.serialutil.SerialException:
import glob
port = glob.glob('/dev/serial/by-id/usb-RFXCOM_*-port0')[0]
self.serial = serial.Serial(port, 38400, timeout=0.1)
def receive_blocking(self):
""" Wait until a packet is received and return with an RFXtrxEvent """
data = None
while self._run_event.is_set():
try:
data = self.serial.read()
except TypeError:
continue
except serial.serialutil.SerialException:
import time
try:
self.connect()
except serial.serialutil.SerialException:
time.sleep(5)
continue
if not data or data == '\x00':
continue
pkt = bytearray(data)
data = self.serial.read(pkt[0])
pkt.extend(bytearray(data))
if self.debug:
print("RFXTRX: Recv: " +
" ".join("0x{0:02x}".format(x) for x in pkt))
return self.parse(pkt)
def send(self, data):
""" Send the given packet """
if isinstance(data, bytearray):
pkt = data
elif isinstance(data, (bytes, str)):
pkt = bytearray(data)
else:
raise ValueError("Invalid type")
if self.debug:
print("RFXTRX: Send: " +
" ".join("0x{0:02x}".format(x) for x in pkt))
self.serial.write(pkt)
def reset(self):
""" Reset the RFXtrx """
self.send(b'\x0D\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
sleep(0.3) # Should work with 0.05, but not for me
self.serial.flushInput()
def close(self):
""" close connection to rfxtrx device """
self._run_event.clear()
self.serial.close()

My original topic on HA forum, where i recursed source to this repository:
https://community.home-assistant.io/t/adding-network-to-rfxtrx-component/125615/2

@barrystaes
Copy link
Author

barrystaes commented Jul 16, 2019

I forked, tried python (and thanks for the Dockerfile) and made a start on a PyNetworkTransport..

diff: master...barrystaes:master

Current state:

  • Not tested actual connect yet.
  • Not sure about receive_blocking implementation yet.
  • Not configurable yet. (host + port)

@barrystaes
Copy link
Author

Ok so i just added the working socket code to my repo. 😄

Current state:

  • Test network restore connection behavior, compare to serial
  • Not configurable yet. (host + port)
  • Found potential race condition (timeout but no retry) in existing serial receive code.

Using PyCharm with breakpoints and such was of great help, only had to add Docker and configure that as interpreter.

@barrystaes
Copy link
Author

Ok i think the feature is ready, going to make a PR now.

Usage minimal example:

RFXtrx.Core(('192.168.0.123', 10001), transport_protocol=RFXtrx.PyNetworkTransport)

Todo:

  • make a PR for pyRFXtrx for this
  • make a PR for Home Assistant to enable usage.
  • hear from @Danielhiversen am i on the right track here?

@barrystaes barrystaes changed the title Adding network to rfxtrx Adding network to rfxtrx [see PR] Jul 26, 2019
@barrystaes
Copy link
Author

barrystaes commented Jul 29, 2019

Network support is now in v0.24.0

If there are questions or suggestions for me regarding network feature, mention me with @ please so it gets my attention.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant