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)

154 lines 8.59 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 utils_1 = require("../../api/utils"); const command_utils_1 = require("./command-utils"); const fs = require("fs"); const path = require("path"); let color = (0, command_utils_1.getColor)("magenta"); exports.default = (program) => { program .command("sdi-iot-registries") .alias("sdt") .option("-m, --mode [list|create|template|info|delete]", "list | create | template | info | delete", "list") .option("-d, --iotregistry <iotregistry>", "data registry file with definition for --mode create or update command") .option("-i, --registryid <registryid>", "the iotregistry id for --mode info, update or delete command") .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("manage iot data registries for SDI *")) .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 listIotRegistries(sdk, options); break; case "info": yield dataRegistryInfo(sdk, options); break; case "template": createTemplate(options); console.log("Edit the file before submitting it to MindSphere."); break; case "create": yield createIotRegistry(options, sdk); break; case "delete": yield deleteIotRegistry(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 sdi-iot-registries --mode list \t\t list all sdi dataregistries`); (0, console_1.log)(` mdsp sdi-iot-registries --mode template \t create template file`); (0, console_1.log)(` mdsp sdi-iot-registries --mode create --iotregistry <iotregistryfile> \t create sdi iot registry`); (0, console_1.log)(` mdsp sdi-iot-registries --mode update --iotregistry <iotregistryfile> --registryid <registryid> \ \t\t update sdi iot registry`); (0, console_1.log)(` mdsp sdi-iot-registries --mode info --registryid <registryid> \t\t get sdi iot registry info`); (0, console_1.log)(` mdsp sdi-iot-registries --mode delete --registryid <registryid> \t\t delete sdi iot registry`); (0, command_utils_1.serviceCredentialLog)(); }); }; function checkRequiredParamaters(options) { options.mode === "create" && !options.iotregistry && (0, command_utils_1.errorLog)("you have to provide a iotregistry template file to create a sdi iotregistry (see mdsp sdi-iot-registries --help for more details)", true); options.mode === "update" && !options.registryid && (0, command_utils_1.errorLog)("you have to provide the registryid of the iotregistry you want to update (see mdsp sdi-iot-registries --help for more details)", true); options.mode === "info" && !options.registryid && (0, command_utils_1.errorLog)("you have to provide the registryid to get infos about the sdi iot registry (see mdsp sdi-iot-registries --help for more details)", true); options.mode === "delete" && !options.registryid && (0, command_utils_1.errorLog)("you have to provide the registryid to delete the sdi iot registry (see mdsp sdi-iot-registries --help for more details)", true); } function listIotRegistries(sdk, options) { return __awaiter(this, void 0, void 0, function* () { var _a, _b; const sdiClient = sdk.GetSemanticDataInterConnectClient(); let pageToken = undefined; let count = 0; console.log(`${color("registryId")} ${"sourceName"} ${color("assetId")} ${"aspectName"} ${"dataTag"} ${"category"}`); do { const dataRegistries = yield sdiClient.GetIotDataRegistries({ pageToken: pageToken, }); (_a = dataRegistries.iotDataRegistries) === null || _a === void 0 ? void 0 : _a.forEach((registry) => { console.log(`${color(registry.registryId)} ${registry.sourceName} ${color(registry.assetId)} ${registry.aspectName} ${registry.dataTag} ${registry.category}`); (0, command_utils_1.verboseLog)(JSON.stringify(registry, null, 2), options.verbose); count++; }); pageToken = (_b = dataRegistries.page) === null || _b === void 0 ? void 0 : _b.nextToken; } while (pageToken); console.log(`${color(count)} sdi data registries listed.`); }); } function dataRegistryInfo(sdk, options) { return __awaiter(this, void 0, void 0, function* () { const sdiClient = sdk.GetSemanticDataInterConnectClient(); const dataRegistry = yield sdiClient.GetIotDataRegistry(`${options.registryid}`); printIotRegistryInfos(dataRegistry, options); }); } function printIotRegistryInfos(dataRegistry, options) { (0, command_utils_1.printObjectInfo)("Data Registry:", dataRegistry, options, ["registryId", "category", "filePattern"], color); } function createTemplate(options) { const templateType = { assetId: "<assetid>", aspectName: "<aspectname>", }; (0, command_utils_1.verboseLog)(JSON.stringify(templateType, null, 2), options.verbose); writeToFile(options, templateType); } function writeToFile(options, dataRegistry) { const fileName = options.file || `sdi.iotregistry.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(dataRegistry, null, 2)); console.log(`The data was written into ${color(fileName)} run \n\n\tmdsp sdi-iot-registries --mode create --iotregistry ${fileName} \n\nto create the sdi iot registry`); } function createIotRegistry(options, sdk) { return __awaiter(this, void 0, void 0, function* () { const filePath = path.resolve(options.iotregistry); const file = fs.readFileSync(filePath); const dataRegistry = JSON.parse(file.toString()); const result = yield sdk.GetSemanticDataInterConnectClient().PostIotDataRegistry(dataRegistry); printIotRegistryInfos(result, options); }); } function deleteIotRegistry(options, sdk) { return __awaiter(this, void 0, void 0, function* () { const sdiClient = sdk.GetSemanticDataInterConnectClient(); yield sdiClient.DeleteIotRegistry(`${options.registryid}`); console.log(`The sdi iot registry with id : ${color(options.registryid)} was deleted.`); }); } //# sourceMappingURL=sdi-iot-registries.js.map