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)

232 lines 12.3 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 __1 = require("../.."); 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("delivery-jobs") .alias("dj") .option("-m, --mode [list|create|delete|template|info|commands|commandinfo]", "list | create | delete | template | info | commands | commandinfo", "list") .option("-f, --file <file>", ".mdsp.json file with job definition", "deliveryjob.mqtt.mdsp.json") .option("-i, --jobid <jobid>", "the job id") .option("-c, --commandid <commandid>", "the command id") .option("-e, --name <name>", "the name filter (contains) for list command") .option("-t, --filter <filter>", "the JSON filter as specified in the API documentation") .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 mqtt delivery jobs to publish MQTT commands to the clients *")) .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, true); (0, command_utils_1.homeDirLog)(options.verbose, color); (0, command_utils_1.proxyLog)(options.verbose, color); switch (options.mode) { case "list": yield listJobs(sdk, options); break; case "template": createTemplate(options); console.log("Edit the file before submitting it to MindSphere."); break; case "delete": yield deleteJob(options, sdk); break; case "create": yield createJob(options, sdk); break; case "info": yield jobInfo(options, sdk); break; case "commandinfo": yield commandInfo(options, sdk); break; case "commands": yield listCommands(sdk, options); 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 delivery-jobs --mode list \t\t\t\t\t list all jobs`); (0, console_1.log)(` mdsp delivery-jobs --mode list --name <name> \t\t\t\t list all mqtt delivery jobs with the given name`); (0, console_1.log)(` mdsp delivery-jobs --mode list --filter '{"createdAt": {"after": "2021-11-06T13:46:00Z"}}' \ list all mqtt delivery jobs created after the specified date`); (0, console_1.log)(` mdsp delivery-jobs --mode template \t\t\t\t\t create template file for job creation`); (0, console_1.log)(` mdsp delivery-jobs --mode create --file <templatefile> \t\t create job`); (0, console_1.log)(` mdsp delivery-jobs --mode info --jobid <jobid> \t\t\t get infos about the job`); (0, console_1.log)(` mdsp delivery-jobs --mode delete --jobid <jobid> \t\t\t delete job with job id`); (0, console_1.log)(` mdsp delivery-jobs --mode commands --jobid <jobid> \t\t\t list all commands for specified job id`); (0, console_1.log)(` mdsp delivery-jobs --mode commandinfo --jobid <jobid> --commandid <commandid> \ get info for selected command`); (0, command_utils_1.serviceCredentialLog)(); }); }; function createJob(options, sdk) { return __awaiter(this, void 0, void 0, function* () { const filePath = path.resolve(options.file); const file = fs.readFileSync(filePath); const job = JSON.parse(file.toString()); const result = yield sdk.GetCommandingClient().PostDeliveryJob(job); console.log(`created job ${color(result.id)} with status ${color(result.status)} \n`); }); } function createTemplate(options) { const templateType = { name: "example delivery job", clientIds: ["tenantId_device8"], data: { comment: "see https://developer.mindsphere.io/howto/howto-nativemqtt-commanding.html", additionalProp1: "string", additionalProp2: "string", additionalProp3: "string", }, }; (0, command_utils_1.verboseLog)(JSON.stringify(templateType, null, 2), options.verbose); writejobToFile(options, templateType); } function writejobToFile(options, job) { const fileName = options.file || `deliveryjob.mqtt.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(job, null, 2)); console.log(`The data was written into ${color(fileName)} run \n\n\tmdsp delivery-jobs --mode create --file ${fileName} \n\nto create the job`); } function deleteJob(options, sdk) { return __awaiter(this, void 0, void 0, function* () { const id = options.jobid; yield sdk.GetCommandingClient().DeleteDeliveryJob(id); console.log(`deleted job with id ${color(id)}`); }); } function listJobs(sdk, options) { return __awaiter(this, void 0, void 0, function* () { const commandingClient = sdk.GetCommandingClient(); let page = 0; let jobs; const filter = buildFilter(options); (0, command_utils_1.verboseLog)(JSON.stringify(filter, null, 2), options.verbose); console.log(`id ${color("status")} name createdAt `); let jobCount = 0; do { jobs = (yield (0, __1.retry)(options.retry, () => commandingClient.GetDeliveryJobs({ page: page, size: 100, filter: Object.keys(filter).length === 0 ? undefined : JSON.stringify(filter), }))); jobs._embedded = jobs._embedded || []; jobs.page = jobs.page || { totalPages: 0 }; for (const job of jobs._embedded.deliveryJobs || []) { jobCount++; console.log(`${job.id} ${color(job.status)} ${job.name || ""} ${job.createdAt}`); (0, command_utils_1.verboseLog)(JSON.stringify(job, null, 2), options.verbose); } } while (page++ < (jobs.page.totalPages || 0)); console.log(`${color(jobCount)} mqtt delivery jobs listed.\n`); }); } function listCommands(sdk, options) { return __awaiter(this, void 0, void 0, function* () { const commandingClient = sdk.GetCommandingClient(); let page = 0; let commands; const filter = buildFilter(options); (0, command_utils_1.verboseLog)(JSON.stringify(filter, null, 2), options.verbose); console.log(`id ${color("status")} clientId updatedAt tenantId `); let jobCount = 0; do { commands = (yield (0, __1.retry)(options.retry, () => commandingClient.GetDeliveryJobCommands(options.jobid, { page: page, size: 100, filter: Object.keys(filter).length === 0 ? undefined : JSON.stringify(filter), }))); commands._embedded = commands._embedded || []; commands.page = commands.page || { totalPages: 0 }; for (const command of commands._embedded.commands || []) { jobCount++; console.log(`${command.id} ${color(command.status)} ${command.clientId || ""} ${command.updatedAt} ${command.tenantId}`); (0, command_utils_1.verboseLog)(JSON.stringify(command, null, 2), options.verbose); } } while (page++ < (commands.page.totalPages || 0)); console.log(`${color(jobCount)} mqtt delivery jobs listed.\n`); }); } function buildFilter(options) { let filter = {}; if (options.name) { filter.name = { contains: `${options.name}` }; } if (options.status) { filter.status = { eq: `${options.status}` }; } if (options.filter) { filter = JSON.parse(options.filter); } (0, command_utils_1.verboseLog)(JSON.stringify(filter, null, 2), options.verbose); return filter; } function jobInfo(options, sdk) { return __awaiter(this, void 0, void 0, function* () { const id = options.jobid; const job = yield sdk.GetCommandingClient().GetDeliveryJob(id); (0, command_utils_1.printObjectInfo)("MQTT Delivery Job", job, options, ["id", "status"], color); (0, command_utils_1.verboseLog)(JSON.stringify(job, null, 2), options.verbose); }); } function commandInfo(options, sdk) { return __awaiter(this, void 0, void 0, function* () { const id = options.jobid; const commandid = options.commandid; const command = yield sdk.GetCommandingClient().GetDeliveryJobCommand(id, commandid); (0, command_utils_1.printObjectInfo)(`MQTT Delivery Job Command [for job ${color(id)}]`, command, options, ["id", "status"], color); (0, command_utils_1.verboseLog)(JSON.stringify(command, null, 2), options.verbose); }); } function checkRequiredParamaters(options) { options.mode === "create" && !options.file && (0, command_utils_1.errorLog)("you have to provide a file with job parameters to create an job (see mdsp delivery-jobs --help for more details)", true); options.mode === "delete" && !options.jobid && (0, command_utils_1.errorLog)("you have to provide the jobid of the job you want to delete (see mdsp delivery-jobs --help for more details)", true); options.mode === "commands" && !options.jobid && (0, command_utils_1.errorLog)("you have to provide the jobid of the job to see the list of commands (see mdsp delivery-jobs --help for more details)", true); options.mode === "commandinfo" && !options.jobid && (0, command_utils_1.errorLog)("you have to provide the jobid of the job for commandifo command (see mdsp delivery-jobs --help for more details)", true); options.mode === "info" && !options.jobid && (0, command_utils_1.errorLog)("you have to provide the jobid to get infos about the job (see mdsp delivery-jobs --help for more details)", true); options.mode === "info" && !options.commandid && (0, command_utils_1.errorLog)("you have to provide the commandid to get infos about the command (see mdsp delivery-jobs --help for more details)", true); } //# sourceMappingURL=delivery-jobs.js.map