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)

91 lines 5.31 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 path = require("path"); const test_utils_1 = require("../../../test/test-utils"); const utils_1 = require("../../api/utils"); const command_utils_1 = require("./command-utils"); const ora = require("ora-classic"); let color = (0, command_utils_1.getColor)("magenta"); exports.default = (program) => { program .command("download-bulk") .alias("db") .option("-d, --dir <directoryname>", "directory for the download (shouldn't exist)", "bulkdownload") .option("-i, --assetid <assetid>", "mindsphere asset id") .option("-a, --aspectname <aspectname>", "aspectname") .option("-f, --from <from>", "from date") .option("-t, --to <to>", "to date") .option("-s, --size <size>", "max entries per file ", "200") .option("-p, --passkey <passkey>", `passkey`) .option("-y, --retry <number>", "retry attempts before giving up", "3") .option("-v, --verbose", "verbose output") .description(`${color("download the timeseries data in bulk from mindsphere *")}`) .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); fs.mkdirSync(path.resolve(options.dir)); const iotTs = sdk.GetTimeSeriesClient(); let file = 0; const spinner = ora(`downloading ${options.aspectname} data`); !options.verbose && spinner.start(); let ts = yield (0, utils_1.retry)(options.retry, () => iotTs.GetTimeSeriesBulkStyle(options.assetid, options.aspectname, { from: new Date(options.from), to: new Date(options.to), limit: options.size, })); fs.writeFileSync(`${path.resolve(options.dir)}/${options.aspectname}_${file++}.json`, JSON.stringify(ts.records)); (0, command_utils_1.verboseLog)(`downloaded ${ts.records.length} records`, options.verbose, spinner); yield (0, test_utils_1.sleep)(500); for (;;) { if (ts.nextRecord) { const url = `${ts.nextRecord}`; ts = yield (0, utils_1.retry)(options.retry, () => iotTs.GetNextRecordsBulkStyle(url)); fs.writeFileSync(`${path.resolve(options.dir)}/${options.aspectname}_${file++}.json`, JSON.stringify(ts.records)); (0, command_utils_1.verboseLog)(`downloaded ${ts.records.length} records`, options.verbose, spinner); yield (0, test_utils_1.sleep)(500); } else { break; } } !options.verbose && spinner.succeed("Done"); console.log(`Files with timeseries data are in ${color(path.resolve(options.dir))} directory`); } catch (err) { (0, command_utils_1.errorLog)(err, options.verbose); } }))(); }) .on("--help", () => { (0, console_1.log)("\n Examples:\n"); (0, console_1.log)(` mdsp download-bulk --assetid 12345..ef --from 12/10/2019 --to 12/16/2019 \t\t download timeseries from specified asset`); (0, command_utils_1.serviceCredentialLog)(); }); }; function checkParameters(options) { !options.dir && (0, command_utils_1.errorLog)("Missing dir name for download-bulk command. Run mdsp db --help for full syntax and examples.", true); !options.assetid && (0, command_utils_1.errorLog)(" You have to specify assetid. Run mdsp db --help for full syntax and examples.", true); !options.aspectname && (0, command_utils_1.errorLog)(" You have to specify aspectname. Run mdsp db --help for full syntax and examples.", true); !options.from && (0, command_utils_1.errorLog)(" You have to specify from date. Run mdsp db --help for full syntax and examples.", true); !options.to && (0, command_utils_1.errorLog)(" You have to specify to date. Run mdsp db --help for full syntax and examples.", true); } //# sourceMappingURL=iot-download-bulk.js.map