告警推送
Note
告警推送分为厂家规则和用户规则,其中厂家规则必须由厂家用户调用console-openapi.hekr.me, 用户规则由厂家用户调用user-openapi.hekr.me
厂家查询脚本¶
GET https://console-openapi.hekr.me/api/v1/product/{mid}/ifttt HTTP/1.1
Authorization: Bearer {JWT_TOKEN}
HTTP/1.1 200 OK
Content-Type: application/json;charset=UTF-8
{
"id": {
"pid": "01240737641",
"mid": "IyFauxgNdjFs"
},
"enable": true,
"language": "rule",
"ifttt": {
"javascript": {
"code": "var onDevSend = function (message) {}"
},
"rule": {
"rules": [
{
"name": "关灯通知",
"enable": true,
"triggerOn": "onDevSend",
"condition": {
"type": "and",
"conditions": [
{
"type": "eq",
"left": "cmdId",
"right": 1
},
{
"type": "eq",
"left": "switch",
"right": 0
}
]
},
"actions": [
{
"type": "notification",
"push": true,
"subject": {
"zh": "灯关了",
"en": "lights out"
},
"content": {
"zh": "开关 = ${switch}",
"en": "switch = ${switch}"
},
"notificationType": "INFO"
}
]
}
]
}
}
}
厂家添加脚本(javascript)¶
PUT https://console-openapi.hekr.me/api/v1/product/{mid}/ifttt HTTP/1.1
Content-Type: application/json;charset=UTF-8
Authorization: Bearer {JWT_TOKEN}
{
"enable": false,
"language": "javascript",
"ifttt": {
"javascript": {
"code": "var onDevSend = function (message) {}"
}
}
}
HTTP/1.1 200 OK
Content-Type: application/json;charset=UTF-8
{
"id": {
"pid": "01240737641",
"mid": "IyFauxgNdjFs"
},
"enable": false,
"language": "javascript",
"ifttt": {
"javascript": {
"code": "var onDevSend = function (message) {}"
}
}
}
请求参数¶
参数 | 类型 | 是否必须 | 默认 | 含义 |
---|---|---|---|---|
enable | Boolean | true | 是否启用该IFTTT | |
language | String | true | IFTTT编写方式,当前支持[javascript, rule] | |
ifttt.javascript.code | String | true | javascript 方式脚本,编写规则参考下面的详细说明) |
使用告警脚本¶
厂家可以通过编写Javascript脚本自定义针对产品的告警推送逻辑。脚本使用ES5语法。以下是一个简单例子:
var onDevSend = function(message) {
if (message.cmdId === 1 && message.switch === 1){
router.addNotification('灯开了', 'light = ' + message.light, message);
router.sendAppPush('灯开了', 'light = ' + message.light);
}
};
该脚本表示设备如果上报命令(cmdId=1) 中switch
的值为1,便为设备用户的消息中心中添加一条记录,并且用APP推送消息。
router是预定义的消息路由,里面定义了添加消息及APP推送等接口。所有可用的方法见下面列表。
router.addNotification¶
语法¶
router.addNotification(subject, content)
router.addNotification(subject, content, reportData)
router.addNotification(subject, content, reportData, options)
参数¶
名称 | 类型 | 是否必须 | 例子 | 说明 |
---|---|---|---|---|
subject | String、Object | 是 | 'foo' {'zh': 'foo', 'en': 'foo'} | 通知标题,可以使用字符串或LanguageTag-String 键值对 |
content | String、Object | 是 | 'foo' {'zh': 'foo', 'en': 'foo'} | 通知内容,可以使用字符串或LanguageTag-String 键值对 |
reportData | Object | 否 | {foo: 'bar'} | 自动定义上报数据,用户在拉取设备告警时可以获得该值,默认不会显示 |
options | Object | 否 | {type: 'INFO'} | 其他选项 |
options.type | String | 否 | WARNING | 通知类型 [ WARNING, INFO ], 如果options为空,或者未指定options.type,则默认为'WARNING' |
多语言匹配规则
subject或content使用LanguageTag-String 键值对时将根据用户上报的locale返回响应的内容, 具体匹配规则如下:
- 如果用户未上报locale则默认用户locale为
en
- 如果存在精确匹配则返回精确匹配内容
- 查找最接近的匹配,例如若用户locale为
zh-CN#hans
,则依次查找zh-CN#Hans -> zh-CN -> zh
- 返回键值对的第一项
例如,subject为{en: 'foo', zh: '中文'}
,用户locale为fr
,则用户将看到'foo'(根据第4条规则)
返回值¶
空
router.sendAppPush¶
Note
使用该action发送推送给APP, APP将接受到的设备告警推送。
语法¶
router.sendAppPush(subject, content)
router.sendAppPush(subject, content, hideParams, options)
参数¶
名称 | 类型 | 是否必须 | 例子 | 说明 |
---|---|---|---|---|
subject | String、Object | 是 | 'foo' {'zh': 'foo', 'en': 'foo'} | 通知标题,可以使用字符串或LanguageTag-String 键值对 |
content | String、Object | 是 | 'foo' {'zh': 'foo', 'en': 'foo'} | 通知内容,可以使用字符串或LanguageTag-String 键值对 |
hideParams | Object | 否 | {foo: 'bar'} | APP推送隐藏参数 |
options | Object | 否 | {} | APP推送其他参数 |
options.icon | String | 否 | https://example.png/f/logo.png | 推送图标,当前仅在华为推送中生效 |
返回值¶
空
厂家添加脚本(rule)¶
PUT https://console-openapi.hekr.me/api/v1/product/{mid}/ifttt HTTP/1.1
Content-Type: application/json;charset=UTF-8
Authorization: Bearer {JWT_TOKEN}
{
"enable": true,
"language": "rule",
"ifttt": {
"javascript": {
"code": "var onDevSend = function (message) {}"
},
"rule": {
"rules": [
{
"name": "关灯通知",
"enable": true,
"triggerOn": "onDevSend",
"condition": {
"type": "and",
"conditions": [
{
"type": "eq",
"left": "cmdId",
"right": 1
},
{
"type": "eq",
"left": "switch",
"right": 0
}
]
},
"actions": [
{
"type": "notification",
"notificationType": "INFO",
"subject": {
"zh": "灯关了",
"en": "lights out"
},
"content": {
"zh": "开关 = ${switch}",
"en": "switch = ${switch}"
}
}
]
}
]
}
}
}
HTTP/1.1 200 OK
Content-Type: application/json;charset=UTF-8
{
"id": {
"pid": "01240737641",
"mid": "IyFauxgNdjFs"
},
"enable": true,
"language": "rule",
"ifttt": {
"javascript": {
"code": "var onDevSend = function (message) {}"
},
"rule": {
"rules": [
{
"name": "关灯通知",
"enable": true,
"triggerOn": "onDevSend",
"condition": {
"type": "and",
"conditions": [
{
"type": "eq",
"left": "cmdId",
"right": 1
},
{
"type": "eq",
"left": "switch",
"right": 0
}
]
},
"actions": [
{
"type": "notification",
"subject": {
"zh": "灯关了",
"en": "lights out"
},
"content": {
"zh": "开关 = ${switch}",
"en": "switch = ${switch}"
},
"notificationType": "INFO"
}
]
}
]
}
}
}
请求参数¶
参数 | 类型 | 是否必须 | 默认 | 含义 |
---|---|---|---|---|
ifttt.rule.rules.*.name | String | true | 规则名称 | |
ifttt.rule.rules.*.enable | Boolean | true | 是否启用该规则 | |
ifttt.rule.rules.*.triggerOn | String | true | 触发时机 当前支持[onDevSend] | |
ifttt.rule.rules.*.condition | Object | true | 触发条件 | |
ifttt.rule.rules.*.actions | Array | true | 执行动作 |
condition¶
condition支持多层复合,约定客户端仅使用2层以便于客户端解析。第一层and, or 逻辑条件第二层eq, lt...
逻辑条件包含type和conditions,type有"and", "or"两种分别对应逻辑与和逻辑或,conditions是提交列表 可以是任何条件的集合(逻辑条件或者算术条件)。
{
"type": "and",
"conditions": []
}
算术条件由三部分组成,type表示条件类型,left为左操作数,right为右操作数 例如下例表示switch 等于 10
{
"type": "eq",
"left": "switch",
"right": 10
}
所有可用的操作符:
名称 | 含义 |
---|---|
lt | 小于 |
gt | 大于 |
eq | 等于 |
lte | 小于等于 |
gte | 大于等于 |
Note
- 服务端并不强制要求left为变量或数字,但建议保证left一定是变量
- 变量名必须满足正则表达式
^[a-zA-Z_][a-zA-Z0-9]*$
- 可以是2个变量之间进行比较,
{ "type": "gte", "left": "param1", "right": "param2" }
表示param1 >= param2
action¶
rule可以指定多个action,通过type区分,目前支持[notification, appPush]
notification
添加用户通知
参数 | 类型 | 是否必须 | 默认 | 含义 |
---|---|---|---|---|
notificationType | String | true | 通知类型 当前支持[WARNING, INFO] | |
subject | Object | true | 多语言标题 | |
content | Object | true | 多语言内容 |
appPush
发送APP应用推送
参数 | 类型 | 是否必须 | 默认 | 含义 |
---|---|---|---|---|
subject | Object | true | 多语言标题 | |
content | Object | true | 多语言内容 |
http
参数 | 类型 | 是否必须 | 默认 | 含义 |
---|---|---|---|---|
url | String | true | 消息转发地址 | |
method | [PUT, POST] | true | 消息转发HTTP 方法 |
消息的格式为
{
"ctrlKey": "298722...",
"mid": "JIxjWqjxk",
"payload": {
"cmdId": 1,
"light": 23
}
}
字段 | 含义 |
---|---|
ctrlKey | 设备唯一标识 |
mid | 产品唯一标识 |
payload | 具体的设备上报信息 |