UNPKG

@citrineos/reporting

Version:

The reporting module for OCPP v2.0.1. This module is not intended to be used directly, but rather as a dependency for other modules.

182 lines 10.7 kB
"use strict"; // Copyright (c) 2023 S44, LLC // Copyright Contributors to the CitrineOS Project // // SPDX-License-Identifier: Apache 2.0 var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; 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 }); exports.ReportingOcpp201Api = void 0; const base_1 = require("@citrineos/base"); const util_1 = require("@citrineos/util"); /** * Server API for the Reporting module. */ class ReportingOcpp201Api extends base_1.AbstractModuleApi { /** * Constructs a new instance of the class. * * @param {ReportingModule} reportingModule - The Reporting module. * @param {FastifyInstance} server - The Fastify server instance. * @param {Logger<ILogObj>} [logger] - The logger instance. */ constructor(reportingModule, server, logger) { super(reportingModule, server, base_1.OCPPVersion.OCPP2_0_1, logger); this._componentDeviceDataCtrlr = 'DeviceDataCtrlr'; } getBaseReport(identifier, tenantId, request, callbackUrl) { // For each station, send the GetBaseReport call const results = identifier.map((id) => this._module.sendCall(id, tenantId, base_1.OCPPVersion.OCPP2_0_1, base_1.OCPP2_0_1_CallAction.GetBaseReport, request, callbackUrl)); return Promise.all(results); } getCustomReport(identifier, tenantId, request, callbackUrl) { return __awaiter(this, void 0, void 0, function* () { // if request size is bigger than BytesPerMessageGetReport, return error const bytesPerMessageGetReport = yield this._module._deviceModelService.getBytesPerMessageByComponentAndVariableInstanceAndStationId(this._componentDeviceDataCtrlr, base_1.OCPP2_0_1_CallAction.GetReport, identifier); const requestBytes = (0, util_1.getSizeOfRequest)(request); if (bytesPerMessageGetReport && requestBytes > bytesPerMessageGetReport) { const errorMsg = `The request is too big. The max size is ${bytesPerMessageGetReport} bytes.`; this._logger.error(errorMsg); return { success: false, payload: errorMsg }; } const componentVariables = request.componentVariable; if (componentVariables.length === 0) { // Send everything in one call return yield this._module.sendCall(identifier, tenantId, base_1.OCPPVersion.OCPP2_0_1, base_1.OCPP2_0_1_CallAction.GetReport, request, callbackUrl); } // Batching logic let itemsPerMessageGetReport = yield this._module._deviceModelService.getItemsPerMessageByComponentAndVariableInstanceAndStationId(this._componentDeviceDataCtrlr, base_1.OCPP2_0_1_CallAction.GetReport, identifier); itemsPerMessageGetReport = itemsPerMessageGetReport === null ? componentVariables.length : itemsPerMessageGetReport; const confirmations = []; // Using multiple calls if needed for (const [index, batch] of (0, util_1.getBatches)(componentVariables, itemsPerMessageGetReport).entries()) { try { const batchResult = yield this._module.sendCall(identifier, tenantId, base_1.OCPPVersion.OCPP2_0_1, base_1.OCPP2_0_1_CallAction.GetReport, { componentVariable: batch, componentCriteria: request.componentCriteria, requestId: request.requestId, }, callbackUrl); confirmations.push({ success: batchResult.success, batch: `[${index}:${index + batch.length}]`, message: `${batchResult.payload}`, }); } catch (error) { confirmations.push({ success: false, batch: `[${index}:${index + batch.length}]`, message: `${error}`, }); } } // Returns a single IMessageConfirmation containing details of each batched call return { success: true, payload: confirmations }; }); } getMonitoringReport(identifier, tenantId, request, callbackUrl) { return __awaiter(this, void 0, void 0, function* () { // If monitoringCriteria & componentVariable are both empty, just call once const componentVariable = request.componentVariable; const monitoringCriteria = request.monitoringCriteria; if (componentVariable.length === 0 && monitoringCriteria.length === 0) { return yield this._module.sendCall(identifier, tenantId, base_1.OCPPVersion.OCPP2_0_1, base_1.OCPP2_0_1_CallAction.GetMonitoringReport, request, callbackUrl); } // Otherwise, do batching if needed let itemsPerMessageGetReport = yield this._module._deviceModelService.getItemsPerMessageByComponentAndVariableInstanceAndStationId(this._componentDeviceDataCtrlr, base_1.OCPP2_0_1_CallAction.GetReport, identifier); itemsPerMessageGetReport = itemsPerMessageGetReport === null ? componentVariable.length : itemsPerMessageGetReport; const confirmations = []; for (const [index, batch] of (0, util_1.getBatches)(componentVariable, itemsPerMessageGetReport).entries()) { try { const batchResult = yield this._module.sendCall(identifier, tenantId, base_1.OCPPVersion.OCPP2_0_1, base_1.OCPP2_0_1_CallAction.GetMonitoringReport, { componentVariable: batch, monitoringCriteria, requestId: request.requestId, }, callbackUrl); confirmations.push({ success: batchResult.success, batch: `[${index}:${index + batch.length}]`, message: `${batchResult.payload}`, }); } catch (error) { confirmations.push({ success: false, batch: `[${index}:${index + batch.length}]`, message: `${error}`, }); } } return { success: true, payload: confirmations }; }); } getLog(identifier, tenantId, request, callbackUrl) { const results = identifier.map((id) => this._module.sendCall(id, tenantId, base_1.OCPPVersion.OCPP2_0_1, base_1.OCPP2_0_1_CallAction.GetLog, request, callbackUrl)); return Promise.all(results); } customerInformation(identifier, tenantId, request, callbackUrl) { const results = identifier.map((id) => this._module.sendCall(id, tenantId, base_1.OCPPVersion.OCPP2_0_1, base_1.OCPP2_0_1_CallAction.CustomerInformation, request, callbackUrl)); return Promise.all(results); } /** * Overrides superclass method to generate the URL path based on the input {@link CallAction} * and the module's endpoint prefix configuration. * * @param {CallAction} input - The input {@link CallAction}. * @return {string} - The generated URL path. */ _toMessagePath(input) { const endpointPrefix = this._module.config.modules.reporting.endpointPrefix; return super._toMessagePath(input, endpointPrefix); } } exports.ReportingOcpp201Api = ReportingOcpp201Api; __decorate([ (0, base_1.AsMessageEndpoint)(base_1.OCPP2_0_1_CallAction.GetBaseReport, base_1.OCPP2_0_1.GetBaseReportRequestSchema), __metadata("design:type", Function), __metadata("design:paramtypes", [Array, String, Object, String]), __metadata("design:returntype", Promise) ], ReportingOcpp201Api.prototype, "getBaseReport", null); __decorate([ (0, base_1.AsMessageEndpoint)(base_1.OCPP2_0_1_CallAction.GetReport, base_1.OCPP2_0_1.GetReportRequestSchema), __metadata("design:type", Function), __metadata("design:paramtypes", [String, String, Object, String]), __metadata("design:returntype", Promise) ], ReportingOcpp201Api.prototype, "getCustomReport", null); __decorate([ (0, base_1.AsMessageEndpoint)(base_1.OCPP2_0_1_CallAction.GetMonitoringReport, base_1.OCPP2_0_1.GetMonitoringReportRequestSchema), __metadata("design:type", Function), __metadata("design:paramtypes", [String, String, Object, String]), __metadata("design:returntype", Promise) ], ReportingOcpp201Api.prototype, "getMonitoringReport", null); __decorate([ (0, base_1.AsMessageEndpoint)(base_1.OCPP2_0_1_CallAction.GetLog, base_1.OCPP2_0_1.GetLogRequestSchema), __metadata("design:type", Function), __metadata("design:paramtypes", [Array, String, Object, String]), __metadata("design:returntype", Promise) ], ReportingOcpp201Api.prototype, "getLog", null); __decorate([ (0, base_1.AsMessageEndpoint)(base_1.OCPP2_0_1_CallAction.CustomerInformation, base_1.OCPP2_0_1.CustomerInformationRequestSchema), __metadata("design:type", Function), __metadata("design:paramtypes", [Array, String, Object, String]), __metadata("design:returntype", Promise) ], ReportingOcpp201Api.prototype, "customerInformation", null); //# sourceMappingURL=MessageApi.js.map