Skip to content

Commit

Permalink
Read connection string from Toolkit, rename 'tempSensor' to 'Simulate… (
Browse files Browse the repository at this point in the history
#477)

* Read connection string from Toolkit, rename 'tempSensor' to 'SimulatedTemperatureSensor'

* Resolve comments
  • Loading branch information
formulahendry authored Jul 11, 2019
1 parent 522c55a commit 3c4a159
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 15 deletions.
12 changes: 9 additions & 3 deletions src/common/configuration.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use strict";
import * as vscode from "vscode";
import { Utility } from "./utility";

export class Configuration {
public static getConfiguration(): vscode.WorkspaceConfiguration {
Expand All @@ -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<string | undefined> {
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;
}
}
}
13 changes: 9 additions & 4 deletions src/common/utility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any> {
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<void> {
if (!(await azureAccount.waitForLogin())) {
await vscode.commands.executeCommand("azure-account.askForLogin");
Expand Down
4 changes: 2 additions & 2 deletions src/edge/edgeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/edge/simulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}"`;
}
Expand Down
6 changes: 3 additions & 3 deletions test/utility.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -38,15 +38,15 @@ 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(
deployment.modulesContent.$edgeAgent["properties.desired"].systemModules.edgeAgent.settings.createOptions,
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);
Expand Down
4 changes: 2 additions & 2 deletions testResources/deployment.template.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
}
},
"modules": {
"tempSensor": {
"SimulatedTemperatureSensor": {
"version": "1.0",
"type": "docker",
"status": "running",
Expand Down Expand Up @@ -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": {
Expand Down

0 comments on commit 3c4a159

Please sign in to comment.