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)

134 lines 8.12 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 fs = require("fs"); const ora = require("ora-classic"); 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 rotatingColors = [magenta, blue, yellow, red, green, cyan]; const today = new Date(); const yesterday = new Date(); yesterday.setDate(yesterday.getDate() - 1); exports.default = (program) => { program .command("timeseries") .alias("ts") .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", today.toISOString()) .option("-s, --select <select>", "comma separated list of variable names") .option("-d, --download", "download timeseries to a set of files") .option("-a, --all", "include also quality codes not just variable values") .option("-r, --raw", "don't strip the nextRecord URLs from downloaded JSON") .option("-h, --formatted", "write JSON strings with indentation") .option("-l, --local", "use local time in timeseries list") .option("-c, --count <count>", "number of timeseries entries per request", "2000") .option("-p, --passkey <passkey>", `passkey`) .option("-y, --retry <number>", "retry attempts before giving up", "3") .option("-v, --verbose", "verbose output") .description(`${color("list timeseries data *")}`) .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 listTimeSeries(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 timeseries --asssetid 1234567..ef --aspectname Environment \tlist recent timeseries for aspect Environment`); (0, console_1.log)(` mdsp timeseries --asssetid 1234567..ef --aspectname Environment --select Temperature \n\t\t\t\t\t\t\t\t\tlist recent temperature timeseries `); (0, console_1.log)(` mdsp timeseries --asssetid 1234567..ef --aspectname Environment --select Temperature --all \n\t\t\t\t\t\t\t\t\tlist all recent temperature timeseries`); (0, console_1.log)(` mdsp timeseries --asssetid 1234567..ef --aspectname Environment --select Temperature --all \n\t\t\t\t\t\t\t\t\tlist all recent temperature timeseries`); (0, console_1.log)(` mdsp timeseries --asssetid 1234567..ef --aspectname Environment --download \n\t\t\t\t\t\t\t\t\tdownload the recent timeseries data for the Environment aspect`); (0, console_1.log)("\n Important:\n"); (0, console_1.log)(` Please use ${color("bulk commands")} if you want to download a lot of data.\n`); (0, command_utils_1.serviceCredentialLog)(); }); }; function listTimeSeries(options, sdk) { return __awaiter(this, void 0, void 0, function* () { const timeSeriesClient = sdk.GetTimeSeriesClient(); const fromDate = new Date(options.from); const toDate = new Date(options.to); let timeseries = yield (0, utils_1.retry)(options.retry, () => timeSeriesClient.GetTimeSeriesBulkStyle(options.assetid, options.aspectname, { from: fromDate, to: toDate, select: options.select, limit: options.count || 2000, })); const propertyColors = {}; let colorIndex = 0; let totalRecords = 0; const spinner = ora("downloading timeseries data"); do { if (options.download) { const asset = yield sdk.GetAssetManagementClient().GetAsset(options.assetid); !options.verbose && spinner.start(); const fileName = `${asset.name}-${options.aspectname}-${totalRecords}-${totalRecords + timeseries.records.length - 1}.mdsp.json`; (0, command_utils_1.verboseLog)(`Downloading timeseries data to: ${fileName}`, options.verbose, spinner); const jsonToSerialize = options.raw ? timeseries : timeseries.records; fs.writeFileSync(fileName, options.formatted ? JSON.stringify(jsonToSerialize, null, 2) : JSON.stringify(jsonToSerialize)); totalRecords += timeseries.records.length; } else { for (const timeseriesRecord of timeseries.records) { let line = !options.local ? `${timeseriesRecord._time} ` : `${new Date(timeseriesRecord._time).toLocaleString()} `; for (const key of Object.keys(timeseriesRecord).sort()) { if (key === "_time") continue; const propertyName = key.replace(/(_qc$)/, ""); if (!propertyColors[propertyName]) { propertyColors[propertyName] = rotatingColors[colorIndex++ % rotatingColors.length]; } const currentColor = propertyColors[propertyName]; if (options.all || !key.endsWith("_qc")) { line += ` ${currentColor(key)}: ${timeseriesRecord[key]}`; } } console.log(line); totalRecords++; } } if (!timeseries.nextRecord) break; timeseries = yield (0, utils_1.retry)(options.retry, () => timeSeriesClient.GetNextRecordsBulkStyle(timeseries.nextRecord)); } while (timeseries); options.download && !options.verbose && spinner.succeed("Download of the timeseries data is done."); console.log(`There were ${color(totalRecords)} timeseries entries in the specified interval [${options.local ? fromDate.toLocaleString() : fromDate.toISOString()} - ${options.local ? toDate.toLocaleString() : toDate.toISOString()}]`); }); } function checkParameters(options) { !options.assetid && (0, command_utils_1.errorLog)("Missing assetid for timeseries command. Run mdsp timeseries --help for full syntax and examples.", true); !options.aspectname && (0, command_utils_1.errorLog)("Missing aspectname for timeseries command. Run mdsp timeseries --help for full syntax and examples.", true); } //# sourceMappingURL=timeseries.js.map