package com.mortals.xhx.daemon.applicationservice;

import com.alibaba.fastjson.JSON;
import com.mortals.framework.springcloud.service.IApplicationService;
import com.mortals.xhx.common.utils.SendTaskThreadPool;
import com.mortals.xhx.common.utils.SmsQueueManager;
import com.mortals.xhx.module.alarm.model.AlarmSmsSendEntity;
import com.mortals.xhx.queue.TbQueueMsg;
import com.mortals.xhx.utils.IotThreadFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

@Component
@Slf4j
public class DeviceStartService implements IApplicationService {

    @Autowired
    private SendTaskThreadPool sendTaskThreadPool;

    protected Boolean stopped = false;

    @Override
    public void start() {
        log.info("初始化发送线程数量");
        sendTaskThreadPool.init(20);
        //启动短信发送响应更新线程
        
        sendTaskThreadPool.execute(() -> {
            int waitTime = 1000;
            while (!stopped) {
                try {
                    AlarmSmsSendEntity message = SmsQueueManager.pollRespQueue();

                    if (!ObjectUtils.isEmpty(message)) {
                        // TODO: 2022/7/5 更新响应
                    }
                } catch (Exception e) {
                    if (!stopped) {
                        try {
                            Thread.sleep(waitTime);
                        } catch (InterruptedException e2) {
                        }
                    }
                }
            }
        });


        log.info("开始服务..[配置已加载完成，但部分框架还未初始化，比如：Kafka]");
    }

    @Override
    public void stop() {
        log.info("停止服务..");
    }

}
