@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)
167 lines • 8.66 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-ontologies")
.alias("sdo")
.option("-m, --mode [list|infer|update|template|info|delete]", "list | infer | update | template | info | delete", "list")
.option("-d, --ontology <ontology>", "ontology file with definition for --mode infer or update command")
.option("-i, --ontologyid <ontologyid>", "the ontology 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 ontologies 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 listDataOntologies(sdk, options);
break;
case "info":
yield dataOntologyInfo(sdk, options);
break;
case "template":
createTemplate(options);
console.log("Edit the file before submitting it to MindSphere.");
break;
case "infer":
yield createDataOntology(options, sdk);
break;
case "delete":
yield deleteDataOntology(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-ontologies --mode list \t\t list all sdi ontologies`);
(0, console_1.log)(` mdsp sdi-ontologies --mode template \t\t create template file`);
(0, console_1.log)(` mdsp sdi-ontologies --mode infer --ontology <ontologyfile> \t\t infer sdi ontology`);
(0, console_1.log)(` mdsp sdi-ontologies --mode info --ontologyid <ontologyid> \t\t get sdi ontology info`);
(0, console_1.log)(` mdsp sdi-ontologies --mode delete --ontologyid <ontologyid> \t\t delete sdi ontology`);
(0, command_utils_1.serviceCredentialLog)();
});
};
function checkRequiredParamaters(options) {
options.mode === "create" &&
!options.ontology &&
(0, command_utils_1.errorLog)("you have to provide a ontology template file to create a sdi ontology (see mdsp sdi-ontologies --help for more details)", true);
options.mode === "update" &&
!options.ontologyid &&
(0, command_utils_1.errorLog)("you have to provide the ontologyid of the ontology you want to update (see mdsp sdi-ontologies --help for more details)", true);
options.mode === "info" &&
!options.ontologyid &&
(0, command_utils_1.errorLog)("you have to provide the ontologyid to get infos about the sdi ontology (see mdsp sdi-ontologies --help for more details)", true);
options.mode === "delete" &&
!options.ontologyid &&
(0, command_utils_1.errorLog)("you have to provide the ontologyid to delete the sdi ontology (see mdsp sdi-ontologies --help for more details)", true);
}
function listDataOntologies(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("ontologyId")} ${"ontologyName"} ${"keyMappingType"} ${"updatedDate"} ${"ontologyDescription"}`);
do {
const ontologies = yield sdiClient.GetOntologies({
pageToken: pageToken,
});
(_a = ontologies.ontologies) === null || _a === void 0 ? void 0 : _a.forEach((ontology) => {
console.log(`${color(ontology.id)} ${ontology.ontologyName} ${ontology.keyMappingType || ""} ${ontology.updatedDate} ${ontology.ontologyDescription || ""}`);
(0, command_utils_1.verboseLog)(JSON.stringify(ontology, null, 2), options.verbose);
count++;
});
pageToken = (_b = ontologies.page) === null || _b === void 0 ? void 0 : _b.nextToken;
} while (pageToken);
console.log(`${color(count)} sdi ontologies listed.`);
});
}
function dataOntologyInfo(sdk, options) {
return __awaiter(this, void 0, void 0, function* () {
const sdiClient = sdk.GetSemanticDataInterConnectClient();
const dataOntology = yield sdiClient.GetOntology(`${options.ontologyid}`);
printDataOntologyInfos(dataOntology, options);
});
}
function printDataOntologyInfos(dataOntology, options) {
(0, command_utils_1.printObjectInfo)("Data Ontology:", dataOntology, options, ["ontologyId", "category", "filePattern"], color);
}
function createTemplate(options) {
const templateType = {
schemas: [
{
dataTag: "string",
schemaName: "Schema1 (you need at least two schemas to infer ontology)",
sourceName: "string",
assetId: "string",
aspectName: "string",
},
{
dataTag: "string",
schemaName: "string",
sourceName: "string",
assetId: "string",
aspectName: "string",
},
],
excludeProperties: ["string"],
};
(0, command_utils_1.verboseLog)(JSON.stringify(templateType, null, 2), options.verbose);
writeToFile(options, templateType);
}
function writeToFile(options, dataOntology) {
const fileName = options.file || `sdi.ontology.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(dataOntology, null, 2));
console.log(`The data was written into ${color(fileName)} run \n\n\tmdsp sdi-ontologies --mode create --ontology ${fileName} \n\nto create the sdi ontology`);
}
function createDataOntology(options, sdk) {
return __awaiter(this, void 0, void 0, function* () {
const filePath = path.resolve(options.ontology);
const file = fs.readFileSync(filePath);
const dataOntology = JSON.parse(file.toString());
const result = yield sdk.GetSemanticDataInterConnectClient().InferOntology(dataOntology);
printDataOntologyInfos(result, options);
});
}
function deleteDataOntology(options, sdk) {
return __awaiter(this, void 0, void 0, function* () {
const sdiClient = sdk.GetSemanticDataInterConnectClient();
yield sdiClient.DeleteOntology(`${options.ontologyid}`);
console.log(`The sdi ontology with id : ${color(options.ontologyid)} was deleted.`);
});
}
//# sourceMappingURL=sdi-ontologies.js.map