Skip to content

Commit

Permalink
Tidy up broadcast in python client
Browse files Browse the repository at this point in the history
  • Loading branch information
zvecr committed Aug 29, 2023
1 parent e9206d2 commit 87fd4b4
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
1 change: 1 addition & 0 deletions data/templates/xap/client/python/types.py.j2
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class {{ name }}(namedtuple('{{ name }}', '{{ members }}')):
{% set type_definitions = [
{'name':'XAPRequest', 'members': 'token length data', 'fmt':'<HB61s'},
{'name':'XAPResponse', 'members': 'token flags length data', 'fmt':'<HBB60s'},
{'name':'XAPBroadcast', 'members': 'token event length data', 'fmt':'<HBB60s'},
{'name':'XAPConfigBacklight', 'members': 'enable mode val', 'fmt':'<BBB'},
{'name':'XAPConfigRgblight', 'members': 'enable mode hue sat val speed', 'fmt':'<BBBBBB'},
{'name':'XAPConfigRgbMatrix', 'members': 'enable mode hue sat val speed flags', 'fmt':'<BBBBBBB'}
Expand Down
2 changes: 1 addition & 1 deletion data/xap/xap_0.1.0.hjson
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
type_docs: {
bool:
'''
Data type that contains values 0 and 1. Implementaed as an alias of `u8`.
Data type that contains values 0 and 1. Implemented as an alias of `u8`.
'''
u64:
'''
Expand Down
6 changes: 3 additions & 3 deletions lib/python/xap_client/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from struct import pack, unpack
from platform import platform

from .types import XAPSecureStatus, XAPFlags, XAPRequest, XAPResponse
from .types import XAPSecureStatus, XAPFlags, XAPRequest, XAPResponse, XAPBroadcast
from .routes import XAPRoutes, XAPRouteError


Expand Down Expand Up @@ -116,8 +116,8 @@ def listen(self) -> dict:
while not hasattr(event, '_ret'):
event.wait(timeout=0.25)

r = XAPResponse.from_bytes(event._ret)
return (r.flags, r.data[:r.length])
r = XAPBroadcast.from_bytes(event._ret)
return (r.event, r.data[:r.length])


class XAPDevice(XAPDeviceBase):
Expand Down
14 changes: 14 additions & 0 deletions lib/python/xap_client/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,20 @@ def to_bytes(self):
return self.fmt.pack(*list(self))


class XAPBroadcast(namedtuple('XAPBroadcast', 'token event length data')):
fmt = Struct('<HBB60s')

def __new__(cls, *args):
return super().__new__(cls, *args)

@classmethod
def from_bytes(cls, data):
return cls._make(cls.fmt.unpack(data))

def to_bytes(self):
return self.fmt.pack(*list(self))


class XAPConfigBacklight(namedtuple('XAPConfigBacklight', 'enable mode val')):
fmt = Struct('<BBB')

Expand Down

0 comments on commit 87fd4b4

Please sign in to comment.