我将本地执行用于我的智能家居操作。 我试图弄清楚如何向执行处理程序中的集线器设备发送消息。 我收到一条错误消息:
堆栈:“错误:新Wc上的DEVICE_NOT_FOUND↵ (https://www.gstatic.com/eureka/smarthome/smarthome_sdk.js:88:304)↵
请注意,我在识别处理程序中对集线器设备ID进行了硬编码,并在执行处理程序中使用了相同的名称。
我的本地应用代码为:
import App = smarthome.App;
import DataFlow = smarthome.DataFlow;
import IntentFlow = smarthome.IntentFlow;
import Intents = smarthome.Intents;
import Constants = smarthome.Constants;
import Execute = smarthome.Execute;
const myApp: App = new App("1.0.0");
const HexToA = (hex: string) : string => {
var str = '';
for (var i = 0; i < hex.length; i += 2) str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
return str;
}
console.log("Loaded");
const identifyHandler = (request: IntentFlow.IdentifyRequest): IntentFlow.IdentifyResponse => {
// Obtain scan data from protocol defined in your scan config
const device = request.inputs[0].payload.device;
const scanData = HexToA(device.udpScanData.data);
console.log("identify:" + scanData);
// Decode scan data to obtain metadata about local device
// Return a response
const response: IntentFlow.IdentifyResponse = {
intent: Intents.IDENTIFY,
requestId: request.requestId,
payload: {
device: {
id: "VeraConcierge",
isLocalOnly: true,
isProxy: true
}
}
};
return response;
};
const devicesHandler = (request: IntentFlow.ReachableDevicesRequest): IntentFlow.ReachableDevicesResponse => {
const device = request.inputs[0].payload.device;
console.log("devices:" +device.id);
const reachableDevices = [];
request.devices.forEach((device) => {
if (device.id != "VeraConcierge") reachableDevices.push({verificationId:"L-" + device.id});
});
// Return a response
const response: IntentFlow.ReachableDevicesResponse = {
intent: Intents.REACHABLE_DEVICES,
requestId: request.requestId,
payload: {
devices: reachableDevices,
},
};
return response;
};
const executeHandler = (request: IntentFlow.ExecuteRequest): Promise<IntentFlow.ExecuteResponse> => {
const device = request.inputs[0].payload.commands[0].devices[0];
console.log("execute:" +device.id);
const response = new Execute.Response.Builder()
.setRequestId(request.requestId);
const result = request.inputs.map((data) => {
// Convert execution command into payload for local device
let postData: string = JSON.stringify(data);
// Construct a local device command over TCP
const deviceCommand = new DataFlow.HttpRequestData();
deviceCommand.requestId = request.requestId;
deviceCommand.deviceId = "VeraConcierge";
deviceCommand.port = 8998;
deviceCommand.path = "/SHLocalExecute";
deviceCommand.dataType = "application/json";
deviceCommand.data = postData;
deviceCommand.method = Constants.HttpOperation.POST;
// Send command to the local device
return myApp.getDeviceManager()
.send(deviceCommand)
.then((result) => {
response.setSuccessState(result.deviceId, {});
})
.catch((err: IntentFlow.HandlerError) => {
err.errorCode = err.errorCode || "invalid_request";
response.setErrorState(device.id, err.errorCode);
});
});
// Respond once all commands complete
return Promise.all(result)
.then(() => response.build());
};
myApp
.onExecute(executeHandler)
.onIdentify(identifyHandler)
.onReachableDevices(devicesHandler)
.listen()
.then(() => {
console.log("Ready");
});