From 74d42818b0a4a2d4afb104c519ab0ff964bcc423 Mon Sep 17 00:00:00 2001 From: Jun Han Date: Mon, 19 Nov 2018 13:02:55 +0800 Subject: [PATCH] Better hint user when they fail to set IoT Hub connection string (#207) * Basic version * Better hint user when they fail to set IoT Hub connection string * Add dispose * Remove one br tag --- package.json | 2 +- resources/iot-hub-connection-string.md | 15 ++++++++ src/constants.ts | 2 + src/utility.ts | 51 +++++++++++--------------- 4 files changed, 39 insertions(+), 31 deletions(-) create mode 100644 resources/iot-hub-connection-string.md diff --git a/package.json b/package.json index a6670f3..4ea4281 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "aiKey": "0caaff90-cc1c-4def-b64c-3ef33615bc9b", "icon": "logo.png", "engines": { - "vscode": "^1.23.0" + "vscode": "^1.26.0" }, "license": "SEE LICENSE IN LICENSE.txt", "repository": { diff --git a/resources/iot-hub-connection-string.md b/resources/iot-hub-connection-string.md new file mode 100644 index 0000000..9ecd81a --- /dev/null +++ b/resources/iot-hub-connection-string.md @@ -0,0 +1,15 @@ +
+
+ +# How to retrieve connection string for IoT hub + +1. Log in to the [Azure portal](https://portal.azure.com). + +1. Click on your hub to see the IoT Hub pane with Settings, and so on. Click **Shared access policies**. + +1. In **Shared access policies**, select the **iothubowner** policy. + +1. Under **Shared access keys**, copy the **Connection string -- primary key** to be used later. + + ![Show how to retrieve the connection string](https://raw.githubusercontent.com/wiki/Microsoft/vscode-azure-iot-toolkit/images/iot-hub-get-connection-string.png) + diff --git a/src/constants.ts b/src/constants.ts index 6496986..b9d3342 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -26,6 +26,7 @@ enum TemplateType { } export class Constants { + public static ExtensionContext: vscode.ExtensionContext; public static ExtensionId = "vsciot-vscode.azure-iot-toolkit"; public static CampaignID = "vsciottoolkit"; @@ -147,6 +148,7 @@ export class Constants { }; public static initialize(context: vscode.ExtensionContext) { + Constants.ExtensionContext = context; const directory = context.storagePath ? context.storagePath : os.tmpdir(); Constants.ModuleTwinJosnFilePath = path.join(directory, Constants.ModuleTwinJosnFileName); Constants.DeviceTwinJosnFilePath = path.join(directory, Constants.DeviceTwinJosnFileName); diff --git a/src/utility.ts b/src/utility.ts index 1699d2a..3cdace0 100644 --- a/src/utility.ts +++ b/src/utility.ts @@ -31,43 +31,34 @@ export class Utility { public static async setConnectionString(id: string, name: string) { TelemetryClient.sendEvent("General.SetConfig.Popup"); - return vscode.window.showInputBox({ - prompt: `${name}`, - placeHolder: Constants.ConnectionStringFormat[id], - ignoreFocusOut: true, - }).then(async (value: string) => { - if (value !== undefined) { + return new Promise((resolve, reject) => { + let value; + const input = vscode.window.createInputBox(); + input.prompt = name; + input.placeholder = Constants.ConnectionStringFormat[id]; + input.ignoreFocusOut = true; + input.onDidAccept(async () => { + value = input.value; if (this.isValidConnectionString(id, value)) { TelemetryClient.sendEvent("General.SetConfig.Done", { Result: "Success" }); let config = Utility.getConfiguration(); await config.update(id, value, true); + resolve(value); + input.dispose(); } else { TelemetryClient.sendEvent("General.SetConfig.Done", { Result: "Fail" }); - value = null; - const reset = "Reset"; - const GoToConnectionStringPage = "More info"; - await vscode.window.showErrorMessage(`The format should be "${Constants.ConnectionStringFormat[id]}". Please enter a valid ${name}.`, - reset, GoToConnectionStringPage).then(async (selection) => { - switch (selection) { - case reset: - TelemetryClient.sendEvent("General.Reset.ConnectionString"); - value = await this.setConnectionString(id, name); - break; - case GoToConnectionStringPage: - vscode.commands.executeCommand("vscode.open", - vscode.Uri.parse( - `https://blogs.msdn.microsoft.com/iotdev/2017/05/09/understand-different-connection-strings-in-azure-iot-hub/?WT.mc_id=${Constants.CampaignID}`)); - TelemetryClient.sendEvent("General.Open.ConnectionStringPage"); - break; - default: - } - }); + vscode.commands.executeCommand("markdown.showPreview", vscode.Uri.file(Constants.ExtensionContext.asAbsolutePath(path.join("resources", "iot-hub-connection-string.md")))); + input.validationMessage = `The format should be "${Constants.ConnectionStringFormat[id]}"`; } - return value; - } else { - this.showIoTHubInformationMessage(); - } - return null; + }); + input.onDidHide(() => { + resolve(); + input.dispose(); + if (!value) { + this.showIoTHubInformationMessage(); + } + }); + input.show(); }); }