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)

120 lines 7.76 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 utils_1 = require("../../api/utils"); const command_utils_1 = require("./command-utils"); const path = require("path"); const fs = require("fs"); let color = (0, command_utils_1.getColor)("magenta"); const magenta = (0, command_utils_1.getColor)("magenta"); const blue = (0, command_utils_1.getColor)("blue"); const yellow = (0, command_utils_1.getColor)("yellow"); const red = (0, command_utils_1.getColor)("red"); const green = (0, command_utils_1.getColor)("green"); const cyan = (0, command_utils_1.getColor)("cyan"); const yesterday = new Date(); yesterday.setDate(yesterday.getDate() - 1); exports.default = (program) => { program .command("aggregates") .alias("ag") .option("-i, --assetid <assetid>", "mindsphere asset id ") .option("-n, --aspectname <aspectname>", "mindsphere aspect name") .option("-f, --from <from>", "begining of the time range to read", yesterday.toISOString()) .option("-t, --to <to>", "end of the time range to read") .option("-r, --intervalvalue <intervalvalue>", "interval duration for the aggregates in interval units") .option("-u, --intervalunit <intervalunit>", "interval duration unit [minute |hour |day |week | month]") .option("-s, --select <select>", "comma separated list of variable names") .option("-d, --download [download]", "download aggregates to specified file") .option("-a, --all", "show all aggregates not just average, min, max, sum and sd") .option("-l, --local", "use localtime in aggregate list") .option("-c, --count <count>", "number of aggregates in response ") .option("-h, --formatted", "write JSON strings with indentation") .option("-p, --passkey <passkey>", `passkey`) .option("-y, --retry <number>", "retry attempts before giving up", "3") .option("-v, --verbose", "verbose output") .description(`${color("list timeseries aggregates *")}`) .action((options) => { (() => __awaiter(void 0, void 0, void 0, function* () { try { checkParameters(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); yield listAggregates(options, sdk); } catch (err) { (0, command_utils_1.errorLog)(err, options.verbose); } }))(); }) .on("--help", () => { (0, console_1.log)("\n Examples:\n"); (0, console_1.log)(` mdsp aggregates --asssetid 1234567..ef --aspectname Environment \tlist recent aggregates for aspect Environment`); (0, console_1.log)(` mdsp aggregates --asssetid 1234567..ef --aspectname Environment --select Temperature \n\t\t\t\t\t\t\t\t\tlist recent temperature aggregates `); (0, console_1.log)(` mdsp aggregates --asssetid 1234567..ef --aspectname Environment --select Temperature --all \n\t\t\t\t\t\t\t\t\tlist all recent temperature aggregates`); (0, console_1.log)(` mdsp aggregates --asssetid 1234567..ef --aspectname Environment --intervalunit hour --intervalvalue 2 \n\t\t\t\t\t\t\t\t\tlist all recent temperatre aggregates over every 2 hours`); (0, console_1.log)("\n Important:\n"); (0, console_1.log)(` see ${color("https://documentation.mindsphere.io/MindSphere/apis/iot-iottsaggregates/api-iottsaggregates-samples-v4.html")} for documentation about the aggregate parameters\n`); (0, command_utils_1.serviceCredentialLog)(); }); }; function listAggregates(options, sdk) { return __awaiter(this, void 0, void 0, function* () { var _a; const aggregatesV4Client = sdk.GetTimeSeriesAggregateClientV4(); const fromDate = new Date(options.from).toISOString(); const toDate = options.to ? new Date(options.to).toISOString() : undefined; const aggregates = (yield (0, utils_1.retry)(options.retry, () => aggregatesV4Client.GetAggregates({ assetId: options.assetid, aspectName: options.aspectname, from: fromDate, to: toDate, intervalValue: options.intervalvalue, intervalUnit: options.intervalunit, select: options.select, count: options.count, }))); if (options.download) { const download = options.download === true ? "aggregates.mdsp.json" : options.download; const downloadPath = path.resolve(download); fs.writeFileSync(downloadPath, options.formatted ? JSON.stringify(aggregates, null, 2) : JSON.stringify(aggregates)); console.log(`The aggregates were downloaded to ${color(downloadPath)}.`); return; } !options.all && console.log(`[from - to] ${color("variable")} ${green("average")} ${yellow("sum")} ${blue("min")} ${red("max")} ${cyan("sd")} ${magenta("count")}`); for (const aggregate of aggregates.aggregates || []) { let line; for (const key of Object.keys(aggregate).sort()) { if (key !== "starttime" && key !== "endtime") { line = `${color(key)} ${green(aggregate[key].average.toFixed(3))} ${yellow(aggregate[key].sum.toFixed(3))} ${blue(aggregate[key].minvalue.toFixed(3))} ${red(aggregate[key].maxvalue.toFixed(3))} ${cyan(aggregate[key].sd.toFixed(3))} ${magenta(0 + aggregate[key].countgood + aggregate[key].countbad + aggregate[key].countuncertain)} `; options.all && console.log(`from: ${options.local ? new Date(aggregate.starttime).toLocaleString() : aggregate.starttime} to: ${options.local ? new Date(aggregate.endtime).toLocaleString() : aggregate.endtime} variable: ${color(key)}`); options.all && console.table(aggregate[key]); !options.all && console.log(`[${options.local ? new Date(aggregate.starttime).toLocaleString() : aggregate.starttime} - ${options.local ? new Date(aggregate.endtime).toLocaleString() : aggregate.endtime}] ${line}`); } } (0, command_utils_1.verboseLog)(JSON.stringify(aggregate, null, 2), options.verbose); } console.log(`${color((_a = aggregates.aggregates) === null || _a === void 0 ? void 0 : _a.length)} aggregates listed.\n`); }); } function checkParameters(options) { !options.assetid && (0, command_utils_1.errorLog)("Missing assetid for aggregates command. Run mdsp ag --help for full syntax and examples.", true); !options.aspectname && (0, command_utils_1.errorLog)("Missing aspectname for aggregates command. Run mdsp ag --help for full syntax and examples.", true); } //# sourceMappingURL=aggregates.js.map