package com.zt.plat.module.qms.iot.tcpserver.listener;

import com.zt.plat.framework.tenant.core.context.TenantContextHolder;
import com.zt.plat.module.qms.iot.tcpserver.IotPacket;
import com.zt.plat.module.qms.iot.tcpserver.core.EventConstant;
import com.zt.plat.module.qms.iot.tcpserver.core.IotDeviceSessionForRedisContext;
import com.zt.plat.module.qms.iot.tcpserver.device.RedisSessionComponent;
import com.zt.plat.module.qms.iot.tcpserver.event.IotDeviceRegisterEvent;
import com.zt.plat.module.qms.resource.device.dal.dataobject.DeviceInfomationDO;
import com.zt.plat.module.qms.resource.device.service.DeviceInfomationService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import tech.zzjc.tio.core.ChannelContext;
import tech.zzjc.tio.core.Tio;
import tech.zzjc.tio.starter.TioServerBootstrap;

import java.time.LocalDateTime;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * <b>IotDeviceRegisterListener</b>
 * <p>更新历史：</p>
 * <pre> 版本         更新时间            更新者        更新内容
 * V1.0      2021-10-17          王兴荣         Add</pre>
 * <b>Copyright (C)  云南志者竟成科技有限公司</b> 
 * @author 王兴荣 &lt;xingrong_wang@will-way.cn&gt;
 * @version V1.0 
 * @since 2021-10-17
 */
@Component
@AllArgsConstructor
@Slf4j
public class IotDeviceRegisterListener {

//    private final IDeviceLaboratoryService deviceLaboratoryService;
    private final DeviceInfomationService deviceInfomationService;
    private final RedisSessionComponent redisSessionComponent;

    private final TioServerBootstrap tioServerBootstrap;
    
    /** TH11S-B 温湿度查询 **/
    private static final byte[] TEMP_QUERY_TH11S_B = {(byte)0x01, (byte)0x03, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x02, (byte)0xC4, (byte)0x0B};

    @Async
    @Order
    @EventListener(IotDeviceRegisterEvent.class)
    @SuppressWarnings("unchecked")
    public void iotDeviceRegister(IotDeviceRegisterEvent event) {
        Map<String, Object> source = (Map<String, Object>) event.getSource();
        DeviceInfomationDO deviceInfo = (DeviceInfomationDO) source.get(EventConstant.EVENT_IOT_DEVICE_REGISTER);
        Long deviceId = deviceInfo.getId();
        String deviceType = deviceInfo.getCollectDeviceType();
        final String bsId = Long.toString(deviceId, Character.MAX_RADIX);
        if("temp".equals(deviceInfo.getCollectDeviceType())) {
            ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(5);
            scheduledExecutorService.scheduleAtFixedRate(new Runnable() {

                @Override
                public void run() {
                    IotPacket iotPacket = new IotPacket();
                    iotPacket.setBody(TEMP_QUERY_TH11S_B);
                    Tio.sendToBsId(tioServerBootstrap.getServerTioConfig(), bsId, iotPacket);
                }
            }, 1, 1, TimeUnit.SECONDS);
        }
        ChannelContext channelContext = Tio.getByBsId(tioServerBootstrap.getServerTioConfig(), bsId);
        redisSessionComponent.regDevice(channelContext, bsId, deviceType);
    }
}
