package com.zd.laboratory.event;

import com.zd.laboratory.mqtt.service.impl.SubMessageSendManager;
import com.zd.laboratory.service.ILabSensorService;
import com.zd.laboratory.service.impl.LabSubjectManagerService;
import com.zd.system.api.laboratory.domain.SensorFunctionStatus;
import com.zd.system.api.laboratory.domain.vo.SubFunction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 测点解析监听器
 *
 * @author zhoupan
 * @date 2021/11/4
 */
@Component
public class SubFunctionListener implements ApplicationListener <SubFunctionEvent> {

    private static final Logger log = LoggerFactory.getLogger(SubFunctionListener.class);

    @Autowired
    SubMessageSendManager subMessageSendManager;

    @Autowired
    ILabSensorService labSensorService;

    @Autowired
    LabSubjectManagerService labSubjectManagerService;

    @Override
    public void onApplicationEvent(SubFunctionEvent subFunctionEvent) {

        String num = subFunctionEvent.getSource();
        //根据编号获取实验室ID
        Long subId = labSensorService.getCacheSubByGateway(num);
        if(subId==null) return;
//        List<SensorFunctionStatus> list = labSubjectManagerService.querySenseFunctionBySubId(subId);
        List<SensorFunctionStatus> list = labSubjectManagerService.querySenseNewFunctionBySubId(subId);
        SubFunction subFunction= new SubFunction();
        subFunction.setFunctionStatuses(list);
        subFunction.setSubId(subId);
        subMessageSendManager.sendFunctionUpdateToCommon(subFunction);
    }
}
