package com.shkj.wms.plc;

import com.shkj.plc.sdk.device.PLCReadAndWrite;
import com.shkj.plc.sdk.device.PlcOperate;
import com.shkj.wms.domain.WcsPlcConnect;
import com.shkj.wms.domain.WcsPlcProperty;
import com.shkj.wms.service.IWcsPlcConnectService;
import com.shkj.wms.service.IWcsPlcPropertyService;
import com.shkj.wms.utils.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Component
public class TeskSTKfive {
    @Autowired
    IWcsPlcConnectService iWcsPlcConnectService;
    @Autowired
    IWcsPlcPropertyService iWcsPlcPropertyService;

    @Resource
    PlcInit plcInit;

//    @Scheduled(cron = "0/2 * * * * ? ")
    public void teskstk(){
        WcsPlcConnect wcsPlcConnectstk = iWcsPlcConnectService.getById(43);
        String plcIp = wcsPlcConnectstk.getPlcIp();
        PlcOperate automation = plcInit.getPlcOperateByIp(plcIp);

        PLCReadAndWrite plcReadAndWrite = new PLCReadAndWrite();
        Map<String, Object> pointMap = plcReadAndWrite.readPlcData(automation, wcsPlcConnectstk.getPointTables());

        List<WcsPlcProperty> propertyList = iWcsPlcPropertyService.getWcsPlcPropertyByPlcId(wcsPlcConnectstk.getId());
        String readAddress = "";
        String plcTaskId ="";
        String plcToRow ="";
        String plcToColumn ="";
        String plcToLayer ="";
        String plcHs ="";
        String plcml ="";
        for (WcsPlcProperty wcsPlcProperty : propertyList) {

            if (wcsPlcProperty.getPointType().equals("101")) {
                plcTaskId = wcsPlcProperty.getAddress();
            }else if (wcsPlcProperty.getPointType().equals("4")) {
                //堆垛机完成信号
                readAddress = wcsPlcProperty.getAddress();
            }
            else if (wcsPlcProperty.getPointType().equals("108")){
                plcToRow = wcsPlcProperty.getAddress();
            }else if (wcsPlcProperty.getPointType().equals("109")){
                plcToColumn =wcsPlcProperty.getAddress();
            }else if (wcsPlcProperty.getPointType().equals("110")){
                plcToLayer=wcsPlcProperty.getAddress();
            }else if (wcsPlcProperty.getPointType().equals("103")){
                plcHs=wcsPlcProperty.getAddress();
            }else if(wcsPlcProperty.getPointType().equals("102")){
                plcml=wcsPlcProperty.getAddress();
            }
        }


        Integer readInt = 1;
        if (pointMap.containsKey(readAddress)) {
            readInt = Integer.valueOf(pointMap.get(readAddress).toString());
        }
        readInt = (readInt == 1) ? 0 : 1;


        //给堆垛机写命令
        Map<String, Object> allMap = new HashMap<>(1);
        Map<String, Object> plcTaskIdMap = new HashMap<>(1);
        plcTaskIdMap.put("INTEGER", readInt);
        allMap.put(plcTaskId, plcTaskIdMap);
        Map<String, Object> plcToRowMap = new HashMap<>(1);
        plcToRowMap.put("SHORT", readInt);
        allMap.put(plcToRow, plcToRowMap);
        Map<String, Object> plcToColumnMap = new HashMap<>(1);
        plcToColumnMap.put("SHORT", readInt);
        allMap.put(plcToColumn, plcToColumnMap);
        Map<String, Object> plcToLayerMap = new HashMap<>(1);
        plcToLayerMap.put("SHORT", readInt);
        allMap.put(plcToLayer, plcToLayerMap);
        Map<String, Object> plcHsMap = new HashMap<>(1);
        plcHsMap.put("SHORT", 1);
        allMap.put(plcHs, plcHsMap);
        Map<String, Object> plcMLMap = new HashMap<>(1);
        plcMLMap.put("Short", 1);
        allMap.put(plcml, plcMLMap);
        Map<String, Object> writeHbMap = new HashMap<>(1);
        writeHbMap.put("SHORT", String.valueOf(readInt));
        allMap.put(readAddress, writeHbMap);

        plcReadAndWrite.execWrite(automation, JsonUtil.toJSONString(allMap));

    }
}
