@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.
186 lines • 10.2 kB
JavaScript
;
// 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.ConfigurationOcpp16Api = void 0;
const base_1 = require("@citrineos/base");
const uuid_1 = require("uuid");
/**
* Server API for the Configuration component.
*/
class ConfigurationOcpp16Api extends base_1.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, base_1.OCPPVersion.OCPP1_6, logger);
}
triggerMessage(identifier, tenantId, request, callbackUrl) {
return __awaiter(this, void 0, void 0, function* () {
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, base_1.OCPPVersion.OCPP1_6, base_1.OCPP2_0_1_CallAction.TriggerMessage, request, callbackUrl));
return Promise.all(results);
});
}
changeConfiguration(identifier, tenantId, request, callbackUrl) {
return __awaiter(this, void 0, void 0, function* () {
this._logger.debug('ChangeConfiguration request received:', request);
const confirmations = identifier.map((stationId) => __awaiter(this, void 0, void 0, function* () {
const chargingStation = yield this._module.locationRepository.readChargingStationByStationId(stationId);
if (!chargingStation) {
return {
success: false,
payload: `Charging station ${stationId} not found`,
};
}
return yield this._module.sendCall(stationId, tenantId, base_1.OCPPVersion.OCPP1_6, base_1.OCPP1_6_CallAction.ChangeConfiguration, request, callbackUrl);
}));
return Promise.all(confirmations);
});
}
getConfiguration(identifier, tenantId, request, callbackUrl) {
return __awaiter(this, void 0, void 0, function* () {
this._logger.debug('GetConfiguration request received:', request);
const confirmations = [];
yield Promise.all(identifier.map((stationId) => __awaiter(this, void 0, void 0, function* () {
const chargingStation = yield this._module.locationRepository.readChargingStationByStationId(stationId);
if (!chargingStation) {
confirmations.push({
success: false,
payload: {
batch: `Station ${stationId}`,
message: `Charging station ${stationId} not found`,
stationId,
},
});
return;
}
const maxKeysConfig = yield this._module.changeConfigurationRepository.readOnlyOneByQuery({
where: {
stationId: stationId,
key: 'GetConfigurationMaxKeys',
},
});
const maxKeys = (maxKeysConfig === null || maxKeysConfig === void 0 ? void 0 : maxKeysConfig.value)
? parseInt(maxKeysConfig.value, 10)
: Number.MAX_SAFE_INTEGER;
const keys = request.key || [];
const sendBatches = (batches) => __awaiter(this, void 0, void 0, function* () {
return Promise.all(batches.map((batch, index) => __awaiter(this, void 0, void 0, function* () {
try {
const correlationId = (0, uuid_1.v4)();
const batchResult = yield this._module.sendCall(stationId, tenantId, base_1.OCPPVersion.OCPP1_6, base_1.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) {
yield sendBatches([keys]);
}
else {
const batches = [];
for (let i = 0; i < keys.length; i += maxKeys) {
batches.push(keys.slice(i, i + maxKeys));
}
yield sendBatches(batches);
}
})));
return confirmations;
});
}
reset(identifier, tenantId, request, callbackUrl) {
const results = identifier.map((id) => this._module.sendCall(id, tenantId, base_1.OCPPVersion.OCPP1_6, base_1.OCPP1_6_CallAction.Reset, request, callbackUrl));
return Promise.all(results);
}
changeAvailability(identifier, tenantId, request, callbackUrl) {
const results = identifier.map((id) => this._module.sendCall(id, tenantId, base_1.OCPPVersion.OCPP1_6, base_1.OCPP1_6_CallAction.ChangeAvailability, 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);
}
}
exports.ConfigurationOcpp16Api = ConfigurationOcpp16Api;
__decorate([
(0, base_1.AsMessageEndpoint)(base_1.OCPP1_6_CallAction.TriggerMessage, base_1.OCPP1_6.TriggerMessageRequestSchema),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Array, String, Object, String]),
__metadata("design:returntype", Promise)
], ConfigurationOcpp16Api.prototype, "triggerMessage", null);
__decorate([
(0, base_1.AsMessageEndpoint)(base_1.OCPP1_6_CallAction.ChangeConfiguration, base_1.OCPP1_6.ChangeConfigurationRequestSchema),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Array, String, Object, String]),
__metadata("design:returntype", Promise)
], ConfigurationOcpp16Api.prototype, "changeConfiguration", null);
__decorate([
(0, base_1.AsMessageEndpoint)(base_1.OCPP1_6_CallAction.GetConfiguration, base_1.OCPP1_6.GetConfigurationRequestSchema),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Array, String, Object, String]),
__metadata("design:returntype", Promise)
], ConfigurationOcpp16Api.prototype, "getConfiguration", null);
__decorate([
(0, base_1.AsMessageEndpoint)(base_1.OCPP1_6_CallAction.Reset, base_1.OCPP1_6.ResetRequestSchema),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Array, String, Object, String]),
__metadata("design:returntype", Promise)
], ConfigurationOcpp16Api.prototype, "reset", null);
__decorate([
(0, base_1.AsMessageEndpoint)(base_1.OCPP1_6_CallAction.ChangeAvailability, base_1.OCPP1_6.ChangeAvailabilityRequestSchema),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Array, String, Object, String]),
__metadata("design:returntype", Promise)
], ConfigurationOcpp16Api.prototype, "changeAvailability", null);
//# sourceMappingURL=MessageApi.js.map