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.

166 lines 9.41 kB
// SPDX-FileCopyrightText: 2025 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); }; import { Logger } from 'tslog'; import { AbstractModuleApi, AsMessageEndpoint, DEFAULT_TENANT_ID, OCPP2_0_1, OCPP2_0_1_CallAction, OCPPVersion, } from '@citrineos/base'; import { ReportingModule } from '../module.js'; import { getBatches, getSizeOfRequest } from '@citrineos/util'; /** * Server API for the Reporting module. */ export class ReportingOcpp201Api extends AbstractModuleApi { _componentDeviceDataCtrlr = 'DeviceDataCtrlr'; /** * 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, OCPPVersion.OCPP2_0_1, logger); } getBaseReport(identifier, request, callbackUrl, tenantId = DEFAULT_TENANT_ID) { // For each station, send the GetBaseReport call const results = identifier.map((id) => this._module.sendCall(id, tenantId, OCPPVersion.OCPP2_0_1, OCPP2_0_1_CallAction.GetBaseReport, request, callbackUrl)); return Promise.all(results); } async getCustomReport(identifier, request, callbackUrl, tenantId = DEFAULT_TENANT_ID) { // if request size is bigger than BytesPerMessageGetReport, return error const bytesPerMessageGetReport = await this._module._deviceModelService.getBytesPerMessageByComponentAndVariableInstanceAndStationId(this._componentDeviceDataCtrlr, OCPP2_0_1_CallAction.GetReport, tenantId, identifier); const requestBytes = 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 await this._module.sendCall(identifier, tenantId, OCPPVersion.OCPP2_0_1, OCPP2_0_1_CallAction.GetReport, request, callbackUrl); } // Batching logic let itemsPerMessageGetReport = await this._module._deviceModelService.getItemsPerMessageByComponentAndVariableInstanceAndStationId(this._componentDeviceDataCtrlr, OCPP2_0_1_CallAction.GetReport, tenantId, identifier); itemsPerMessageGetReport = itemsPerMessageGetReport === null ? componentVariables.length : itemsPerMessageGetReport; const confirmations = []; // Using multiple calls if needed for (const [index, batch] of getBatches(componentVariables, itemsPerMessageGetReport).entries()) { try { const batchResult = await this._module.sendCall(identifier, tenantId, OCPPVersion.OCPP2_0_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 }; } async getMonitoringReport(identifier, request, callbackUrl, tenantId = DEFAULT_TENANT_ID) { // 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 await this._module.sendCall(identifier, tenantId, OCPPVersion.OCPP2_0_1, OCPP2_0_1_CallAction.GetMonitoringReport, request, callbackUrl); } // Otherwise, do batching if needed let itemsPerMessageGetReport = await this._module._deviceModelService.getItemsPerMessageByComponentAndVariableInstanceAndStationId(this._componentDeviceDataCtrlr, OCPP2_0_1_CallAction.GetReport, tenantId, identifier); itemsPerMessageGetReport = itemsPerMessageGetReport === null ? componentVariable.length : itemsPerMessageGetReport; const confirmations = []; for (const [index, batch] of getBatches(componentVariable, itemsPerMessageGetReport).entries()) { try { const batchResult = await this._module.sendCall(identifier, tenantId, OCPPVersion.OCPP2_0_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, request, callbackUrl, tenantId = DEFAULT_TENANT_ID) { const results = identifier.map((id) => this._module.sendCall(id, tenantId, OCPPVersion.OCPP2_0_1, OCPP2_0_1_CallAction.GetLog, request, callbackUrl)); return Promise.all(results); } customerInformation(identifier, request, callbackUrl, tenantId = DEFAULT_TENANT_ID) { const results = identifier.map((id) => this._module.sendCall(id, tenantId, OCPPVersion.OCPP2_0_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); } } __decorate([ AsMessageEndpoint(OCPP2_0_1_CallAction.GetBaseReport, OCPP2_0_1.GetBaseReportRequestSchema), __metadata("design:type", Function), __metadata("design:paramtypes", [Array, Object, String, Number]), __metadata("design:returntype", Promise) ], ReportingOcpp201Api.prototype, "getBaseReport", null); __decorate([ AsMessageEndpoint(OCPP2_0_1_CallAction.GetReport, OCPP2_0_1.GetReportRequestSchema), __metadata("design:type", Function), __metadata("design:paramtypes", [String, Object, String, Number]), __metadata("design:returntype", Promise) ], ReportingOcpp201Api.prototype, "getCustomReport", null); __decorate([ AsMessageEndpoint(OCPP2_0_1_CallAction.GetMonitoringReport, OCPP2_0_1.GetMonitoringReportRequestSchema), __metadata("design:type", Function), __metadata("design:paramtypes", [String, Object, String, Number]), __metadata("design:returntype", Promise) ], ReportingOcpp201Api.prototype, "getMonitoringReport", null); __decorate([ AsMessageEndpoint(OCPP2_0_1_CallAction.GetLog, OCPP2_0_1.GetLogRequestSchema), __metadata("design:type", Function), __metadata("design:paramtypes", [Array, Object, String, Number]), __metadata("design:returntype", Promise) ], ReportingOcpp201Api.prototype, "getLog", null); __decorate([ AsMessageEndpoint(OCPP2_0_1_CallAction.CustomerInformation, OCPP2_0_1.CustomerInformationRequestSchema), __metadata("design:type", Function), __metadata("design:paramtypes", [Array, Object, String, Number]), __metadata("design:returntype", Promise) ], ReportingOcpp201Api.prototype, "customerInformation", null); //# sourceMappingURL=MessageApi.js.map