package com.mvc.netty.dealconstant;

import io.netty.util.internal.StringUtil;

/**
 * 报文操作符
 */
public enum PackageOperateCode {
    SOH("7E7E","帧起始",4),
    STX("02","传输正文起始",2),
    PIC("AA","图片起始符",2),
    SYN("16","多包传输正文起始",2),
    ETX("03","报文结束，后续无报文",2),
    ETB("17","报文结束，后续有报文",2),
    ENQ("05","询问",2),
    EOT("04","传输结束，退出",2),
    ACK("06","肯定确认，继续发送",2),
    NAK("15","否定应答，反馈重发",2),
    ESC("1B","传输结束，终端保持在线",2),
    O_F1F1("F1F1","地址标识符",4),
    O_F0F0("F0F0","观测时间标识符",4),
    ;

    private String key;
    private String desc;
    private int size;

    private PackageOperateCode(String key, String desc, int size) {
        this.key = key;
        this.desc = desc;
        this.size = size;
    }

    /**
     * 解析处理报文操作符，返回剩余报文
     * @param msg
     * @return
     */
    public String parse(String msg) {
        String result = msg;
        if (!msg.toUpperCase().startsWith(this.key)) {
            return result;
        }
        result = result.substring(this.key.length());
        return result;
    }

    /**
     * 编制报文
     * @param msg
     * @return
     */
    public String encode(String msg) {
        String result = msg;
        if (StringUtil.isNullOrEmpty(result)) {
            result = "";
        }
        result += this.key.toLowerCase();
        return result;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }
}
