规则引擎(rule engine)

创建规则

API 定义:

  1. POST api/v3/rules

参数定义:

nameString,规则名字
forString,Hook 的名字,可以为: “message.publish”,”client.connected” … 详见 plugins
rawsqlString,用于筛选和转换原始数据的 SQL 语句
actionsJSON Array,动作列表
    1. | name | String, 动作名字
    1. | params | JSON Object, 动作参数

    description | String,可选,规则描述 |

请求参数示例:

  1. {
  2. "name": "test-rule",
  3. "for": "message.publish",
  4. "rawsql": "select * from \"t/a\"",
  5. "actions": [{
  6. "name": "built_in:inspect_action",
  7. "params": {
  8. "a": 1
  9. }
  10. }],
  11. "description": "test-rule"
  12. }

返回数据示例:

  1. {
  2. "code": 0,
  3. "data": {
  4. "actions": [{
  5. "name": "built_in:inspect_action",
  6. "params": {
  7. "$resource": "built_in:test-resource",
  8. "a": 1
  9. }
  10. }],
  11. "description": "test-rule",
  12. "enabled": true,
  13. "for": "message.publish",
  14. "id": "test-rule:1556263150688255821",
  15. "name": "test-rule",
  16. "rawsql": "select * from \"t/a\""
  17. }
  18. }

查询规则

API 定义:

  1. GET api/v3/rules/${rule_id}

请求参数示例:

  1. GET api/v3/rules/test-rule:1556263150688255821

返回数据示例:

  1. {
  2. "code": 0,
  3. "data": {
  4. "actions": [{
  5. "name": "built_in:inspect_action",
  6. "params": {
  7. "$resource": "built_in:test-resource",
  8. "a": 1
  9. }
  10. }],
  11. "description": "test-rule",
  12. "enabled": true,
  13. "for": "message.publish",
  14. "id": "test-rule:1556263150688255821",
  15. "name": "test-rule",
  16. "rawsql": "select * from \"t/a\""
  17. }
  18. }

获取当前规则列表

API 定义:

  1. GET api/v3/rules

返回数据示例:

  1. {
  2. "code": 0,
  3. "data": [{
  4. "actions": [{
  5. "name": "built_in:inspect_action",
  6. "params": {
  7. "$resource": "built_in:test-resource",
  8. "a": 1
  9. }
  10. }],
  11. "description": "test-rule",
  12. "enabled": true,
  13. "for": "message.publish",
  14. "id": "test-rule:1556263150688255821",
  15. "name": "test-rule",
  16. "rawsql": "select * from \"t/a\""
  17. }]
  18. }

删除规则

API 定义:

  1. DELETE api/v3/rules/${rule_id}

请求参数示例:

  1. DELETE api/v3/rules/test-rule:1556263150688255821

返回数据示例:

  1. {
  2. "code": 0
  3. }

获取当前动作列表

API 定义:

  1. GET api/v3/actions?for=${hook_type}

请求参示例:

  1. GET api/v3/actions

返回数据示例:

  1. {
  2. "code": 0,
  3. "data": [{
  4. "app": "emqx_rule_engine",
  5. "description": "Republish a MQTT message to a another topic",
  6. "for": "message.publish",
  7. "name": "built_in:republish_action",
  8. "params": {
  9. "target_topic": {
  10. "description": "Repubilsh the message to which topic",
  11. "format": "topic",
  12. "required": true,
  13. "title": "To Which Topic",
  14. "type": "string"
  15. }
  16. },
  17. "type": "built_in"
  18. }, {
  19. "app": "emqx_web_hook",
  20. "description": "Forward Events to Web Server",
  21. "for": "$events",
  22. "name": "web_hook:event_action",
  23. "params": {
  24. "$resource": {
  25. "description": "Bind a resource to this action",
  26. "required": true,
  27. "title": "Resource ID",
  28. "type": "string"
  29. },
  30. "template": {
  31. "description": "The payload template to be filled with variables before sending messages",
  32. "required": false,
  33. "schema": {},
  34. "title": "Payload Template",
  35. "type": "object"
  36. }
  37. },
  38. "type": "web_hook"
  39. }, {
  40. "app": "emqx_web_hook",
  41. "description": "Forward Messages to Web Server",
  42. "for": "message.publish",
  43. "name": "web_hook:publish_action",
  44. "params": {
  45. "$resource": {
  46. "description": "Bind a resource to this action",
  47. "required": true,
  48. "title": "Resource ID",
  49. "type": "string"
  50. }
  51. },
  52. "type": "web_hook"
  53. }, {
  54. "app": "emqx_rule_engine",
  55. "description": "Inspect the details of action params for debug purpose",
  56. "for": "$any",
  57. "name": "built_in:inspect_action",
  58. "params": {},
  59. "type": "built_in"
  60. }]
  61. }

请求参数示例:

  1. GET api/v3/actions?for=client.connected

返回数据示例:

  1. {
  2. "code": 0,
  3. "data": [{
  4. "app": "emqx_rule_engine",
  5. "description": "Inspect the details of action params for debug purpose",
  6. "for": "$any",
  7. "name": "built_in:inspect_action",
  8. "params": {},
  9. "type": "built_in"
  10. }]
  11. }

