from cun import CUL, MAXPacketFactory, MAXPacket, MAXPairPingPacket, MAXPairPongPacket
import time

if __name__ == '__main__':
    #cun = CUN()
    #cun.connect("192.168.0.244", 2323)
    #cun.request(b"V\n")
    #print(cun.response())
    #version = cun.version_string()
    #print(version)
    #cun.set_moritz_mode(True)
    #print(cun.send_moritz())
    #cun.request("Zsl:")
    #print(cun.response())
    #cun.disconnect()

    cul = CUL()
    cul.connect("COM8")
    print(cul.version_string())
    cul.set_moritz_mode(True)
    while cul.is_connected():
        resp = cul.response()
        if resp is not None and resp[0:1] == "Z":
            pkt = MAXPacketFactory.create_packet(resp)
            print(pkt.to_string())
            if isinstance(pkt, MAXPairPingPacket):
                print("Sending Pong!")
                pong = MAXPairPongPacket(message_counter="00", message_flag="00", sender_address="FDF7CA",
                                         dest_address=pkt.sender_address, group_id="00")
                pong_str = pong.serialize()
                cul.request(pong_str)

        time.sleep(1)
    cul.disconnect()

