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