EMQ X 之 emqx_web_hook


前言

       该文仅为个人学习记录

       EMQ版本:3.1

       个人仅用于处理设备离线。


配置

       软件根目录/etc/emqx/plugins/emqx_web_hook.conf

#设置访问地址
web.hook.api.url = http://192.168.100.151:8080/webHook

#这里设置了各种事件
web.hook.rule.client.connected.1     = {"action": "on_client_connected"}
web.hook.rule.client.disconnected.1  = {"action": "on_client_disconnected"}
web.hook.rule.client.subscribe.1     = {"action": "on_client_subscribe"}
web.hook.rule.client.unsubscribe.1   = {"action": "on_client_unsubscribe"}
web.hook.rule.session.created.1      = {"action": "on_session_created"}
web.hook.rule.session.subscribed.1   = {"action": "on_session_subscribed"}
web.hook.rule.session.unsubscribed.1 = {"action": "on_session_unsubscribed"}
web.hook.rule.session.terminated.1   = {"action": "on_session_terminated"}
web.hook.rule.message.publish.1      = {"action": "on_message_publish"}
web.hook.rule.message.deliver.1    = {"action": "on_message_deliver"}
web.hook.rule.message.acked.1        = {"action": "on_message_acked"}

接口

/**
 * @Description emqx_web_hook插件相关 此处主要用于监听设备连接情况
 * @Author 姚句
 * @Date 2019/6/10 11:42
 **/
@Controller
public class WebHookController {


    @PostMapping("/webHook")
    public void onWebHook(@RequestBody Map param){
        String action = param.get(WebHookConstant.ACTION);
        String username = param.get(WebHookConstant.USERNAME);
        //String reson = param.get(WebHookConstant.REASON);
        //String clientId = param.get(WebHookConstant.CLINET_ID);
        if (WebHookConstant.ACTION_DISCONNECTED.equals(action)){
            onClientConnected(username);
        }
    }

    /**
     * 设备连接中断监听
     */
    private void onClientConnected(String username){
        //...
    }

}
/**
 * @Description emqx_web_hook插件相关常量类
 * @Author 姚句
 * @Date 2019/6/10 14:22
 **/
public class WebHookConstant {

    //固定的属性名
    public static final String ACTION = "action";
    public static final String CLINET_ID= "client_id";
    public static final String USERNAME = "username";
    public static final String REASON = "reason";

    //ACTION
    public static final String ACTION_DISCONNECTED = "client_disconnected"; //断开连接

}

说明

       当设备触发了对应的事件后,消息服务都会去调用web.hook.api.url配置的地址,随后在接口中使用action值来判断对应哪个事件,而后进行对应的操作。

 

你可能感兴趣的:(#,EMQX)