package com.ruoyi.iot.queue;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.iot.service.ITDevicePlanService;
import com.ruoyi.iot.socket.WebSocket;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class TaskRunner {
    @Autowired
    private ITDevicePlanService itDevicePlanService;

    @Async("myTaskAsyncPool")
    public void handlerMsg(String s) {
        try {
            JSONObject jsonObject = JSONObject.parseObject(s);
            Integer type = jsonObject.getInteger("type");
            if (type == null) {
                type = 0;
            }

            log.info("msg coming,{}",s);
            String imei = jsonObject.getString("imei");
            if(type == 101 || type == 116 || type == 103 || type == 96 || type == 113){//
                JSONObject real_data = jsonObject.getJSONObject("real_data");
                JSONObject data = new JSONObject();
                data.put("real_data",real_data);
                data.put("type",type);
                WebSocket.sendMsg(imei,data);


                if(type == 103){
                    itDevicePlanService.runPlanChargeJob(imei,real_data);
                }

                if(type == 113){
//                    itChargeRecordService.endChargeNotice(imei);
                }
            }
//            /**
//             * 116端口发生变化
//             * 113 停止充电
//             * 104直接开始充电
//             */
//            if (type.equals(116) || type.equals(113) || type.equals(104)) {//代表端口发生变化
//                itDevicePlanService.portChange(imei, jsonObject);
//            }


        }catch (Exception e){
            log.error("解析异常",e);
        }
    }
}
