package org.nl.acs.device_driver.basedriver.slit_two_manipulator;

import cn.hutool.core.util.StrUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.nl.acs.device.device_driver.standard_inspect.ItemDto;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Data
public class ItemProtocol {

    //心跳
    public static String item_heartbeat = "heartbeat";
    //工作模式
    public static String item_mode = "mode";
    //设备状态
    public static String item_status = "status";
    //前工位光电信号
    public static String item_move1 = "move1";
    //后工位光电信号
    public static String item_move2 = "move2";
    //前工位动作信号
    public static String item_action1 = "action1";
    //后工位动作信号
    public static String item_action2 = "action2";
    //行走列
    public static String item_walk_y = "walk_y";
    //报警信号
    public static String item_error = "error";
    //任务类型
    public static String item_type = "type";
    //前工位任务号
    public static String item_task1 = "task1";
    //后工位任务号
    public static String item_task2 = "task2";


    //前工位下发命令
    public static String item_to_command1 = "to_command1";
    //前工位下发起始站
    public static String item_to_onset1 = "to_onset1";
    //前工位下发目标站
    public static String item_to_target1 = "to_target1";
    //前工位下发任务号
    public static String item_to_task1 = "to_task1";
    //后工位下发命令
    public static String item_to_command2 = "to_command2";
    //后工位下发起始站
    public static String item_to_onset2 = "to_onset2";
    //后工位下发目标站
    public static String item_to_target2 = "to_target2";
    //后工位下发任务号
    public static String item_to_task2 = "to_task2";
    //任务类型 1前工位 2后工位 3双工位
    public static String item_to_type = "to_type";


    private SlitTwoManipulatorDeviceDriver driver;

    public ItemProtocol(SlitTwoManipulatorDeviceDriver driver) {
        this.driver = driver;
    }

    public int getHeartbeat() {
        return this.getOpcIntegerValue(item_heartbeat);
    }

    public int getMode() {
        return this.getOpcIntegerValue(item_mode);
    }

    public int getStatus() {
        return this.getOpcIntegerValue(item_status);
    }

    public int getError() {
        return this.getOpcIntegerValue(item_error);
    }

    public int getMove1() {
        return this.getOpcIntegerValue(item_move1);
    }

    public int getMove2() {
        return this.getOpcIntegerValue(item_move2);
    }

    public int getAction1() {
        return this.getOpcIntegerValue(item_action1);
    }

    public int getAction2() {
        return this.getOpcIntegerValue(item_action2);
    }

    public int getWalk_y() {
        return this.getOpcIntegerValue(item_walk_y);
    }

    public int getType() {
        return this.getOpcIntegerValue(item_type);
    }

    public int getTask1() {
        return this.getOpcIntegerValue(item_task1);
    }

    public int getTask2() {
        return this.getOpcIntegerValue(item_task2);
    }


    //是否有货
    public int hasGoods(int move) {
        return move;
    }

    Boolean isonline;

    public int getOpcIntegerValue(String protocol) {
        Integer value = this.driver.getIntegeregerValue(protocol);
        if (value == null) {
            // log.error(this.getDriver().getDeviceCode() + ":protocol " + protocol + " 信号同步异常!");
            setIsonline(false);
        } else {
            setIsonline(true);
            return value;
        }
        return 0;

    }

    public String getOpcStringValue(String protocol) {
        String value = this.driver.getStringValue(protocol);
        if (StrUtil.isEmpty(value)) {

        } else {
            return value;
        }
        return "0";
    }

    public static List<ItemDto> getReadableItemDtos() {
        ArrayList list = new ArrayList();
        list.add(new ItemDto(item_heartbeat, "心跳", "DB1.B0"));
        list.add(new ItemDto(item_mode, "工作模式", "DB1.B1"));
        list.add(new ItemDto(item_status, "设备状态", "DB1.B2"));
        list.add(new ItemDto(item_move1, "前工位光电信号", "DB1.B3"));
        list.add(new ItemDto(item_move2, "后工位光电信号", "DB1.B4"));
        list.add(new ItemDto(item_action1, "前工位动作信号", "DB1.B5"));
        list.add(new ItemDto(item_action2, "后工位动作信号", "DB2.B6"));
        list.add(new ItemDto(item_walk_y, "行走列", "DB1.B7"));
        list.add(new ItemDto(item_error, "报警信号", "DB1.B8"));
        list.add(new ItemDto(item_type, "任务类型", "DB1.B9"));
        list.add(new ItemDto(item_task1, "前工位任务号", "DB1.D10"));
        list.add(new ItemDto(item_task2, "后工位任务号", "DB1.D14"));
        return list;
    }

    public static List<ItemDto> getWriteableItemDtos() {
        ArrayList list = new ArrayList();
        list.add(new ItemDto(item_to_command1, "前工位下发命令", "DB2.W0"));
        list.add(new ItemDto(item_to_onset1, "前工位下发起始站", "DB2.W2"));
        list.add(new ItemDto(item_to_target1, "前工位下发目标站", "DB2.W4"));
        list.add(new ItemDto(item_to_task1, "前工位下发任务号", "DB2.D6"));
        list.add(new ItemDto(item_to_command2, "后工位下发命令", "DB2.W10"));
        list.add(new ItemDto(item_to_onset2, "后工位下发起始站", "DB2.W12"));
        list.add(new ItemDto(item_to_target2, "后工位下发目标站", "DB2.W14"));
        list.add(new ItemDto(item_to_task2, "后工位下发任务号", "DB2.D16"));
        list.add(new ItemDto(item_to_type, "下发任务类型", "DB2.W20"));
        return list;
    }

    @Override
    public String toString() {
        return "";
    }

}

