Show Module Identity for Edge device (#130)

* all modules

* use $edgeAgent's connectionState for $edgeHub, show modules in desiredTwin

* Allow '*' target condition
This commit is contained in:
Jun Han 2018-08-02 17:02:37 +08:00 коммит произвёл GitHub
Родитель 1f73a40bba
Коммит c58f7d66e9
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
5 изменённых файлов: 33 добавлений и 9 удалений

Просмотреть файл

@ -393,6 +393,10 @@
{
"command": "azure-iot-toolkit.getModuleTwin",
"when": "view == iotHubDevices && viewItem == module"
},
{
"command": "azure-iot-toolkit.getModuleTwin",
"when": "view == iotHubDevices && viewItem == edge-module"
}
],
"editor/context": [

Просмотреть файл

@ -8,8 +8,8 @@ export class ModuleItem extends TreeItem {
public readonly deviceId: string,
public readonly moduleId: string,
public readonly runtimeStatus: string,
public readonly iconPath: string) {
public readonly iconPath: string,
public readonly contextValue: string) {
super(runtimeStatus ? `${moduleId} (${runtimeStatus})` : moduleId);
this.contextValue = "module";
}
}

Просмотреть файл

@ -122,6 +122,7 @@ export class IoTEdgeExplorer extends BaseExplorer {
vscode.window.showTextDocument(document);
TelemetryClient.sendEvent(Constants.IoTHubAIGetModuleTwinDoneEvent, { Result: "Success" });
} catch (error) {
this._outputChannel.show();
this.outputLine(Constants.IoTHubModuleTwinLabel, `Failed to get Module Twin: ${error}`);
TelemetryClient.sendEvent(Constants.IoTHubAIGetModuleTwinDoneEvent, { Result: "Fail", Message: error });
}

Просмотреть файл

@ -171,22 +171,31 @@ export class Utility {
* edgeAgent.properties.reported: contains runtime status of each module
*/
const [modules, edgeAgent] = await Promise.all([Utility.getModules(iotHubConnectionString, deviceId), Utility.getModuleTwin(iotHubConnectionString, deviceId, "$edgeAgent")]);
const desiredTwin = (edgeAgent as any).properties.desired;
const reportedTwin = (edgeAgent as any).properties.reported;
// Only return modules that exist in edgeAgent.properties.reported
return modules.map((module) => {
const isConnected = module.connectionState === "Connected";
let isConnected = module.connectionState === "Connected";
// Due to https://github.com/Azure/iotedge/issues/39, use $edgeAgent's connectionState for $edgeHub as workaround
if (module.moduleId === "$edgeHub") {
isConnected = (edgeAgent as any).connectionState === "Connected";
}
const state = isConnected ? "on" : "off";
const iconPath = context.asAbsolutePath(path.join("resources", `module-${state}.svg`));
if (module.moduleId.startsWith("$")) {
const moduleId = module.moduleId.substring(1);
if (reportedTwin.systemModules && reportedTwin.systemModules[moduleId]) {
return new ModuleItem(deviceId, module.moduleId, isConnected ? reportedTwin.systemModules[moduleId].runtimeStatus : null, iconPath);
if (desiredTwin.systemModules && desiredTwin.systemModules[moduleId]) {
return new ModuleItem(deviceId, module.moduleId,
isConnected && reportedTwin ? this.getModuleRuntimeStatus(moduleId, reportedTwin.systemModules) : undefined, iconPath, "edge-module");
}
} else {
if (reportedTwin.modules && reportedTwin.modules[module.moduleId]) {
return new ModuleItem(deviceId, module.moduleId, isConnected ? reportedTwin.modules[module.moduleId].runtimeStatus : null, iconPath);
if (desiredTwin.modules && desiredTwin.modules[module.moduleId]) {
return new ModuleItem(deviceId, module.moduleId,
isConnected && reportedTwin ? this.getModuleRuntimeStatus(module.moduleId, reportedTwin.modules) : undefined, iconPath, "edge-module");
}
}
// If a Module does not exist in desired properties of edgeAgent, then it is a Module Identity.
return new ModuleItem(deviceId, module.moduleId, null, iconPath, "module");
}).filter((module) => module);
}
@ -272,7 +281,7 @@ export class Utility {
}
public static isValidTargetCondition(value: string): boolean {
return /^(deviceId|tags\..+|properties\.reported\..+).*=.+$/.test(value);
return /^(\*|((deviceId|tags\..+|properties\.reported\..+).*=.+))$/.test(value);
}
private static async getFilteredDeviceList(iotHubConnectionString: string, onlyEdgeDevice: boolean): Promise<DeviceItem[]> {
@ -378,4 +387,12 @@ export class Utility {
}
return Constants.ConnectionStringRegex[id].test(value);
}
private static getModuleRuntimeStatus(moduleId: string, modules): string {
if (modules && modules[moduleId]) {
return modules[moduleId].runtimeStatus;
} else {
return undefined;
}
}
}

Просмотреть файл

@ -53,6 +53,8 @@ suite("Utility Tests ", () => {
});
test("should be able to validate target condition", () => {
assert.equal(Utility.isValidTargetCondition("*"), true);
assert.equal(Utility.isValidTargetCondition("tags.city='Shanghai'"), true);
assert.equal(Utility.isValidTargetCondition("properties.reported.lastStatus='200'"), true);