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)

165 lines 9.48 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 }); exports.listInstances = void 0; const console_1 = require("console"); const fs = require("fs"); const path = require("path"); const utils_1 = require("../../api/utils"); const command_utils_1 = require("./command-utils"); let color = (0, command_utils_1.getColor)("magenta"); exports.default = (program) => { program .command("mobile-app-instances") .alias("mbi") .option("-m, --mode [list|info|create|delete|update|template]", "mode [list | info | create | delete | update | template]", "list") .option("-p, --appid <appid>", "mobile app id ") .option("-i, --instanceid <appid>", "mobile app instance id ") .option("-f, --file <file>", "mobile app file [mobileapp.instance.mdsp.json]") .option("-o, --overwrite", "overwrite template files") .option("-k, --passkey <passkey>", "passkey") .option("-y, --retry <number>", "retry attempts before giving up", "3") .option("-v, --verbose", "verbose output") .description(color(`list, create or delete mobile app instances *`)) .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 "template": yield createAppInstanceTemplate(options); console.log("Edit the files before submitting them to mindsphere."); break; case "update": case "create": yield createOrUpdateAppInstance(options, sdk); break; case "list": yield listInstances(options, sdk); break; case "info": { console.log(`App with ${options.appid}:\n`); yield listInstances(options, sdk); console.log(`Mobile App instances:\n`); } break; case "delete": yield deleteAppInstance(options, sdk); break; default: throw Error(`no such option: ${options.mode}`); } } catch (err) { (0, command_utils_1.errorLog)(err, options.verbose); } }))(); }) .on("--help", () => printHelp()); }; function printHelp() { (0, console_1.log)("\n Examples:\n"); (0, console_1.log)(` mdsp mobile-app-instances --appid <appid> --mode list \tlist mobile apps`); (0, console_1.log)(` mdsp mobile-app-instances --appid <appid> --mode template \tcreate template file for mobile app instance`); (0, console_1.log)(` mdsp mobile-app-instances --appid <appid> --mode create --file mobileapp.instance.mdsp.json \n\t\t\t\t\t\tcreate mobile app instance`); (0, console_1.log)(` mdsp mobile-app-instances --appid <appid> --mode info --instanceid <instanceid>\n\t\t\t\t\t\tmobile app instance info`); (0, console_1.log)(` mdsp mobile-app-instances --appid <appid> --mode delete --instanceid <instanceid>\n\t\t\t\t\t\tdelete mobile app instance`); (0, command_utils_1.serviceCredentialLog)(); } function listInstances(options, sdk) { return __awaiter(this, void 0, void 0, function* () { const mobileAppsClient = sdk.GetNotificationClientV4(); let page = 1; // the mobile apps client is starting paging from 1 for some strange reason let mobileAppsInstances; console.log(`instanceId deviceOS deviceModel language pushNotificationToken userEmailAddress`); let instanceCount = 0; do { const params = { page: page, size: 20 }; mobileAppsInstances = (yield (0, utils_1.retry)(options.retry, () => mobileAppsClient.GetMobileAppsInstances(options.appid, params))); mobileAppsInstances.instances = mobileAppsInstances.instances || []; mobileAppsInstances.page = mobileAppsInstances.page || { totalPages: 0 }; for (const instance of mobileAppsInstances.instances || []) { // there is no info method for GET so we are using this way to do this if (options.instanceid && instance.id !== options.instanceid) continue; instanceCount++; console.log(`${instance.id} ${instance.deviceOS} ${instance.deviceModel} ${instance.language} ${instance.pushNotificationToken} ${color(instance.userEmailAddress)}`); (0, command_utils_1.verboseLog)(JSON.stringify(instance, null, 2), options.verbose); } } while (page++ < (mobileAppsInstances.page.totalPages || 0)); console.log(`${color(instanceCount)} mobile app instances listed.\n`); }); } exports.listInstances = listInstances; function createAppInstanceTemplate(options) { return __awaiter(this, void 0, void 0, function* () { const template = { deviceModel: "iPhone X", deviceOS: "iOS 10", language: "de", pushNotificationToken: "7abcd558f29d0c1f048083e2134ad8ea4b3d87d8ae9c038b43c132706ff445f0", }; const fileName = options.file || `mobileapp.instance.mdsp.json`; const filePath = path.resolve(fileName); fs.existsSync(filePath) && !options.overwrite && (0, utils_1.throwError)(`the file ${filePath} already exists.`); fs.writeFileSync(filePath, JSON.stringify(template, null, 2)); console.log(`The ${color(options.type)} template file was written into ${color(fileName)}`); console.log(`Run:\n\n\ \tmdsp mobile-app-instances --mode create --file ${fileName} --appid ${options.appid} \nto create mobile app instance in mindsphere.`); }); } function createOrUpdateAppInstance(options, sdk) { return __awaiter(this, void 0, void 0, function* () { const mobileAppsClient = sdk.GetNotificationClientV4(); const filePath = path.resolve(options.file); const filecontent = fs.readFileSync(filePath); const filedata = JSON.parse(filecontent.toString()); if (options.mode === "create") { const result = (yield (0, utils_1.retry)(options.retry, () => __awaiter(this, void 0, void 0, function* () { return mobileAppsClient.PostMobileAppInstance(options.appid, filedata); }))); console.log(`Mobile app instance ${color(result.id)} [${result.language} ${result.language} ${result.deviceOS} ${result.pushNotificationToken}] was created.`); } else if (options.mode === "update") { const result = (yield (0, utils_1.retry)(options.retry, () => __awaiter(this, void 0, void 0, function* () { return mobileAppsClient.PatchMobileAppInstance(options.appid, options.instanceid, filedata); }))); console.log(`Mobile app instance ${color(result.id)} [${result.language} ${result.language} ${result.deviceOS} ${result.pushNotificationToken}] was updated.`); } else { throw Error(`Invalid mode in createOrUpdateMobileApp ${options.mode}`); } }); } function deleteAppInstance(options, sdk) { return __awaiter(this, void 0, void 0, function* () { const mobileAppsClient = sdk.GetNotificationClientV4(); yield (0, utils_1.retry)(options.retry, () => mobileAppsClient.DeleteMobileAppsInstance(options.appid, options.instanceid)); console.log(`Mobile app instance with id ${color(options.appid)} was deleted.`); }); } function checkRequiredParameters(options) { !options.appid && (0, command_utils_1.errorLog)("You have to specify the mobile app id (--appid)", true); options.mode === "create" && !options.file && (0, command_utils_1.errorLog)("you have to specify the --file for the --mode create command", true); options.mode === "update" && !options.file && (0, command_utils_1.errorLog)("you have to specify the --file for the --mode update command", true); (options.mode === "delete" || options.mode === "update" || options.mode === "info") && !options.instanceid && (0, command_utils_1.errorLog)("you have to specify the instanceid", true); } //# sourceMappingURL=mobile-app-instances.js.map