@mindconnect/mindconnect-nodejs
Version:
MindConnect Library for NodeJS (community based)
93 lines • 5.36 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 path = require("path");
const test_utils_1 = require("../../../test/test-utils");
const sdk_1 = require("../../api/sdk/");
const utils_1 = require("../../api/utils");
const command_utils_1 = require("./command-utils");
const ora = require("ora");
const color = 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>", "asset id from the mindsphere")
.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 from mindsphere")}`)
.action(options => {
(() => __awaiter(void 0, void 0, void 0, function* () {
try {
checkParameters(options);
const auth = utils_1.loadAuth();
const sdk = new sdk_1.MindSphereSdk({
gateway: auth.gateway,
basicAuth: utils_1.decrypt(auth, options.passkey),
tenant: auth.tenant
});
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 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));
command_utils_1.verboseLog(`downloaded ${ts.records.length} records`, options.verbose, spinner);
yield test_utils_1.sleep(500);
for (;;) {
if (ts.nextRecord) {
const url = `${ts.nextRecord}`;
ts = yield utils_1.retry(options.retry, () => iotTs.GetNextRecordsBulkStyle(url));
fs.writeFileSync(`${path.resolve(options.dir)}/${options.aspectname}_${file++}.json`, JSON.stringify(ts.records));
command_utils_1.verboseLog(`downloaded ${ts.records.length} records`, options.verbose, spinner);
yield 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) {
command_utils_1.errorLog(err, options.verbose);
}
}))();
})
.on("--help", () => {
console_1.log("\n Examples:\n");
console_1.log(` mc download-bulk --assetid 12345..ef --from 12/10/2019 --to 12/16/2019 --passkey <yourpasskey> \t\t download timeseries from specified asset`);
command_utils_1.serviceCredentialLog();
});
};
function checkParameters(options) {
!options.passkey &&
command_utils_1.errorLog("you have to provide a passkey to get the service token (run mc db --help for full description)", true);
!options.dir &&
command_utils_1.errorLog("Missing dir name for download-bulk command. Run mc db --help for full syntax and examples.", true);
!options.assetid && command_utils_1.errorLog(" You have to specify assetid. Run mc df --help for full syntax and examples.", true);
!options.from && command_utils_1.errorLog(" You have to specify from date. Run mc df --help for full syntax and examples.", true);
!options.to && command_utils_1.errorLog(" You have to specify to date. Run mc df --help for full syntax and examples.", true);
}
//# sourceMappingURL=iot-download-bulk.js.map