@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)
133 lines • 8.1 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 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("message-broker")
.alias("mbk")
.option("-m, --mode [info|modify|delete|template|send]", "mode [info | modify | delete | template | send]", "info")
.option("-s, --subscriptionid <subscriptionid>", "subscription id")
.option("-e, --versionid <versionid>", "version id")
.option("-t, --topicid <topicid>", "topic id (example: mdsp.core.am.v1.postbox.asset.deleted)")
.option("-w, --webhook <webhook>", "webhook url")
.option("-k, --passkey <passkey>", "passkey")
.option("-f, --file <file>", ".mdsp.json file with message definition", "messagebroker.message.mdsp.json")
.option("-o, --overwrite", "overwrite template file if it already exists")
.option("-y, --retry <number>", "retry attempts before giving up", "3")
.option("-v, --verbose", "verbose output")
.description(color(`manage message broker subscriptions and webhooks *`))
.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 "info":
{
const result = yield sdk
.GetMessageBrokerClient()
.GetSubscription(options.subscriptionid, options.versionid, options.topicid);
console.log(`subscription id ${options.subscriptionid} in version ${options.versionid} for topic ${options.topicid}`);
console.log(`configured webhook: ${color(result.uri)}`);
}
break;
case "modify":
{
const result = yield sdk
.GetMessageBrokerClient()
.PutSubscription(options.subscriptionid, options.versionid, options.topicid, {
uri: options.webhook,
});
console.log(`subscription id ${options.subscriptionid} in version ${options.versionid} for topic ${options.topicid} was ${color("modified")}`);
console.log(`configured webhook: ${color(result.uri)}`);
}
break;
case "delete":
{
yield sdk
.GetMessageBrokerClient()
.DeleteSubscription(options.subscriptionid, options.versionid, options.topicid);
console.log(`subscription id ${options.subscriptionid} in version ${options.versionid} for topic ${options.topicid} was ${color("modified")}`);
}
break;
case "template":
{
createTemplate(options);
console.log("Edit the file before submitting it to MindSphere.");
}
break;
case "send":
{
const filePath = path.resolve(options.file);
const file = fs.readFileSync(filePath);
const message = JSON.parse(file.toString());
yield sdk.GetMessageBrokerClient().SendMessage(options.topicid, message);
console.log("Edit the file before submitting it to MindSphere.");
}
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 message-broker --mode info --subscriptionid <subscriptionid> --versionid <versionid> --topicid <topicid> \t get subscription webhook uri`);
(0, console_1.log)(` mdsp message-broker --mode delete --subscriptionid <subscriptionid> --versionid <versionid> --topicid <topicid> \t delete webhook `);
(0, console_1.log)(` mdsp message-broker --mode modify --subscriptionid <subscriptionid> --versionid <versionid> --topicid <topicid> --webhook <uri> \t configure webhook `);
(0, console_1.log)(` mdsp message-broker --mode template --file messagebroker.message.mdsp.json \t\t\t create template message file `);
(0, console_1.log)(` mdsp message-broker --mode send --file messagebroker.message.mdsp.json --topicid <topicid> \t send message to the topic `);
(0, command_utils_1.serviceCredentialLog)();
}
function createTemplate(options) {
const templateType = { content: "You can use any valid JSON as message" };
(0, command_utils_1.verboseLog)(JSON.stringify(templateType, null, 2), options.verbose);
writeTemplateToFile(options, templateType);
}
function writeTemplateToFile(options, job) {
const fileName = options.file || `messagebroker.message.mdsp.json`;
const filePath = path.resolve(fileName);
fs.existsSync(filePath) &&
!options.overwrite &&
(0, utils_1.throwError)(`The file ${filePath} already exists. (use --overwrite to overwrite) `);
fs.writeFileSync(filePath, JSON.stringify(job, null, 2));
console.log(`The data was written into ${color(fileName)} run \n\n\tmdsp message-broker --mode send --file ${fileName} \n\nto send the message`);
}
function checkRequiredParameters(options) {
["info", "modify", "delete"].indexOf(options.mode) > 0 &&
!options.topicid &&
(0, command_utils_1.errorLog)("you have to specify the --topicid option", true);
["info", "modify", "delete"].indexOf(options.mode) > 0 &&
!options.versionid &&
(0, command_utils_1.errorLog)("you have to specify the --versionid option", true);
["info", "modify", "delete"].indexOf(options.mode) > 0 &&
!options.subscriptionid &&
(0, command_utils_1.errorLog)("you have to specify the --subscriptionid option", true);
options.mode === "modify" && !options.webhook && (0, command_utils_1.errorLog)("you have to specify the --webhook option", true);
options.mode === "send" && !options.file && (0, command_utils_1.errorLog)("you have to specify the --file option", true);
options.mode === "send" && !options.topicid && (0, command_utils_1.errorLog)("you have to specify the --topicid option", true);
}
//# sourceMappingURL=messagebroker.js.map