package com.shkj.wms.plc;

import HslCommunication.Core.Types.OperateResult;
import com.shkj.common.constant.RedisConstant;
import com.shkj.plc.sdk.device.PlcHelper;
import com.shkj.plc.sdk.device.PlcOperate;
import com.shkj.wms.domain.WcsPlcConnect;
import com.shkj.wms.service.IWcsPlcConnectService;
import com.shkj.wms.vo.WcsPlcConnectVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Slf4j
@Component("PlcInit")
public class PlcInit {

    private PlcOperate plcOperate_41;
    private PlcOperate plcOperate_50;
    private PlcOperate plcOperate_60;

    private PlcOperate plcOperate_70;
    private PlcOperate plcOperate_80;

    private PlcOperate plcOperate_90;
    private PlcOperate plcOperate_100;
    private PlcOperate plcOperate_110;

    private PlcOperate plcOperate_1;

    @Resource
    IWcsPlcConnectService iWcsPlcConnectService;

    @Resource
    RedisTemplate redisTemplate;
    private ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);

   // @PostConstruct
    public void init(){
        log.info("初始化连接对象");
        executorService.scheduleWithFixedDelay(
                () -> reconnect(), 20, 20, TimeUnit.SECONDS
        );
        connect(null);
    }

    public void  connect(String plcIp){
        WcsPlcConnect wcsPlcConnect = new WcsPlcConnect();
        wcsPlcConnect.setIsActive("1");
        if (plcIp !=null){
            wcsPlcConnect.setPlcIp(plcIp);
        }
        //查询需要连接的PLC,初始化连接对象
        List<WcsPlcConnectVo> plcList = iWcsPlcConnectService.selectWcsPlcConnectList(wcsPlcConnect);
        if (plcList.size() == 0) {
            return;
        }
        for (WcsPlcConnectVo conn : plcList) {
            redisTemplate.opsForValue().set(RedisConstant.redisPlcConnect+conn.getDevCode(),conn);
            PlcHelper plc = new PlcHelper(conn.getPlcType(), conn.getPlcIp(), conn.getPlcFactory(), conn.getPort());
            if (conn.getPlcIp().contains(".41")){
                plcOperate_41 = plc.getAutomation();
                if(plcOperate_41 !=null){
                    OperateResult connect = plcOperate_41.connect();
                    if (connect.IsSuccess){
                        log.info(conn.getPlcIp()+" 41初始化成功");
                    }else {
                        log.info(conn.getPlcIp()+" 41初始化失败");
                    }
                }else {
                    log.info(conn.getPlcIp()+" 41连接失败");
                }

            }

            if (conn.getPlcIp().contains(".50")){
                plcOperate_50 = plc.getAutomation();
                if(plcOperate_50 !=null){
                    OperateResult connect = plcOperate_50.connect();
                    if (connect.IsSuccess){
                        log.info(conn.getPlcIp()+" 50初始化成功");
                    }else {
                        log.info(conn.getPlcIp()+" 50初始化失败");
                    }
                }else {
                    log.info(conn.getPlcIp()+" 50连接失败");
                }
            }

            if (conn.getPlcIp().contains(".60")){
                plcOperate_60 = plc.getAutomation();
                if(plcOperate_60 !=null){
                    OperateResult connect = plcOperate_60.connect();
                    if (connect.IsSuccess){
                        log.info(conn.getPlcIp()+" 60初始化成功");
                    }else {
                        log.info(conn.getPlcIp()+" 60初始化失败");
                    }
                }else {
                    log.info(conn.getPlcIp()+" 60连接失败");
                }
            }

            if (conn.getPlcIp().contains(".70")){
                plcOperate_70 = plc.getAutomation();
                if (plcOperate_70 != null){
                    OperateResult connect = plcOperate_70.connect();
                    if (connect.IsSuccess){
                        log.info(conn.getPlcIp()+" 70初始化成功");
                    }else {
                        log.info(conn.getPlcIp()+" 70初始化失败");
                    }
                }else {
                    log.info(conn.getPlcIp()+" 70连接失败");
                }
            }
            if (conn.getPlcIp().contains(".80")){
                plcOperate_80 = plc.getAutomation();
                if (plcOperate_80 != null){
                    OperateResult connect = plcOperate_80.connect();
                    if (connect.IsSuccess){
                        log.info(conn.getPlcIp()+" 80初始化成功");
                    }else {
                        log.info(conn.getPlcIp()+" 80初始化失败");
                    }
                }else {
                    log.info(conn.getPlcIp()+" 80连接失败");
                }
            }

            if (conn.getPlcIp().contains(".90")){
                plcOperate_90 = plc.getAutomation();
                if(plcOperate_90 !=null){
                    OperateResult connect = plcOperate_90.connect();
                    if (connect.IsSuccess){
                        log.info(conn.getPlcIp()+" 90初始化成功");
                    }else {
                        log.info(conn.getPlcIp()+" 90初始化失败");
                    }
                }else {
                    log.info(conn.getPlcIp()+" 90连接失败");
                }
            }

            if (conn.getPlcIp().contains(".100")){
                plcOperate_100 = plc.getAutomation();
                if (plcOperate_100 != null){
                    OperateResult connect = plcOperate_100.connect();
                    if (connect.IsSuccess){
                        log.info(conn.getPlcIp()+" 100初始化成功");
                    }else {
                        log.info(conn.getPlcIp()+" 100初始化失败");
                    }
                }else {
                    log.info(conn.getPlcIp()+" 100连接失败");
                }

            }

            if (conn.getPlcIp().contains(".110")){
                plcOperate_110 = plc.getAutomation();
                if (plcOperate_110 != null){
                    OperateResult connect = plcOperate_110.connect();
                    if (connect.IsSuccess){
                        log.info(conn.getPlcIp()+" 110初始化成功");
                    }else {
                        log.info(conn.getPlcIp()+" 110初始化失败");
                    }
                }else {
                    log.info(conn.getPlcIp()+" 110连接失败");
                }

            }


            //本地测试用
            /*if (conn.getPlcIp().contains("127.0.0.1")){
                plcOperate_1 = plc.getAutomation();
                OperateResult connect = plcOperate_1.connect();
                if (connect.IsSuccess){
                    log.info(conn.getPlcIp()+" 本地初始化成功");
                }else {
                    log.info(conn.getPlcIp()+" 本地初始化失败");
                }
            }*/
        }
    }


    private void reconnect(){
        if (!plcOperate_41.isConnectd()){
            connect(null);
        }
        if (!plcOperate_50.isConnectd()){
            connect(null);
        }
        if (!plcOperate_60.isConnectd()){
            connect(null);
        }
        if (!plcOperate_70.isConnectd()){
            connect(null);
        }
        if (!plcOperate_80.isConnectd()){
            connect(null);
        }
        if (!plcOperate_90.isConnectd()){
            connect(null);
        }
        if (!plcOperate_100.isConnectd()){
            connect(null);
        }
        if (!plcOperate_110.isConnectd()){
            connect(null);
        }
    }

    public PlcOperate getPlcOperate_41() {
        return plcOperate_41;
    }

    public PlcOperate getPlcOperate_50() {
        return plcOperate_50;
    }

    public PlcOperate getPlcOperate_60() {
        return plcOperate_60;
    }

    public PlcOperate getPlcOperate_70() {
        return plcOperate_70;
    }

    public PlcOperate getPlcOperate_80() {
        return plcOperate_80;
    }

    public PlcOperate getPlcOperate_90() {
        return plcOperate_90;
    }

    public PlcOperate getPlcOperate_100() {
        return plcOperate_100;
    }

    public PlcOperate getPlcOperate_110() {
        return plcOperate_110;
    }

    public PlcOperate getPlcOperate_1() {
        return plcOperate_1;
    }


    public PlcOperate getPlcOperateByIp(String plcIp){
        if (plcIp.contains(".41")){
            return plcOperate_41;
        }
        if (plcIp.contains(".50")){
            return plcOperate_50;
        }
        if (plcIp.contains(".60")){
            return plcOperate_60;
        }
        if (plcIp.contains(".70")){
            return plcOperate_70;
        }
        if (plcIp.contains(".80")){
            return plcOperate_80;
        }

        if (plcIp.contains(".90")){
            return plcOperate_90;
        }

        if (plcIp.contains(".100")){
            return plcOperate_100;
        }

        if (plcIp.contains(".110")){
            return plcOperate_110;
        }
        return null;
    }


}
