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

import com.alibaba.fastjson2.annotation.JSONField;
import jodd.util.StringUtil;
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}
 */
public class WebSocketParam {

    // 消息topic
    private String Topic;

    // 设备ID
    private String MainboardID;

    // 消息类型
    private MethodEnum method;

    @JSONField(name = "Topic")
    public String getTopic() {
        return Topic;
    }

    public void setTopic(String topic) {
        Topic = topic;
    }

    public void setMainboardID(String mainboardID) {
        MainboardID = mainboardID;
    }

    @JSONField(name = "MainboardID")
    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;
    }

}
