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

support TCP-linked transceivers #130

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions enoceanmqtt/communicator.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import platform

from enocean.communicators.serialcommunicator import SerialCommunicator
from enoceanmqtt.tcpclientcommunicator import TCPClientCommunicator
from enocean.protocol.packet import RadioPacket
from enocean.protocol.constants import PACKET, RETURN_CODE, RORG
import enocean.utils
Expand Down Expand Up @@ -65,7 +66,15 @@ def __init__(self, config, sensors):
self.mqtt.loop_start()

# setup enocean communication
self.enocean = SerialCommunicator(self.conf['enocean_port'])
eport = self.conf['enocean_port']
seport = eport.split(':')
if seport[0] == "tcp":
logging.info("connecting TCPClient to %s port %d", seport[1],int(seport[2]))
self.enocean = TCPClientCommunicator(seport[1],int(seport[2]))
else:
logging.info("connecting Serial to %s", eport)
self.enocean = SerialCommunicator(eport)

self.enocean.start()
# sender will be automatically determined
self.enocean_sender = None
Expand Down Expand Up @@ -532,11 +541,7 @@ def run(self):
# Loop to empty the queue...
try:
# get next packet
if platform.system() == 'Windows':
# only timeout on Windows for KeyboardInterrupt checking
packet = self.enocean.receive.get(block=True, timeout=1)
else:
packet = self.enocean.receive.get(block=True)
packet = self.enocean.receive.get(block=True, timeout=1)

# check packet type
if packet.packet_type == PACKET.RADIO:
Expand Down
68 changes: 68 additions & 0 deletions enoceanmqtt/tcpclientcommunicator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@

# -*- encoding: utf-8 -*-
from __future__ import print_function, unicode_literals, division, absolute_import
import logging
import socket
import time


from enocean.communicators.communicator import Communicator
from enocean.protocol.constants import PACKET
from enocean.protocol.packet import Packet


class TCPClientCommunicator(Communicator):
''' Socket communicator class for EnOcean radio '''
logger = logging.getLogger('enocean.communicators.TCPClientCommunicator')

def __init__(self, host='', port=9637):
super(TCPClientCommunicator, self).__init__()
self.host = host
self.port = port
self.sock = socket.socket( socket.AF_INET, socket.SOCK_STREAM)

def run(self):
self.logger.info('TCPClientCommunicator started')
self.sock.settimeout(3)
pinged = time.time()

try:
self.sock.connect(( self.host, self.port))
except Exception as e:
self.logger.error('Exception occured while connecting: ' + str(e))
self.stop()

self.sock.settimeout(0.5)

while not self._stop_flag.is_set():
# If there's messages in transmit queue
# send them
while True:
packet = self._get_from_send_queue()
if not packet:
break
try:
self.sock.send(bytearray(packet.build()))
pinged = time.time()
except Exception as e:
self.logger.error('Exception occured while sending: ' + str(e))
self.stop()

try:
self._buffer.extend(bytearray(self.sock.recv(16)))
self.parse()
except socket.timeout:
time.sleep(0)
except ConnectionResetError as e:
self.logger.error('Exception occured while recv: ' + str(e))
self.stop()
except Exception as e:
self.logger.error('Exception occured while parsing: ' + str(e))

time.sleep(0)
if time.time() > pinged + 30:
self.send(Packet(PACKET.COMMON_COMMAND, data=[0x08]))
pinged = time.time()

self.sock.close()
self.logger.info('TCPClientCommunicator stopped')