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

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_carrier_direction = "carrier_direction";
    //报警
    public static String item_error = "error";
    //任务号
    public static String item_task = "task";
    //重量
    public static String item_weight = "weight";

    //下发命令
    public static String item_to_command = "to_command";
    //下发目标站
    public static String item_to_target = "to_target";
    //下发托盘类型
    public static String item_to_container_type = "to_container_type";
    //下发任务号
    public static String item_to_task = "to_task";
    //困扎次数
    public static String item_to_strap_times = "to_strap_times";
    //木箱长度
    public static String item_to_length = "to_length";
    //木箱宽度
    public static String item_to_weight = "to_weight";
    //木箱高度
    public static String item_to_height = "to_height";


    private SiemensConveyorLabelingDeviceDriver driver;

    public ItemProtocol(SiemensConveyorLabelingDeviceDriver 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 getCarrier_direction() {
        return this.getOpcIntegerValue(item_carrier_direction);
    }

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

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

    public int getWeight() {
        return this.getOpcIntegerValue(item_weight);
    }


    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, "心跳", "DB101.B0"));
        list.add(new ItemDto(item_mode, "工作模式", "DB101.B152"));
        list.add(new ItemDto(item_move, "光电信号", "DB101.B153"));
        list.add(new ItemDto(item_carrier_direction, "托盘方向", "DB101.B154"));
        list.add(new ItemDto(item_error, "报警信号", "DB101.B156"));
        list.add(new ItemDto(item_task, "任务号", "DB101.D160"));
        list.add(new ItemDto(item_weight, "重量", "DB101.D160"));
        return list;
    }

    public static List<ItemDto> getWriteableItemDtos() {
        ArrayList list = new ArrayList();
        list.add(new ItemDto(item_to_command, "下发命令", "DB102.W152"));
        list.add(new ItemDto(item_to_target, "下发目标站", "DB601.W154"));
        list.add(new ItemDto(item_to_task, "下发任务号", "DB102.D158"));
        return list;
    }

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

}

