Factor out some common code between OCPP 1.6 and 2.0.1 stack
[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 11import {
96a52d08 12 type OCPP20BootNotificationRequest,
d270cc87
JB
13 OCPP20RequestCommand,
14} from '../../../types/ocpp/2.0/Requests';
953d6b02 15import { ErrorType } from '../../../types/ocpp/ErrorType';
d270cc87 16import { OCPPVersion } from '../../../types/ocpp/OCPPVersion';
953d6b02 17import type { RequestParams } from '../../../types/ocpp/Requests';
953d6b02
JB
18import Utils from '../../../utils/Utils';
19import type ChargingStation from '../../ChargingStation';
20import OCPPRequestService from '../OCPPRequestService';
21import type OCPPResponseService from '../OCPPResponseService';
22import { OCPP20ServiceUtils } from './OCPP20ServiceUtils';
23
24const moduleName = 'OCPP20RequestService';
25
26export default class OCPP20RequestService extends OCPPRequestService {
b3fc3ff5 27 protected jsonSchemas: Map<OCPP20RequestCommand, JSONSchemaType<JsonObject>>;
953d6b02
JB
28
29 public constructor(ocppResponseService: OCPPResponseService) {
30 if (new.target?.name === moduleName) {
31 throw new TypeError(`Cannot construct ${new.target?.name} instances directly`);
32 }
d270cc87
JB
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 ]);
953d6b02 48 this.buildRequestPayload.bind(this);
953d6b02
JB
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 );
953d6b02
JB
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) {
d270cc87 87 case OCPP20RequestCommand.BOOT_NOTIFICATION:
98fc1389
JB
88 commandParams.chargingStation = commandParams.chargingStation as JsonObject;
89 commandParams.chargingStation.modem = commandParams.chargingStation.modem as JsonObject;
d270cc87
JB
90 return {
91 reason: commandParams?.reason,
92 chargingStation: {
98fc1389
JB
93 model: commandParams?.chargingStation?.model,
94 vendorName: commandParams?.chargingStation?.vendorName,
95 ...(!Utils.isUndefined(commandParams?.chargingStation?.firmwareVersion) && {
96 firmwareVersion: commandParams.chargingStation?.firmwareVersion,
d270cc87 97 }),
98fc1389
JB
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 },
d270cc87 110 }),
d270cc87
JB
111 },
112 } as unknown as Request;
953d6b02
JB
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 }
953d6b02 124}