private invokeDirectMethod()

in src/iotHubDirectMethodExplorer.ts [44:83]


    private invokeDirectMethod(iotHubConnectionString: string, deviceId: string, moduleId?: string) {
        const target = moduleId ? `[${deviceId}/${moduleId}]` : `[${deviceId}]`;

        vscode.window.showInputBox({ prompt: `Enter [Method Name] sent to ${target}`, ignoreFocusOut: true }).then((methodName: string) => {
            if (methodName === undefined) {
                return;
            }
            vscode.window.showInputBox({ prompt: `Enter [Payload] sent to ${target}`, ignoreFocusOut: true }).then((payload: string) => {
                if (payload === undefined) {
                    return;
                }
                try {
                    payload = JSON.parse(payload);
                } catch (e) { }
                const methodParams: DeviceMethodParams = {
                    methodName,
                    payload,
                    responseTimeoutInSeconds: 10,
                    connectTimeoutInSeconds: 10,
                };
                const serviceClient = ServiceClient.fromConnectionString(iotHubConnectionString);
                this._outputChannel.show();
                this.outputLine(Constants.IoTHubDirectMethodLabel, `Invoking Direct Method [${methodName}] to ${target} ...`);
                serviceClient.open((error) => {
                    if (error) {
                        this.outputLine(Constants.IoTHubDirectMethodLabel, error.message);
                    } else {
                        this.invokeDirectMethodWithServiceClient(serviceClient, deviceId, methodParams, (err, result) => {
                            if (err) {
                                this.outputLine(Constants.IoTHubDirectMethodLabel, `Failed to invoke Direct Method: ${err.message}`);
                            } else {
                                this.outputLine(Constants.IoTHubDirectMethodLabel, `Response from ${target}:`);
                                this._outputChannel.appendLine(JSON.stringify(result, null, 2));
                            }
                        }, moduleId);
                    }
                });
            });
        });
    }