我已经在google上建立了一个actions项目,我应该可以在google助手中添加一个恒温器。但是,把它添加到googlehome中是行不通的,你知道为什么吗?
在“开发”选项卡中,我添加了:
实现url
客户id和密码
授权和令牌url
通过授权url,我可以(存根)登录,并返回所需的令牌: {"token_type":"bearer","access_token":"123access","expires_in":86400}
返回令牌后,我会收到一个带有请求id的sync调用,如预期的那样。我返回以下内容。
{"payload":{"agentUserId":"sampleClientId","devices":[{"traits":["action.devices.traits.TemperatureSetting"],"willReportState":true,"attributes":{"availableThermostatModes":["off","heat","cool","heatcool","on"],"thermostatTemperatureRange":{"maxThresholdCelsius":30,"minThresholdCelsius":15},"thermostatTemperatureUnit":"F"},"id":"123","type":"action.devices.types.THERMOSTAT","deviceInfo":{"swVersion":"11.4","model":"hs1234","manufacturer":"smart-home-inc","hwVersion":"3.2"}}]},"requestId":"16241058020468313677"}
这个json结构来自关于恒温器的文档。然而,一旦请求完成,我只会收到3个同步调用与完全相同的结果。我的googlehome和助手应用程序首先告诉我添加设备成功了,3秒钟后通知我出了问题。我没有从谷歌得到错误代码,所以我不确定我做错了什么。“agentuserid”属性是我添加到google控制台上的操作的客户机id,因为我不确定要添加什么。
我将在下面添加最重要的代码以使其完整。
onsync(..)方法:
@NotNull
@Override
public SyncResponse onSync(@NotNull SyncRequest syncRequest, @Nullable Map<?, ?> headers) {
SyncResponse res = new SyncResponse();
res.setRequestId(syncRequest.requestId);
res.setPayload(new SyncResponse.Payload());
String token = (String) headers.get("authorization");
String user = TokenServlet.user.getUserId();
res.payload.agentUserId = user;
List<DevicesItem> devices = TokenServlet.user.getDevices();
int numOfDevices = devices.size();
res.payload.devices = new SyncResponse.Payload.Device[numOfDevices];
for (int i = 0; i < numOfDevices; i++) {
DevicesItem device = devices.get(i);
SyncResponse.Payload.Device.Builder deviceBuilder =
new SyncResponse.Payload.Device.Builder()
.setId(device.getId())
.setType(device.getType())
.setTraits(device.getTraits())
.setWillReportState(device.isWillReportState())
.setDeviceInfo(
DeviceProto.DeviceInfo.newBuilder()
.setManufacturer(device.getDeviceInfo().getManufacturer())
.setModel(device.getDeviceInfo().getModel())
.setHwVersion(device.getDeviceInfo().getHwVersion())
.setSwVersion(device.getDeviceInfo().getSwVersion())
.build())
;
if (!Objects.isNull(device.getAttributes())) {
String attributesJson = new Gson().toJson(device.getAttributes());
Struct.Builder attributeBuilder = Struct.newBuilder();
try {
JsonFormat.parser().ignoringUnknownFields().merge(attributesJson, attributeBuilder);
} catch (Exception e) {
LOGGER.error("FAILED TO BUILD");
}
deviceBuilder.setAttributes(attributeBuilder.build());
}
res.payload.devices[i] = deviceBuilder.build();
}
return res;
}
暂无答案!
目前还没有任何答案,快来回答吧!