@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)
253 lines • 13.9 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-firm-deploy")
.alias("oefd")
.option("-m, --mode [list|create|update|info|check|accept|template]", "list | create | update | info | check | accept |template", "list")
.option("-i, --id <id>", "the installation task id")
.option("-d, --deviceid <deviceid>", "deviceid to filter")
.option("-r, --realeaseid <realeaseid>", "firmware realease id")
.option("-t, --type <type>", "filter for tasks of a specific type (firmware, app, etc)")
.option("-s, --status <status> [closed|open]", "filter based on task progress, one of “closed” or “open”")
.option("-f, --file <file>", ".mdsp.json file with app data")
.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, update firmware deployment task(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 listInstalls(sdk, options);
break;
case "template":
yield createTemplateInstallationTask(options, sdk);
yield createTemplateTaskStatus(options, sdk);
console.log("Edit the file(s) before submitting it to MindSphere.");
break;
case "create":
yield createInstllationTask(options, sdk);
break;
case "update":
yield updateTaskStatus(options, sdk);
break;
case "check":
yield checkTermAndConditions(options, sdk);
break;
case "accept":
yield acceptTermAndConditions(options, sdk);
break;
case "info":
yield taskInstInfo(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-firm-deploy --mode list --deviceid "7d018c..." \n\tlist all firmware deployment taks on a specified device.`);
(0, console_1.log)(` mdsp oe-firm-deploy --mode template \n\tcreate template files to define an firmware installation/update task.`);
(0, console_1.log)(` mdsp oe-firm-deploy --mode create --file edge.install.firmware.mdsp.json \n\tcreates a new firmware installtion tak.`);
(0, console_1.log)(` mdsp oe-firm-deploy --mode update --id "7d018c..." --file edge.firmware.status.mdsp.json \n\tupdate a firmware installation task from status template file.`);
(0, console_1.log)(` mdsp oe-firm-deploy --mode info --id <id>\n\tget details of a firmware installation task.`);
(0, console_1.log)(` mdsp oe-firm-deploy --mode check --deviceid <deviceid> --realeaseid <realeaseid> \n\tcheck terms and condition of a firmware realease on a a specific device.`);
(0, console_1.log)(` mdsp oe-firm-deploy --mode accept --deviceid <deviceid> --realeaseid <realeaseid> \n\taccept terms and condition of a firmware realease on a a specific device.`);
(0, command_utils_1.serviceCredentialLog)();
});
};
function checkRequiredParameters(options) {
options.mode === "list" &&
!options.deviceid &&
(0, command_utils_1.errorLog)("you have to provide the device id to list all the app installation/removal tasks (see mdsp oe-firm-deploy --help for more details)", true);
options.mode === "create" &&
!options.file &&
(0, command_utils_1.errorLog)("you have to provide a file with the task data to create a new installation task (see mdsp oe-firm-deploy --help for more details)", true);
options.mode === "update" &&
!options.file &&
(0, command_utils_1.errorLog)("you have to provide a file with the update data to update the status of the firmware installation task (see mdsp oe-firm-deploy --help for more details)", true);
options.mode === "update" &&
!options.id &&
(0, command_utils_1.errorLog)("you have to provide the id of the installation/removal task to update it (see mdsp oe-firm-deploy --help for more details)", true);
options.mode === "check" &&
!options.deviceid &&
(0, command_utils_1.errorLog)("you have to provide the deviceid to check for terms and conditions (see mdsp oe-firm-deploy --help for more details)", true);
options.mode === "check" &&
!options.realeaseid &&
(0, command_utils_1.errorLog)("you have to provide the realeaseid to check for terms and conditions (see mdsp oe-firm-deploy --help for more details)", true);
options.mode === "accept" &&
!options.deviceid &&
(0, command_utils_1.errorLog)("you have to provide the deviceid to accept the terms and conditions (see mdsp oe-firm-deploy --help for more details)", true);
options.mode === "accept" &&
!options.realeaseid &&
(0, command_utils_1.errorLog)("you have to provide the realeaseid to accept the terms and conditions (see mdsp oe-firm-deploy --help for more details)", true);
options.mode === "info" &&
!options.id &&
(0, command_utils_1.errorLog)("you have to provide the id of the installation task (see mdsp oe-firm-deploy --help for more details)", true);
}
function listInstalls(sdk, options) {
return __awaiter(this, void 0, void 0, function* () {
const firmDeploymentClient = sdk.GetFirmwareDeploymentClient();
// Parse all options
const deviceid = options.deviceid;
let page = 0;
let iTaskPage;
let rslt_table = [];
do {
iTaskPage = (yield (0, __1.retry)(options.retry, () => firmDeploymentClient.GetInstallationTasks(deviceid, options.type, options.status, 100, page, undefined, options.history)));
iTaskPage.content = iTaskPage.content || [];
iTaskPage.page = iTaskPage.page || { totalPages: 0 };
rslt_table = rslt_table.concat(iTaskPage.content);
} while (page++ < (iTaskPage.page.totalPages || 0));
// Print out the table
console.log("Firmware installation tasks");
console.table(rslt_table, [
"id",
"createdAt",
"softwareType",
"softwareId",
"softwareReleaseId",
"actionType",
"currentState",
]);
console.log(`${color(rslt_table.length)} firmware installation/update task(s) listed.\n`);
});
}
function createTemplateInstallationTask(options, sdk) {
return __awaiter(this, void 0, void 0, function* () {
const template = {
deviceId: "7d018c...",
softwareType: "FIRMWARE",
softwareId: "7d018c...",
softwareReleaseId: "7d018c...",
transitions: [
{
type: "string",
from: "DOWNLOAD",
to: "INSTALL",
details: {},
},
],
customData: {
userDefined: {},
},
};
(0, command_utils_1.verboseLog)(template, options.verbose);
writeInstallTaskTemplateToFile(options, template);
});
}
function writeInstallTaskTemplateToFile(options, templateType) {
const fileName = options.file || `edge.install.firmware.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 firmware installation task template was written into ${color(filePath)} run \n\n\tmdsp oe-firm-deploy --mode create --file ${fileName} \n\nto create a new instalation task.\n`);
}
function createTemplateTaskStatus(options, sdk) {
return __awaiter(this, void 0, void 0, function* () {
const template = {
state: "CREATED",
progress: 0,
message: "string",
details: {},
};
(0, command_utils_1.verboseLog)(template, options.verbose);
writeTaskStatusTemplateToFile(options, template);
});
}
function writeTaskStatusTemplateToFile(options, templateType) {
const fileName = options.file || `edge.firmware.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 firmware installation task status template was written into ${color(filePath)} run \n\n\tmdsp oe-firm-deploy --mode update --id <id> --file ${fileName} \n\nto update the instalation task.\n`);
}
function taskInstInfo(options, sdk) {
return __awaiter(this, void 0, void 0, function* () {
let info = null;
const id = options.id ? options.id : `${options.id}`;
info = (yield (0, __1.retry)(options.retry, () => sdk.GetFirmwareDeploymentClient().GetInstallationTask(id)));
console.log(`Task with id "${id}":`);
console.log(JSON.stringify(info, null, 4));
});
}
function updateTaskStatus(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.GetFirmwareDeploymentClient().PatchInstallationTask(id, data);
console.log(`updated the status of the firmware installation task "${color(id)}" as specified in ${color(filePath)}`);
});
}
function checkTermAndConditions(options, sdk) {
return __awaiter(this, void 0, void 0, function* () {
let info = null;
const deviceid = options.deviceid ? options.deviceid : `${options.deviceid}`;
const realeaseid = options.realeaseid ? options.realeaseid : `${options.realeaseid}`;
info = (yield (0, __1.retry)(options.retry, () => sdk.GetFirmwareDeploymentClient().GetTermsAndConditions(deviceid, realeaseid)));
if (info.firstAccepted) {
console.log(`Terms and conditions accepted on ${color(info.firstAccepted)}.\n`);
}
else {
console.log(`Terms and conditions are not accepted for deviceid: ${deviceid} and realeaseid: ${realeaseid}.\n`);
}
});
}
function acceptTermAndConditions(options, sdk) {
return __awaiter(this, void 0, void 0, function* () {
let info = null;
const deviceid = options.deviceid ? options.deviceid : `${options.deviceid}`;
const realeaseid = options.realeaseid ? options.realeaseid : `${options.realeaseid}`;
info = (yield (0, __1.retry)(options.retry, () => sdk.GetFirmwareDeploymentClient().PostAcceptTermsAndConditions({
deviceId: deviceid,
releaseId: realeaseid,
})));
(0, command_utils_1.verboseLog)(JSON.stringify(info, null, 2), options.verbose);
});
}
function createInstllationTask(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.GetFirmwareDeploymentClient().PostInstallationTask(data);
(0, command_utils_1.verboseLog)(JSON.stringify(inst, null, 2), options.verbose);
console.log(`created a new firmware installation task as specified by the file ${color(filePath)}`);
});
}
//# sourceMappingURL=oe-firmware-deployment.js.map