指令交互组件
更新时间:2020-07-07
端云交互指令组件整体介绍
用户通过机器人输入语音、文字、图像等信息后,SDK将输入的信息包装成事件(event)发送给服务器,并接收服务器返回的指令(directive)集,开发者通过传入指令监听回调接口的实现来接收指令集,进行对应的逻辑处理。
- 指令和事件是完成机器人与人交互的最基本的要素,设备端上发生的变化都通过上报相应的事件来通知服务端,服务端通过下发指令给设备端,对用户请求进行响应。
事件(event)
是设备端上报给服务端,通知服务端在设备端发生的事情。比如语音识别到用户说的话,人脸功能检测到人脸等。
指令(directive)
是服务端下发给设备端,设备端需要执行的操作。比如根据语音识别到的用户语义下发相关的语义指令或图文信息,根据人脸识别的图片信息下发人脸图片对应的用户信息等。
指令监听回调接口分类
- IDirectiveCallback 指令回调基础接口,用于接收服务端下发的原始json指令信息,用户可以基于原始指令信息开发业务功能。
- IDirectiveListener 指令监听扩展接口,在指令回调基础接口上,对指令数据进行了分类,简化原始json指令信息解析,每个分类定义了对应的接口,方便用户快速实现业务功能。指令监听扩展接口开源代码可在SampleApp中查找修改。
组件配置
指令组件作为SDK基础组件功能,在SDK初始化时会默认一起完成指令组件的配置对象创建和组件初始化。
获取配置对象
Java
1DirectiveModuleConfig directiveModuleConfig = new DirectiveModuleConfig();
配置组件
Java
1// 私有化部署环境时,可以设置指令上传的服务端中转地址
2directiveModuleConfig.robotRequestHostUrl = "http://172.0.0.1:8080/";
指令回调基础接口
通过实现IDirectiveCallback指令监听回调接口,进行对指令数据回调的监听。
Java
1/**
2 * 指令回调基础接口
3 */
4public interface IDirectiveCallback {
5 /**
6 * 服务端指令下发接口
7 *
8 * @param directiveStatusCode 指令状态码
9 * @param directiveStatusMsg 指令状态信息
10 * @param directiveModel 指令数据对象
11 */
12 void onDirectiveReceived(int directiveStatusCode,
13 String directiveStatusMsg,
14 DirectiveModel directiveModel);
15}
指令数据对象
Java
1public class DirectiveModel {
2 // 请求数据:指令命名空间
3 public String requestNamespace = "";
4 // 请求数据:指令名称
5 public String requestName = "";
6 // 请求数据:指令信息ID
7 public String requestMessageId = "";
8 // 请求数据:指令交互ID用于维护多轮对话
9 public String requestInteractionId = "";
10
11 // 返回数据:指令请求状态码
12 public int responseStatusCode = 0;
13 // 返回数据:指令请求状态信息
14 public String responseStatusMsg = "";
15 // 返回数据:指令返回原数据json格式
16 public String responseDirectives = "";
17 // 返回数据:指令返回图片数据
18 public byte[] responseByteData = null;
19}
指令监听扩展接口
目前SDK内置的IDirectiveListener接口如下图:
如图,目前共有七种IDirectiveListener
的实现。
开发者需要在SDK中注册自己相应的IDirectiveListener
来获取SDK的指令回调,步骤如下:
- 开发者编写
IDirectiveListener
的接口实现类 - 通过
RobotSDKManager
的addDirectiveListeners
方法传入IDirectiveListener
实现类到SDK中 - 当接口对应事件触发时,在
IDirectiveListener
的回调方法中获得对应数据并实现相应业务逻辑
可以通过以下代码片段来理解这段过程:
Java
1// 1. 实现 IDirective 的接口实现类,此处以 IDialogListener 为例
2IDialogListener mDialogListener = new IDialogListener() {
3 @Override
4 public void onVoiceOutput(String content) {
5 // 处理语音回复播放相关逻辑
6 }
7
8 @Override
9 public void onTextOutput(String content) {
10 // 处理文字对话显示相关逻辑
11 }
12
13 @Override
14 public void onHints(List <string> hints) {
15 // 处理提示文案显示相关逻辑
16 }
17
18 @Override
19 public void onEnd() {
20 // 对话结束相关逻辑
21 }
22 };
23......
24
25// 2. 将接口实现类传入 SDK
26RobotSDKManager.getInstance().addDirectiveListener(NamespaceGroup.DIALOG, mDialogListener);
各个Listener说明如下:
IDialogListener
IDialogListener :对话相关指令监听器
Java
1public interface IDialogListener extends IDirectiveListener {
2 /**
3 * 语音播报指令
4 * @param content 需要播报的语音内容
5 */
6 void onVoiceOutput(String content);
7
8 /**
9 * 文字回复展示指令
10 * @param content 回复用户的对话内容
11 */
12 void onTextOutput(String content);
13
14 /**
15 * 提示文本展示指令
16 * @param hints 需要展示的提示文本 List
17 */
18 void onHints(List <string> hints);
19
20 /**
21 * 对话结束指令
22 * 当用户说出对话结束关键字(例如"再见"、"拜拜")时触发回调
23 */
24 void onEnd();
25}
IScreenListener
IScreenListener - 屏幕内容展示相关指令
Java
1public interface IScreenListener extends IDirectiveListener {
2
3 /**
4 * 文本卡片展示指令
5 * @param card 文本类型卡片 model
6 */
7 void onRenderTextCard(TextCard card);
8
9 /**
10 * 图片卡片展示指令
11 * @param card 图片卡片 model
12 */
13 void onRenderImageCard(ImageCard card);
14
15 /**
16 * 普通列表卡片展示指令
17 * @param listCard 普通列表卡片 model
18 */
19 void onRenderNormalList(ListCard <normalcarditem> listCard);
20
21 /**
22 * 图片列表卡片展示指令
23 * @param listCard 图片列表卡片 model
24 */
25 void onRenderSimpleImgList(ListCard<simpleimagecarditem> listCard);
26
27 /**
28 * 视频列表卡片展示指令
29 * @param listCard 视频列表卡片 model
30 */
31 void onRenderVideoList(ListCard<videocarditem> listCard);
32
33 /**
34 * 用户卡片展示指令
35 * @param card 用户卡片 model
36 * @param data 用户照片的二进制数据
37 */
38 void onRenderPerson(UserCard card, byte[] data);
39
40 /**
41 * 天气卡片展示指令
42 * @param card 天气卡片 model
43 */
44 void onRenderWeather(WeatherInfoCard card);
45
46 /**
47 * 表情展示指令
48 * @param expression 表情关键字,例如 Sad、Happy
49 */
50 void onRenderExpression(String expression);
51}
IActionListener
IActionListener - 动作控制指令
Java
1public interface IActionListener extends IDirectiveListener {
2 /**
3 * 行走指令
4 * @param direction 行走方向
5 * @param distance 行走距离
6 * @param distanceUnit 距离单位
7 */
8 void onWalk(String direction, String distance, String distanceUnit);
9
10 /**
11 * 转向指令
12 * @param direction 转向方向
13 * @param angle 转向角度
14 * @param angleUnit 角度单位
15 */
16 void onTurn(String direction, String angle, String angleUnit);
17
18 /**
19 * 举手指令
20 * @param hands hand字段可为以下内容:hand, right_hand, left_hand, double_hand
21 */
22 void onRaiseHands(String hands);
23
24 /**
25 * 巡航指令
26 * @param switchStatus 状态开关
27 */
28 void onCruise(String switchStatus);
29
30 /**
31 * 充电指令
32 * @param switchStatus 状态开关
33 */
34 void onCharge(String switchStatus);
35
36 /**
37 * 握手指令
38 */
39 void onShakeHands();
40
41 /**
42 * 拥抱指令
43 */
44 void onHug();
45
46 /**
47 * 摇头指令
48 */
49 void onTwistHead();
50
51 /**
52 * 向左看指令
53 */
54 void onTurnHeadLeft();
55
56 /**
57 * 向右看指令
58 */
59 void onTurnHeadRight();
60
61 /**
62 * 停止运动指令
63 */
64 void onStop();
65
66 /**
67 * 打招呼指令
68 */
69 void onWave();
70
71 /**
72 * 动一下指令
73 */
74 void onDoAnAction();
75}
ISpeakerControllerListener
ISpeakerControllerListener - 扬声器控制指令
Java
1public interface ISpeakerControllerListener extends IDirectiveListener {
2 /**
3 * 静音指令
4 */
5 void onSetMute();
6
7 /**
8 * 音量调节指令
9 * @param volumeControl
10 * @param volumeValue
11 */
12 void onAdjustVolume(String volumeControl, String volumeValue);
13}
IInstructionListener
IInstructionListener - 特殊指令
Java
1public interface IInstructionListener extends IDirectiveListener {
2
3 /**
4 * 电量查询指令
5 */
6 void onInquireEnergy();
7
8 /**
9 * 展示功能指令
10 */
11 void onShowFeatures();
12}
IErrorListener
IErrorListener - 错误监听
Java
1public interface IErrorListener extends IDirectiveListener {
2 /**
3 * 错误监听
4 * @param code 错误码
5 * @param errorDescription 错误描述
6 */
7 void onError(int code, String errorDescription);
8}
IStateListener
IStateListener - 指令处理状态监听
Java
1public interface IStateListener extends IDirectiveListener {
2 /**
3 * 当NLU能力识别完成本次对话请求中的用户事件,
4 * SDK 开始向开发者回传本次对话请求返回的事件指令(Directives)时回调
5 */
6 void onDirectiveStart();
7
8 /**
9 * 当 SDK 完成了本次对话请求返回的全部事件指令的回传时回调
10 */
11 void onDirectiveStop();
12}