import socket
import json
import random
import time

# 创建UDP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

def send(addr, port, data):
    try:
        json_data = json.dumps(data)
        message = json_data.encode('utf-8')
        # 发送数据
        sock.sendto(message, (addr, port))
        # print(f"send to {addr}:{port}")
        # print(f"content: {data}")
    except Exception as e:
        print(f"error: {e}")
    finally:
        pass


if __name__ == "__main__":
    # 目标地址和端口
    addr = "192.168.1.101"  # 本地回环地址
    port = 18000

    while True:
        data = {
            # TimeHistory
            "IASC2_CHS_ARINC_OUT_L231_CH_FAULT": random.uniform(-50.0, 1001.0),
            "IASC2B_ARINC_OUT_L260_TAV2_OPEN": random.uniform(-50.0, 1001.0),
            "IASC2A_ARINC_OUT_L270_CABIN_NEG_DIFF_PRESS_W": random.uniform(-50.0, 1001.0),
            "IASC1B_ARINC_OUT_L270_CABIN_HI_DIFF_PRESS_W": random.uniform(-50.0, 1001.0),
            # Dial
            "IASC2B_ARINC_OUT_L270_HAAO_MODE_ON": random.uniform(0.0, 100.0),
            "IASC2A_ARINC_OUT_L272_FCV_FAIL_IN_POSITION": random.uniform(0.0, 100.0),
            # Number
            "IASC2B_ARINC_OUT_L272_PACK_DEGRADED": random.uniform(-5.0, 5.0),
            "IASC1A_ARINC_OUT_L272_TRIM_AIR_DEGRADED": random.uniform(-5.0, 5.0),
            "IASC2B_ARINC_OUT_L272_TRIM_AIR_OFF": random.uniform(-5.0, 5.0),
            "IASC2B_ARINC_OUT_L272_TAPRV_OPEN": random.uniform(-5.0, 5.0),
            "IASC2A_ARINC_OUT_L304_PACK_CLOSE_CMD": random.uniform(-5.0, 5.0),
            # CrossCoordinates
            "IASC2A_ARINC_OUT_L306_FWD_EFAN_SPEED_CMD": random.uniform(-5.0, 5.0),
            "LRM_B1_L345_R_Lane1_Shield6_Failed_Shorted": random.uniform(-5.0, 5.0),
            "LRM_B1_L344_L_Lane2_Shield6_Failed_Open": random.uniform(-5.0, 5.0),
            "LRM_A1_L347_L_FRDC_CH_A_CANbus_Fault": random.uniform(-5.0, 5.0),
            # Envelope
            "LRM_A1_L344_L_Lane2_Shield6_Failed_Open": random.uniform(-2.0, 2.0),
            "FADEC_L_CHB_PHM_Maintenance_Word_9_Fault_Repot_021": random.uniform(-2.0, 2.0),
            # ScaleSlider
            "HM_R1_RDIU_07_Status_RDIU_RTD_PT500_01_Validity_Fault": random.uniform(-10.0, 30.0),
            "FCM_1_429_IDU_LI_LO_L204_SSM_FCM1": random.uniform(-10.0, 30.0),
            # RollingBelt
            "EPS1_L_BPCU_LTRU_VOLT": random.uniform(-100.0, 600.0),
            # SignalLight
            "sp_op_xch_1_005_wd_08_spare_3D84_17_27_27": random.uniform(-5.0, 5.0),
            "ssu_capt_ap_det_sov_tst_flt_set_3D65": random.uniform(-5.0, 5.0),
            "sp_op_xch_1_010_wd_01_spare_2D83_3_25_25": random.uniform(-5.0, 5.0),
            "ail_rib_adb_health_mon_tst_maint_rst_2D65": random.uniform(-5.0, 5.0),
            "pf_op_xch_1_040_wd_10_spare_2D62_21_0_31": random.uniform(-5.0, 5.0),
            "irs_1_ir_vert_accel_comp_fl_1D84": random.uniform(-5.0, 5.0),
            "pf_op_xch_1_005_wd_21_spare_1D65_44_8_8": random.uniform(-5.0, 5.0),
            "elev_l_rig_positive_flt_set_1D65": random.uniform(-5.0, 5.0),
            "RUD_LWR_ENGAGE_EHSV_HI_CMD_WA_REU_7D44": random.uniform(-5.0, 5.0),
            "AOA1_L1_SIN_4D30": random.uniform(-5.0, 5.0) 
        }

        send(addr, port, data)
        time.sleep(1 / 32)