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)

112 lines 6.74 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 = (0, command_utils_1.getColor)("cyan"); exports.default = (program) => { program .command("upload-timeseries") .alias("uts") .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) { (0, command_utils_1.errorLog)("Missing file name for upload-timeseries command. Run mdsp ts --help for full syntax and examples.", true); process.exit(1); } (0, command_utils_1.homeDirLog)(options.verbose, color); (0, command_utils_1.proxyLog)(options.verbose, color); const configFile = path.resolve(options.config); (0, 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}`); } (0, 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 = (0, utils_1.checkCertificate)(configuration, options); const agentFolder = (0, utils_1.getAgentDir)(path.dirname(options.config)); (0, 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 (0, __1.retry)(options.retry, () => agent.OnBoard(), 300, (0, command_utils_1.retrylog)("OnBoard")); (0, console_1.log)(color(`Your agent with id ${agent.ClientId()} was succesfully onboarded.`)); } if (!agent.HasDataSourceConfiguration()) { yield (0, __1.retry)(options.retry, () => agent.GetDataSourceConfiguration(), 300, (0, command_utils_1.retrylog)("GetDataSourceConfiguration")); (0, command_utils_1.verboseLog)("Getting client configuration", options.verbose); } const mimeType = options.mime || mime.lookup(uploadFile) || "application/octet-stream"; (0, 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); (0, 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"); } (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) { (0, command_utils_1.errorLog)(err, options.verbose); } }))(); }) .on("--help", () => { (0, command_utils_1.displayCsvHelp)(color); }); }; function postChunk(messageCount, data, options, agent) { return __awaiter(this, void 0, void 0, function* () { (0, command_utils_1.verboseLog)(`posting timeseries message Nr. ${color(++messageCount + "")} with ${color(data.length + "")} records `, true); const tdpArray = (0, utils_1.convertToTdpArray)(data); yield (0, __1.retry)(options.retry, () => agent.BulkPostData(tdpArray, options.validation ? true : false), 300, (0, command_utils_1.retrylog)("TimeSeriesBulkUpload")); return messageCount; }); } //# sourceMappingURL=mc-upload-timeseries.js.map