Skip to content

Commit

Permalink
Guide user setup connection string from UI (#476)
Browse files Browse the repository at this point in the history
* Guide user setup connection string from UI

* Update setup connections string tips, change code flow

* Add fromCommandPalette property of telemetry
  • Loading branch information
SLdragon authored Aug 6, 2019
1 parent 7c14136 commit 8132d51
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/common/ConfigNotSetError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

"use strict";

export class ConfigNotSetError extends Error {
constructor(msg: string) {
super(msg);
}
}
3 changes: 3 additions & 0 deletions src/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,12 @@ export class Constants {
public static installFailedMsg = "'iotedgehubdev' tool installation has failed. Please install it manually for IoT Edge Simulator.";
public static installManuallyMsg = "Please install 'iotedgehubdev' tool first for IoT Edge Simulator.";
public static queryASAJobInfoFailedMsg = "The maximum retry count has been exceeded with empty response from the Stream Analytics.";
public static needSetupSimulatorMsg = "Please setup iotedgehubdev first before starting simulator.";
public static skipForNow = "Skip for Now";
public static learnMore = "Learn More";
public static install = "Install";
public static Setup = "Setup";
public static Cancel = "Cancel";

public static noWorkspaceSetDefaultPlatformMsg = "No workspace is opened for setting default platform. Please open a workspace and try again.";
public static noWorkspaceMsg = "This extension only works when folders are opened.";
Expand Down
23 changes: 22 additions & 1 deletion src/edge/simulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import * as path from "path";
import * as request from "request-promise";
import * as semver from "semver";
import * as vscode from "vscode";
import { ConfigNotSetError } from "../common/ConfigNotSetError";
import { Configuration } from "../common/configuration";
import { Constants } from "../common/constants";
import { Executor } from "../common/executor";
Expand Down Expand Up @@ -175,6 +176,7 @@ export class Simulator {

public async startEdgeHubSingleModule(outputChannel: vscode.OutputChannel): Promise<void> {
return await this.callWithInstallationCheck(outputChannel, async () => {
await this.checkIoTedgehubdevConnectionString(outputChannel);
const inputs = await this.inputInputNames();
await this.setModuleCred(outputChannel);
await Executor.runInTerminal(Utility.adjustTerminalCommand(`iotedgehubdev start -i "${inputs}"`));
Expand Down Expand Up @@ -206,6 +208,7 @@ export class Simulator {

public async runSolution(outputChannel: vscode.OutputChannel, deployFileUri?: vscode.Uri, commands: string[] = []): Promise<void> {
return await this.callWithInstallationCheck(outputChannel, async () => {
await this.checkIoTedgehubdevConnectionString(outputChannel);
const pattern = "{**/deployment.*.json,**/deployment.json,**/deployment.*.debug.json,**/config/*.json}";
const excludePattern = `{${Constants.tsonPattern}}`;
const deployFile: string = await Utility.getInputFilePath(deployFileUri,
Expand All @@ -223,13 +226,31 @@ export class Simulator {
});
}

private async checkIoTedgehubdevConnectionString(outputChannel: vscode.OutputChannel) {
if (await this.isValidateConfigSupported()) {
try {
await Executor.executeCMD(null, "iotedgehubdev", { shell: true }, "validateconfig");
} catch (error) {
throw new ConfigNotSetError(error.message);
}
}
}

private async isModuleTwinSupported(): Promise<boolean> {
return this.isSupported("0.8.0");
}

private async isValidateConfigSupported(): Promise<boolean> {
return this.isSupported("0.10.0");
}

private async isSupported(supportedVersion: string): Promise<boolean> {
let isSupported = false;
try {
const output = await Executor.executeCMD(undefined, "iotedgehubdev", { shell: true }, "--version");
const version: string | null = Simulator.extractVersion(output);
if (version && semver.valid(version)) {
isSupported = semver.gte(version, "0.8.0");
isSupported = semver.gte(version, supportedVersion);
}
} catch (err) {}
return isSupported;
Expand Down
20 changes: 20 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"use strict";
import * as tls from "tls";
import * as vscode from "vscode";
import { ConfigNotSetError } from "./common/ConfigNotSetError";
import { Constants } from "./common/constants";
import { ErrorData } from "./common/ErrorData";
import { Executor } from "./common/executor";
Expand Down Expand Up @@ -222,6 +223,21 @@ async function showLearnMoreError(error: LearnMoreError): Promise<void> {
}
}

async function guideUserToSetupIotedgehubdev(outputChannel: vscode.OutputChannel) {
const setup: vscode.MessageItem = { title: Constants.Setup };
const cancel: vscode.MessageItem = { title: Constants.Cancel };
const items: vscode.MessageItem[] = [setup, cancel];
const input = await vscode.window.showWarningMessage(Constants.needSetupSimulatorMsg, ...items);
const telemetryName = "guideUserSetupConnectionString";

if (input === setup) {
TelemetryClient.sendEvent(`${telemetryName}.${Constants.Setup.toLocaleLowerCase()}`);
await vscode.commands.executeCommand("azure-iot-edge.setupIotedgehubdev", undefined);
} else {
TelemetryClient.sendEvent(`${telemetryName}.${Constants.Cancel.toLocaleLowerCase()}`);
}
}

function initCommandAsync(context: vscode.ExtensionContext,
outputChannel: vscode.OutputChannel,
commandId: string, callback: (...args: any[]) => Promise<any>): void {
Expand All @@ -230,6 +246,8 @@ function initCommandAsync(context: vscode.ExtensionContext,
let errorData: ErrorData | undefined;
const properties: { [key: string]: string; } = {};
properties.result = "Succeeded";
properties.fromCommandPalette = (!args || !args[0]).toString();

TelemetryClient.sendEvent(`${commandId}.start`);
outputChannel.appendLine(`${commandId}: `);
try {
Expand All @@ -244,6 +262,8 @@ function initCommandAsync(context: vscode.ExtensionContext,
outputChannel.appendLine(`Error: ${errorData.message}`);
if (error instanceof LearnMoreError) {
showLearnMoreError(error);
} else if (error instanceof ConfigNotSetError) {
guideUserToSetupIotedgehubdev(outputChannel);
} else {
vscode.window.showErrorMessage(errorData.message);
}
Expand Down

0 comments on commit 8132d51

Please sign in to comment.