@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)
302 lines • 16.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 __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("oe-deploy-workflow")
.alias("oedw")
.option("-m, --mode [list|create|instantiate|update|cancel|delete|template|info]", "list | create | instantiate | update | cancel | delete | template | info", "list")
.option("-k, --key <key>", "the workflow model key")
.option("-i, --id <id>", "the deployment model instance id")
.option("-f, --file <file>", ".mdsp.json file")
.option("-md, --model", "include the model")
.option("-u, --history", "include the transition history")
.option("-c, --currentstate <currentstate>", "current state value to filter")
.option("-g, --group <group>", "group value to filter")
.option("-d, --deviceid <deviceid>", "deviceid to filter")
.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("list, create/instantiate, update or delete/cancel workflow deployment model or instance(s) (open edge) *"))
.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 "list":
yield listInstances(sdk, options);
break;
case "template":
yield createTemplateWorkflowModel(options, sdk);
yield createTemplateWorkflowInstance(options, sdk);
yield createTemplateWorkflowStatus(options, sdk);
console.log("Edit the file(s) before submitting it to MindSphere.");
break;
case "delete":
yield deleteWorflowModel(options, sdk);
break;
case "cancel":
yield cancelWorflowInstance(options, sdk);
break;
case "create":
yield createWorkflowModel(options, sdk);
break;
case "instantiate":
yield createWorkflowInstance(options, sdk);
break;
case "update":
yield updateWorkflowInstanceStatus(options, sdk);
break;
case "info":
yield workflowDeploymentInstInfo(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 oe-deploy-workflow --mode list \t\t\tlist all workflow instances descriptions belonging to the caller's tenant.`);
(0, console_1.log)(` mdsp oe-deploy-workflow --mode template \t\tcreate a template files to define the workflow state machine model.`);
(0, console_1.log)(` mdsp oe-deploy-workflow --mode create --file edge.app.model.mdsp.json \n\tcreates a new workflow model from template file.`);
(0, console_1.log)(` mdsp oe-deploy-workflow --mode instantiate --file edge.app.instance.mdsp.json \n\tcreates a new workflow instance from template file.`);
(0, console_1.log)(` mdsp oe-deploy-workflow --mode update --id "7d018c..." --file edge.app.status.mdsp.json \n\tupdate a workflow instance.`);
(0, console_1.log)(` mdsp oe-deploy-workflow --mode info --id <id>\t\tget details of a deployment workflow instance.`);
(0, console_1.log)(` mdsp oe-deploy-workflow --mode info --key <key>\tget details of a deployment workflow model.`);
(0, console_1.log)(` mdsp oe-deploy-workflow --mode cancel --id <id> \tcancel a deployment workflow.`);
(0, console_1.log)(` mdsp oe-deploy-workflow --mode delete --key <key> \tdelete a deployment workflow model.`);
(0, command_utils_1.serviceCredentialLog)();
});
};
function checkRequiredParameters(options) {
options.mode === "create" &&
!options.file &&
(0, command_utils_1.errorLog)("you have to provide a file with the model data to create a new model (see mdsp oe-deploy-workflow --help for more details)", true);
options.mode === "instantiate" &&
!options.file &&
(0, command_utils_1.errorLog)("you have to provide a file with the instance data to create a new deployment workflow (see mdsp oe-deploy-workflow --help for more details)", true);
options.mode === "info" &&
!options.id &&
!options.key &&
(0, command_utils_1.errorLog)("you have to provide the id of the workflow instance or the key of the worflow moel (see mdsp oe-deploy-workflow --help for more details)", true);
options.mode === "delete" &&
!options.key &&
(0, command_utils_1.errorLog)("you have to provide the key of the workflow model to delete it (see mdsp oe-deploy-workflow --help for more details)", true);
options.mode === "cancel" &&
!options.id &&
(0, command_utils_1.errorLog)("you have to provide the id of the workflow instance to cancel it (see mdsp oe-deploy-workflow --help for more details)", true);
options.mode === "update" &&
!options.id &&
(0, command_utils_1.errorLog)("you have to provide the id of the workflow instance to update it (see mdsp oe-deploy-workflow --help for more details)", true);
}
function listInstances(sdk, options) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b;
const workflowDeploymentClient = sdk.GetDeploymentWorkflowClient();
// Parse all options
const model = options.model ? true : false;
const history = options.history ? true : false;
const currentstate = options.currentstate;
const group = options.group;
const deviceid = options.deviceid;
const modelKey = options.key;
let page = 0;
let instanceCount = 0;
let instancePage;
console.log(`id \tdeviceId \tcreatedAt \tcurrentState \tmodel`);
do {
instancePage = (yield (0, __1.retry)(options.retry, () => workflowDeploymentClient.GetWorkflowInstances(model, history, currentstate, group, deviceid, modelKey)));
instancePage.content = instancePage.content || [];
instancePage.page = instancePage.page || { totalPages: 0 };
for (const inst of instancePage.content || []) {
instanceCount++;
console.log(`${color(inst.id)}\t${inst.deviceId}\t${inst.createdAt} \t${color((_a = inst.currentState) === null || _a === void 0 ? void 0 : _a.state)}\t${color((_b = inst.model) === null || _b === void 0 ? void 0 : _b.key)}`);
(0, command_utils_1.verboseLog)(JSON.stringify(inst, null, 2), options.verbose);
}
console.log(page, instancePage.page.totalPages);
} while (page++ < (instancePage.page.totalPages || 0));
console.log(`${color(instanceCount)} worflow instance(s) listed.\n`);
});
}
function createTemplateWorkflowModel(options, sdk) {
return __awaiter(this, void 0, void 0, function* () {
const template = {
key: "com.siemens.nano.fwupdate",
states: [
{
name: "string",
description: "string",
initial: true,
final: true,
cancel: true,
},
],
transitions: [
{
from: "string",
to: "string",
type: "INSTANTANEOUS",
allowedTypes: ["INSTANTANEOUS"],
},
],
groups: [
{
name: "string",
states: ["string"],
},
],
};
(0, command_utils_1.verboseLog)(template, options.verbose);
writeWFModelTemplateToFile(options, template);
});
}
function writeWFModelTemplateToFile(options, templateType) {
const fileName = options.file || `edge.workflow.model.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(templateType, null, 2));
console.log(`The deployment workflow template was written into ${color(filePath)} run \n\n\tmdsp oe-deploy-workflow --mode create --file ${fileName} \n\nto create a new workflow model.\n`);
}
function createTemplateWorkflowInstance(options, sdk) {
return __awaiter(this, void 0, void 0, function* () {
const templateType = {
deviceId: "string",
model: {
key: "string",
customTransitions: [
{
from: "string",
to: "string",
type: "INSTANTANEOUS",
details: {
userDefined: {},
},
},
],
},
data: {
userDefined: {},
},
};
(0, command_utils_1.verboseLog)(templateType, options.verbose);
writeWFInstTemplateToFile(options, templateType);
});
}
function writeWFInstTemplateToFile(options, templateType) {
const fileName = options.file || `edge.workflow.instance.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(templateType, null, 2));
console.log(`The workflow instance template was written into ${color(filePath)} run \n\n\tmdsp oe-deploy-workflow --mode instantiate --file ${fileName} \n\nto create a workflow instance\n`);
}
function createTemplateWorkflowStatus(options, sdk) {
return __awaiter(this, void 0, void 0, function* () {
const template = {
progress: 0,
message: "string",
details: {
userDefined: {},
},
state: "DOWNLOAD",
};
(0, command_utils_1.verboseLog)(template, options.verbose);
writeWFStatusTemplateToFile(options, template);
});
}
function writeWFStatusTemplateToFile(options, templateType) {
const fileName = options.file || `edge.workflow.status.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(templateType, null, 2));
console.log(`The workflow status template was written into ${color(filePath)} run \n\n\tmdsp oe-deploy-workflow --mode update --id <id> --file ${fileName} \n\nto update a workflow instance.\n`);
}
function workflowDeploymentInstInfo(options, sdk) {
return __awaiter(this, void 0, void 0, function* () {
let info = null;
if (options.id) {
const id = options.id ? options.id : `${options.id}`;
info = (yield (0, __1.retry)(options.retry, () => sdk.GetDeploymentWorkflowClient().GetWorkflowInstance(id, options.model, options.history)));
}
else if (options.key) {
const key = options.key ? options.key : `${options.key}`;
info = (yield (0, __1.retry)(options.retry, () => sdk.GetDeploymentWorkflowClient().GetWorkflowModel(key)));
}
(0, command_utils_1.printObjectInfo)("Workflow Instance/Model Info", info, options, ["deviceId", "appId", "appReleaseId", "appInstanceId", "configuration"], color);
});
}
function deleteWorflowModel(options, sdk) {
return __awaiter(this, void 0, void 0, function* () {
const key = options.key ? options.key : `${options.key}`;
yield sdk.GetDeploymentWorkflowClient().DeleteWorkflowModel(key);
console.log(`Deployment workflow model with the key ${color(key)} deleted.`);
});
}
function cancelWorflowInstance(options, sdk) {
return __awaiter(this, void 0, void 0, function* () {
const id = options.id ? options.id : `${options.id}`;
yield sdk.GetDeploymentWorkflowClient().PostToCancelWorkflowInstance(id);
console.log(`Deployment workflow instance with the id ${color(id)} canceled.`);
});
}
function updateWorkflowInstanceStatus(options, sdk) {
return __awaiter(this, void 0, void 0, function* () {
const filePath = path.resolve(options.file);
const file = fs.readFileSync(filePath);
const data = JSON.parse(file.toString());
const id = options.id ? options.id : `${options.id}`;
yield sdk.GetDeploymentWorkflowClient().PatchWorkflowInstance(id, data, options.model, options.history);
console.log(`updated the status of the deployment model instance ${color(id)} as specified in ${color(filePath)}`);
});
}
function createWorkflowInstance(options, sdk) {
return __awaiter(this, void 0, void 0, function* () {
const filePath = path.resolve(options.file);
const file = fs.readFileSync(filePath);
const data = JSON.parse(file.toString());
const inst = yield sdk.GetDeploymentWorkflowClient().PostNewWorflowInstance(data, options.model, options.history);
(0, command_utils_1.verboseLog)(JSON.stringify(inst, null, 2), options.verbose);
console.log(`created a new deployment model instance on the device ${color(inst.deviceId)} as specified by the file ${color(filePath)}`);
});
}
function createWorkflowModel(options, sdk) {
return __awaiter(this, void 0, void 0, function* () {
const filePath = path.resolve(options.file);
const file = fs.readFileSync(filePath);
const data = JSON.parse(file.toString());
const inst = yield sdk.GetDeploymentWorkflowClient().PostNewWorkflowModel(data);
(0, command_utils_1.verboseLog)(JSON.stringify(inst, null, 2), options.verbose);
console.log(`created a new deployment model as specified by the file ${color(filePath)}`);
});
}
//# sourceMappingURL=oe-deployment-workflow.js.map