package com.shkj.wcs.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.wcs.domain.WcsPlcConnect;
import com.shkj.wcs.service.IWcsPlcConnectService;
import com.shkj.wcs.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_dps;
    private PlcOperate plcOperate_stk;

    @Resource
    IWcsPlcConnectService iWcsPlcConnectService;

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

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

    public void  connect(){
        //查询需要连接的PLC,初始化连接对象
        List<WcsPlcConnectVo> plcList = iWcsPlcConnectService.selectWcsPlcConnectList(new WcsPlcConnect().setIsActive("1"));
        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("155.180")){
                plcOperate_stk = plc.getAutomation();
                OperateResult connect = plcOperate_stk.connect();
                if (connect.IsSuccess){
                    log.info(conn.getPlcIp()+"stk连接成功");
                }else {
                    log.info(conn.getPlcIp()+"stk连接失败");
                }
            }

            if (conn.getPlcIp().contains("155.170")){
                plcOperate_dps = plc.getAutomation();
                OperateResult connect = plcOperate_dps.connect();
                if (connect.IsSuccess){
                    log.info(conn.getPlcIp()+"dps连接成功");
                }else {
                    log.info(conn.getPlcIp()+"dps连接失败");
                }
            }
        }
    }
    private void reconnect(){
        if (!plcOperate_dps.isConnectd()){
            //connect();
        }
        if (!plcOperate_stk.isConnectd()){
            //connect();
        }
    }

    public PlcOperate getPlcOperate_dps() {
        return plcOperate_dps;
    }

    public PlcOperate getPlcOperate_stk() {
        return plcOperate_stk;
    }


    public PlcOperate getPlcOperateByIp(String plcIp){
        if (plcIp.contains("155.180")){
            return plcOperate_stk;
        }
        if (plcIp.contains("155.170")){
            return plcOperate_dps;
        }
        return null;
    }
}
