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)

206 lines 9.95 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 command_utils_1 = require("./command-utils"); const path = require("path"); let color = (0, command_utils_1.getColor)("magenta"); exports.default = (program) => { program .command("asset-types") .alias("at") .option("-m, --mode [list|create|delete|template | info]", "list | create | delete | template | info", "list") .option("-f, --file <file>", ".mdsp.json file with asset type definition") .option("-i, --idonly", "list only ids") .option("-s, --schema <schema>", "JSON Schema") .option("-n, --assettype <assettype>", "the asset type name") .option("-c, --includeshared", "include shared asset types") .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 asset types *")) .action((options) => { (() => __awaiter(void 0, void 0, void 0, function* () { try { checkRequiredParamaters(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 listAssetTypes(sdk, options); break; case "template": createTemplate(options); console.log("Edit the file before submitting it to MindSphere."); break; case "delete": yield deleteAssetType(options, sdk); break; case "create": yield createAssetType(options, sdk); break; case "info": yield assetTypeInfo(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 asset-types --mode list \t\t\t\t\t list all asset types`); (0, console_1.log)(` mdsp asset-types --mode list --assettype Pump\t\t list all asset types which are named Pump`); (0, console_1.log)(` mdsp asset-types --mode template --assettype Pump \n\tcreate a template file (Enironment.assettype.mdsp.json) for assettype Pump`); (0, console_1.log)(` mdsp asset-types --mode create --file Pump.assettype.mdsp.json \n\tcreate asset type Pump in MindSphere`); (0, command_utils_1.serviceCredentialLog)(); }); }; function createAssetType(options, sdk) { return __awaiter(this, void 0, void 0, function* () { const includeShared = options.includeshared; const filePath = path.resolve(options.file); const file = fs.readFileSync(filePath); const assettype = JSON.parse(file.toString()); const name = assettype.name.includes(".") ? assettype.name : `${sdk.GetTenant()}.${assettype.name}`; yield sdk.GetAssetManagementClient().PutAssetType(name, assettype, { includeShared: includeShared }); console.log(`creted asset type ${color(name)}`); }); } function createTemplate(options) { const templateType = { name: options.assettype, description: "", parentTypeId: "core.basicasset", instantiable: true, scope: "private", aspects: [ { name: "leftWing", aspectTypeId: "mdsp.wing", }, ], variables: [ { name: "temperature", dataType: "STRING", unit: "C/F", searchable: true, length: 5, defaultValue: "25/77", }, ], fileAssignments: [ { key: "logo_small", fileId: "c27a28b6eb16b507fabc11e75da8b4ce", }, ], }; (0, command_utils_1.verboseLog)(JSON.stringify(templateType, null, 2), options.verbose); writeAssetTypeToFile(options, templateType); } function writeAssetTypeToFile(options, AssetType) { const fileName = options.file || `${options.assettype}.assettype.mdsp.json`; fs.writeFileSync(fileName, JSON.stringify(AssetType, null, 2)); console.log(`The data was written into ${color(fileName)} run \n\n\t mdsp asset-types --mode create --file ${fileName} \n\nto create the asset type`); } function deleteAssetType(options, sdk) { return __awaiter(this, void 0, void 0, function* () { const includeShared = options.includeshared; const id = options.assettype.includes(".") ? options.assettype : `${sdk.GetTenant()}.${options.assettype}`; const aspType = yield sdk.GetAssetManagementClient().GetAssetType(id, { includeShared: includeShared }); yield sdk.GetAssetManagementClient().DeleteAssetType(id, { ifMatch: aspType.etag, includeShared: includeShared }); console.log(`assettype with id ${color(id)} deleted.`); }); } function listAssetTypes(sdk, options) { return __awaiter(this, void 0, void 0, function* () { var _a, _b; const includeShared = options.includeshared; const assetMgmt = sdk.GetAssetManagementClient(); let page = 0; let assettypes; const filter = buildFilter(options); (0, command_utils_1.verboseLog)(JSON.stringify(filter, null, 2), options.verbose); !options.idonly && console.log(`id etag aspects name\t sharing`); let assetCount = 0; do { assettypes = (yield (0, __1.retry)(options.retry, () => assetMgmt.GetAssetTypes({ page: page, size: 100, filter: Object.keys(filter).length === 0 ? undefined : JSON.stringify(filter), sort: "id,asc", includeShared: includeShared, }))); assettypes._embedded = assettypes._embedded || { assetTypes: [] }; assettypes.page = assettypes.page || { totalPages: 0 }; for (const assettype of assettypes._embedded.assetTypes || []) { assetCount++; !options.idonly && console.log(`${assettype.id}\t${assettype.etag} aspects[${(_a = assettype.aspects) === null || _a === void 0 ? void 0 : _a.length}]\t${color(assettype.name)}\t${(_b = assettype.sharing) === null || _b === void 0 ? void 0 : _b.modes}`); options.idonly && console.log(`${assettype.id}`); (0, command_utils_1.verboseLog)(JSON.stringify(assettype, null, 2), options.verbose); } } while (page++ < (assettypes.page.totalPages || 0)); console.log(`${color(assetCount)} asset types listed.\n`); }); } function buildFilter(options) { const filter = (options.filter && JSON.parse(options.filter)) || {}; let pointer = filter; if (options.assettype !== undefined) { filter.and = {}; pointer = filter.and; } if (options.assettype) { pointer.id = { contains: `${options.assettype}` }; } if (options.typeid) { pointer.id = { contains: `${options.typeid}` }; } return filter; } function assetTypeInfo(options, sdk) { return __awaiter(this, void 0, void 0, function* () { const includeShared = options.includeshared; const id = options.assettype.includes(".") ? options.assettype : `${sdk.GetTenant()}.${options.assettype}`; const assetType = yield sdk.GetAssetManagementClient().GetAssetType(id, { includeShared: includeShared }); console.log(JSON.stringify(assetType, null, 2)); }); } function checkRequiredParamaters(options) { options.mode === "template" && !options.assettype && (0, command_utils_1.errorLog)("you have to provide asset type to create a template (see mdsp asset-types --help for more details)", true); options.mode === "create" && !options.file && (0, command_utils_1.errorLog)("you have to provide a file with asset type to create an asset type (see mdsp asset-types --help for more details)", true); options.mode === "delete" && !options.assettype && (0, command_utils_1.errorLog)("you have to provide the asset type to delete (see mdsp asset-types --help for more details)", true); options.mode === "info" && !options.assettype && (0, command_utils_1.errorLog)("you have to provide the asset type (see mdsp asset-types --help for more details)", true); } //# sourceMappingURL=asset-types.js.map