UNPKG

@mindconnect/mindconnect-nodejs

Version:

NodeJS Library for Siemens Insights Hub Connectivity - TypeScript SDK for Insights Hub and Industrial IoT - Command Line Interface - Insights Hub Development Proxy (Siemens Insights Hub was formerly known as MindSphere)

259 lines 14 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const console_1 = require("console"); const fs = require("fs"); const __1 = require("../.."); const utils_1 = require("../../api/utils"); const command_utils_1 = require("./command-utils"); const path = require("path"); let color = (0, command_utils_1.getColor)("magenta"); exports.default = (program) => { program .command("oe-device-status") .alias("oeds") .option("-m, --mode [list|info|update|template]", "list | info | update | template", "list") .option("-i, --deviceid <deviceid>", "the device id") .option("-t, --target [health|health-config-data|inventory|connection-status]", "type of status information to retrieve or to update. [ health | health-config-data | inventory | connection-status]", "health") .option("-w, --softwaretype [APP|FIRMWARE]", "software type [ APP | FIRMWARE ]") .option("-s, --softwareid <softwareid>", "software id") .option("-f, --file <file>", "openedge.*.mdsp.json file with update information definition") .option("-o, --overwrite", "overwrite template file if it already exists") .option("-k, --passkey <passkey>", "passkey") .option("-y, --retry <number>", "retry attempts before giving up", "3") .option("-v, --verbose", "verbose output") .description(color("list, get, or update (open edge) device status information *")) .action((options) => { (() => __awaiter(void 0, void 0, void 0, function* () { try { checkRequiredParameters(options); const sdk = (0, command_utils_1.getSdk)(options); color = (0, command_utils_1.adjustColor)(color, options); (0, command_utils_1.homeDirLog)(options.verbose, color); (0, command_utils_1.proxyLog)(options.verbose, color); switch (options.mode) { case "list": yield listSoftware(sdk, options); break; case "template": yield createTemplate(options); console.log("Edit the file before submitting it to MindSphere."); break; case "update": yield createDeviceStatusInfo(options, sdk); break; case "info": yield deviceInfo(options, sdk); break; default: throw Error(`no such option: ${options.mode}`); } } catch (err) { (0, command_utils_1.errorLog)(err, options.verbose); } }))(); }) .on("--help", () => { (0, console_1.log)("\n Examples:\n"); (0, console_1.log)(` mdsp oe-device-status --mode list --deviceid <deviceid>\n\ list all installed software on the device`); (0, console_1.log)(` mdsp oe-device-status --mode list --deviceid 12345... --softwaretype APP\n \ list all apps installed on the device`); (0, console_1.log)(` mdsp oe-device-status --mode info --target health --deviceid <deviceid>\n \ get the device health status`); (0, console_1.log)(` mdsp oe-device-status --mode info --target health-config-data --deviceid <deviceid>\n \ get the device health config data`); (0, console_1.log)(` mdsp oe-device-status --mode info --target inventory --deviceid <deviceid>\n \ get the software inventory of the device`); (0, console_1.log)(` mdsp oe-device-status --mode info --target connection-status --deviceid <deviceid>\n \ get the device connection status`); (0, console_1.log)(` mdsp oe-device-status --mode template --target inventory\n \ create template file for software inventory`); (0, console_1.log)(` mdsp oe-device-status --mode template --target connection-status \n \ create template file for connection status`); (0, console_1.log)(` mdsp oe-device-status --mode update --target inventory --file openedge.inventory.mdsp.json --deviceid <deviceid>\n \ update the software inventory of the device`); (0, console_1.log)(` mdsp oe-device-status --mode update --target connection-status --deviceid <deviceid>\n \ send a heartbeat to the device`); (0, command_utils_1.serviceCredentialLog)(); }); }; function createDeviceStatusInfo(options, sdk) { return __awaiter(this, void 0, void 0, function* () { if (!options.file && options.target === "connection-status") { yield sdk.GetDeviceStatusManagementClient().PostDeviceHeartbeat(options.deviceid); console.log(`connection-status is updated.`); return; } const filePath = path.resolve(options.file); const file = fs.readFileSync(filePath); const statusReport = JSON.parse(file.toString()); switch (options.target) { case "health": yield sdk.GetDeviceStatusManagementClient().PatchDeviceHealth(options.deviceid, statusReport); console.log(`patched device heath status`); break; case "health-config-data": yield sdk.GetDeviceStatusManagementClient().PatchDeviceHealthDataConfig(options.deviceid, statusReport); console.log(`patched device heath config data`); break; case "inventory": if (options.softwaretype && options.softwaretype === "APP") { yield sdk .GetDeviceStatusManagementClient() .PatchDeviceApplicationInventory(options.deviceid, statusReport); console.log(`patched device application inventory`); break; } else if (options.softwaretype && options.softwaretype === "FIRMWARE") { yield sdk .GetDeviceStatusManagementClient() .PatchDeviceFirmwareInventory(options.deviceid, statusReport); console.log(`patched device application inventory`); } else { yield sdk .GetDeviceStatusManagementClient() .PatchDeviceSoftwareInventory(options.deviceid, statusReport); console.log(`patched device application inventory`); } break; default: break; } }); } function createTemplate(options) { return __awaiter(this, void 0, void 0, function* () { const now = new Date(); let template = {}; switch (options.target) { case "health": template = { overall: { lastUpdate: now, health: __1.DeviceStatusModels.HealthStatus.OK, message: "Reporting overall health status.", }, }; break; case "health-config-data": template = { lastUpdate: now, configurationId: "5re520...", dataSources: [], }; break; case "inventory": const _template = [ { softwareId: "a7d6da...", version: "1.3", type: __1.DeviceStatusModels.SoftwareType.FIRMWARE, description: "MyDevice Firmware 1.3 debug build", installedAt: now, }, { softwareId: "a7d6da...", version: "1.5", type: __1.DeviceStatusModels.SoftwareType.APP, description: "MyDevice Edge Application 1.5 debug build", installedAt: now, }, ]; if (options.softwaretype && options.softwaretype === "APP") { template = _template[1]; } else if (options.softwaretype && options.softwaretype === "FIRMWARE") { template = _template[0]; } else { template = _template; } break; case "connection-status": (0, command_utils_1.errorLog)(`option --target ${options.target} is not supported for the mode template (see mdsp oe-device-status --help for more details)`, true); break; default: break; } (0, command_utils_1.verboseLog)(template, options.verbose); writeDeviceTypeToFile(options, template); }); } function writeDeviceTypeToFile(options, templateType) { const fileName = options.file || `openedge.${options.target}.mdsp.json`; const filePath = path.resolve(fileName); fs.existsSync(filePath) && !options.overwrite && (0, utils_1.throwError)(`The ${filePath} already exists. (use --overwrite to overwrite) `); fs.writeFileSync(filePath, JSON.stringify(templateType, null, 2)); console.log(`The data was written into ${color(fileName)} run \n\n\tmdsp oe-device-status --mode update --deviceid ${options.deviceid || "<deviceid>"} --target ${options.target} ${options.softwaretype ? "--softwaretype " + options.softwaretype : ""} --file ${fileName} \n\nto update the device status`); } function listSoftware(sdk, options) { return __awaiter(this, void 0, void 0, function* () { const deviceStatusMgmt = sdk.GetDeviceStatusManagementClient(); let page = 0; console.log(`id \tdeviceId \tsoftwareType \tsoftwareId \tsoftwareReleaseId \tversion \tinstalledAt \tinstalledBy`); let softwareCount = 0; let installedSoftware; do { installedSoftware = (yield (0, __1.retry)(options.retry, () => deviceStatusMgmt.GetDeviceSoftwares(options.deviceid, options.softwaretype, options.softwareid, page, 100))); installedSoftware.content = installedSoftware.content || []; installedSoftware.page = installedSoftware.page || { totalPages: 0 }; for (const _software of installedSoftware.content || []) { const software = _software; softwareCount++; console.log(`${color(software.id)}\t${software.deviceId}\t${software.softwareType}\t${software.softwareId}\t${software.softwareReleaseId}\t${software.version}\t${software.installedAt}\t${software.installedBy}`); (0, command_utils_1.verboseLog)(JSON.stringify(_software, null, 2), options.verbose); } } while (page++ < (installedSoftware.page.totalPages || 0)); console.log(`${color(softwareCount)} software(s) listed.\n`); }); } function deviceInfo(options, sdk) { return __awaiter(this, void 0, void 0, function* () { const deviceid = options.deviceid.includes(".") ? options.deviceid : `${options.deviceid}`; switch (options.target) { case "health": const health = yield sdk.GetDeviceStatusManagementClient().GetDeviceHealth(deviceid); console.log(JSON.stringify(health, null, 2)); break; case "health-config-data": const healthConfigData = yield sdk.GetDeviceStatusManagementClient().GetDeviceHealthDataConfig(deviceid); console.log(JSON.stringify(healthConfigData, null, 2)); break; case "inventory": const deviceInventory = yield sdk.GetDeviceStatusManagementClient().GetDeviceInventory(deviceid); console.log(JSON.stringify(deviceInventory, null, 2)); break; case "connection-status": const connectionStatus = yield sdk.GetDeviceStatusManagementClient().GetDeviceConnectionStatus(deviceid); console.log(JSON.stringify(connectionStatus, null, 2)); break; default: break; } }); } function checkRequiredParameters(options) { options.mode !== "template" && !options.deviceid && (0, command_utils_1.errorLog)("you have to provide the deviceid to get or update the device status information (see mdsp oe-device-status --help for more details)", true); options.mode === "template" && !options.target && (0, command_utils_1.errorLog)("you have to provide the status information type to create the corresponding template (see mdsp oe-device-status --help for more details)", true); options.mode === "info" && !options.target && (0, command_utils_1.errorLog)("you have to provide the type of information you want to retrieve (see mdsp oe-device-status --help for more details)", true); } //# sourceMappingURL=oe-device-status.js.map