package com.data.emqx.test;

import com.LoaderApplication;
import com.data.emqx.service.IDeviceDataService;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ApplicationContext;

/**
 * @author licd
 * @className DeviceDataTest
 * @description 设备数据保存测试
 * @date 2026/03/23
 */
public class DeviceDataTest {

    public static void main(String[] args) {
        System.out.println("=== 设备数据保存测试 ===");
        
        // 启动Spring Boot应用
        ApplicationContext context = SpringApplication.run(LoaderApplication.class, args);
        
        // 获取设备数据服务
        IDeviceDataService deviceDataService = context.getBean(IDeviceDataService.class);
        
        if (deviceDataService == null) {
            System.err.println("无法获取DeviceDataService实例");
            return;
        }
        
        System.out.println("成功获取DeviceDataService实例");
        
        // 测试保存称重&标签数据
        testSaveWeightRfidData(deviceDataService);
        
        // 测试保存告警数据
        testSaveAlarmData(deviceDataService);
        
        // 测试保存心跳数据
        testSaveHeartData(deviceDataService);
        
        System.out.println("=== 测试完成 ===");
        
        // 关闭应用
        System.exit(0);
    }

    /**
     * 测试保存称重&标签数据
     */
    private static void testSaveWeightRfidData(IDeviceDataService deviceDataService) {
        System.out.println("\n=== 测试保存称重&标签数据 ===");
        try {
            String deviceId = "test_device_001";
            Long seqNum = 123456789L;
            Long sendTime = System.currentTimeMillis() / 1000;
            Integer weight = 1000;
            String rfid = "e280111122223333,b001111122223333";

            deviceDataService.saveWeightRfidData(deviceId, seqNum, sendTime, weight, rfid);
            System.out.println("称重&标签数据保存测试成功");
        } catch (Exception e) {
            System.err.println("称重&标签数据保存测试失败: " + e.getMessage());
            e.printStackTrace();
        }
    }

    /**
     * 测试保存告警数据
     */
    private static void testSaveAlarmData(IDeviceDataService deviceDataService) {
        System.out.println("\n=== 测试保存告警数据 ===");
        try {
            String deviceId = "test_device_001";
            Long seqNum = 987654321L;
            Long sendTime = System.currentTimeMillis() / 1000;
            String uwb = "ID:1234,dist:2000; ID:5678,dist:1500";
            com.alibaba.fastjson.JSONArray uwbArray = new com.alibaba.fastjson.JSONArray();
            com.alibaba.fastjson.JSONObject uwbObj1 = new com.alibaba.fastjson.JSONObject();
            uwbObj1.put("ID", "1234");
            uwbObj1.put("Dist", "2000 cm");
            uwbArray.add(uwbObj1);
            com.alibaba.fastjson.JSONObject uwbObj2 = new com.alibaba.fastjson.JSONObject();
            uwbObj2.put("ID", "5678");
            uwbObj2.put("Dist", "1500 cm");
            uwbArray.add(uwbObj2);

            deviceDataService.saveAlarmData(deviceId, seqNum, sendTime, uwb, uwbArray);
            System.out.println("告警数据保存测试成功");
        } catch (Exception e) {
            System.err.println("告警数据保存测试失败: " + e.getMessage());
            e.printStackTrace();
        }
    }

    /**
     * 测试保存心跳数据
     */
    private static void testSaveHeartData(IDeviceDataService deviceDataService) {
        System.out.println("\n=== 测试保存心跳数据 ===");
        try {
            String deviceId = "test_device_001";
            Double sysVol = 12.8;
            Integer netRSSI = -75;
            Long sendTime = System.currentTimeMillis() / 1000;

            deviceDataService.saveHeartData(deviceId, sysVol, netRSSI, sendTime);
            System.out.println("心跳数据保存测试成功");
        } catch (Exception e) {
            System.err.println("心跳数据保存测试失败: " + e.getMessage());
            e.printStackTrace();
        }
    }
}
