@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)
174 lines • 9.47 kB
JavaScript
;
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-data-registries")
.alias("sdr")
.option("-m, --mode [list|create|update|template|info|delete]", "list | create | update | template | info | delete", "list")
.option("-d, --dataregistry <dataregistry>", "data registry file with definition for --mode create or update command")
.option("-i, --registryid <registryid>", "the dataregistry 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 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 listDataRegistries(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 "update":
yield updateDataRegistry(options, sdk);
break;
case "create":
yield createDataRegistry(options, sdk);
break;
case "delete":
yield deleteDataRegistry(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-data-registries --mode list \t\t list all sdi dataregistries`);
(0, console_1.log)(` mdsp sdi-data-registries --mode template \t create template file`);
(0, console_1.log)(` mdsp sdi-data-registries --mode create --dataregistry <dataregistryfile> \t create sdi data registry`);
(0, console_1.log)(` mdsp sdi-data-registries --mode update --dataregistry <dataregistryfile> --registryid <registryid> \
\t\t update sdi data registry`);
(0, console_1.log)(` mdsp sdi-data-registries --mode info --registryid <registryid> \t\t get sdi data registry info`);
(0, console_1.log)(` mdsp sdi-data-registries --mode delete --registryid <registryid> \t\t delete sdi data registry`);
(0, command_utils_1.serviceCredentialLog)();
});
};
function checkRequiredParamaters(options) {
options.mode === "create" &&
!options.dataregistry &&
(0, command_utils_1.errorLog)("you have to provide a dataregistry template file to create a sdi dataregistry (see mdsp sdi-data-registries --help for more details)", true);
options.mode === "update" &&
!options.registryid &&
(0, command_utils_1.errorLog)("you have to provide the registryid of the dataregistry you want to update (see mdsp sdi-data-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 data registry (see mdsp sdi-data-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 data registry (see mdsp sdi-data-registries --help for more details)", true);
}
function listDataRegistries(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"} ${"filePattern"} ${"fileUploadStrategy"} ${"dataTag"} ${"defaultRootTag"}`);
do {
const dataRegistries = yield sdiClient.GetDataRegistries({
pageToken: pageToken,
});
(_a = dataRegistries.dataRegistries) === null || _a === void 0 ? void 0 : _a.forEach((registry) => {
console.log(`${color(registry.registryId)} ${registry.sourceName} ${registry.filePattern} ${registry.fileUploadStrategy} ${registry.dataTag} ${registry.defaultRootTag || "<none>"}`);
(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.GetDataRegistry(`${options.registryid}`);
printDataRegistryInfos(dataRegistry, options);
});
}
function printDataRegistryInfos(dataRegistry, options) {
(0, command_utils_1.printObjectInfo)("Data Registry:", dataRegistry, options, ["registryId", "category", "filePattern"], color);
}
function createTemplate(options) {
const templateType = {
dataTag: "string",
defaultRootTag: "string",
filePattern: "string",
fileUploadStrategy: "append",
metaDataTags: ["string"],
sourceName: "string",
xmlProcessRules: ["string"],
schemaFrozen: false,
};
(0, command_utils_1.verboseLog)(JSON.stringify(templateType, null, 2), options.verbose);
writeToFile(options, templateType);
}
function writeToFile(options, dataRegistry) {
const fileName = options.file || `sdi.dataregistry.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-data-registries --mode create --dataregistry ${fileName} \n\nto create the sdi data registry`);
}
function createDataRegistry(options, sdk) {
return __awaiter(this, void 0, void 0, function* () {
const filePath = path.resolve(options.dataregistry);
const file = fs.readFileSync(filePath);
const dataRegistry = JSON.parse(file.toString());
const result = yield sdk.GetSemanticDataInterConnectClient().PostDataRegistry(dataRegistry);
printDataRegistryInfos(result, options);
});
}
function updateDataRegistry(options, sdk) {
return __awaiter(this, void 0, void 0, function* () {
const filePath = path.resolve(options.dataregistry);
const file = fs.readFileSync(filePath);
const dataRegistry = JSON.parse(file.toString());
const result = yield sdk
.GetSemanticDataInterConnectClient()
.PatchDataRegistry(`${options.registryid}`, dataRegistry);
printDataRegistryInfos(result, options);
});
}
function deleteDataRegistry(options, sdk) {
return __awaiter(this, void 0, void 0, function* () {
const sdiClient = sdk.GetSemanticDataInterConnectClient();
yield sdiClient.DeleteDataRegistry(`${options.registryid}`);
console.log(`The sdi data registry with id : ${color(options.registryid)} was deleted.`);
});
}
//# sourceMappingURL=sdi-data-registries.js.map