|
本帖最后由 灰哥哥 于 2018-10-24 16:25 编辑
1、生成一个规则
postman工具提交数据
post application/json to http://localhost:48075/api/v1/rule
{
"name": "motortoofastsignal",
"condition": {
"device": "GS1-VariableSpeedMotor01",
"checks": [{
"parameter": "RPM",
"operand1": "Integer.parseInt(value)",
"operation": ">",
"operand2": "2"
}]
},
"action": {
"device": "5bce72a0e4b055b4b7bf2d4f",
"command": "5bce72a0e4b055b4b7bf2d4c",
"body": "{\\\"RPM\\\":\\\"1\\\"}"
},
"log": "Patlite warning triggered for engine speed too high"
}
如下图:
上图提交内容说明:
- condition 中的device:对应device集合中的name
- check中的parameter:对应deviceprofile中定义了一个deviceresource
- action中的device:对应device集合中的id
- action中的command:先找到device对应的deviceprofile,command即是在deviceprofile中command的ID
此规则的含义
当support-rulesengine接收到export-distro发过来的event时,会判断此event如下:
设备名称为“GS1-VariableSpeedMotor01”,参数“RPM” 值大于2时,则会执行action。
执行action是指:调用设备ID为5bce72a0e4b055b4b7bf2d4f的ID为5bce72a0e4b055b4b7bf2d4c的command命令的put方法 ,put的内容为:{"RPM":"1"}
综述:当马达(RPM)转速高于2时,则将其设置为1
2、查看生成的规则
使用 docker exec -it support-rulesengine /bin/sh命令进入容器,在容器/edgex/edgex-support-rulesengine/rules目录下生成了motortoofastsignal.drl规则文件,查看过程如下:- myEdgex@ubuntu:/var/lib/docker$ docker exec -it support-rulesengine /bin/sh
- /edgex/edgex-support-rulesengine # ls
- rules support-rulesengine.jar templates
- /edgex/edgex-support-rulesengine # cd rules/
- /edgex/edgex-support-rulesengine/rules # ls
- motortoofastsignal.drl
- /edgex/edgex-support-rulesengine/rules # cat motortoofastsignal.drl
- package org.edgexfoundry.rules;
- global org.edgexfoundry.engine.CommandExecutor executor;
- global org.edgexfoundry.support.logging.client.EdgeXLogger logger;
- import org.edgexfoundry.domain.core.Event;
- import org.edgexfoundry.domain.core.Reading;
- import java.util.Map;
- rule "motortoofastsignal"
- when
- $e:Event($rlist: readings && device=="GS1-VariableSpeedMotor01")
- $r0:Reading(name=="RPM" && Integer.parseInt(value) > 2) from $rlist
- then
- executor.fireCommand("5bce72a0e4b055b4b7bf2d4f", "5bce72a0e4b055b4b7bf2d4c", "{"RPM":"1"}");
- logger.info("Patlite warning triggered for engine speed too high");
- end/edgex/edgex-support-rulesengine/rules #
复制代码
流程:
1、ZeroMQEventSubscriber类接收来自export-distro 的event消息(也可直接接收core-data的,可配置)后,调用RuleEngine的execute
2、RuleEngine的execute方法,执行规则判断(drools语句是按顺序执行的,每句通过再算规则满足)
3、规则满足,则执行CommandExecutor.fireCommand方法
drools语方
3、验证此规则引擎
本例是通过device-virtual来验证,首先打开device-virtual的 H2数据库,看一下当前的RPM值,如下图:
上图可以看出RPM=3,是可以满足规则的,会触发执行executor.fireCommand("5bce72a0e4b055b4b7bf2d4f", "5bce72a0e4b055b4b7bf2d4c", "{\"RPM\":\"1\"}");
3.1 构造满足规则的场景
向command模块发一个命令
用postman工具 发送GET http://localhost:48082/api/v1/device/5bce72a0e4b055b4b7bf2d4f/command/5bce72a0e4b055b4b7bf2d4c
注意:上面红色部分与当时规则创建时的一致。上面请求会触发一个event
再查看device-vitrul H2:
查看rulesengine日志
- docker-compose -f docker-compose-registry-java.yml logs rulesengine
复制代码
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
|