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)

218 lines 12.5 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 utils_1 = require("../../api/utils"); const command_utils_1 = require("./command-utils"); const path = require("path"); let color = (0, command_utils_1.getColor)("magenta"); exports.default = (program) => { program .command("visual-flow-creator") .alias("vfc") .option("-m, --mode [list|create|update|delete|template|get-nodes|put-nodes|info|trigger]", "list | create | update | delete | template | get-nodes | put-nodes | info | trigger", "list") .option("-u, --userid <userid>", "user email") .option("-f, --file <file>", ".mdsp.json file with vfc project definition", "vfc.project.mdsp.json") .option("-n, --nodes <nodes>", ".mdsp.json file with vfc project nodes", "vfc.nodes.mdsp.json") .option("-p, --project <project>", "the project name", `vfc-project-${new Date().getTime()}`) .option("-i, --id <id>", "the vfc project id") .option("-t, --nodeid <nodeid>", "trigger the inject node") .option("-m, --message <message>", ".mdsp.json file with trigger message", "vfc.message.mdsp.json") .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 vfc projects and nodes *")) .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 listVfcProjects(sdk, options); break; case "template": createTemplate(options, sdk.GetTenant()); console.log("Edit the file before submitting it to MindSphere."); break; case "delete": yield deleteVfcProject(options, sdk); break; case "update": yield updateVfcProject(options, sdk); break; case "create": yield createVfcProject(options, sdk); break; case "info": yield vfcProjectInfo(options, sdk); break; case "get-nodes": yield getNodes(options, sdk); break; case "put-nodes": yield putNodes(options, sdk); break; case "trigger": yield trigger(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 vfc --user <email> --mode list \t\t\t\tlist all vfc projects`); (0, console_1.log)(` mdsp vfc --user <email> --mode template --project <projectname> \tcreate a template file for vfc project <project>`); (0, console_1.log)(` mdsp vfc --user <email> --mode create --file <vfc project> \t\tcreate vfc project `); (0, console_1.log)(` mdsp vfc --user <email> --mode update --file <vfc project> --id <id> \t update project `); (0, console_1.log)(` mdsp vfc --user <email> --mode info --id <id> \tvfc project info for specified id`); (0, console_1.log)(` mdsp vfc --user <email> --mode delete --id <id> \tdelete vfc project with specified id`); (0, console_1.log)(` mdsp vfc --user <email> --mode get-nodes --id <id> \tget vfc nodes for the specified project (using default file)`); (0, console_1.log)(` mdsp vfc --user <email> --mode put-nodes --id <id> \tstore vfc nodes to the specified project (using default file)`); (0, console_1.log)(` mdsp vfc --user <email> --mode trigger --id <id> --nodeid <nodeid> \ttrigger flow by sending the message to specified node (using default file)`); (0, command_utils_1.serviceCredentialLog)(); }); }; function createVfcProject(options, sdk) { return __awaiter(this, void 0, void 0, function* () { const filePath = path.resolve(options.file); const file = fs.readFileSync(filePath); const project = JSON.parse(file.toString()); const result = yield sdk.GetVisualFlowCreatorClient().PostProject(project, { userId: options.userid }); console.log(`creted vfc project with id: ${color(result.id)}`); }); } function updateVfcProject(options, sdk) { return __awaiter(this, void 0, void 0, function* () { const filePath = path.resolve(options.file); const file = fs.readFileSync(filePath); const project = JSON.parse(file.toString()); yield sdk.GetVisualFlowCreatorClient().GetProject(options.id, { userId: options.userid }); const result = yield sdk.GetVisualFlowCreatorClient().PatchProject(options.id, project, { userId: options.userid }); console.log(`updated vfc project with id: ${color(result.id)}`); }); } function createTemplate(options, tenant) { const project = { name: options.project, }; (0, command_utils_1.verboseLog)(project, options.verbose); writeVfcProjectToFile(options, project); } function writeVfcProjectToFile(options, project) { const fileName = options.file || `vfc.project.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(fileName, JSON.stringify(project, null, 2)); console.log(`The data was written into ${color(fileName)} run \n\n\tmdsp vfc --mode create --file ${fileName} --user ${options.userid || "<user email>"} \n\nto create the vfc project`); } function writeNodesToFile(options, nodes) { const fileName = options.nodes || `vfc.nodes.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(fileName, JSON.stringify(nodes)); console.log(`The data was written into ${color(fileName)}`); } function deleteVfcProject(options, sdk) { return __awaiter(this, void 0, void 0, function* () { const id = options.handle; yield (0, utils_1.retry)(options.retry, () => sdk.GetVisualFlowCreatorClient().DeleteProject(id, { userId: options.userid })); console.log(`vfc project with id ${color(id)} deleted.`); }); } function listVfcProjects(sdk, options) { return __awaiter(this, void 0, void 0, function* () { var _a, _b; const projects = (yield (0, utils_1.retry)(options.retry, () => sdk.GetVisualFlowCreatorClient().GetProjects({ userId: options.userid }))); console.log(`${color("id")} name ${color("userId")} tenant `); (_a = projects.projects) === null || _a === void 0 ? void 0 : _a.forEach((project) => { console.log(`${color(project.id)} ${project.name} ${color(project.userId)} ${project.tenant} `); }); console.log(`${color(((_b = projects.projects) === null || _b === void 0 ? void 0 : _b.length) || 0)} vfc projects listed.\n`); }); } function vfcProjectInfo(options, sdk) { return __awaiter(this, void 0, void 0, function* () { var _a; const project = yield (0, utils_1.retry)(options.retry, () => sdk.GetVisualFlowCreatorClient().GetProject(options.id, { userId: options.userid })); (0, command_utils_1.verboseLog)(JSON.stringify(project, null, 2), options.verbose); (0, command_utils_1.printObjectInfo)("Visual Flow Creator Project", project, options, ["id", "userId"], color); const nodes = (yield (0, utils_1.retry)(options.retry, () => sdk.GetVisualFlowCreatorClient().GetProjecNodes(options.id, { userId: options.userid }))); console.log(`The project has ${color(((_a = nodes.nodes) === null || _a === void 0 ? void 0 : _a.length) || 0)} vfc nodes`); }); } function getNodes(options, sdk) { return __awaiter(this, void 0, void 0, function* () { const nodes = (yield (0, utils_1.retry)(options.retry, () => sdk.GetVisualFlowCreatorClient().GetProjecNodes(options.id, { userId: options.userid }))); (0, command_utils_1.verboseLog)(JSON.stringify(nodes, null, 2), options.verbose); writeNodesToFile(options, nodes); }); } function putNodes(options, sdk) { return __awaiter(this, void 0, void 0, function* () { const filePath = path.resolve(options.nodes); const file = fs.readFileSync(filePath); let nodes = JSON.parse(file.toString()); if (nodes.nodes) { nodes = nodes.nodes; } !Array.isArray(nodes) && (0, utils_1.throwError)("the nodes must be an array"); yield sdk.GetVisualFlowCreatorClient().PutProjectNodes(options.id, nodes, { userId: options.userid }); console.log(`updated nodes of the vfc project with id: ${color(options.id)}`); }); } function trigger(options, sdk) { return __awaiter(this, void 0, void 0, function* () { const filePath = path.resolve(options.message); const file = fs.readFileSync(filePath); const message = JSON.parse(file.toString()); const result = yield sdk .GetVisualFlowCreatorClient() .TriggerFlow(options.id, options.nodeid, message, { userId: options.userid }); console.log(`${JSON.stringify(message)} was sent to ${options.nodeid} in ${options.id} flow`); }); } function checkRequiredParamaters(options) { !options.userid && options.mode !== "template" && (0, command_utils_1.errorLog)("You must specify the user id (email) (see mdsp vfc --help for more details)", true); options.mode === "create" && !options.file && (0, command_utils_1.errorLog)("you have to provide a file with vfc project definition to create a vfc project (see mdsp vfc --help for more details)", true); options.mode === "put-nodes" && !options.nodes && (0, command_utils_1.errorLog)("you have to provide a file with vfc nodes (see mdsp vfc --help for more details)", true); ["info", "update", "delete", "get-nodes", "put-nodes", "trigger"].indexOf(options.mode) > 0 && !options.id && (0, command_utils_1.errorLog)("you have to provide the vfc project id (see mdsp vfc --help for more details)", true); options.mode === "trigger" && !options.nodeid && (0, command_utils_1.errorLog)("you have to provide the nodeid to trigger (see mdsp vfc --help for more details)", true); options.mode === "trigger" && !options.message && (0, command_utils_1.errorLog)("you have to provide the file with the message to send (see mdsp vfc --help for more details)", true); } //# sourceMappingURL=vfc.js.map