Skip to content

Commit

Permalink
Merge 8099d74 into cecf7dd
Browse files Browse the repository at this point in the history
  • Loading branch information
stv0g committed Mar 4, 2019
2 parents cecf7dd + 8099d74 commit c6e6a23
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 33 deletions.
11 changes: 9 additions & 2 deletions enocean/communicators/communicator.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import queue
except ImportError:
import Queue as queue
from enocean.protocol.packet import Packet
from enocean.protocol.packet import Packet, UTETeachInPacket
from enocean.protocol.constants import PACKET, PARSE_RESULT, RETURN_CODE


Expand Down Expand Up @@ -60,13 +60,20 @@ 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(self._buffer, communicator=self)
status, self._buffer, packet = Packet.parse_msg(self._buffer)
# 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 isinstance(packet, UTETeachInPacket) and self.teach_in:
response_packet = packet.create_response_packet(self.base_id)

self.logger.info('Sending response to UTE teach-in.')
self.send(response_packet)

if self.__callback is None:
self.receive.put(packet)
else:
Expand Down
29 changes: 5 additions & 24 deletions enocean/protocol/packet.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def _bit_status(self, value):
self.status = enocean.utils.from_bitarray(value)

@staticmethod
def parse_msg(buf, communicator=None):
def parse_msg(buf):
'''
Parses message from buffer.
returns:
Expand Down Expand Up @@ -144,11 +144,7 @@ def parse_msg(buf, communicator=None):
if packet_type == PACKET.RADIO:
# Need to handle UTE Teach-in here, as it's a separate packet type...
if data[0] == RORG.UTE:
packet = UTETeachIn(packet_type, data, opt_data, communicator=communicator)
# Send a response automatically, works only if
# - communicator is set
# - communicator.teach_in == True
packet.send_response()
packet = UTETeachInPacket(packet_type, data, opt_data)
else:
packet = RadioPacket(packet_type, data, opt_data)
elif packet_type == PACKET.RESPONSE:
Expand Down Expand Up @@ -346,7 +342,7 @@ def parse(self):
return super(RadioPacket, self).parse()


class UTETeachIn(RadioPacket):
class UTETeachInPacket(RadioPacket):
# Request types
TEACH_IN = 0b00
DELETE = 0b01
Expand All @@ -367,10 +363,6 @@ class UTETeachIn(RadioPacket):

contains_eep = True

def __init__(self, packet_type, data=None, optional=None, communicator=None):
self.__communicator = communicator
super(UTETeachIn, self).__init__(packet_type=packet_type, data=data, optional=optional)

@property
def bidirectional(self):
return not self.unidirectional
Expand All @@ -384,7 +376,7 @@ def delete(self):
return self.request_type == self.DELETE

def parse(self):
super(UTETeachIn, self).parse()
super(UTETeachInPacket, self).parse()
self.unidirectional = not self._bit_data[DB6.BIT_7]
self.response_expected = not self._bit_data[DB6.BIT_6]
self.request_type = enocean.utils.from_bitarray(self._bit_data[DB6.BIT_5:DB6.BIT_3])
Expand All @@ -397,7 +389,7 @@ def parse(self):
self.learn = True
return self.parsed

def _create_response_packet(self, sender_id, response=TEACHIN_ACCEPTED):
def create_response_packet(self, sender_id, response=TEACHIN_ACCEPTED):
# Create data:
# - Respond with same RORG (UTE Teach-in)
# - Always use bidirectional communication, set response code, set command identifier.
Expand All @@ -413,17 +405,6 @@ def _create_response_packet(self, sender_id, response=TEACHIN_ACCEPTED):

return RadioPacket(PACKET.RADIO, data=data, optional=optional)

def send_response(self, response=TEACHIN_ACCEPTED):
if self.__communicator is None:
self.logger.error('Communicator not set, cannot send UTE teach-in response.')
return
if not self.__communicator.teach_in:
self.logger.info('Communicator not set to teach-in mode, not sending UTE teach-in response.')
return
self.logger.info('Sending response to UTE teach-in.')
self.__communicator.send(
self._create_response_packet(self.__communicator.base_id))


class ResponsePacket(Packet):
response = 0
Expand Down
5 changes: 2 additions & 3 deletions enocean/protocol/tests/test_teachin.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ def test_ute_in():
0xD4, 0xA0, 0xFF, 0x3E, 0x00, 0x01, 0x01, 0xD2, 0x01, 0x94, 0xE3, 0xB9, 0x00,
0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0x40, 0x00,
0xAB
]),
communicator=communicator
])
)

assert packet.sender_hex == '01:94:E3:B9'
Expand All @@ -38,7 +37,7 @@ def test_ute_in():
assert packet.learn is True
assert packet.contains_eep is True

response_packet = packet._create_response_packet(communicator.base_id)
response_packet = packet.create_response_packet(communicator.base_id)
assert response_packet.sender_hex == 'DE:AD:BE:EF'
assert response_packet.destination_hex == '01:94:E3:B9'
assert response_packet._bit_data[DB6.BIT_5:DB6.BIT_3] == [False, True]
Expand Down
4 changes: 2 additions & 2 deletions examples/example_D2-05-00.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import traceback
import enocean.utils
from enocean.communicators import SerialCommunicator
from enocean.protocol.packet import RadioPacket, UTETeachIn
from enocean.protocol.packet import RadioPacket, UTETeachInPacket
from enocean.protocol.constants import RORG

try:
Expand Down Expand Up @@ -47,7 +47,7 @@ def set_position(destination, percentage):
try:
# Loop to empty the queue...
packet = communicator.receive.get(block=True, timeout=1)
if isinstance(packet, UTETeachIn):
if isinstance(packet, UTETeachInPacket):
print('New device learned! The ID is %s.' % (packet.sender_hex))
devices_learned.append(packet.sender)
except queue.Empty:
Expand Down
4 changes: 2 additions & 2 deletions examples/example_DO21-11B-E.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import traceback
import enocean.utils
from enocean.communicators import SerialCommunicator
from enocean.protocol.packet import RadioPacket, UTETeachIn
from enocean.protocol.packet import RadioPacket, UTETeachInPacket
from enocean.protocol.constants import RORG

try:
Expand Down Expand Up @@ -60,7 +60,7 @@ def turn_off(destination):
try:
# Loop to empty the queue...
packet = communicator.receive.get(block=True, timeout=1)
if isinstance(packet, UTETeachIn):
if isinstance(packet, UTETeachInPacket):
print('New device learned! The ID is %s.' % (packet.sender_hex))
devices_learned.append(packet.sender)
except queue.Empty:
Expand Down

0 comments on commit c6e6a23

Please sign in to comment.