Migrated OCPI Command endpoint
MODIFY
src/server/ocpi/ocpi-services-impl/ocpi-2.1.1/EMSPService.ts
src/server/ocpi/ocpi-services-impl/ocpi-2.1.1/EMSPService.ts
import AbstractOCPIService from '../../AbstractOCPIService';
-import EMSPCommandsEndpoint from './emsp/EMSPCommandsEndpoint';
import EMSPTariffsEndpoint from './emsp/EMSPTariffsEndpoint';
import OCPIServiceConfiguration from '../../../../types/configuration/OCPIServiceConfiguration';
@@ -9,7 +8,6 @@ export default class EMSPService extends AbstractOCPIService {
public constructor(ocpiRestConfig: OCPIServiceConfiguration) {
super(ocpiRestConfig, EMSPService.PATH, EMSPService.VERSION);
- this.registerEndpoint(new EMSPCommandsEndpoint(this));
this.registerEndpoint(new EMSPTariffsEndpoint(this));
}
}

DELETE
src/server/ocpi/ocpi-services-impl/ocpi-2.1.1/emsp/EMSPCommandsEndpoint.ts
-import { NextFunction, Request, Response } from 'express';
-
-import AbstractEndpoint from '../../AbstractEndpoint';
-import AbstractOCPIService from '../../../AbstractOCPIService';
-import Logging from '../../../../../utils/Logging';
-import { OCPICommandType } from '../../../../../types/ocpi/OCPICommandType';
-import OCPIEndpoint from '../../../../../types/ocpi/OCPIEndpoint';
-import { OCPIResponse } from '../../../../../types/ocpi/OCPIResponse';
-import OCPIUtils from '../../../OCPIUtils';
-import { ServerAction } from '../../../../../types/Server';
-import Tenant from '../../../../../types/Tenant';
-
-const MODULE_NAME = 'EMSPCommandsEndpoint';
-
-export default class EMSPCommandsEndpoint extends AbstractEndpoint {
- public constructor(ocpiService: AbstractOCPIService) {
- super(ocpiService, 'commands');
- }
-
- public async process(req: Request, res: Response, next: NextFunction, tenant: Tenant, ocpiEndpoint: OCPIEndpoint): Promise<OCPIResponse> {
- switch (req.method) {
- case 'POST':
- // Split URL Segments
- // /ocpi/cpo/2.0/commands/{command}
- // eslint-disable-next-line no-case-declarations
- const urlSegment = req.path.substring(1).split('/');
- // Remove action
- urlSegment.shift();
- // Get filters
- // eslint-disable-next-line no-case-declarations
- const command = urlSegment.shift();
- // eslint-disable-next-line no-case-declarations
- const commandId = urlSegment.shift();
- switch (command) {
- case OCPICommandType.START_SESSION:
- case OCPICommandType.STOP_SESSION:
- case OCPICommandType.RESERVE_NOW:
- case OCPICommandType.UNLOCK_CONNECTOR:
- await Logging.logInfo({
- tenantID: tenant.id,
- action: this.getAction(command),
- message: `OCPI response received for Command '${command}' with ID '${commandId}' - No action to be done` ,
- module: MODULE_NAME, method: 'process',
- detailedMessages: { response : req.body, ocpiEndpoint }
- });
- return OCPIUtils.success();
- }
- }
- }
-
- private getAction(command: OCPICommandType): ServerAction {
- switch (command) {
- case OCPICommandType.START_SESSION:
- return ServerAction.OCPI_EMSP_START_SESSION;
- case OCPICommandType.STOP_SESSION:
- return ServerAction.OCPI_EMSP_STOP_SESSION;
- case OCPICommandType.RESERVE_NOW:
- return ServerAction.OCPI_EMSP_RESERVE_NOW;
- case OCPICommandType.UNLOCK_CONNECTOR:
- return ServerAction.OCPI_EMSP_UNLOCK_CONNECTOR;
- }
- }
-}
-

