Further implementation

This commit is contained in:
Christian Loch 2021-09-06 01:30:04 +02:00
parent d92af91893
commit 94c613abe9
4 changed files with 47 additions and 6 deletions

12
MAXDevice.py Normal file
View File

@ -0,0 +1,12 @@
from MAXPacket import PushButtonState
class MAXDevice:
def __init__(self, address):
self.address = address
class MAXPushButton(MAXDevice):
def __init__(self, address):
super(MAXPushButton, self).__init__(address)
self.state = PushButtonState.UNKNOWN

View File

@ -118,6 +118,20 @@ class MAXAckPacket(MAXPacket):
return result
class MAXCubeAckPacket(MAXPacket):
def __init__(self, message_counter: str, message_flag: str, sender_address: str, dest_address: str,
group_id: str, acknowledged):
self.set_values(message_counter, "01", message_flag, sender_address, dest_address, group_id)
if acknowledged:
self.payload = "01"
else:
self.payload = "81"
def to_string(self):
result = "{}\nMAXCubeAckPacket: payload={}".format(super().to_string(), self.payload)
return result
class PushButtonState(Enum):
AUTO = 0
ECO = 1

View File

@ -1,6 +1,7 @@
from MAXCube import MAXCube
from MAXPacket import MAXPacket, MAXPacketFactory, MAXPairPingPacket, MAXPairPongPacket, MAXAckPacket, \
MAXResetPacket, MAXWakeUpPacket, MAXSetGroupIdPacket
MAXResetPacket, MAXWakeUpPacket, MAXSetGroupIdPacket, MAXPushButtonPacket, MAXCubeAckPacket
from MAXDevice import MAXDevice, MAXPushButton
from time import sleep
from enum import Enum
@ -26,6 +27,7 @@ class MAXPacketHandler:
self.cube = cube
self.quit_flag = False
self.handshakes = []
self.devices: list[MAXDevice] = []
def handle_msg(self, pkt: MAXPacket):
print(pkt.to_string())
@ -36,7 +38,7 @@ class MAXPacketHandler:
pong = MAXPairPongPacket(message_counter="00", message_flag="00", sender_address=self.cube.addr,
dest_address=pkt.sender_address, group_id="00")
pong_str = pong.serialize()
print(pong.to_string())
#print(pong.to_string())
self.cube.request(pong_str)
handshake.state = HandshakeState.PONG_SENT
elif isinstance(pkt, MAXAckPacket):
@ -63,7 +65,22 @@ class MAXPacketHandler:
cur_handshake.state = HandshakeState.GROUP_ID_SENT
if cur_handshake.state == HandshakeState.GROUP_ID_SENT:
cur_handshake.state = HandshakeState.GROUP_ID_ACK
elif isinstance(pkt, MAXPushButtonPacket):
dev = None
for known_dev in self.devices:
if known_dev.address == pkt.sender_address:
dev = known_dev
break
if dev is None:
dev = MAXPushButton(pkt.sender_address)
dev.state = pkt.button_state
print("Button {} state={}".format(dev.address, dev.state))
if not pkt.retransmit:
act_pkt = MAXCubeAckPacket(pkt.counter, "00", self.cube.addr,
pkt.sender_address, pkt.group_id, True)
act_str = act_pkt.serialize()
#print(act_str)
self.cube.request(act_str)
else:
print("Paket is not ours!")
@ -73,6 +90,4 @@ class MAXPacketHandler:
if resp is not None and resp[0:1] == "Z":
pkt = MAXPacketFactory.create_packet(resp)
self.handle_msg(pkt)
sleep(0.1)

View File

@ -5,7 +5,7 @@ DEBUG = True
if __name__ == '__main__':
cube = CUL("123456")
cube.connect("COM11")
cube.connect("COM5")
print(cube.version_string())
handler = MAXPacketHandler(cube)
handler.receive_loop()