@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.
227 lines • 11.5 kB
JavaScript
;
// 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.ConfigurationDataApi = void 0;
const base_1 = require("@citrineos/base");
const data_1 = require("@citrineos/data");
const sequelize_1 = require("sequelize");
const util_1 = require("@citrineos/util");
const uuid_1 = require("uuid");
/**
* Server API for the Configuration component.
*/
class ConfigurationDataApi 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, null, logger);
}
putBootConfig(request) {
return this._module.bootRepository.createOrUpdateByKey(request.body, request.query.stationId);
}
getBootConfig(request) {
return this._module.bootRepository.readByKey(request.query.stationId);
}
deleteBootConfig(request) {
return this._module.bootRepository.deleteByKey(request.query.stationId);
}
updatePassword(request) {
return __awaiter(this, void 0, void 0, function* () {
const stationId = request.body.stationId;
this._logger.debug(`Updating password for ${stationId} station`);
if (request.body.setOnCharger && !request.body.password) {
return {
success: false,
payload: 'Password is required when setOnCharger is true',
};
}
if (request.body.password && !(0, util_1.isValidPassword)(request.body.password)) {
return { success: false, payload: 'Invalid password' };
}
const password = request.body.password || (0, util_1.generatePassword)();
if (!request.body.setOnCharger) {
try {
yield this.updatePasswordOnStation(password, stationId, request.query.callbackUrl);
}
catch (error) {
this._logger.warn(`Failed updating password on ${stationId} station`, error);
return {
success: false,
payload: `Failed updating password on ${stationId} station`,
};
}
}
const variableAttributes = yield this.updatePasswordForStation(password, stationId);
this._logger.debug(`Successfully updated password for ${stationId} station`);
return {
success: true,
payload: `Updated ${variableAttributes.length} attributes`,
};
});
}
getNetworkProfiles(request) {
return __awaiter(this, void 0, void 0, function* () {
return data_1.ChargingStationNetworkProfile.findAll({
where: { stationId: request.query.stationId },
include: [data_1.SetNetworkProfile, data_1.ServerNetworkProfile],
});
});
}
deleteNetworkProfiles(request) {
return __awaiter(this, void 0, void 0, function* () {
const destroyedRows = yield data_1.ChargingStationNetworkProfile.destroy({
where: {
stationId: request.query.stationId,
configurationSlot: {
[sequelize_1.Op.in]: request.query.configurationSlot,
},
},
});
return {
success: true,
payload: `${destroyedRows} rows successfully destroyed`,
};
});
}
/**
* Overrides superclass method to generate the URL path based on the input {@link Namespace}
* and the module's endpoint prefix configuration.
*
* @param {Namespace} input - The input {@link Namespace}.
* @return {string} - The generated URL path.
*/
_toDataPath(input) {
const endpointPrefix = this._module.config.modules.configuration.endpointPrefix;
return super._toDataPath(input, endpointPrefix);
}
updatePasswordOnStation(password, stationId, callbackUrl) {
return __awaiter(this, void 0, void 0, function* () {
const correlationId = (0, uuid_1.v4)();
const cacheCallbackPromise = this._module.cache.onChange(correlationId, this._module.config.maxCachingSeconds, stationId);
const messageConfirmation = yield this._module.sendCall(stationId, 'T01', // TODO: adjust when multi-tenancy is implemented
base_1.OCPPVersion.OCPP2_0_1, base_1.OCPP2_0_1_CallAction.SetVariables, {
setVariableData: [
{
variable: { name: 'BasicAuthPassword' },
attributeValue: password,
attributeType: base_1.OCPP2_0_1.AttributeEnumType.Actual,
component: { name: 'SecurityCtrlr' },
},
],
}, callbackUrl, correlationId);
if (!messageConfirmation.success) {
throw new Error(`Failed sending request to ${stationId} station for updating password`);
}
const responseJsonString = yield cacheCallbackPromise;
if (!responseJsonString) {
throw new Error(`${stationId} station did not respond in time for updating password`);
}
const setVariablesResponse = JSON.parse(responseJsonString);
const passwordUpdated = setVariablesResponse.setVariableResult.every((result) => result.attributeStatus === base_1.OCPP2_0_1.SetVariableStatusEnumType.Accepted);
if (!passwordUpdated) {
throw new Error(`Failure updating password on ${stationId} station`);
}
});
}
updatePasswordForStation(password, stationId) {
return __awaiter(this, void 0, void 0, function* () {
const timestamp = new Date().toISOString();
const variableAttributes = yield this._module.deviceModelRepository.createOrUpdateDeviceModelByStationId({
component: {
name: 'SecurityCtrlr',
},
variable: {
name: 'BasicAuthPassword',
},
variableAttribute: [
{
type: base_1.OCPP2_0_1.AttributeEnumType.Actual,
value: password,
mutability: base_1.OCPP2_0_1.MutabilityEnumType.WriteOnly,
},
],
variableCharacteristics: {
dataType: base_1.OCPP2_0_1.DataEnumType.passwordString,
supportsMonitoring: false,
},
}, stationId, timestamp);
for (let variableAttribute of variableAttributes) {
variableAttribute = yield variableAttribute.reload({
include: [data_1.Variable, data_1.Component],
});
yield this._module.deviceModelRepository.updateResultByStationId({
attributeType: variableAttribute.type,
attributeStatus: base_1.OCPP2_0_1.SetVariableStatusEnumType.Accepted,
attributeStatusInfo: { reasonCode: 'SetOnCharger' },
component: variableAttribute.component,
variable: variableAttribute.variable,
}, stationId, timestamp);
}
return variableAttributes;
});
}
}
exports.ConfigurationDataApi = ConfigurationDataApi;
__decorate([
(0, base_1.AsDataEndpoint)(base_1.Namespace.BootConfig, base_1.HttpMethod.Put, data_1.ChargingStationKeyQuerySchema, base_1.BootConfigSchema),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], ConfigurationDataApi.prototype, "putBootConfig", null);
__decorate([
(0, base_1.AsDataEndpoint)(base_1.Namespace.BootConfig, base_1.HttpMethod.Get, data_1.ChargingStationKeyQuerySchema),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], ConfigurationDataApi.prototype, "getBootConfig", null);
__decorate([
(0, base_1.AsDataEndpoint)(base_1.Namespace.BootConfig, base_1.HttpMethod.Delete, data_1.ChargingStationKeyQuerySchema),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], ConfigurationDataApi.prototype, "deleteBootConfig", null);
__decorate([
(0, base_1.AsDataEndpoint)(base_1.OCPP2_0_1_Namespace.PasswordType, base_1.HttpMethod.Post, data_1.UpdateChargingStationPasswordQuerySchema, base_1.UpdateChargingStationPasswordSchema),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], ConfigurationDataApi.prototype, "updatePassword", null);
__decorate([
(0, base_1.AsDataEndpoint)(base_1.OCPP2_0_1_Namespace.ServerNetworkProfile, base_1.HttpMethod.Get, data_1.NetworkProfileQuerySchema),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], ConfigurationDataApi.prototype, "getNetworkProfiles", null);
__decorate([
(0, base_1.AsDataEndpoint)(base_1.OCPP2_0_1_Namespace.ServerNetworkProfile, base_1.HttpMethod.Delete, data_1.NetworkProfileDeleteQuerySchema),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], ConfigurationDataApi.prototype, "deleteNetworkProfiles", null);
//# sourceMappingURL=DataApi.js.map