rvis-aiui-kit 1.1.1 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/sdk/README.md +29 -2
- package/sdk/api-map.js +2 -0
- package/sdk/core/client.js +52 -15
- package/sdk/core/constants.js +1 -0
- package/sdk/core/transport.js +21 -2
- package/sdk/modules/status-bar/index.js +78 -0
- package/sdk/modules/status-bar/readme.md +66 -0
package/package.json
CHANGED
package/sdk/README.md
CHANGED
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
- [API 总览](#api-总览)
|
|
14
14
|
- [通用调用约定](#通用调用约定)
|
|
15
15
|
- [Device Context 设备上下文](#device-context-设备上下文)
|
|
16
|
+
- [Status Bar 原生状态栏](#status-bar-原生状态栏)
|
|
16
17
|
- [Screen 屏幕控制](#screen-屏幕控制)
|
|
17
18
|
- [Notification 原生通知](#notification-原生通知)
|
|
18
19
|
- [TTS 语音播报](#tts-语音播报)
|
|
@@ -65,6 +66,8 @@ import glass3, {
|
|
|
65
66
|
|
|
66
67
|
| 模块 | 方法 | 用途 |
|
|
67
68
|
| --- | --- | --- |
|
|
69
|
+
| `statusBar` | `setTitle({ title })` | 设置或清理当前 AIX 页面的状态栏标题。 |
|
|
70
|
+
| `statusBar` | `setVisible({ visible })` | 显示或重新隐藏 Native 状态栏。 |
|
|
68
71
|
| `deviceContext` | `get(options?)` | 一次性获取设备上下文原子快照。 |
|
|
69
72
|
| `deviceContext` | `observe(options?, callOptions?)` | 订阅设备上下文快照和增量变化。 |
|
|
70
73
|
| `deviceContext` | `stopObserve({ requestId })` | 停止指定的设备上下文订阅。 |
|
|
@@ -218,6 +221,25 @@ const result = await glass3.deviceContext.stopObserve({
|
|
|
218
221
|
|
|
219
222
|
详见 [`modules/device-context/readme.md`](./modules/device-context/readme.md)。
|
|
220
223
|
|
|
224
|
+
## Status Bar 原生状态栏
|
|
225
|
+
|
|
226
|
+
状态栏是 Host 管理的系统 UI,可在屏幕顶部显示标题、时间、网络和电量。该能力只在
|
|
227
|
+
通过 `OPEN_AIX_INK` 外部入口打开的 AIX 页面中注册,页面创建后默认隐藏。
|
|
228
|
+
|
|
229
|
+
建议先设置标题,再显示:
|
|
230
|
+
|
|
231
|
+
```js
|
|
232
|
+
await glass3.statusBar.setTitle({ title: '设备巡检' });
|
|
233
|
+
const result = await glass3.statusBar.setVisible({ visible: true });
|
|
234
|
+
// { updated: true, visible: true }
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
`title` 必须是字符串;空字符串或全空白字符串用于清理标题。`visible` 必须是
|
|
238
|
+
Boolean。两个方法直接解析 Host RPC HTTP response body,不依赖 `onMessage`,并有
|
|
239
|
+
5 秒本地等待上限。
|
|
240
|
+
|
|
241
|
+
详见 [`modules/status-bar/readme.md`](./modules/status-bar/readme.md)。
|
|
242
|
+
|
|
221
243
|
## Screen 屏幕控制
|
|
222
244
|
|
|
223
245
|
### `glass3.screen.turnOff()`
|
|
@@ -986,8 +1008,8 @@ export default {
|
|
|
986
1008
|
|
|
987
1009
|
## Native 映射
|
|
988
1010
|
|
|
989
|
-
|
|
990
|
-
`method: "invoke"`。
|
|
1011
|
+
除 `statusBar` 外的 Host RPC 业务调用使用 `version: "2.0.0"`、
|
|
1012
|
+
`namespace: "rokid.tools"`、`method: "invoke"`。
|
|
991
1013
|
|
|
992
1014
|
| JavaScript API | `toolName` | `toolAction` |
|
|
993
1015
|
| --- | --- | --- |
|
|
@@ -1014,12 +1036,16 @@ export default {
|
|
|
1014
1036
|
| `motion.startDetect` | `motionDetect` | `start` |
|
|
1015
1037
|
| `motion.stopDetect` | `motionDetect` | `stop` |
|
|
1016
1038
|
|
|
1039
|
+
`statusBar.setTitle` 和 `statusBar.setVisible` 使用 `namespace: "rokid.statusBar"`,
|
|
1040
|
+
Native method 分别为 `setTitle` 和 `setVisible`,业务参数直接放在 `params` 中。
|
|
1041
|
+
|
|
1017
1042
|
业务代码应调用公开 JavaScript API,不要直接依赖这张 Native 映射表发 RPC。
|
|
1018
1043
|
|
|
1019
1044
|
## 详细文档与 Demo
|
|
1020
1045
|
|
|
1021
1046
|
### 模块文档
|
|
1022
1047
|
|
|
1048
|
+
- [Status Bar](./modules/status-bar/readme.md)
|
|
1023
1049
|
- [Device Context](./modules/device-context/readme.md)
|
|
1024
1050
|
- [Screen](./modules/screen/readme.md)
|
|
1025
1051
|
- [Notification](./modules/notification/readme.md)
|
|
@@ -1032,6 +1058,7 @@ export default {
|
|
|
1032
1058
|
|
|
1033
1059
|
### 示例页面
|
|
1034
1060
|
|
|
1061
|
+
- [Status Bar Demo](../pages/sdk-demo/status-bar/index.ink)
|
|
1035
1062
|
- [Device Context Demo](../pages/sdk-demo/device-context/index.ink)
|
|
1036
1063
|
- [Screen Demo](../pages/sdk-demo/screen/index.ink)
|
|
1037
1064
|
- [Notification Demo Suite](../pages/sdk-demo/notification/index.ink)
|
package/sdk/api-map.js
CHANGED
|
@@ -6,11 +6,13 @@ import motion from './modules/motion/index.js';
|
|
|
6
6
|
import notification from './modules/notification/index.js';
|
|
7
7
|
import offlineCommand from './modules/offline-command/index.js';
|
|
8
8
|
import screen from './modules/screen/index.js';
|
|
9
|
+
import statusBar from './modules/status-bar/index.js';
|
|
9
10
|
import tts from './modules/tts/index.js';
|
|
10
11
|
|
|
11
12
|
// This map is the only mapping between the public JavaScript API and Host RPC.
|
|
12
13
|
// Public names are never inferred from the native namespace or method strings.
|
|
13
14
|
export const API_MAP = [].concat(
|
|
15
|
+
statusBar,
|
|
14
16
|
deviceContext,
|
|
15
17
|
notification,
|
|
16
18
|
screen,
|
package/sdk/core/client.js
CHANGED
|
@@ -43,12 +43,14 @@ export class Glass3Client {
|
|
|
43
43
|
requestId,
|
|
44
44
|
namespace: definition.namespace,
|
|
45
45
|
method: definition.method,
|
|
46
|
-
params:
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
46
|
+
params: typeof definition.buildRequestParams === 'function'
|
|
47
|
+
? definition.buildRequestParams(nativeArgs)
|
|
48
|
+
: {
|
|
49
|
+
toolName: definition.toolName,
|
|
50
|
+
toolAction: definition.toolAction,
|
|
51
|
+
args: nativeArgs,
|
|
52
|
+
extra: {}
|
|
53
|
+
}
|
|
52
54
|
};
|
|
53
55
|
|
|
54
56
|
if (definition.completion === COMPLETION_EVENT) {
|
|
@@ -59,7 +61,12 @@ export class Glass3Client {
|
|
|
59
61
|
return this.invokeUntilReadyEventStream(definition, request, callOptions);
|
|
60
62
|
}
|
|
61
63
|
|
|
62
|
-
return this.invokeUntilAccepted(
|
|
64
|
+
return this.invokeUntilAccepted(
|
|
65
|
+
definition,
|
|
66
|
+
request,
|
|
67
|
+
callOptions,
|
|
68
|
+
timeoutMs
|
|
69
|
+
);
|
|
63
70
|
}
|
|
64
71
|
|
|
65
72
|
sendRequest(definition, request) {
|
|
@@ -104,7 +111,7 @@ export class Glass3Client {
|
|
|
104
111
|
return true;
|
|
105
112
|
}
|
|
106
113
|
|
|
107
|
-
invokeUntilAccepted(definition, request, callOptions) {
|
|
114
|
+
invokeUntilAccepted(definition, request, callOptions, timeoutMs) {
|
|
108
115
|
if (typeof callOptions.onEvent === 'function') {
|
|
109
116
|
this.activeInvocations.set(request.requestId, {
|
|
110
117
|
requestId: request.requestId,
|
|
@@ -113,13 +120,37 @@ export class Glass3Client {
|
|
|
113
120
|
});
|
|
114
121
|
}
|
|
115
122
|
|
|
116
|
-
|
|
123
|
+
let timeoutId = null;
|
|
124
|
+
const responsePromise = this.sendRequest(definition, request);
|
|
125
|
+
|
|
126
|
+
if (timeoutMs !== undefined) {
|
|
127
|
+
timeoutId = setTimeout(() => {
|
|
128
|
+
this.rejectPendingResponse(request.requestId, new Glass3Error(
|
|
129
|
+
`Glass3 call timed out after ${timeoutMs}ms: ${definition.publicModule}.${definition.publicMethod}`,
|
|
130
|
+
{
|
|
131
|
+
code: 'CALL_TIMEOUT',
|
|
132
|
+
stage: 'timeout',
|
|
133
|
+
requestId: request.requestId,
|
|
134
|
+
namespace: definition.namespace,
|
|
135
|
+
method: definition.method,
|
|
136
|
+
details: { timeout: timeoutMs }
|
|
137
|
+
}
|
|
138
|
+
));
|
|
139
|
+
}, timeoutMs);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return responsePromise.then(response => {
|
|
143
|
+
if (timeoutId !== null) clearTimeout(timeoutId);
|
|
117
144
|
this.validateAcceptedResponse(definition, request.requestId, response);
|
|
145
|
+
if (typeof definition.selectAcceptedResult === 'function') {
|
|
146
|
+
return definition.selectAcceptedResult(response);
|
|
147
|
+
}
|
|
118
148
|
return {
|
|
119
149
|
requestId: response.requestId,
|
|
120
150
|
ok: response.ok
|
|
121
151
|
};
|
|
122
152
|
}).catch(error => {
|
|
153
|
+
if (timeoutId !== null) clearTimeout(timeoutId);
|
|
123
154
|
this.activeInvocations.delete(request.requestId);
|
|
124
155
|
throw this.normalizeInvocationError(error, definition, request.requestId);
|
|
125
156
|
});
|
|
@@ -235,22 +266,28 @@ export class Glass3Client {
|
|
|
235
266
|
acceptedResult.toolName === definition.toolName &&
|
|
236
267
|
acceptedResult.toolAction === definition.toolAction
|
|
237
268
|
);
|
|
238
|
-
const
|
|
269
|
+
const validChannel = !definition.responseChannel || (
|
|
270
|
+
response && response.channel === definition.responseChannel
|
|
271
|
+
);
|
|
272
|
+
const validEnvelope = response &&
|
|
273
|
+
validChannel &&
|
|
239
274
|
response.version === RPC_VERSION &&
|
|
240
275
|
response.kind === 'response' &&
|
|
241
|
-
response.requestId === requestId
|
|
242
|
-
|
|
243
|
-
validAcceptedResult;
|
|
276
|
+
response.requestId === requestId;
|
|
277
|
+
const validResponse = validEnvelope &&
|
|
278
|
+
response.ok === true && validAcceptedResult;
|
|
244
279
|
|
|
245
280
|
if (validResponse) return;
|
|
246
281
|
|
|
247
|
-
const nativeError =
|
|
282
|
+
const nativeError = validEnvelope && response.ok === false
|
|
283
|
+
? response.error
|
|
284
|
+
: null;
|
|
248
285
|
throw new Glass3Error(
|
|
249
286
|
nativeError && nativeError.message
|
|
250
287
|
? nativeError.message
|
|
251
288
|
: `Native request was not accepted: ${definition.publicModule}.${definition.publicMethod}`,
|
|
252
289
|
{
|
|
253
|
-
code: nativeError && nativeError.code
|
|
290
|
+
code: nativeError && nativeError.code !== undefined
|
|
254
291
|
? nativeError.code
|
|
255
292
|
: 'NATIVE_REQUEST_NOT_ACCEPTED',
|
|
256
293
|
stage: 'response',
|
package/sdk/core/constants.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export const RPC_URL = 'https://ink-host-rpc.invalid/rpc';
|
|
2
2
|
export const RPC_ORIGIN = 'rokid://host-rpc';
|
|
3
3
|
export const RPC_VERSION = '2.0.0';
|
|
4
|
+
export const RPC_CHANNEL = 'rokid.host.rpc';
|
|
4
5
|
|
|
5
6
|
export const RPC_SUCCESS_STATUS_MIN = 200;
|
|
6
7
|
export const RPC_SUCCESS_STATUS_MAX = 299;
|
package/sdk/core/transport.js
CHANGED
|
@@ -22,10 +22,20 @@ export class HostRpcTransport {
|
|
|
22
22
|
JSON.stringify(redactSensitiveDataForLog(request))
|
|
23
23
|
);
|
|
24
24
|
|
|
25
|
-
return requestFetch(this.url, {
|
|
25
|
+
return Promise.resolve().then(() => requestFetch(this.url, {
|
|
26
26
|
method: 'POST',
|
|
27
27
|
headers: { 'content-type': 'application/json' },
|
|
28
28
|
body: requestBody
|
|
29
|
+
})).catch(error => {
|
|
30
|
+
if (error instanceof Glass3Error) throw error;
|
|
31
|
+
throw new Glass3Error('Host RPC transport failed', {
|
|
32
|
+
code: 'TRANSPORT_ERROR',
|
|
33
|
+
stage: 'transport',
|
|
34
|
+
requestId: request.requestId,
|
|
35
|
+
namespace: request.namespace,
|
|
36
|
+
method: request.method,
|
|
37
|
+
cause: error
|
|
38
|
+
});
|
|
29
39
|
}).then(response => {
|
|
30
40
|
if (
|
|
31
41
|
response.status < RPC_SUCCESS_STATUS_MIN ||
|
|
@@ -43,7 +53,16 @@ export class HostRpcTransport {
|
|
|
43
53
|
);
|
|
44
54
|
}
|
|
45
55
|
|
|
46
|
-
return response.json()
|
|
56
|
+
return Promise.resolve().then(() => response.json()).catch(error => {
|
|
57
|
+
throw new Glass3Error('Host RPC response is not valid JSON', {
|
|
58
|
+
code: 'TRANSPORT_INVALID_RESPONSE',
|
|
59
|
+
stage: 'transport',
|
|
60
|
+
requestId: request.requestId,
|
|
61
|
+
namespace: request.namespace,
|
|
62
|
+
method: request.method,
|
|
63
|
+
cause: error
|
|
64
|
+
});
|
|
65
|
+
});
|
|
47
66
|
}).then(result => {
|
|
48
67
|
console.log(
|
|
49
68
|
'[glass3] Native fetch response:',
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { RPC_CHANNEL } from '../../core/constants.js';
|
|
2
|
+
import { Glass3Error } from '../../utils/errors.js';
|
|
3
|
+
|
|
4
|
+
const STATUS_BAR_NAMESPACE = 'rokid.statusBar';
|
|
5
|
+
const STATUS_BAR_TIMEOUT_MS = 5000;
|
|
6
|
+
|
|
7
|
+
function createParameterError(message, method) {
|
|
8
|
+
return new Glass3Error(message, {
|
|
9
|
+
code: 'INVALID_PARAMS',
|
|
10
|
+
stage: 'params',
|
|
11
|
+
namespace: STATUS_BAR_NAMESPACE,
|
|
12
|
+
method
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function buildSingleFieldArgs(field, type, method) {
|
|
17
|
+
return args => {
|
|
18
|
+
if (!args || typeof args !== 'object' || Array.isArray(args)) {
|
|
19
|
+
throw createParameterError(
|
|
20
|
+
`statusBar.${method} parameters must be an object`,
|
|
21
|
+
method
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const fields = Object.keys(args);
|
|
26
|
+
if (fields.length !== 1 || fields[0] !== field) {
|
|
27
|
+
throw createParameterError(
|
|
28
|
+
`statusBar.${method} only supports ${field}`,
|
|
29
|
+
method
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (typeof args[field] !== type) {
|
|
34
|
+
throw createParameterError(
|
|
35
|
+
`statusBar.${method} ${field} must be a ${type}`,
|
|
36
|
+
method
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return { [field]: args[field] };
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function directParams(args) {
|
|
45
|
+
return args;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function selectResult(response) {
|
|
49
|
+
return response.result;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function createStatusBarDefinition(publicMethod, field, type) {
|
|
53
|
+
return {
|
|
54
|
+
publicModule: 'statusBar',
|
|
55
|
+
publicMethod,
|
|
56
|
+
|
|
57
|
+
namespace: STATUS_BAR_NAMESPACE,
|
|
58
|
+
method: publicMethod,
|
|
59
|
+
responseChannel: RPC_CHANNEL,
|
|
60
|
+
|
|
61
|
+
buildArgs: buildSingleFieldArgs(field, type, publicMethod),
|
|
62
|
+
buildRequestParams: directParams,
|
|
63
|
+
getTimeoutMs() {
|
|
64
|
+
return STATUS_BAR_TIMEOUT_MS;
|
|
65
|
+
},
|
|
66
|
+
isAcceptedResponseResult(result) {
|
|
67
|
+
if (!result || typeof result.updated !== 'boolean') return false;
|
|
68
|
+
return publicMethod !== 'setVisible' ||
|
|
69
|
+
typeof result.visible === 'boolean';
|
|
70
|
+
},
|
|
71
|
+
selectAcceptedResult: selectResult
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export default [
|
|
76
|
+
createStatusBarDefinition('setTitle', 'title', 'string'),
|
|
77
|
+
createStatusBarDefinition('setVisible', 'visible', 'boolean')
|
|
78
|
+
];
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# Status Bar 原生状态栏
|
|
2
|
+
|
|
3
|
+
`glass3.statusBar` 通过同步 HTTP response body 的 Host RPC 控制 AIX 页面顶部的
|
|
4
|
+
系统状态栏。状态栏由 Native 绘制,可显示标题、时间、网络和电量;调用结果不依赖
|
|
5
|
+
页面 `onMessage`。
|
|
6
|
+
|
|
7
|
+
该能力只在通过外部 `OPEN_AIX_INK` 入口打开的 AIX 页面中注册。页面建立后默认隐藏
|
|
8
|
+
状态栏,普通浏览器、普通 WebView 和普通 AI 卡片中调用会失败。
|
|
9
|
+
|
|
10
|
+
## API
|
|
11
|
+
|
|
12
|
+
### `glass3.statusBar.setTitle({ title })`
|
|
13
|
+
|
|
14
|
+
设置当前页面的状态栏标题:
|
|
15
|
+
|
|
16
|
+
```js
|
|
17
|
+
const result = await glass3.statusBar.setTitle({
|
|
18
|
+
title: '设备巡检'
|
|
19
|
+
});
|
|
20
|
+
// { updated: true }
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
- `title` 必须是字符串,SDK 不会隐式转换类型。
|
|
24
|
+
- Native 会去除首尾空白,当前最多展示前 40 个字符。
|
|
25
|
+
- 空字符串或全空白字符串表示清理标题。
|
|
26
|
+
- 状态栏隐藏时会保存标题,并在下次显示时应用。
|
|
27
|
+
|
|
28
|
+
### `glass3.statusBar.setVisible({ visible })`
|
|
29
|
+
|
|
30
|
+
申请显示或重新隐藏状态栏:
|
|
31
|
+
|
|
32
|
+
```js
|
|
33
|
+
const result = await glass3.statusBar.setVisible({ visible: true });
|
|
34
|
+
// { updated: true, visible: true }
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
`visible` 必须是 Boolean,字符串 `"true"` 和数字 `1` 均会被 SDK 拒绝。
|
|
38
|
+
|
|
39
|
+
建议先设置标题,再显示状态栏:
|
|
40
|
+
|
|
41
|
+
```js
|
|
42
|
+
await glass3.statusBar.setTitle({ title: '设备巡检' });
|
|
43
|
+
await glass3.statusBar.setVisible({ visible: true });
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
`visible: false` 会隐藏当前状态栏,但不会删除页面保存的标题。再次显示时,Native 会
|
|
47
|
+
恢复此前的标题。
|
|
48
|
+
|
|
49
|
+
## 返回、超时与错误
|
|
50
|
+
|
|
51
|
+
两个方法都直接返回 Host RPC `result`,并严格校验响应的 `channel`、`version`、
|
|
52
|
+
`kind` 和 `requestId`。每次调用的本地等待上限为 5 秒;`CALL_TIMEOUT` 只表示 SDK
|
|
53
|
+
停止等待,不能据此判断 Native 一定没有执行。
|
|
54
|
+
|
|
55
|
+
- 参数错误:`INVALID_PARAMS`,`stage: "params"`。
|
|
56
|
+
- fetch 失败:`TRANSPORT_ERROR`,`stage: "transport"`。
|
|
57
|
+
- HTTP 非 2xx:`TRANSPORT_REJECTED`,`stage: "transport"`。
|
|
58
|
+
- body 不是合法 JSON:`TRANSPORT_INVALID_RESPONSE`,`stage: "transport"`。
|
|
59
|
+
- Native RPC 错误:保留 Native `error.code`,`stage: "response"`。
|
|
60
|
+
- 响应信封不合法:`NATIVE_REQUEST_NOT_ACCEPTED`,`stage: "response"`。
|
|
61
|
+
|
|
62
|
+
## 生命周期
|
|
63
|
+
|
|
64
|
+
Native 按 AIX 页面 owner 保存和回收状态栏资源。页面进入后台、释放或进程结束时,
|
|
65
|
+
Native 会清理对其他页面可能产生的影响。页面可以主动调用
|
|
66
|
+
`setVisible({ visible: false })`,但不需要用全局 JavaScript 状态恢复资源。
|