Further implementation
This commit is contained in:
parent
d92af91893
commit
94c613abe9
12
MAXDevice.py
Normal file
12
MAXDevice.py
Normal 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
|
14
MAXPacket.py
14
MAXPacket.py
@ -118,6 +118,20 @@ class MAXAckPacket(MAXPacket):
|
|||||||
return result
|
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):
|
class PushButtonState(Enum):
|
||||||
AUTO = 0
|
AUTO = 0
|
||||||
ECO = 1
|
ECO = 1
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
from MAXCube import MAXCube
|
from MAXCube import MAXCube
|
||||||
from MAXPacket import MAXPacket, MAXPacketFactory, MAXPairPingPacket, MAXPairPongPacket, MAXAckPacket, \
|
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 time import sleep
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
@ -26,6 +27,7 @@ class MAXPacketHandler:
|
|||||||
self.cube = cube
|
self.cube = cube
|
||||||
self.quit_flag = False
|
self.quit_flag = False
|
||||||
self.handshakes = []
|
self.handshakes = []
|
||||||
|
self.devices: list[MAXDevice] = []
|
||||||
|
|
||||||
def handle_msg(self, pkt: MAXPacket):
|
def handle_msg(self, pkt: MAXPacket):
|
||||||
print(pkt.to_string())
|
print(pkt.to_string())
|
||||||
@ -36,7 +38,7 @@ class MAXPacketHandler:
|
|||||||
pong = MAXPairPongPacket(message_counter="00", message_flag="00", sender_address=self.cube.addr,
|
pong = MAXPairPongPacket(message_counter="00", message_flag="00", sender_address=self.cube.addr,
|
||||||
dest_address=pkt.sender_address, group_id="00")
|
dest_address=pkt.sender_address, group_id="00")
|
||||||
pong_str = pong.serialize()
|
pong_str = pong.serialize()
|
||||||
print(pong.to_string())
|
#print(pong.to_string())
|
||||||
self.cube.request(pong_str)
|
self.cube.request(pong_str)
|
||||||
handshake.state = HandshakeState.PONG_SENT
|
handshake.state = HandshakeState.PONG_SENT
|
||||||
elif isinstance(pkt, MAXAckPacket):
|
elif isinstance(pkt, MAXAckPacket):
|
||||||
@ -63,7 +65,22 @@ class MAXPacketHandler:
|
|||||||
cur_handshake.state = HandshakeState.GROUP_ID_SENT
|
cur_handshake.state = HandshakeState.GROUP_ID_SENT
|
||||||
if cur_handshake.state == HandshakeState.GROUP_ID_SENT:
|
if cur_handshake.state == HandshakeState.GROUP_ID_SENT:
|
||||||
cur_handshake.state = HandshakeState.GROUP_ID_ACK
|
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:
|
else:
|
||||||
print("Paket is not ours!")
|
print("Paket is not ours!")
|
||||||
|
|
||||||
@ -73,6 +90,4 @@ class MAXPacketHandler:
|
|||||||
if resp is not None and resp[0:1] == "Z":
|
if resp is not None and resp[0:1] == "Z":
|
||||||
pkt = MAXPacketFactory.create_packet(resp)
|
pkt = MAXPacketFactory.create_packet(resp)
|
||||||
self.handle_msg(pkt)
|
self.handle_msg(pkt)
|
||||||
|
|
||||||
|
|
||||||
sleep(0.1)
|
sleep(0.1)
|
2
main.py
2
main.py
@ -5,7 +5,7 @@ DEBUG = True
|
|||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
cube = CUL("123456")
|
cube = CUL("123456")
|
||||||
cube.connect("COM11")
|
cube.connect("COM5")
|
||||||
print(cube.version_string())
|
print(cube.version_string())
|
||||||
handler = MAXPacketHandler(cube)
|
handler = MAXPacketHandler(cube)
|
||||||
handler.receive_loop()
|
handler.receive_loop()
|
||||||
|
Loading…
Reference in New Issue
Block a user