public static async getModuleItemsForEdge()

in src/utility.ts [180:220]


    public static async getModuleItemsForEdge(iotHubConnectionString: string, deviceItem: DeviceItem, context: vscode.ExtensionContext) {
        /**
         * modules: contains connection state of each module
         * edgeAgent.properties.reported: contains runtime status of each module
         */
        const [modules, edgeAgent] = await Promise.all([
            Utility.getModules(iotHubConnectionString, deviceItem.deviceId),
            Utility.getModuleTwin(iotHubConnectionString, deviceItem.deviceId, "$edgeAgent"),
        ]);
        const desiredTwin = (edgeAgent as any).properties.desired;
        const reportedTwin = (edgeAgent as any).properties.reported;

        return modules.map((module) => {
            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 = isConnected || (edgeAgent as any).connectionState === "Connected";
                if (isConnected) {
                    module.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 (desiredTwin.systemModules && desiredTwin.systemModules[moduleId]) {
                    return new ModuleItem(deviceItem, module.moduleId, module.connectionString, module.connectionState,
                        reportedTwin ? this.getModuleRuntimeStatus(moduleId, reportedTwin.systemModules) : undefined, iconPath, "edge-module");
                }
            } else {
                if (desiredTwin.modules && desiredTwin.modules[module.moduleId]) {
                    return new ModuleItem(deviceItem, module.moduleId, module.connectionString, module.connectionState,
                        reportedTwin ? this.getModuleRuntimeStatus(module.moduleId, reportedTwin.modules) : undefined, iconPath, "edge-module");
                }
            }
            const moduleType = module.moduleId.startsWith("$") ? "edge-module" : "module";
            // If Module Id starts with "$", then it is a IoT Edge System Module.
            // Otherwise, if a Module does not exist in desired properties of edgeAgent, then it is a Module Identity.
            return new ModuleItem(deviceItem, module.moduleId, module.connectionString, module.connectionState, null, iconPath, moduleType);
        }).filter((module) => module);
    }