From 3c4a159900ea813beed7a65d92da9631e8d6fae4 Mon Sep 17 00:00:00 2001 From: Jun Han Date: Thu, 11 Jul 2019 00:18:38 -0500 Subject: [PATCH] =?UTF-8?q?Read=20connection=20string=20from=20Toolkit,=20?= =?UTF-8?q?rename=20'tempSensor'=20to=20'Simulate=E2=80=A6=20(#477)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Read connection string from Toolkit, rename 'tempSensor' to 'SimulatedTemperatureSensor' * Resolve comments --- src/common/configuration.ts | 12 +++++++++--- src/common/utility.ts | 13 +++++++++---- src/edge/edgeManager.ts | 4 ++-- src/edge/simulator.ts | 2 +- test/utility.test.ts | 6 +++--- testResources/deployment.template.json | 4 ++-- 6 files changed, 26 insertions(+), 15 deletions(-) diff --git a/src/common/configuration.ts b/src/common/configuration.ts index f1c9c736..b9d250a5 100644 --- a/src/common/configuration.ts +++ b/src/common/configuration.ts @@ -1,5 +1,6 @@ "use strict"; import * as vscode from "vscode"; +import { Utility } from "./utility"; export class Configuration { public static getConfiguration(): vscode.WorkspaceConfiguration { @@ -18,8 +19,13 @@ export class Configuration { await Configuration.getConfiguration().update(id, value, false); } - public static getIotHubConnectionString(): string | undefined { - const toolkitConfig: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("azure-iot-toolkit"); - return toolkitConfig ? toolkitConfig.get("iotHubConnectionString") : undefined; + public static async getIotHubConnectionString(): Promise { + const toolkit = Utility.getToolkit(); + try { + return toolkit.exports.azureIoTExplorer.getIotHubConnectionString(); + } catch (error) { + const toolkitConfig: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("azure-iot-toolkit"); + return toolkitConfig ? toolkitConfig.get("iotHubConnectionString") : undefined; + } } } diff --git a/src/common/utility.ts b/src/common/utility.ts index 4870699e..7c75d250 100644 --- a/src/common/utility.ts +++ b/src/common/utility.ts @@ -418,16 +418,21 @@ export class Utility { return deviceItem; } - const toolkit = vscode.extensions.getExtension("vsciot-vscode.azure-iot-toolkit"); - if (toolkit === undefined) { - throw new Error("Error loading Azure IoT Toolkit extension"); - } + const toolkit = Utility.getToolkit(); // TODO: only get Edge devices when Toolkit API supports this parameter deviceItem = await toolkit.exports.azureIoTExplorer.getDevice(undefined, undefined, outputChannel); return deviceItem; } + public static getToolkit(): vscode.Extension { + const toolkit = vscode.extensions.getExtension("vsciot-vscode.azure-iot-toolkit"); + if (toolkit === undefined) { + throw new Error("Error loading Azure IoT Toolkit extension"); + } + return toolkit; + } + public static async waitForAzLogin(azureAccount: AzureAccount): Promise { if (!(await azureAccount.waitForLogin())) { await vscode.commands.executeCommand("azure-account.askForLogin"); diff --git a/src/edge/edgeManager.ts b/src/edge/edgeManager.ts index 74d14932..16ebe354 100644 --- a/src/edge/edgeManager.ts +++ b/src/edge/edgeManager.ts @@ -415,10 +415,10 @@ export class EdgeManager { createOptions: {}, }, }; - modules.tempSensor = tempSensor; + modules.SimulatedTemperatureSensor = tempSensor; const tempSensorToModule = `sensorTo${moduleInfo.moduleName}`; routes[tempSensorToModule] = - `FROM /messages/modules/tempSensor/outputs/temperatureOutput INTO BrokeredEndpoint(\"/modules/${moduleInfo.moduleName}/inputs/input1\")`; + `FROM /messages/modules/SimulatedTemperatureSensor/outputs/temperatureOutput INTO BrokeredEndpoint(\"/modules/${moduleInfo.moduleName}/inputs/input1\")`; } await fse.writeFile(templateFile, JSON.stringify(templateJson, null, 2), { encoding: "utf8" }); return { diff --git a/src/edge/simulator.ts b/src/edge/simulator.ts index 72295f2b..ef129dcd 100644 --- a/src/edge/simulator.ts +++ b/src/edge/simulator.ts @@ -163,7 +163,7 @@ export class Simulator { if (deviceItem) { let commandStr = `iotedgehubdev setup -c "${deviceItem.connectionString}"`; if (await this.isModuleTwinSupported()) { - const iotHubConnectionStr = Configuration.getIotHubConnectionString(); + const iotHubConnectionStr = await Configuration.getIotHubConnectionString(); if (iotHubConnectionStr) { commandStr = `${commandStr} -i "${iotHubConnectionStr}"`; } diff --git a/test/utility.test.ts b/test/utility.test.ts index b74d310a..4f32dc36 100644 --- a/test/utility.test.ts +++ b/test/utility.test.ts @@ -21,7 +21,7 @@ suite("utility tests", () => { const generatedObj = JSON.parse(generated); assert.equal(generatedObj.modulesContent .$edgeAgent["properties.desired"] - .modules.tempSensor.settings.image, imageString); + .modules.SimulatedTemperatureSensor.settings.image, imageString); }).timeout(60 * 1000); test("expandModules", async () => { @@ -38,7 +38,7 @@ suite("utility tests", () => { test("convertCreateOptions", async () => { const input: string = await fse.readFile(path.resolve(__dirname, "../../testResources/deployment.template.json"), "utf8"); let deployment = JSON.parse(input); - const oldOptionObj = deployment.modulesContent.$edgeAgent["properties.desired"].modules.tempSensor.settings.createOptions; + const oldOptionObj = deployment.modulesContent.$edgeAgent["properties.desired"].modules.SimulatedTemperatureSensor.settings.createOptions; deployment = Utility.convertCreateOptions(deployment); const depStr = JSON.stringify(deployment, null, 2); assert.equal( @@ -46,7 +46,7 @@ suite("utility tests", () => { deployment.modulesContent.$edgeAgent["properties.desired"].systemModules.edgeHub.settings.createOptions, ); - const settings = deployment.modulesContent.$edgeAgent["properties.desired"].modules.tempSensor.settings; + const settings = deployment.modulesContent.$edgeAgent["properties.desired"].modules.SimulatedTemperatureSensor.settings; assert.equal(settings.hasOwnProperty("createOptions"), true); assert.equal(settings.hasOwnProperty("createOptions01"), true); assert.equal(settings.hasOwnProperty("createOptions02"), true); diff --git a/testResources/deployment.template.json b/testResources/deployment.template.json index dbf9e9c2..68496755 100644 --- a/testResources/deployment.template.json +++ b/testResources/deployment.template.json @@ -37,7 +37,7 @@ } }, "modules": { - "tempSensor": { + "SimulatedTemperatureSensor": { "version": "1.0", "type": "docker", "status": "running", @@ -137,7 +137,7 @@ "properties.desired": { "schemaVersion": "1.0", "routes": { - "sensorToFilter": "FROM /messages/modules/tempSensor/outputs/temperatureOutput INTO BrokeredEndpoint(\"/modules/%MODULE%/inputs/input1\")", + "sensorToFilter": "FROM /messages/modules/SimulatedTemperatureSensor/outputs/temperatureOutput INTO BrokeredEndpoint(\"/modules/%MODULE%/inputs/input1\")", "filterToIoTHub": "FROM /messages/modules/samplemodule/outputs/output1 INTO $upstream" }, "storeAndForwardConfiguration": {