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

ESP2 Protocol with Eltako Manufacturer Device Classes #78

Open
wants to merge 1 commit into
base: master
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
108 changes: 108 additions & 0 deletions enocean/communicators/ESP2communicator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# -*- encoding: utf-8 -*-
from __future__ import print_function, unicode_literals, division, absolute_import
import logging

import threading
try:
import queue
except ImportError:
import Queue as queue
from enocean.protocol.packet import Packet
from enocean.protocol.constants import PACKET, PARSE_RESULT, RETURN_CODE


class ESP2Communicator(threading.Thread):
'''
Communicator base-class for EnOcean.
Not to be used directly, only serves as base class for SerialCommunicator etc.
'''
logger = logging.getLogger('enocean.communicators.ESP2Communicator')

def __init__(self, callback=None, teach_in=True):
super(ESP2Communicator, self).__init__()
# Create an event to stop the thread
self._stop_flag = threading.Event()
# Input buffer
self._buffer = []
# Setup packet queues
self.transmit = queue.Queue()
self.receive = queue.Queue()
# Set the callback method
self.__callback = callback
# Internal variable for the Base ID of the module.
self._base_id = None
# Should new messages be learned automatically? Defaults to True.
# TODO: Not sure if we should use CO_WR_LEARNMODE??
self.teach_in = teach_in

def _get_from_send_queue(self):
''' Get message from send queue, if one exists '''
try:
packet = self.transmit.get(block=False)
#self.logger.info('Sending packet')
#self.logger.debug(packet)
return packet
except queue.Empty:
pass
return None

def send(self, packet):
if not isinstance(packet, Packet):
self.logger.error('Object to send must be an instance of Packet')
return False
self.transmit.put(packet)
return True

def stop(self):
self._stop_flag.set()

def parse(self):
''' Parses messages and puts them to receive queue '''
# Loop while we get new messages
while True:
status, self._buffer, packet = Packet.parse_msg_ESP2(self._buffer, communicator=self)
# If message is incomplete -> break the loop
if status == PARSE_RESULT.INCOMPLETE:
return status

# If message is OK, add it to receive queue or send to the callback method
if status == PARSE_RESULT.OK and packet:
if self.__callback is None:
self.receive.put(packet)
else:
self.__callback(packet)
#self.logger.debug(packet)

@property
def base_id(self):
''' Fetches Base ID from the transmitter, if required. Otherwise returns the currently set Base ID. '''
# If base id is already set, return it.
if self._base_id is not None:
return self._base_id

# Send COMMON_COMMAND 0x08, CO_RD_IDBASE request to the module
self.send(Packet(PACKET.COMMON_COMMAND, data=[0x08]))
# Loop over 10 times, to make sure we catch the response.
# Thanks to timeout, shouldn't take more than a second.
# Unfortunately, all other messages received during this time are ignored.
for i in range(0, 10):
try:
packet = self.receive.get(block=True, timeout=0.1)
# We're only interested in responses to the request in question.
if packet.packet_type == PACKET.RESPONSE and packet.response == RETURN_CODE.OK and len(packet.response_data) == 4:
# Base ID is set in the response data.
self._base_id = packet.response_data
# Put packet back to the Queue, so the user can also react to it if required...
self.receive.put(packet)
break
# Put other packets back to the Queue.
self.receive.put(packet)
except queue.Empty:
continue
# Return the current Base ID (might be None).
return self._base_id

@base_id.setter
def base_id(self, base_id):
''' Sets the Base ID manually, only for testing purposes. '''
self._base_id = base_id
43 changes: 43 additions & 0 deletions enocean/communicators/ESP2serialcommunicator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# -*- encoding: utf-8 -*-
from __future__ import print_function, unicode_literals, division, absolute_import
import logging
import serial
import time

from enocean.communicators.ESP2communicator import ESP2Communicator


class ESP2SerialCommunicator(ESP2Communicator):
''' Serial port communicator class for EnOcean radio '''
logger = logging.getLogger('enocean.communicators.ESP2SerialCommunicator')

def __init__(self, port='/dev/ttyS0', callback=None):
super(ESP2SerialCommunicator, self).__init__(callback)
# Initialize serial port
self.__ser = serial.Serial(port, 57600, timeout=0.1)

def run(self):
self.logger.info('SerialCommunicator started')
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.__ser.write(bytearray(packet.build_ESP2()))
except serial.SerialException:
self.stop()

# Read chars from serial port as hex numbers
try:
self._buffer.extend(bytearray(self.__ser.read(16)))
except serial.SerialException:
self.logger.error('Serial port exception! (device disconnected or multiple access on port?)')
self.stop()
self.parse()
time.sleep(0)

self.__ser.close()
self.logger.info('SerialCommunicator stopped')
Loading