package org.nl.acs.device_driver.tianneng.baoshijian;

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 org.nl.acs.device_driver.tianneng.guhuashi.GuhuashiSiteDeviceDriver;

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

@Slf4j
@Data
public class ItemProtocol {

    public static String item_x0 = "x0";
    public static String item_x1 = "x1";
    public static String item_x2 = "x2";
    public static String item_x3 = "x3";
    public static String item_x4 = "x4";
    public static String item_x5 = "x5";
    public static String item_x6 = "x6";
    public static String item_x7 = "x7";
    public static String item_x8 = "x8";
    public static String item_x9 = "x9";
    public static String item_x10 = "x10";
    public static String item_x35 = "x35";
    public static String item_x36 = "x36";
    public static String item_x37 = "x37";


    private BaoshijianSiteDeviceDriver driver;

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

    public int getX0() {
        return this.getOpcIntegerValue(item_x0);
    }

    public int getX1() {
        return this.getOpcIntegerValue(item_x1);
    }

    public int getX2() {
        return this.getOpcIntegerValue(item_x2);
    }

    public int getX3() {
        return this.getOpcIntegerValue(item_x3);
    }

    public int getX4() {
        return this.getOpcIntegerValue(item_x4);
    }

    public int getX5() {
        return this.getOpcIntegerValue(item_x5);
    }

    public int getX6() {
        return this.getOpcIntegerValue(item_x6);
    }

    public int getX7() {
        return this.getOpcIntegerValue(item_x7);
    }

    public int getX8() {
        return this.getOpcIntegerValue(item_x8);
    }

    public int getX9() {
        return this.getOpcIntegerValue(item_x9);
    }

    public int getX10() {
        return this.getOpcIntegerValue(item_x10);
    }

    public int getX35() {
        return this.getOpcIntegerValue(item_x35);
    }

    public int getX36() {
        return this.getOpcIntegerValue(item_x36);
    }

    public int getX37() {
        return this.getOpcIntegerValue(item_x37);
    }


    Boolean isonline;

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

        }
        return 0;

    }

    public String getOpcStringValue(String protocol) {

        try {
            String value = this.driver.getStringValue(protocol);
            if (value == null) {
                // log.error(this.getDriver().getDeviceCode() + ":protocol " + protocol + " 信号同步异常!");
                setIsonline(false);
            } else {
                setIsonline(true);
                return value;
            }
        } catch (Exception e) {

        }
        return "0";
    }

    public static List<ItemDto> getReadableItemDtos() {
        ArrayList list = new ArrayList();
        list.add(new ItemDto(item_x0, "系统状态", "400001"));
        list.add(new ItemDto(item_x1, "收片完成", "400002"));
        list.add(new ItemDto(item_x2, "需上空架", "400003"));
        list.add(new ItemDto(item_x3, "门开到位", "400004"));
        list.add(new ItemDto(item_x4, "门关到位", "400005"));
        list.add(new ItemDto(item_x10, "备用字", "400011"));
        list.add(new ItemDto(item_x35, "架数", "400036"));
        list.add(new ItemDto(item_x36, "备用字", "400037"));
        list.add(new ItemDto(item_x37, "机号", "400038"));
        return list;
    }

    public static List<ItemDto> getWriteableItemDtos() {
        ArrayList list = new ArrayList();
        list.add(new ItemDto(item_x5, "上空架中", "400006"));
        list.add(new ItemDto(item_x6, "下满架中", "400007"));
        list.add(new ItemDto(item_x7, "上架完成", "400008"));
        list.add(new ItemDto(item_x8, "下架完成(关门)", "400009"));
        list.add(new ItemDto(item_x9, "开门请求", "400010"));
        return list;
    }

}

