/**
 * Copyright 2019 DH
 * All right reserved.
 * 项目名称: 大恒泰山系统
 * 创建日期:2021/12/20
 */
package org.ts.ddcs.process;


import org.glassfish.jersey.internal.guava.ThreadFactoryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;
import org.ts.ddcs.config.Config;
import org.ts.ddcs.controller.simulator.service.RtuSimulatorService;
import org.ts.ddcs.log.LogHelper;
import org.ts.ddcs.mqtt.Mqtt;
import org.ts.ddcs.tcp.TcpListener;
import org.ts.ddcs.tcp.TcpWorker;

import javax.annotation.PostConstruct;
import java.net.Socket;
import java.util.Map;
import java.util.concurrent.*;

/**
 * 主数据中心
 *
 * @author dylan
 */
@Component
public class DataProcess {

    private static ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("data-process-pool-%d").build();

    private static ExecutorService reportTaskThreadPool = new ThreadPoolExecutor(20, 50,
            0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(1024),
            threadFactory, new ThreadPoolExecutor.AbortPolicy());


    private static Map<String, TcpWorker> sockets = new ConcurrentHashMap<>();

    @Autowired
    public Config config;

    @Autowired
    public Mqtt mqttServer;

    @Autowired
    public RedisTemplate redisTemplate;


    @Autowired
    public RtuSimulatorService rtuSimulatorService;

    @PostConstruct
    public void init() {
        try {
            DataProcessHolder.newInstance().dataProcess = this;
            redisTemplate.setKeySerializer(new StringRedisSerializer());
            redisTemplate.setValueSerializer(new StringRedisSerializer());
            redisTemplate.setHashKeySerializer(new StringRedisSerializer());
            redisTemplate.setHashValueSerializer(new StringRedisSerializer());
            startTcpListen();
            startUdpListen();
        } catch (Exception e) {
            LogHelper.error(e);
        }
    }

    public void startTcpListen() {
        TcpListener tcpListener = new TcpListener();
        FutureTask<Integer> futureTask = new FutureTask<>(tcpListener);
        reportTaskThreadPool.execute(futureTask);
    }

    public void startUdpListen() {

    }


    /**
     * 提交RTU 模拟器任务
     */
    public void submitSimulatorTask() {
        SimulatorWorker worker = new SimulatorWorker();
        FutureTask<Integer> futureTask = new FutureTask<>(worker);
        reportTaskThreadPool.execute(futureTask);
    }

    public void submitSocketConnect(String ip, Socket socket) {
        TcpWorker tcpWorker = new TcpWorker(ip, socket);
        if (sockets.containsKey(ip)) {
            TcpWorker oldTcp = sockets.get(ip);
            if (null != oldTcp) {
                oldTcp.close();
            }
        }
        sockets.put(ip, tcpWorker);

        FutureTask<Integer> futureTask = new FutureTask<>(tcpWorker);
        reportTaskThreadPool.execute(futureTask);
    }

    public TcpWorker getTcpWorker(String ip) {
        if (sockets.containsKey(ip)) {
            return sockets.get(ip);
        }
        return null;
    }

    public void removeTcpWorker(String ip) {
        if (sockets.containsKey(ip)) {
            sockets.remove(ip);
        }
    }
}
