package org.nl.acs.opc;

import cn.hutool.core.util.ObjectUtil;
import org.dromara.dynamictp.core.support.ThreadPoolBuilder;
import org.nl.acs.auto.run.AbstractAutoRunnable;
import org.nl.acs.opc.service.dto.OpcServerManageDto;
import org.nl.config.thread.TheadFactoryName;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import static org.dromara.dynamictp.common.em.QueueTypeEnum.MEMORY_SAFE_LINKED_BLOCKING_QUEUE;

/**
 * OPC设备同步启动
 * @author 20220102CG\noblelift
 */
@Component
public class DeviceOpcSynchronizeAutoRun extends AbstractAutoRunnable {

    public static boolean isRun = false;
    ExecutorService executorService = ThreadPoolBuilder.newBuilder()
            .threadPoolName("deviceOpc_thread")
            .threadFactory("deviceOpc_thread")
            .corePoolSize(80)
            .maximumPoolSize(100)
            .keepAliveTime(40)
            .timeUnit(TimeUnit.SECONDS)
            .workQueue(MEMORY_SAFE_LINKED_BLOCKING_QUEUE.getName(), 2000)
            .buildDynamic();

    @Autowired
    private DeviceAppService deviceAppService;
    @Autowired
    private OpcServerManageService opcServerManageService;

    @Override
    public String getCode() {
        return DeviceOpcSynchronizeAutoRun.class.getSimpleName();
    }

    @Override
    public String getName() {
        return "opc设备同步器";
    }

    @Override
    public void autoRun() throws Exception {
        {
            //todo opc保持设备连接
            //Thread.sleep(10000L);
            isRun = true;

            Map<String, OpcServerManageDto> servers = this.opcServerManageService.queryAllServerMap();
            Map<String, List<List<OpcItemDto>>> pros;
            do {
                Thread.sleep(1000L);
                pros = this.deviceAppService.findAllFormatProtocolFromDriver();
            } while (ObjectUtil.isEmpty(pros));
            Set<String> keys = pros.keySet();
            Iterator var4 = keys.iterator();
            //代码执行一次
            while (var4.hasNext()) {
                String key = (String) var4.next();
                List<List<OpcItemDto>> list = (List) pros.get(key);
                OpcServerManageDto opcServer = (OpcServerManageDto) servers.get(key);
                Iterator var8 = list.iterator();
                while (var8.hasNext()) {
                    List<OpcItemDto> groupProtols = (List) var8.next();
                    DeviceOpcProtocolRunable runable = new DeviceOpcProtocolRunable();
                    runable.setProtocols(groupProtols);
                    runable.setOpcServer(opcServer);
                    this.executorService.submit(runable);
                }
            }

            // 同步无光电设备信号
            //Map<String, List<List<OpcItemDto>>> pros1 = this.deviceAppService.findAllFormatProtocolFromDriver();
            //List<DeviceDriver> opcDrivers = this.deviceAppService.findDeviceDriver(DeviceDriver.class);

            while (true) {
                Thread.sleep(3000L);
            }
        }
    }

    @Override
    public void after() {
        isRun = false;
        this.executorService.shutdownNow();
        this.executorService = ThreadPoolBuilder.newBuilder()
                .threadPoolName("deviceOpc_thread")
                .threadFactory("deviceOpc_thread")
                .corePoolSize(80)
                .maximumPoolSize(100)
                .keepAliveTime(40)
                .timeUnit(TimeUnit.SECONDS)
                .workQueue(MEMORY_SAFE_LINKED_BLOCKING_QUEUE.getName(), 2000)
                .buildDynamic();
    }
}
