package iot.lidee.bootstrap.tcp.config;

import iot.lidee.base.session.Session;
import iot.lidee.base.session.SessionListener;
import iot.lidee.base.util.DeviceUtils;
import iot.lidee.common.core.mq.DeviceStatusBo;
import iot.lidee.common.enums.DeviceStatus;
import iot.lidee.mq.producer.MessageProducer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @author bill
 */
@Slf4j
@Service
public class TcpSessionListener implements SessionListener {

//    @Resource
//    private IMqttMessagePublish mqttMessagePublish;
//    @Resource
//    private DeviceStatusConsumer statusConsumer;
//    private MqttRemoteManager remoteManager;


    /** 客户端建立连接 */
    @Override
    public void sessionCreated(Session session) {

    }

    /** 客户端完成注册或鉴权 */
    @Override
    public void sessionRegistered(Session session) {
        DeviceStatusBo statusBo = DeviceUtils.buildStatusMsg(session.getChannel(), session.getClientId(),
                DeviceStatus.ONLINE, session.getIp());
        MessageProducer.sendStatusMsg(statusBo);
//        remoteManager.pushDeviceStatus(-1L,statusBo.getSerialNumber(),statusBo.getStatus());
        log.info("TCP客户端:[{}],注册上线",session.getClientId());
    }

    /** 客户端注销或离线 */
    @Override
    public void sessionDestroyed(Session session) {
        /*推送离线消息到mq处理*/
        DeviceStatusBo statusBo = DeviceUtils.buildStatusMsg(session.getChannel(),
                session.getClientId(), DeviceStatus.OFFLINE, session.getIp());
        MessageProducer.sendStatusMsg(statusBo);
//        remoteManager.pushDeviceStatus(-1L,statusBo.getSerialNumber(),statusBo.getStatus());
        log.info("TCP客户端:[{}],离线",session.getClientId());

    }
}
