package com.dcs.hnyz.task;

import com.alibaba.fastjson2.JSON;
import com.dcs.equipment.server.EquipmentListWebSocketServer;
import com.dcs.hnyz.cache.CacheCenter;
import com.dcs.hnyz.domain.AlarmEvent;
import com.dcs.hnyz.domain.dto.AlarmBroadcastPayload;
import com.dcs.hnyz.domain.vo.EquipmentParamFormVO;
import com.dcs.hnyz.domain.vo.PageDeviceRealtimeVO;
import com.dcs.hnyz.service.IDeviceDataService;
import com.dcs.hnyz.service.impl.AlarmConfigServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


/**
 * WebSocketPushTask
 * 定时推送任务
 * @author: hmy
 * @date: 2025/5/30 16:06
 */
@Component("WebSocketPushTask")
public class WebSocketPushTask {
    @Autowired
    private SimpMessagingTemplate messagingTemplate;

    @Resource
    private EquipmentListWebSocketServer equipmentListWebSocketServer;

    @Autowired
    private IDeviceDataService deviceDataService;

    @Autowired
    private AlarmConfigServiceImpl alarmConfigService;
    /**
     * 每3秒推送一次组态页面所需实时数据
     */
//     @Scheduled(fixedRate = 3000)
    public void pushData() {
        //模拟数据
//        CacheCenter.registerNowDataMap.put("N1fm", create("N1fm", true));
//        CacheCenter.registerNowDataMap.put("N2fm", create("N2fm", true));
//        CacheCenter.registerNowDataMap.put("D5kxh", create("D5kxh", false));
//        CacheCenter.registerNowDataMap.put("D5k", create("D5k", true));
//        CacheCenter.registerNowDataMap.put("D5gxh", create("D5gxh", false));
//        CacheCenter.registerNowDataMap.put("S1wdsj", create("S1wdsj", 130));
//        CacheCenter.registerNowDataMap.put("LB9yxfkpl", create("LB9yxfkpl", 100));
//        CacheCenter.registerNowDataMap.put("LB9bpztz", create("LB9ztz", 2));

        CacheCenter.pageDeviceGroupMap.forEach((pageCode, deviceGroupMap) -> {
            Map<String, List<PageDeviceRealtimeVO>> data = deviceDataService.buildRealtimeData(deviceGroupMap);
            messagingTemplate.convertAndSend("/topic/page/" + pageCode, data);
        });
    }

    //仅用来测试
    private EquipmentParamFormVO create(String code, boolean value) {
        EquipmentParamFormVO ep = new EquipmentParamFormVO();
        ep.setCode(code);
        ep.setValue(value);
        return ep;
    }

    private EquipmentParamFormVO create(String code, int value) {
        EquipmentParamFormVO ep = new EquipmentParamFormVO();
        ep.setCode(code);
        ep.setValue(value);
        return ep;
    }

    /**
     * 告警信息信息检测推送
     */
//    @Scheduled(fixedRate = 1000)
    public void pushAlarmInfo(){
        //测试数据
//        CacheCenter.registerNowDataMap.put("S1wdsj", create("S1wdsj", 130));

//        System.out.println("告警检测");
        List<AlarmEvent> alarms = alarmConfigService.checkAlarms();
        if (!alarms.isEmpty()) {
//            System.out.println("有告警信息");
            AlarmBroadcastPayload payload = new AlarmBroadcastPayload();
            payload.setAlarms(alarms);
            String json = JSON.toJSONString(payload);
            equipmentListWebSocketServer.broadCastInfo(json);
        }
    }
}

