谷歌语音助手智能家居
What is GoogleAssistant-smart-home?
you can use google home mini control our smart device - 使用谷歌智能音响控制智能家电设备
官方文档-需要翻墙
https://console.actions.google.com/project/smarthomeproject-for-myself001/overview
配网教程案例-wifi需要翻墙
1 google home mini 上电开机,四个灯在闪烁-进入配网状态
2 在手机上下载 Google Home App,打开app并且登入google账号,app会自动搜索到mini。 (此步骤比较难连接,多试几次)
3 试一试吧!(四个灯变表示) Ok Google !或者 Hey Google !来唤醒她
1 找到set up (+) 按钮,找到对应的skill。
2 进行账户授权登录。
3 给设备分配不同的地点。(厨房,大厅等)
4 开始控制设备 例如: turn on light
1 新建一个smart home project
2 配置需要授权的Auth2.0的登录H5页面、token获取接口、fulfillment接口地址
3 提供服务接口即可。
4 测试完毕即可发布,等待谷歌工作人员审核,审核成功之后,Google Home App即可找到自己创建的技能
开发文档-需要翻墙
https://developers.google.com/assistant/smarthome/concepts
1 Authentication: Links your users’ Google accounts with user accounts in your authentication system. See fulfillment and authentication.
2 Smart home intent: Simple messaging objects that describe how to perform a smart home Action such as turn on a light or cast audio to a speaker. See smart home intents
3 Fulfillment : A service that handles a smart home intent and carries out the corresponding Action. See fulfillment and authentication. (提供API服务)
1 目前已经开发支持的设备
/**
* module 和 type 转化为 permisison
*
* @param value
* @return
*/
public static GoogleDeviceType value(String value) {
switch (value) {
case "洗衣机":
return WASHER;
case "插座":
return OUTLET;
case "灯光":
return LIGHT;
case "咖啡机":
return COFFEE_MAKER;
// 音响
case "空气净化器":
return AIRPURIFIER;
// 安防报警
case "窗帘":
return BLINDS;
case "空调":
return AC_UNIT;
case "冰箱":
return REFRIGERATOR;
// 水质净化器
case "扫地机器人":
return SWEEP_ROBOT;
// 传感器
case "电视":
return TV;
case "洗碗机":
return DISHWASHER;
}
// 未匹配
return INVALIDA;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
2 设备同步、查询、执行动作、设备断开连接分发
/**
* 根据inputs - intent 判断关键字操作
* 文档: https://developers.google.com/actions/smarthome/create#actiondevicessync
* 0 action.devices.SYNC
* reuqest: null
* response: payload - devices [id,type,name,roomhint]
* 1 action.devices.QUERY
* reuqest: inputs - payload - devices [id,customData]
* response: payload - devices { id{on,online,brightness,color} }
* 2 action.devices.EXECUTE
* reuqest: inputs - payload - commands[ { devices[{id}] , execution[{command ,params }] } ]
* response: inputs - payload - commands[ {ids,status,states{on,online}} ]
* 3 action.devices.DISCONNECT
* reuqest: null
* response: null
*/
@Override
public String intent(JSONObject request, String token) throws DubboException, BusinessException {
// 0 校验
if (request == null || token == null || "".equals(token.trim())) {
// 执行参数不符合
log.error("request参数为空");
throwException(ExpCodeConstant.Google.GOOGLE_PARAMETER_INVALIDATE_REQUEST.getCode());
return null;
}
token = token.replace("Bearer ", "");// 获取到真实的token
// 1 解析数据 inputs[intent]
JSONArray inputs = request.getJSONArray("inputs");
if (inputs == null || inputs.size() <= 0) {
// 无inputs参数,或者inputs数组长度为0
log.error("无inputs参数,或者inputs数组长度为0");
throwException(ExpCodeConstant.Google.GOOGLE_PARAMETER_INVALIDATE_INPUTS.getCode());
return null;
}
for (Object inputObj : inputs) {
if (inputObj == null) continue;
// 1.1 解析出intent
JSONObject input = JsonUtils.objToJSONObject(inputObj);
if (input == null) {
log.error("无input参数");
throwException(ExpCodeConstant.Google.GOOGLE_PARAMETER_INVALIDATE_INPUT.getCode());
return null;
}
String intent = input.getString("intent");
log.info("参数intent:{}", intent);
if (intent == null) {
log.error("无input参数");
throwException(ExpCodeConstant.Google.GOOGLE_PARAMETER_INVALIDATE_INPUT.getCode());
return null;
}
Object requestIdIn = request.get("requestId"); // 访问的请求id
String requestId = null;
if (requestIdIn != null) {
requestId = String.valueOf(requestIdIn);
}
// 2 执行动作
switch (intent) {
case "action.devices.SYNC": //同步
return getGoogleSYNC(token, requestId);
case "action.devices.QUERY": //查询
return getGoogleQUERY(input, requestId);
case "action.devices.EXECUTE": // 执行动作
return getGoogleEXECUTE(input, requestId);
case "action.devices.DISCONNECT": // 断开连接
// 无需响应
break;
default:
break;
}
}
return token;
}
代码待增加。。。。尽请期待