@citrineos/configuration
Version:
The configuration module for OCPP v2.0.1. This module is not intended to be used directly, but rather as a dependency for other modules.
190 lines • 9.96 kB
JavaScript
// 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 { ConfigurationModule } from '../module.js';
import { AbstractModuleApi, AsMessageEndpoint, DEFAULT_TENANT_ID, OCPP1_6, OCPP1_6_CallAction, OCPP2_0_1_CallAction, OCPPVersion, } from '@citrineos/base';
import { v4 as uuidv4 } from 'uuid';
/**
* Server API for the Configuration component.
*/
export class ConfigurationOcpp16Api extends AbstractModuleApi {
/**
* Constructor for the class.
*
* @param {ConfigurationModule} ConfigurationComponent - The Configuration component.
* @param {FastifyInstance} server - The server instance.
* @param {Logger<ILogObj>} [logger] - Optional logger instance.
*/
constructor(ConfigurationComponent, server, logger) {
super(ConfigurationComponent, server, OCPPVersion.OCPP1_6, logger);
}
async triggerMessage(identifier, request, callbackUrl, tenantId = DEFAULT_TENANT_ID) {
const connectorId = request.connectorId;
if (connectorId && connectorId <= 0) {
const errorMsg = `connectorId should be either omitted or greater than 0.`;
this._logger.error(errorMsg);
return [{ success: false, payload: errorMsg }];
}
const results = identifier.map((id) => this._module.sendCall(id, tenantId, OCPPVersion.OCPP1_6, OCPP2_0_1_CallAction.TriggerMessage, request, callbackUrl));
return Promise.all(results);
}
async changeConfiguration(identifier, request, callbackUrl, tenantId = DEFAULT_TENANT_ID) {
this._logger.debug('ChangeConfiguration request received:', request);
const confirmations = identifier.map(async (stationId) => {
const chargingStation = await this._module.locationRepository.readChargingStationByStationId(tenantId, stationId);
if (!chargingStation) {
return {
success: false,
payload: `Charging station ${stationId} not found`,
};
}
return await this._module.sendCall(stationId, tenantId, OCPPVersion.OCPP1_6, OCPP1_6_CallAction.ChangeConfiguration, request, callbackUrl);
});
return Promise.all(confirmations);
}
async getConfiguration(identifier, request, callbackUrl, tenantId = DEFAULT_TENANT_ID) {
this._logger.debug('GetConfiguration request received:', request);
const confirmations = [];
await Promise.all(identifier.map(async (stationId) => {
const chargingStation = await this._module.locationRepository.readChargingStationByStationId(tenantId, stationId);
if (!chargingStation) {
confirmations.push({
success: false,
payload: {
batch: `Station ${stationId}`,
message: `Charging station ${stationId} not found`,
stationId,
},
});
return;
}
const maxKeysConfig = await this._module.changeConfigurationRepository.readOnlyOneByQuery(tenantId, {
where: {
tenantId: tenantId,
stationId: stationId,
key: 'GetConfigurationMaxKeys',
},
});
const maxKeys = maxKeysConfig?.value
? parseInt(maxKeysConfig.value, 10)
: Number.MAX_SAFE_INTEGER;
const keys = request.key || [];
const sendBatches = async (batches) => {
return Promise.all(batches.map(async (batch, index) => {
try {
const correlationId = uuidv4();
const batchResult = await this._module.sendCall(stationId, tenantId, OCPPVersion.OCPP1_6, OCPP1_6_CallAction.GetConfiguration, { key: batch }, callbackUrl, correlationId);
confirmations.push({
success: batchResult.success,
payload: {
batch: `[${index}:${index + batch.length}]`,
message: `${batchResult.payload}`,
stationId,
},
});
}
catch (error) {
confirmations.push({
success: false,
payload: {
batch: `[${index}:${index + batch.length}]`,
message: `${error}`,
stationId,
},
});
}
}));
};
if (keys.length === 0 || keys.length <= maxKeys) {
await sendBatches([keys]);
}
else {
const batches = [];
for (let i = 0; i < keys.length; i += maxKeys) {
batches.push(keys.slice(i, i + maxKeys));
}
await sendBatches(batches);
}
}));
return confirmations;
}
reset(identifier, request, callbackUrl, tenantId = DEFAULT_TENANT_ID) {
const results = identifier.map((id) => this._module.sendCall(id, tenantId, OCPPVersion.OCPP1_6, OCPP1_6_CallAction.Reset, request, callbackUrl));
return Promise.all(results);
}
changeAvailability(identifier, request, callbackUrl, tenantId = DEFAULT_TENANT_ID) {
const results = identifier.map((id) => this._module.sendCall(id, tenantId, OCPPVersion.OCPP1_6, OCPP1_6_CallAction.ChangeAvailability, request, callbackUrl));
return Promise.all(results);
}
updateFirmware(identifier, request, callbackUrl, tenantId = DEFAULT_TENANT_ID) {
const results = identifier.map((id) => this._module.sendCall(id, tenantId, OCPPVersion.OCPP1_6, OCPP1_6_CallAction.UpdateFirmware, request, callbackUrl));
return Promise.all(results);
}
dataTransfer(identifier, request, callbackUrl, tenantId = DEFAULT_TENANT_ID) {
const results = identifier.map((id) => this._module.sendCall(id, tenantId, OCPPVersion.OCPP1_6, OCPP1_6_CallAction.DataTransfer, 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.configuration.endpointPrefix;
return super._toMessagePath(input, endpointPrefix);
}
}
__decorate([
AsMessageEndpoint(OCPP1_6_CallAction.TriggerMessage, OCPP1_6.TriggerMessageRequestSchema),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Array, Object, String, Number]),
__metadata("design:returntype", Promise)
], ConfigurationOcpp16Api.prototype, "triggerMessage", null);
__decorate([
AsMessageEndpoint(OCPP1_6_CallAction.ChangeConfiguration, OCPP1_6.ChangeConfigurationRequestSchema),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Array, Object, String, Number]),
__metadata("design:returntype", Promise)
], ConfigurationOcpp16Api.prototype, "changeConfiguration", null);
__decorate([
AsMessageEndpoint(OCPP1_6_CallAction.GetConfiguration, OCPP1_6.GetConfigurationRequestSchema),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Array, Object, String, Number]),
__metadata("design:returntype", Promise)
], ConfigurationOcpp16Api.prototype, "getConfiguration", null);
__decorate([
AsMessageEndpoint(OCPP1_6_CallAction.Reset, OCPP1_6.ResetRequestSchema),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Array, Object, String, Number]),
__metadata("design:returntype", Promise)
], ConfigurationOcpp16Api.prototype, "reset", null);
__decorate([
AsMessageEndpoint(OCPP1_6_CallAction.ChangeAvailability, OCPP1_6.ChangeAvailabilityRequestSchema),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Array, Object, String, Number]),
__metadata("design:returntype", Promise)
], ConfigurationOcpp16Api.prototype, "changeAvailability", null);
__decorate([
AsMessageEndpoint(OCPP1_6_CallAction.UpdateFirmware, OCPP1_6.UpdateFirmwareRequestSchema),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Array, Object, String, Number]),
__metadata("design:returntype", Promise)
], ConfigurationOcpp16Api.prototype, "updateFirmware", null);
__decorate([
AsMessageEndpoint(OCPP1_6_CallAction.DataTransfer, OCPP1_6.DataTransferRequestSchema),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Array, Object, String, Number]),
__metadata("design:returntype", Promise)
], ConfigurationOcpp16Api.prototype, "dataTransfer", null);
//# sourceMappingURL=MessageApi.js.map