物联网规则引擎设计

代码实现见我的开源项目(gitee)
规则定义
由监听器、过滤条件和执行动作组成
流程
监听器
定义数据源获取方法,通过监听条件对输入消息进行过滤,输出符合条件的消息 。
过滤条件
对监听到消息之外的数据进行判断是否要执行下一步 。
动作定义
对满足条件的数据进行处理 。
场景引擎
规则引擎的一种应用,用于根据触发条件进行场景化设备控制 。
配置
{"listeners":[{"type":"device","conditions":[{"device":"xxpk/xxdn","type":"event","identifier":"event:*","parameters":[]},{"device":"xxpk/xxdn","type":"event","identifier":"xxxevent","parameters":[{"identifier":"p1","value":1,"comparator":">"}]},{"device":"xxpk/xxdn","type":"service_reply","identifier":"service_reply:*","parameters":[]},{"device":"xxpk/xxdn","type":"service_reply","identifier":"xxxx","parameters":[{"identifier":"p1","value":1,"comparator":">"}]},{"device":"xxpk/xxdn","type":"property","identifier":"property:*","parameters":[]},{"device":"xxpk/xxdn","type":"property","identifier":"post","parameters":[{"identifier":"powerstate","value":0,"comparator":"=="}]}]}],"filters":[{"type":"device","conditions":[{"device":"xxPK/xxDN","type":"property","identifier":"powerstate","value":1,"comparator":">"},{"device":"xxPK/xxDN","type":"state","identifier":"online","value":true,"comparator":"=="}]}],"actions":[{"type":"device","services":[{"device":"xxPK/xxDN","identifier":"set","inputData":[{"identifier":"powerstate","value":1}]},{"device":"xxPK/xxDN","identifier":"switchOff","inputData":[{}]}]}]}
注:结构由/连接组成 。其中:中的的可由#代替,代表该产品下所有设备
设备消息场景匹配逻辑
定义物联网平台设备相关数据如何向平台外流转,包含数据源定义、数据转换、目的地定义 。
数据源
指定从哪里获取需要流转的数据 。

物联网规则引擎设计

文章插图
数据包含:设备状态、设备属性、设备服务回复、设备事件 。
指定过滤备件:按产品、设备、以及数据内容进行过滤,见规则引擎监听器过滤部分定义
数据过滤
对流入的设备消息和设备信息进行逻辑判断,返回需要的数据 。
数据转换
按照目的地接收的数据格式,对数据源的数据进行转换 。
编写转换脚本进行转换 。
脚本
//其它任意符合ECMAScript5的js代码xxx...//必须提供translate方法对消息内容进行过滤和转换this.translate=function(msg,device){//device包含设备表所有信息//msg结构/*属性上报{"mid":"xxx","productKey":"xxx",//可根据消息内容判断填写不同产品"deviceName":"xxx","type":"property","identifier":"report","data":{"属性x":属性x值,"属性y":属性y值,...}}*//*事件上报{"mid":"xxx","productKey":"xxx",//可根据消息内容判断填写不同产品"deviceName":"xxx","type":"event","identifier":"xx事件","data":{"参数x":参数x值,"参数y":参数y值,...}}*//*服务回复return {"mid":"xxx",//与服务调用时用的ID对应"productKey":"xxx",//可根据消息内容判断填写不同产品"deviceName":"xxx","type":"service","identifier":"xx服务_reply","code":0,"data":{"参数x":参数x值,"参数y":参数y值,...}}*/return xxxx}
目的地 发到另外一个topic
【物联网规则引擎设计】发到平台中的mq,需要定义topic 。