package me.zhengjie.modules.system.service.webstocket;

import com.alibaba.fastjson.annotation.JSONField;
import jodd.util.StringUtil;
import lombok.Data;
import me.zhengjie.modules.security.config.enums.MethodEnum;

/**
 * 控制请求(小程序->主板): sdcp/request/${MainboardID}
 * 控制响应(主板->小程序): sdcp/response/${MainboardID}
 * 状态信息(主板->小程序)：sdcp/status/${MainboardID}
 * 属性信息(主板->小程序)：sdcp/attributes/${MainboardID}
 * 错误信息(主板->小程序)：sdcp/error/${MainboardID}
 * 通知信息(主板->小程序)：sdcp/notice/${MainboardID}
 */
@Data
public class WebSocketParam {

    // 消息topic
    @JSONField(name = "Topic")
    private String Topic;

    // 设备ID
    @JSONField(name = "MainboardID")
    private String MainboardID;

    // 消息类型
    private MethodEnum method;

    public String getMainboardID() {
        if (StringUtil.isBlank(MainboardID)) {
            return getTopic().substring(getTopic().lastIndexOf("/") + 1);
        }
        return MainboardID;
    }

    public MethodEnum getMethod() {
        if (StringUtil.isEmpty(getTopic())) { return null; }
        for (MethodEnum os : MethodEnum.values()) {
            if (getTopic().contains(os.name())) {
                return os;
            }
        }
        return null;
    }

}
