package org.nl.acs.device_driver.yinfei.hj1;

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_error = "error";
    public static String item_barcode = "barcode";
    public static String item_to_command = "to_command";
    public static String item_to_target = "to_target";
    public static String item_to_task = "to_task";

    private Hj1DeviceDriver driver;

    public ItemProtocol(Hj1DeviceDriver 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 getError() {
        return this.getOpcIntegerValue(item_error);
    }

    public int getToCommand() {
        return this.getOpcIntegerValue(item_to_command);
    }

    public int getToTarget() {
        return this.getOpcIntegerValue(item_to_target);
    }

    public int getToTask() {
        return this.getOpcIntegerValue(item_to_task);
    }


    //是否有货
    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 static List<ItemDto> getReadableItemDtos() {
        ArrayList list = new ArrayList();
        list.add(new ItemDto(item_heartbeat, "心跳", "DB600.B0"));
        list.add(new ItemDto(item_mode, "工作状态", "DB600.B1", Boolean.TRUE));
        list.add(new ItemDto(item_move, "光电开关信号", "DB600.B2"));
        list.add(new ItemDto(item_action, "取放信号", "DB600.B3"));
        list.add(new ItemDto(item_error, "报警信号", "DB600.B6"));
        list.add(new ItemDto(item_barcode, "任务号", "DB600.D22"));
        return list;
    }

    public static List<ItemDto> getWriteableItemDtos() {
        ArrayList list = new ArrayList();
        list.add(new ItemDto(item_to_command, "作业命令", "DB601.W2", Boolean.TRUE));
        list.add(new ItemDto(item_to_target, "目标站", "DB601.W4"));
        list.add(new ItemDto(item_to_task, "任务号", "DB601.D8"));
        return list;
    }

}