MODIFY
src/server/ocpi/router/cpo/V2.1.1/CPOCommandsRouterV211.ts
src/server/ocpi/router/cpo/V2.1.1/CPOCommandsRouterV211.ts
@@ -13,13 +13,13 @@ export default class CPOCommandsRouterV211 {
}
public buildRoutes(): express.Router {
- this.buildRouteCommands();
+ this.buildRouteCommand();
return this.router;
}
- protected buildRouteCommands(): void {
+ protected buildRouteCommand(): void {
this.router.post(`/${OCPIServerRoute.OCPI_COMMANDS}*`, async (req: Request, res: Response, next: NextFunction) => {
- await RouterUtils.handleServerAction(CPOCommandsService.handleCommands.bind(this), ServerAction.OCPI_CPO_COMMAND, req, res, next);
+ await RouterUtils.handleServerAction(CPOCommandsService.handleCommand.bind(this), ServerAction.OCPI_CPO_COMMAND, req, res, next);
});
}
}

MODIFY
src/server/ocpi/router/emsp/EMSPRouter.ts
src/server/ocpi/router/emsp/EMSPRouter.ts
-import { OCPIServerRoute, ServerAction } from '../../../../types/Server';
/* eslint-disable @typescript-eslint/no-misused-promises */
+import { OCPIServerRoute, ServerAction } from '../../../../types/Server';
import express, { NextFunction, Request, Response } from 'express';
import CPOEMSPCredentialsRouterV211 from '../common/V2.1.1/CPOEMSPCredentialsRouterV211';
import EMSPCdrsRouterV211 from './V2.1.1/EMSPCdrsRouterV211';
+import EMSPCommandsRouterV211 from './V2.1.1/EMSPCommandsRouterV211';
import EMSPLocationsRouterV211 from './V2.1.1/EMSPLocationsRouterV211';
import EMSPSessionsRouterV211 from './V2.1.1/EMSPSessionsRouterV211';
import EMSPTokensRouterV211 from './V2.1.1/EMSPTokensRouterV211';
@@ -35,6 +36,7 @@ export default class EMSPRouter {
new EMSPLocationsRouterV211().buildRoutes(),
new EMSPSessionsRouterV211().buildRoutes(),
new EMSPCdrsRouterV211().buildRoutes(),
+ new EMSPCommandsRouterV211().buildRoutes(),
new CPOEMSPCredentialsRouterV211().buildRoutes(),
]);
}

ADD
src/server/ocpi/router/emsp/V2.1.1/EMSPCommandsRouterV211.ts
+/* eslint-disable @typescript-eslint/no-misused-promises */
+import { OCPIServerRoute, ServerAction } from '../../../../../types/Server';
+import express, { NextFunction, Request, Response } from 'express';
+
+import EMSPCommandsService from '../../../service/emsp/v2.1.1/EMSPCommandsService';
+import RouterUtils from '../../../../../utils/RouterUtils';
+
+export default class EMSPCdrsREMSPCommandsRouterV211uterV211 {
+ private router: express.Router;
+
+ public constructor() {
+ this.router = express.Router();
+ }
+
+ public buildRoutes(): express.Router {
+ this.buildRouteCommand();
+ return this.router;
+ }
+
+ protected buildRouteCommand(): void {
+ this.router.post(`/${OCPIServerRoute.OCPI_COMMANDS}*`, async (req: Request, res: Response, next: NextFunction) => {
+ await RouterUtils.handleServerAction(EMSPCommandsService.handleCommand.bind(this), ServerAction.OCPI_EMSP_COMMAND, req, res, next);
+ });
+ }
+}

