Fix timestamping at OCPP firmware update command handling
[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
78202038 9import { OCPP20ServiceUtils } from './OCPP20ServiceUtils';
953d6b02
JB
10import OCPPError from '../../../exception/OCPPError';
11import type { JsonObject, JsonType } from '../../../types/JsonType';
d270cc87 12import {
96a52d08 13 type OCPP20BootNotificationRequest,
81533a20 14 type OCPP20HeartbeatRequest,
d270cc87 15 OCPP20RequestCommand,
6e939d9e 16 type OCPP20StatusNotificationRequest,
d270cc87 17} from '../../../types/ocpp/2.0/Requests';
953d6b02 18import { ErrorType } from '../../../types/ocpp/ErrorType';
d270cc87 19import { OCPPVersion } from '../../../types/ocpp/OCPPVersion';
953d6b02 20import type { RequestParams } from '../../../types/ocpp/Requests';
953d6b02
JB
21import Utils from '../../../utils/Utils';
22import type ChargingStation from '../../ChargingStation';
4d20f040 23import OCPPConstants from '../OCPPConstants';
953d6b02
JB
24import OCPPRequestService from '../OCPPRequestService';
25import type OCPPResponseService from '../OCPPResponseService';
953d6b02
JB
26
27const moduleName = 'OCPP20RequestService';
28
29export default class OCPP20RequestService extends OCPPRequestService {
b3fc3ff5 30 protected jsonSchemas: Map<OCPP20RequestCommand, JSONSchemaType<JsonObject>>;
953d6b02
JB
31
32 public constructor(ocppResponseService: OCPPResponseService) {
33 if (new.target?.name === moduleName) {
34 throw new TypeError(`Cannot construct ${new.target?.name} instances directly`);
35 }
d270cc87
JB
36 super(OCPPVersion.VERSION_20, ocppResponseService);
37 this.jsonSchemas = new Map<OCPP20RequestCommand, JSONSchemaType<JsonObject>>([
38 [
39 OCPP20RequestCommand.BOOT_NOTIFICATION,
e9a4164c
JB
40 this.parseJsonSchemaFile<OCPP20BootNotificationRequest>(
41 '../../../assets/json-schemas/ocpp/2.0/BootNotificationRequest.json'
42 ),
d270cc87 43 ],
81533a20
JB
44 [
45 OCPP20RequestCommand.HEARTBEAT,
e9a4164c
JB
46 this.parseJsonSchemaFile<OCPP20HeartbeatRequest>(
47 '../../../assets/json-schemas/ocpp/2.0/HeartbeatRequest.json'
48 ),
81533a20 49 ],
6e939d9e
JB
50 [
51 OCPP20RequestCommand.STATUS_NOTIFICATION,
e9a4164c
JB
52 this.parseJsonSchemaFile<OCPP20StatusNotificationRequest>(
53 '../../../assets/json-schemas/ocpp/2.0/StatusNotificationRequest.json'
54 ),
6e939d9e 55 ],
d270cc87 56 ]);
953d6b02 57 this.buildRequestPayload.bind(this);
953d6b02
JB
58 }
59
60 public async requestHandler<RequestType extends JsonType, ResponseType extends JsonType>(
61 chargingStation: ChargingStation,
62 commandName: OCPP20RequestCommand,
63 commandParams?: JsonType,
64 params?: RequestParams
65 ): Promise<ResponseType> {
66 if (OCPP20ServiceUtils.isRequestCommandSupported(chargingStation, commandName) === true) {
67 const requestPayload = this.buildRequestPayload<RequestType>(
68 chargingStation,
69 commandName,
70 commandParams
71 );
953d6b02
JB
72 return (await this.sendMessage(
73 chargingStation,
74 Utils.generateUUID(),
75 requestPayload,
76 commandName,
77 params
78 )) as unknown as ResponseType;
79 }
80 // OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError().
81 throw new OCPPError(
82 ErrorType.NOT_SUPPORTED,
83 `Unsupported OCPP command '${commandName}'`,
84 commandName,
85 commandParams
86 );
87 }
88
89 private buildRequestPayload<Request extends JsonType>(
90 chargingStation: ChargingStation,
91 commandName: OCPP20RequestCommand,
92 commandParams?: JsonType
93 ): Request {
94 commandParams = commandParams as JsonObject;
95 switch (commandName) {
d270cc87 96 case OCPP20RequestCommand.BOOT_NOTIFICATION:
36c462a4 97 return commandParams as unknown as Request;
81533a20 98 case OCPP20RequestCommand.HEARTBEAT:
4d20f040 99 return OCPPConstants.OCPP_RESPONSE_EMPTY as unknown as Request;
6e939d9e
JB
100 case OCPP20RequestCommand.STATUS_NOTIFICATION:
101 return {
36c462a4
JB
102 timestamp: new Date(),
103 ...commandParams,
6e939d9e 104 } as unknown as Request;
953d6b02
JB
105 default:
106 // OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError().
107 throw new OCPPError(
108 ErrorType.NOT_SUPPORTED,
109 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
110 `Unsupported OCPP command '${commandName}'`,
111 commandName,
112 commandParams
113 );
114 }
115 }
e9a4164c
JB
116
117 private parseJsonSchemaFile<T extends JsonType>(relativePath: string): JSONSchemaType<T> {
118 return JSON.parse(
119 fs.readFileSync(
120 path.resolve(path.dirname(fileURLToPath(import.meta.url)), relativePath),
121 'utf8'
122 )
123 ) as JSONSchemaType<T>;
124 }
953d6b02 125}