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)

172 lines 9.11 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("oe-device-types") .alias("oedt") .option("-m, --mode [list|create|delete|template|info]", "list | create | delete | template | info", "list") .option("-f, --file <file>", ".mdsp.json file with device type definition") .option("-t, --tenant <tenant>", "tenant tenant of the device type definition") .option("-n, --devicetype <devicetype>", "the device type name") .option("-c, --code <code>", "device type code") .option("-a, --assettype <assettype>", "the device type associated asset type id") .option("-i, --id <id>", "the device type id") .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 or delete device types (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 listDeviceTypes(sdk, options); break; case "template": yield createTemplate(options, sdk); console.log("Edit the file before submitting it to MindSphere."); break; case "delete": yield deleteDeviceType(options, sdk); break; case "create": yield createDeviceType(options, sdk); break; case "info": yield deviceTypeInfo(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-device-types --mode list \t\t\t list all device types`); (0, console_1.log)(` mdsp oe-device-types --mode list --tenant siemens\t list all device types which belongs to the tenant \"siemens\"`); (0, console_1.log)(` mdsp oe-device-types --mode info --id <deviceid>\t get details of device type with the specified device id`); (0, console_1.log)(` mdsp oe-device-types --mode template --devicetype board \n\tcreate a template file for specified device type`); (0, console_1.log)(` mdsp oe-device-types --mode create --file board.devicetype.mdsp.json \n\tcreate device type board in MindSphere`); (0, console_1.log)(` mdsp oe-device-types --mode delete --id <devicetype>\t delete the device type with the device id`); (0, command_utils_1.serviceCredentialLog)(); }); }; function createDeviceType(options, sdk) { return __awaiter(this, void 0, void 0, function* () { const filePath = path.resolve(options.file); const file = fs.readFileSync(filePath); const deviceType = JSON.parse(file.toString()); const name = deviceType.name.includes(".") ? deviceType.name : `${sdk.GetTenant()}.${deviceType.name}`; yield sdk.GetDeviceManagementClient().PostDeviceType(deviceType); console.log(`created device type ${color(name)}`); }); } function createTemplate(options, sdk) { return __awaiter(this, void 0, void 0, function* () { const tenant = sdk.GetTenant(); const templateType = { name: `${tenant}.${options.devicetype || "<devicetype>"}`, description: "created by mindsphere CLI", owner: `${sdk.GetTenant()}`, code: options.code || `${tenant}.${options.devicetype || "<devicetype>"}`, assetTypeId: options.assettype || "<your assetid>", properties: { key1: "value1", key2: "value2", }, }; (0, command_utils_1.verboseLog)(templateType, options.verbose); writeDeviceTypeToFile(options, templateType); }); } function writeDeviceTypeToFile(options, templateType) { const fileName = options.file || `openedge.devicetype.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 data was written into ${color(filePath)} run \n\n\tmdsp oe-device-types --mode create --file ${fileName} \n\nto create the device type`); } function deleteDeviceType(options, sdk) { return __awaiter(this, void 0, void 0, function* () { const id = options.id ? options.id : `${options.id}`; // !important! this will not work (no support in mindsphere) // !but we are leaving it here for future yield sdk.GetDeviceManagementClient().DeleteDeviceType(id); console.log(`Device type with id ${color(id)} deleted.`); }); } function listDeviceTypes(sdk, options) { return __awaiter(this, void 0, void 0, function* () { const deviceMgmt = sdk.GetDeviceManagementClient(); let page = 0; console.log(`id \towner \tcode \tassetTypeId \tname \tcreatedAt`); let deviceCount = 0; let deviceTypes; do { const filter = { page: page, size: 100, sort: "id,asc", owner: options.tenant, code: options.code, assetTypeId: options.assettype, }; deviceTypes = (yield (0, __1.retry)(options.retry, () => deviceMgmt.GetDeviceTypes(filter))); deviceTypes.content = deviceTypes.content || []; deviceTypes.page = deviceTypes.page || { totalPages: 0 }; for (const id of deviceTypes.content || []) { deviceCount++; console.log(`${color(id.id)}\t${id.owner}\t${id.code}\t${id.assetTypeId}\t${color(id.name)}\t${id.createdAt}`); (0, command_utils_1.verboseLog)(JSON.stringify(id, null, 2), options.verbose); } } while (page++ < (deviceTypes.page.totalPages || 0)); console.log(`${color(deviceCount)} device type(s) listed.\n`); }); } function deviceTypeInfo(options, sdk) { return __awaiter(this, void 0, void 0, function* () { const id = options.id.includes(".") ? options.id : `${options.id}`; const deviceType = yield sdk.GetDeviceManagementClient().GetDeviceType(id); (0, command_utils_1.printObjectInfo)("Device Type", deviceType, options, ["id", "code", "name", "status"], color); }); } function checkRequiredParameters(options) { options.mode === "create" && !options.file && (0, command_utils_1.errorLog)("you have to provide a file with device type to create device type (see mdsp oe-device-types --help for more details)", true); options.mode === "delete" && !options.id && (0, command_utils_1.errorLog)("you have to provide the id of the device type to delete (see mdsp oe-device-types --help for more details)", true); options.mode === "info" && !options.id && (0, command_utils_1.errorLog)("you have to provide the id of the device type (see mdsp oe-device-types --help for more details)", true); } //# sourceMappingURL=oe-device-types.js.map