MODIFY
src/server/ocpi/service/cpo/v2.1.1/CPOCommandsService.ts
src/server/ocpi/service/cpo/v2.1.1/CPOCommandsService.ts
@@ -29,7 +29,7 @@ import moment from 'moment';
const MODULE_NAME = 'CPOCommandsService';
export default class CPOCommandsService {
- public static async handleCommands(action: ServerAction, req: Request, res: Response, next: NextFunction): Promise<void> {
+ public static async handleCommand(action: ServerAction, req: Request, res: Response, next: NextFunction): Promise<void> {
const { tenant, ocpiEndpoint } = req;
// Split URL Segments
// /ocpi/cpo/2.0/commands/{command}
@@ -42,7 +42,7 @@ export default class CPOCommandsService {
const command = urlSegment.shift();
if (!command) {
throw new AppError({
- module: MODULE_NAME, method: 'handleCommands', action,
+ module: MODULE_NAME, method: 'handleCommand', action,
errorCode: StatusCodes.BAD_REQUEST,
message: 'OCPI Command is not provided',
ocpiError: OCPIStatusCode.CODE_2001_INVALID_PARAMETER_ERROR
@@ -61,7 +61,7 @@ export default class CPOCommandsService {
break;
default:
throw new AppError({
- module: MODULE_NAME, method: 'handleCommands', action,
+ module: MODULE_NAME, method: 'handleCommand', action,
errorCode: StatusCodes.BAD_REQUEST,
message: `OCPI Command '${command}' is unknown`,
ocpiError: OCPIStatusCode.CODE_2001_INVALID_PARAMETER_ERROR

ADD
src/server/ocpi/service/emsp/v2.1.1/EMSPCommandsService.ts
+import { NextFunction, Request, Response } from 'express';
+
+import AppError from '../../../../../exception/AppError';
+import Logging from '../../../../../utils/Logging';
+import { OCPICommandType } from '../../../../../types/ocpi/OCPICommandType';
+import { OCPIStatusCode } from '../../../../../types/ocpi/OCPIStatusCode';
+import OCPIUtils from '../../../OCPIUtils';
+import { ServerAction } from '../../../../../types/Server';
+import { StatusCodes } from 'http-status-codes';
+
+const MODULE_NAME = 'EMSPCommandsService';
+
+export default class EMSPCommandsService {
+ public static async handleCommand(action: ServerAction, req: Request, res: Response, next: NextFunction): Promise<void> {
+ const { tenant } = req;
+ // Split URL Segments
+ // /ocpi/cpo/2.0/commands/{command}
+ // eslint-disable-next-line no-case-declarations
+ const urlSegment = req.path.substring(1).split('/');
+ // Remove action
+ urlSegment.shift();
+ // Get filters
+ // eslint-disable-next-line no-case-declarations
+ const command = urlSegment.shift();
+ if (!command) {
+ throw new AppError({
+ module: MODULE_NAME, method: 'handleCommand', action,
+ errorCode: StatusCodes.BAD_REQUEST,
+ message: 'OCPI Command is not provided',
+ ocpiError: OCPIStatusCode.CODE_2001_INVALID_PARAMETER_ERROR
+ });
+ }
+ // eslint-disable-next-line no-case-declarations
+ const commandId = urlSegment.shift();
+ switch (command) {
+ case OCPICommandType.START_SESSION:
+ case OCPICommandType.STOP_SESSION:
+ case OCPICommandType.RESERVE_NOW:
+ case OCPICommandType.UNLOCK_CONNECTOR:
+ await Logging.logInfo({
+ tenantID: tenant.id,
+ action: EMSPCommandsService.getAction(command),
+ message: `OCPI response received for Command '${command}' with ID '${commandId}' - No action to be done` ,
+ module: MODULE_NAME, method: 'process',
+ detailedMessages: { response: req.body }
+ });
+ res.json(OCPIUtils.success());
+ next();
+ }
+ }
+
+ private static getAction(command: OCPICommandType): ServerAction {
+ switch (command) {
+ case OCPICommandType.START_SESSION:
+ return ServerAction.OCPI_EMSP_START_SESSION;
+ case OCPICommandType.STOP_SESSION:
+ return ServerAction.OCPI_EMSP_STOP_SESSION;
+ case OCPICommandType.RESERVE_NOW:
+ return ServerAction.OCPI_EMSP_RESERVE_NOW;
+ case OCPICommandType.UNLOCK_CONNECTOR:
+ return ServerAction.OCPI_EMSP_UNLOCK_CONNECTOR;
+ }
+ }
+}
+

MODIFY
src/types/Server.ts
src/types/Server.ts
@@ -165,6 +165,7 @@ export enum ServerAction {
OCPI_CPO_AUTHORIZE_TOKEN = 'OcpiCpoAuthorizeToken',
OCPI_CPO_GET_TARIFFS = 'OcpiCpoGetTariffs',
+ OCPI_EMSP_COMMAND = 'OcpiEmspCommand',
OCPI_EMSP_AUTHORIZE_TOKEN = 'OcpiEmspAuthorizeToken',
OCPI_EMSP_UPDATE_LOCATION = 'OcpiEmspUpdateLocation',
OCPI_EMSP_GET_LOCATIONS = 'OcpiEmspGetLocations',

Migrated OCPI Command endpoint