Fixes to OCPP commands PDU validation code:
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / 2.0 / OCPP20RequestService.ts
1 // Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
2
3 import fs from 'fs';
4 import path from 'path';
5 import { fileURLToPath } from 'url';
6
7 import type { JSONSchemaType } from 'ajv';
8
9 import OCPPError from '../../../exception/OCPPError';
10 import type { JsonObject, JsonType } from '../../../types/JsonType';
11 import {
12 type OCPP20BootNotificationRequest,
13 OCPP20RequestCommand,
14 } from '../../../types/ocpp/2.0/Requests';
15 import { ErrorType } from '../../../types/ocpp/ErrorType';
16 import { OCPPVersion } from '../../../types/ocpp/OCPPVersion';
17 import type { RequestParams } from '../../../types/ocpp/Requests';
18 import Utils from '../../../utils/Utils';
19 import type ChargingStation from '../../ChargingStation';
20 import OCPPRequestService from '../OCPPRequestService';
21 import type OCPPResponseService from '../OCPPResponseService';
22 import { OCPP20ServiceUtils } from './OCPP20ServiceUtils';
23
24 const moduleName = 'OCPP20RequestService';
25
26 export default class OCPP20RequestService extends OCPPRequestService {
27 protected jsonSchemas: Map<OCPP20RequestCommand, JSONSchemaType<JsonObject>>;
28
29 public constructor(ocppResponseService: OCPPResponseService) {
30 if (new.target?.name === moduleName) {
31 throw new TypeError(`Cannot construct ${new.target?.name} instances directly`);
32 }
33 super(OCPPVersion.VERSION_20, ocppResponseService);
34 this.jsonSchemas = new Map<OCPP20RequestCommand, JSONSchemaType<JsonObject>>([
35 [
36 OCPP20RequestCommand.BOOT_NOTIFICATION,
37 JSON.parse(
38 fs.readFileSync(
39 path.resolve(
40 path.dirname(fileURLToPath(import.meta.url)),
41 '../../../assets/json-schemas/ocpp/2.0/BootNotificationRequest.json'
42 ),
43 'utf8'
44 )
45 ) as JSONSchemaType<OCPP20BootNotificationRequest>,
46 ],
47 ]);
48 this.buildRequestPayload.bind(this);
49 }
50
51 public async requestHandler<RequestType extends JsonType, ResponseType extends JsonType>(
52 chargingStation: ChargingStation,
53 commandName: OCPP20RequestCommand,
54 commandParams?: JsonType,
55 params?: RequestParams
56 ): Promise<ResponseType> {
57 if (OCPP20ServiceUtils.isRequestCommandSupported(chargingStation, commandName) === true) {
58 const requestPayload = this.buildRequestPayload<RequestType>(
59 chargingStation,
60 commandName,
61 commandParams
62 );
63 return (await this.sendMessage(
64 chargingStation,
65 Utils.generateUUID(),
66 requestPayload,
67 commandName,
68 params
69 )) as unknown as ResponseType;
70 }
71 // OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError().
72 throw new OCPPError(
73 ErrorType.NOT_SUPPORTED,
74 `Unsupported OCPP command '${commandName}'`,
75 commandName,
76 commandParams
77 );
78 }
79
80 private buildRequestPayload<Request extends JsonType>(
81 chargingStation: ChargingStation,
82 commandName: OCPP20RequestCommand,
83 commandParams?: JsonType
84 ): Request {
85 commandParams = commandParams as JsonObject;
86 switch (commandName) {
87 case OCPP20RequestCommand.BOOT_NOTIFICATION:
88 commandParams.chargingStation = commandParams.chargingStation as JsonObject;
89 commandParams.chargingStation.modem = commandParams.chargingStation.modem as JsonObject;
90 return {
91 reason: commandParams?.reason,
92 chargingStation: {
93 model: commandParams?.chargingStation?.model,
94 vendorName: commandParams?.chargingStation?.vendorName,
95 ...(!Utils.isUndefined(commandParams?.chargingStation?.firmwareVersion) && {
96 firmwareVersion: commandParams.chargingStation?.firmwareVersion,
97 }),
98 ...(!Utils.isUndefined(commandParams?.chargingStation?.serialNumber) && {
99 serialNumber: commandParams.chargingStation?.serialNumber,
100 }),
101 ...(!Utils.isUndefined(commandParams?.chargingStation?.modem) && {
102 modem: {
103 ...(!Utils.isUndefined(commandParams?.chargingStation?.modem?.iccid) && {
104 iccid: commandParams.chargingStation.modem.iccid,
105 }),
106 ...(!Utils.isUndefined(commandParams?.chargingStation?.modem?.imsi) && {
107 imsi: commandParams.chargingStation.modem.imsi,
108 }),
109 },
110 }),
111 },
112 } as unknown as Request;
113 default:
114 // OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError().
115 throw new OCPPError(
116 ErrorType.NOT_SUPPORTED,
117 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
118 `Unsupported OCPP command '${commandName}'`,
119 commandName,
120 commandParams
121 );
122 }
123 }
124 }