@citrineos/ocpprouter
Version:
The ocpprouter module for OCPP v2.0.1. This module is not intended to be used directly, but rather as a dependency for other modules.
107 lines • 5.39 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.AdminApi = void 0;
const base_1 = require("@citrineos/base");
const data_1 = require("@citrineos/data");
/**
* Admin API for the OcppRouter.
*/
class AdminApi extends base_1.AbstractModuleApi {
/**
* Constructs a new instance of the class.
*
* @param {MessageRouterImpl} ocppRouter - The OcppRouter module.
* @param {FastifyInstance} server - The Fastify server instance.
* @param {Logger<ILogObj>} [logger] - The logger instance.
*/
constructor(ocppRouter, server, logger) {
super(ocppRouter, server, null, logger);
}
// N.B.: When adding subscriptions, chargers may be connected to a different instance of Citrine.
// If this is the case, new subscriptions will not take effect until the charger reconnects.
/**
* Creates a {@link Subscription}.
* Will always create a new entity and return its id.
*
* @param {FastifyRequest<{ Body: Subscription }>} request - The request object, containing the body which is parsed as a {@link Subscription}.
* @return {Promise<number>} The id of the created subscription.
*/
postSubscription(request) {
return __awaiter(this, void 0, void 0, function* () {
if (!request.body.onClose &&
!request.body.onConnect &&
!request.body.onMessage &&
!request.body.sentMessage) {
throw new base_1.BadRequestError('Must specify at least one of onConnect, onClose, onMessage, sentMessage to true.');
}
return this._module.subscriptionRepository
.create(request.body)
.then((subscription) => subscription === null || subscription === void 0 ? void 0 : subscription.id);
});
}
getSubscriptionsByChargingStation(request) {
return __awaiter(this, void 0, void 0, function* () {
return this._module.subscriptionRepository.readAllByStationId(request.query.stationId);
});
}
deleteSubscriptionById(request) {
return __awaiter(this, void 0, void 0, function* () {
return this._module.subscriptionRepository
.deleteByKey(request.query.id.toString())
.then(() => true);
});
}
/**
* 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 = '/ocpprouter';
return super._toDataPath(input, endpointPrefix);
}
}
exports.AdminApi = AdminApi;
__decorate([
(0, base_1.AsDataEndpoint)(base_1.OCPP2_0_1_Namespace.Subscription, base_1.HttpMethod.Post, undefined, data_1.CreateSubscriptionSchema),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], AdminApi.prototype, "postSubscription", null);
__decorate([
(0, base_1.AsDataEndpoint)(base_1.OCPP2_0_1_Namespace.Subscription, base_1.HttpMethod.Get, data_1.ChargingStationKeyQuerySchema),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], AdminApi.prototype, "getSubscriptionsByChargingStation", null);
__decorate([
(0, base_1.AsDataEndpoint)(base_1.OCPP2_0_1_Namespace.Subscription, base_1.HttpMethod.Delete, data_1.ModelKeyQuerystringSchema),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", Promise)
], AdminApi.prototype, "deleteSubscriptionById", null);
//# sourceMappingURL=DataApi.js.map