import socket
import struct
import ctypes
from Grifo_E_1553lib import mil1553udp
from Grifo_E_1553lib.mil1553udp import UDP1553Header
from Grifo_E_1553lib.messages.msg_rdr_settings_and_parameters import MsgRdrSettingsAndParameters
from Grifo_E_1553lib.messages.msg_rdr_operation_command import MsgRdrOperationCommand
from Grifo_E_1553lib.data_types.enums import TargetHistory, RdrModes

UDP_IP = "127.0.0.1"
UDP_PORT = 61553

print(f"Sending to {UDP_IP}:{UDP_PORT}")

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

# Crea un header UDP1553 di esempio
header = UDP1553Header()
header.marker1553 = 0x1553
header.otype = 0x4342  # BC
packed_header = bytes(header)

# Crea un messaggio MsgRdrSettingsAndParameters di esempio
message = MsgRdrSettingsAndParameters()
message.settings.set_history_level(TargetHistory.TARGET_HISTORY_LEVEL_03)
packed_message = bytes(message)

# Combina l'header e il messaggio
message = packed_header + struct.pack(">H",message.command_word) + packed_message

sock.sendto(message, (UDP_IP, UDP_PORT))

print("Message sent!")