查询动作

API 定义:

  1. GET api/v3/actions/:action_name

请求参数示例:

  1. GET api/v3/actions/built_in:inspect_action

返回数据示例:

  1. {
  2. "code": 0,
  3. "data": {
  4. "app": "emqx_rule_engine",
  5. "description": "Inspect the details of action params for debug purpose",
  6. "for": "$any",
  7. "name": "built_in:inspect_action",
  8. "params": {},
  9. "type": "built_in"
  10. }
  11. }

获取当前资源类型列表

API 定义:

  1. GET api/v3/resource_types

返回数据示例:

  1. {
  2. "code": 0,
  3. "data": [{
  4. "attrs": "undefined",
  5. "config": {
  6. "url": "http://host-name/chats"
  7. },
  8. "description": "forward msgs to host-name/chats",
  9. "id": "web_hook:webhook1",
  10. "name": "webhook1",
  11. "type": "web_hook"
  12. }, {
  13. "attrs": "undefined",
  14. "config": {
  15. "a": 1
  16. },
  17. "description": "test-resource",
  18. "id": "built_in:test-resource",
  19. "name": "test-resource",
  20. "type": "built_in"
  21. }]
  22. }

查询资源类型

API 定义:

  1. GET api/v3/resource_types/${type}

请求参数示例:

  1. GET api/v3/resource_types/built_in

返回数据示例:

  1. {
  2. "code": 0,
  3. "data": {
  4. "description": "The built in resource type for debug purpose",
  5. "name": "built_in",
  6. "params": {},
  7. "provider": "emqx_rule_engine"
  8. }
  9. }

获取某种类型的资源

API 定义:

  1. GET api/v3/resource_types/${type}/resources

请求参数示例:

  1. GET api/v3/resource_types/built_in/resources

返回数据示例:

  1. {
  2. "code": 0,
  3. "data": [{
  4. "attrs": "undefined",
  5. "config": {
  6. "a": 1
  7. },
  8. "description": "test-resource",
  9. "id": "built_in:test-resource",
  10. "name": "test-resource",
  11. "type": "built_in"
  12. }]
  13. }

获取某种类型的动作

API 定义:

  1. GET api/v3/resource_types/${type}/actions

请求参数示例:

  1. GET api/v3/resource_types/built_in/actions

返回数据示例:

  1. {
  2. "code": 0,
  3. "data": [{
  4. "app": "emqx_rule_engine",
  5. "description": "Inspect the details of action params for debug purpose",
  6. "for": "$any",
  7. "name": "built_in:inspect_action",
  8. "params": {},
  9. "type": "built_in"
  10. }, {
  11. "app": "emqx_rule_engine",
  12. "description": "Republish a MQTT message to a another topic",
  13. "for": "message.publish",
  14. "name": "built_in:republish_action",
  15. "params": {
  16. "target_topic": {
  17. "description": "Repubilsh the message to which topic",
  18. "format": "topic",
  19. "required": true,
  20. "title": "To Which Topic",
  21. "type": "string"
  22. }
  23. },
  24. "type": "built_in"
  25. }]
  26. }

创建资源

API 定义:

  1. POST api/v3/resources

参数定义:

nameString, 资源名字
typeString, 资源类型
configJSON Object, 资源配置
descriptionString,可选,规则描述

参数示例:

  1. {
  2. "name": "test-resource",
  3. "type": "built_in",
  4. "config": {
  5. "a": 1
  6. },
  7. "description": "test-resource"
  8. }

返回数据示例:

  1. {
  2. "code": 0,
  3. "data": {
  4. "attrs": "undefined",
  5. "config": {
  6. "a": 1
  7. },
  8. "description": "test-resource",
  9. "id": "built_in:test-resource",
  10. "name": "test-resource",
  11. "type": "built_in"
  12. }
  13. }

获取资源列表

API 定义:

  1. GET api/v3/resources

返回数据示例:

  1. {
  2. "code": 0,
  3. "data": [{
  4. "attrs": "undefined",
  5. "config": {
  6. "url": "http://host-name/chats"
  7. },
  8. "description": "forward msgs to host-name/chats",
  9. "id": "web_hook:webhook1",
  10. "name": "webhook1",
  11. "type": "web_hook"
  12. }, {
  13. "attrs": "undefined",
  14. "config": {
  15. "a": 1
  16. },
  17. "description": "test-resource",
  18. "id": "built_in:test-resource",
  19. "name": "test-resource",
  20. "type": "built_in"
  21. }]
  22. }

查询资源

API 定义:

  1. GET api/v3/resources/:resource_id

请求参数示例:

  1. GET api/v3/resources/built_in:test-resource

返回数据示例:

  1. {
  2. "code": 0,
  3. "data": {
  4. "attrs": "undefined",
  5. "config": {
  6. "a": 1
  7. },
  8. "description": "test-resource",
  9. "id": "built_in:test-resource",
  10. "name": "test-resource",
  11. "type": "built_in"
  12. }
  13. }

删除资源

API 定义:

  1. DELETE api/v3/resources/:resource_id

请求参数示例:

  1. DELETE api/v3/resources/built_in:test-resource

返回数据示例:

  1. {
  2. "code": 0
  3. }