package org.nl.acs.device_driver.basedriver.oven_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_move = "move";
    //动作信号
    public static String item_action = "action";
    //行走列
    public static String item_walk_y = "walk_y";
    //报警
    public static String item_error = "error";
    //任务号
    public static String item_task = "task";

    //下发命令
    public static String item_to_command = "to_command";
    //下发起始站
    public static String item_to_onset = "to_onset";
    //下发目标站
    public static String item_to_target = "to_target";
    //下发任务号
    public static String item_to_task = "to_task";


    private OvenGantryManipulatorDeviceDriver driver;

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

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

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

    public int getMove() {
        return this.getOpcIntegerValue(item_move);
    }

    public int getAction() {
        return this.getOpcIntegerValue(item_action);
    }

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

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

    public int getTask() {
        return this.getOpcIntegerValue(item_task);
    }


    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_move, "光电信号", "DB1.B2"));
        list.add(new ItemDto(item_action, "动作信号", "DB1.B3"));
        list.add(new ItemDto(item_walk_y, "行走列", "DB1.B4"));
        list.add(new ItemDto(item_error, "报警信号", "DB1.B5"));
        list.add(new ItemDto(item_task, "任务号", "DB1.D6"));
        return list;
    }

    public static List<ItemDto> getWriteableItemDtos() {
        ArrayList list = new ArrayList();
        list.add(new ItemDto(item_to_command, "下发命令", "DB2.W0"));
        list.add(new ItemDto(item_to_onset, "下发起始站", "DB2.W2"));
        list.add(new ItemDto(item_to_target, "下发目标站", "DB2.W4"));
        list.add(new ItemDto(item_to_task, "下发任务号", "DB2.D6"));
        return list;
    }

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

