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)

209 lines 11.5 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-app-inst") .alias("oeai") .option("-m, --mode [list|create|config|delete|template|info]", "list | create | config | delete | template | info", "list") .option("-i, --id <id>", "the app instance id") .option("-d, --deviceid <id>", "the device id") .option("-f, --file <file>", ".mdsp.json file with app inst data") .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, create, configure or delete app instance (open edge) *")) .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 listApps(sdk, options); break; case "template": yield createTemplateApp(options, sdk); yield createTemplateAppConfig(options, sdk); console.log("Edit the file(s) before submitting it to MindSphere."); break; case "delete": yield deleteAppInst(options, sdk); break; case "create": yield createAppInstance(options, sdk); break; case "config": yield createAppInstanceConfiguration(options, sdk); break; case "info": yield appInstanceInfo(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-app-inst --mode list --deviceid <deviceid> \tlist all app instances of device with deviceId.`); (0, console_1.log)(` mdsp oe-app-inst --mode template \t\t\tcreate a template file for new app instance data.`); (0, console_1.log)(` mdsp oe-app-inst --mode create --file edge.app.mdsp.json \n\tcreates a new app instance from template file.`); (0, console_1.log)(` mdsp oe-app-inst --mode config --id <appinstid> --file edge.appconfig.mdsp.json \n\tconfigure an app instance from template file.`); (0, console_1.log)(` mdsp oe-app-inst --mode info --id <appinstid>\t\tget details of an app instance.`); (0, console_1.log)(` mdsp oe-app-inst --mode delete --id <appinstid>\tdelete app instance configuration.`); (0, command_utils_1.serviceCredentialLog)(); }); }; function createAppInstance(options, sdk) { return __awaiter(this, void 0, void 0, function* () { const filePath = path.resolve(options.file); const file = fs.readFileSync(filePath); const data = JSON.parse(file.toString()); const name = data.deviceId ? data.deviceId : `${sdk.GetTenant()}.${data.appInstanceId}`; yield sdk.GetEdgeAppInstanceManagementClient().PostAppInstance(data); console.log(`created new app instance for deviceid ${color(name)}`); }); } function createAppInstanceConfiguration(options, sdk) { return __awaiter(this, void 0, void 0, function* () { const filePath = path.resolve(options.file); const file = fs.readFileSync(filePath); const config = JSON.parse(file.toString()); const name = config.deviceId ? config.deviceId : `${sdk.GetTenant()}.${config.appInstanceId}`; yield sdk.GetEdgeAppInstanceManagementClient().PostAppInstanceConfigurations(config); console.log(`created new configuration for deviceid ${color(name)}`); }); } function createTemplateApp(options, sdk) { return __awaiter(this, void 0, void 0, function* () { const tenant = sdk.GetTenant(); const template = { name: `${tenant}.myAppName`, appInstanceId: "718ca5ad0...", deviceId: "718ca5ad0...", releaseId: "718ca5ad0...", applicationId: "718ca5ad0...", }; (0, command_utils_1.verboseLog)(template, options.verbose); writeAppTemplateToFile(options, template); }); } function createTemplateAppConfig(options, sdk) { return __awaiter(this, void 0, void 0, function* () { const templateType = { deviceId: "718ca5ad0...", appId: "718ca5ad0...", appReleaseId: "718ca5ad0...", appInstanceId: "718ca5ad0...", configuration: { sampleKey1: "sampleValue1", sampleKey2: "sampleValue2", }, }; (0, command_utils_1.verboseLog)(templateType, options.verbose); writeAppInstConfigToFile(options, templateType); }); } function writeAppTemplateToFile(options, templateType) { const fileName = options.file || `edge.app.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 app data template was written into ${color(filePath)} run \n\n\tmdsp oe-app-inst --mode create --file ${fileName} \n\nto create a new app instance.\n`); } function writeAppInstConfigToFile(options, templateType) { const fileName = options.file || `edge.appconfig.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 app config template was written into ${color(filePath)} run \n\n\tmdsp oe-app-inst --mode config --id <appinstid> --file ${fileName} \n\nto create a new app inst. configuration\n`); } function deleteAppInst(options, sdk) { return __awaiter(this, void 0, void 0, function* () { const id = options.id ? options.id : `${options.id}`; yield sdk.GetEdgeAppInstanceManagementClient().DeleteAppInstance(id); console.log(`Application instance with id ${color(id)} deleted.`); }); } function listApps(sdk, options) { return __awaiter(this, void 0, void 0, function* () { const edgeAppInstanceClient = sdk.GetEdgeAppInstanceManagementClient(); let page = 0; console.log(`name \tStatus \tappInstanceId \tdeviceId \treleaseId \tapplicationId \tconfiguration`); let appCount = 0; let appPage; do { appPage = (yield (0, __1.retry)(options.retry, () => edgeAppInstanceClient.GetAppInstances(options.deviceid, 100, page))); appPage.content = appPage.content || []; appPage.page = appPage.page || { totalPages: 0 }; for (const app of appPage.content || []) { appCount++; // read the configuration of these app try { const config = (yield (0, __1.retry)(options.retry, () => edgeAppInstanceClient.GetAppInstanceConfiguration(app.appInstanceId))); const status = (yield (0, __1.retry)(options.retry, () => edgeAppInstanceClient.GetAppInstanceLifecycle(app.appInstanceId))); console.log(`${color(app.name)} \t${color(status.status)} \t${app.appInstanceId} \t${app.deviceId} \t${app.applicationId} \t${app.releaseId} \t${JSON.stringify(config)}`); (0, command_utils_1.verboseLog)(JSON.stringify(app, null, 2), options.verbose); } catch (e) { } } } while (page++ < (appPage.page.totalPages || 0)); console.log(`${color(appCount)} app instance(s) listed.\n`); }); } function appInstanceInfo(options, sdk) { return __awaiter(this, void 0, void 0, function* () { const id = options.id ? options.id : `${options.id}`; const appInstConfig = (yield (0, __1.retry)(options.retry, () => sdk.GetEdgeAppInstanceManagementClient().GetAppInstanceConfiguration(id))); (0, command_utils_1.printObjectInfo)("App Instance Configuration", appInstConfig, options, ["deviceId", "appId", "appReleaseId", "appInstanceId", "configuration"], color); }); } function checkRequiredParameters(options) { options.mode === "create" && !options.file && (0, command_utils_1.errorLog)("you have to provide a file with the app data to create a new application instance (see mdsp oe-app-inst --help for more details)", true); options.mode === "list" && !options.deviceid && (0, command_utils_1.errorLog)("you have to provide the deviceid of the target device (see mdsp oe-app-inst --help for more details)", true); options.mode === "info" && !options.id && (0, command_utils_1.errorLog)("you have to provide the id app instance (see mdsp oe-app-inst --help for more details)", true); options.mode === "delete" && !options.id && (0, command_utils_1.errorLog)("you have to provide the id of the app instance to delete it (see mdsp oe-app-inst --help for more details)", true); options.mode === "config" && !options.file && (0, command_utils_1.errorLog)("you have to provide a file with the config data to configure the application instance (see mdsp oe-app-inst --help for more details)", true); options.mode === "config" && !options.id && (0, command_utils_1.errorLog)("you have to provide the id of the app instance to configure (see mdsp oe-app-inst --help for more details)", true); } //# sourceMappingURL=oe-app-instance.js.map