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

Read EnOcean Chip ID and module version info #136

Open
wants to merge 3 commits 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
84 changes: 78 additions & 6 deletions enocean/communicators/communicator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
import datetime

import threading
from enocean.protocol.version_info import VersionInfo

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


class Communicator(threading.Thread):
Expand All @@ -32,6 +34,8 @@ def __init__(self, callback=None, teach_in=True):
self.__callback = callback
# Internal variable for the Base ID of the module.
self._base_id = None
# Internal variable for the version info of the module.
self._version_info = 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
Expand Down Expand Up @@ -88,12 +92,17 @@ def base_id(self):
if self._base_id is not None:
return self._base_id

start = datetime.datetime.now()

# 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):
self.send(Packet(PACKET.COMMON_COMMAND, data=[COMMON_COMMAND_CODE.CO_RD_IDBASE.value]))

# wait at most 1 second for the response
while True:
seconds_elapsed = (datetime.datetime.now() - start).total_seconds()
if seconds_elapsed > 1:
self.logger.error("Could not obtain base id from module within 1 second (timeout).")
break
try:
packet = self.receive.get(block=True, timeout=0.1)
# We're only interested in responses to the request in question.
Expand All @@ -114,3 +123,66 @@ def base_id(self):
def base_id(self, base_id):
''' Sets the Base ID manually, only for testing purposes. '''
self._base_id = base_id

@property
def chip_id(self):
''' Fetches Chip ID from the transmitter, if required. Otherwise returns the currently set Chip ID. '''
if self.version_info is not None:
return self.version_info.chip_id

return None

@property
def version_info(self):
''' Fetches version info from the transmitter, if required. Otherwise returns the currently set version info. '''

# If version info is already set, return it.
if self._version_info is not None:
return self._version_info

start = datetime.datetime.now()

# Send COMMON_COMMAND 0x03, CO_RD_VERSION request to the module
self.send(Packet(PACKET.COMMON_COMMAND, data=[COMMON_COMMAND_CODE.CO_RD_VERSION.value]))

# wait at most 1 second for the response
while True:
seconds_elapsed = (datetime.datetime.now() - start).total_seconds()
if seconds_elapsed > 1:
self.logger.error("Could not obtain version info from module within 1 second (timeout).")
break

try:
packet = self.receive.get(block=True, timeout=0.1)
if packet.packet_type == PACKET.RESPONSE and packet.response == RETURN_CODE.OK and len(packet.response_data) == 32:
# interpret the version info
self._version_info: VersionInfo = VersionInfo()
res = packet.response_data

self._version_info.app_version.main = res[0]
self._version_info.app_version.beta = res[1]
self._version_info.app_version.alpha = res[2]
self._version_info.app_version.build = res[3]

self._version_info.api_version.main = res[4]
self._version_info.api_version.beta = res[5]
self._version_info.api_version.alpha = res[6]
self._version_info.api_version.build = res[7]

self._version_info.chip_id = [
res[8], res[9], res[10], res[11]
]
self._version_info.chip_version = int.from_bytes(res[12:15], 'big')

self._version_info.app_description = bytearray(res[16:32]).decode('utf8').strip()

# 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 version info (might be None).
return self._version_info

4 changes: 4 additions & 0 deletions enocean/protocol/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,7 @@ class DB6(object):
BIT_5 = -54
BIT_6 = -55
BIT_7 = -56

class COMMON_COMMAND_CODE(IntEnum):
CO_RD_VERSION = 0x03
CO_RD_IDBASE = 0x08
15 changes: 15 additions & 0 deletions enocean/protocol/version_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# -*- encoding: utf-8 -*-
from __future__ import print_function, unicode_literals, division, absolute_import

class VersionIdentifier(object):
main = 0
beta = 0
alpha = 0
build = 0

class VersionInfo(object):
app_version: VersionIdentifier = VersionIdentifier()
api_version: VersionIdentifier = VersionIdentifier()
chip_id: 0
chip_version = 0
app_description = ''
1 change: 1 addition & 0 deletions examples/enocean_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def assemble_radio_packet(transmitter_id):
init_logging()
communicator = SerialCommunicator()
communicator.start()
print('The Chip ID of your module is %s.' % enocean.utils.to_hex_string(communicator.chip_id))
print('The Base ID of your module is %s.' % enocean.utils.to_hex_string(communicator.base_id))

if communicator.base_id is not None:
Expand Down