UNPKG

@mindconnect/mindconnect-nodejs

Version:

MindConnect Library for NodeJS (community based)

112 lines 6.72 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 csv = require("csvtojson"); const fs = require("fs"); const path = require("path"); const __1 = require("../.."); const utils_1 = require("../../api/utils"); const command_utils_1 = require("./command-utils"); const mime = require("mime-types"); const color = command_utils_1.getColor("cyan"); exports.default = (program) => { program .command("upload-timeseries") .alias("ts") .option("-c, --config <agentconfig>", "config file with agent configuration", "agentconfig.json") .option("-r, --cert [privatekey]", "required for agents with RSA_3072 profile. create with: openssl genrsa -out private.key 3072") .option("-f, --file <timeseriesdata.csv>", "csv file containing the timeseries data to upload to mindsphere") .option("-s, --size <size>", "max records per http post", 200) .option("-n, --no-validation", "switch validation off (only if you are sure that the timeseries upload works)") .option("-y, --retry <number>", "retry attempts before giving up", 3) .option("-v, --verbose", "verbose output") .description(color("parse .csv file with timeseriesdata and upload the timeseries data to mindsphere")) .action(options => { (() => __awaiter(void 0, void 0, void 0, function* () { try { if (!options.file) { command_utils_1.errorLog("Missing file name for upload-timeseries command. Run mc ts --help for full syntax and examples.", true); process.exit(1); } command_utils_1.homeDirLog(options.verbose, color); command_utils_1.proxyLog(options.verbose, color); const configFile = path.resolve(options.config); command_utils_1.verboseLog(`Timeseries upload using the agent configuration stored in: ${color(configFile)}.`, options.verbose); if (!fs.existsSync(configFile)) { throw new Error(`Can't find file ${configFile}`); } const uploadFile = path.resolve(options.file); if (!fs.existsSync(uploadFile)) { throw new Error(`Can't find file ${uploadFile}`); } command_utils_1.verboseLog(`Timeseries file to upload: ${color(uploadFile)}.`, options.verbose); if (!fs.existsSync(uploadFile)) { throw new Error(`Can't find file ${uploadFile}`); } const configuration = require(configFile); const profile = utils_1.checkCertificate(configuration, options); const agentFolder = utils_1.getAgentDir(path.dirname(options.config)); command_utils_1.verboseLog(`Using .mc folder for agent: ${color(agentFolder)}`, options.verbose); const agent = new __1.MindConnectAgent(configuration, undefined, agentFolder); if (profile) { agent.SetupAgentCertificate(fs.readFileSync(options.cert)); } if (!agent.IsOnBoarded()) { yield __1.retry(options.retry, () => agent.OnBoard(), 300, command_utils_1.retrylog("OnBoard")); console_1.log(color(`Your agent with id ${agent.ClientId()} was succesfully onboarded.`)); } if (!agent.HasDataSourceConfiguration()) { yield __1.retry(options.retry, () => agent.GetDataSourceConfiguration(), 300, command_utils_1.retrylog("GetDataSourceConfiguration")); command_utils_1.verboseLog("Getting client configuration", options.verbose); } const mimeType = options.mime || mime.lookup(uploadFile) || "application/octet-stream"; command_utils_1.verboseLog(`Mime type of the file is: ${mimeType}. it should be text/csv.`, options.verbose); let data = []; const maxSize = parseInt(options.size, 10); command_utils_1.verboseLog(`Using ${color("" + maxSize)} http post size.`, options.verbose); if (isNaN(maxSize) || maxSize < 1) { throw new Error("the size parameter must be a number > 0"); } command_utils_1.verboseLog(`Using ${color(options.validation ? "validation" : "no validation")}`, options.verbose); let messageCount = 0; yield csv() .fromFile(uploadFile) .subscribe((json) => __awaiter(void 0, void 0, void 0, function* () { data.push(json); if (data.length >= maxSize) { messageCount = yield postChunk(messageCount, data, options, agent); data = []; } })); if (data.length >= 1) { messageCount = yield postChunk(messageCount, data, options, agent); data = []; } } catch (err) { command_utils_1.errorLog(err, options.verbose); } }))(); }) .on("--help", () => { command_utils_1.displayCsvHelp(color); }); }; function postChunk(messageCount, data, options, agent) { return __awaiter(this, void 0, void 0, function* () { command_utils_1.verboseLog(`posting timeseries message Nr. ${color(++messageCount + "")} with ${color(data.length + "")} records `, true); const tdpArray = utils_1.convertToTdpArray(data); yield __1.retry(options.retry, () => agent.BulkPostData(tdpArray, options.validation ? true : false), 300, command_utils_1.retrylog("TimeSeriesBulkUpload")); return messageCount; }); } //# sourceMappingURL=mc-upload-timeseries.js.map