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