Add error handling to JSON schemas file reading
[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
JB
2
3import type { JSONSchemaType } from 'ajv';
4
78202038 5import { OCPP20ServiceUtils } from './OCPP20ServiceUtils';
953d6b02
JB
6import OCPPError from '../../../exception/OCPPError';
7import type { JsonObject, JsonType } from '../../../types/JsonType';
d270cc87 8import {
96a52d08 9 type OCPP20BootNotificationRequest,
81533a20 10 type OCPP20HeartbeatRequest,
d270cc87 11 OCPP20RequestCommand,
6e939d9e 12 type OCPP20StatusNotificationRequest,
d270cc87 13} from '../../../types/ocpp/2.0/Requests';
953d6b02 14import { ErrorType } from '../../../types/ocpp/ErrorType';
d270cc87 15import { OCPPVersion } from '../../../types/ocpp/OCPPVersion';
953d6b02 16import type { RequestParams } from '../../../types/ocpp/Requests';
953d6b02
JB
17import Utils from '../../../utils/Utils';
18import type ChargingStation from '../../ChargingStation';
4d20f040 19import OCPPConstants from '../OCPPConstants';
953d6b02
JB
20import OCPPRequestService from '../OCPPRequestService';
21import type OCPPResponseService from '../OCPPResponseService';
953d6b02
JB
22
23const moduleName = 'OCPP20RequestService';
24
25export default class OCPP20RequestService extends OCPPRequestService {
b3fc3ff5 26 protected jsonSchemas: Map<OCPP20RequestCommand, JSONSchemaType<JsonObject>>;
953d6b02
JB
27
28 public constructor(ocppResponseService: OCPPResponseService) {
29 if (new.target?.name === moduleName) {
30 throw new TypeError(`Cannot construct ${new.target?.name} instances directly`);
31 }
d270cc87
JB
32 super(OCPPVersion.VERSION_20, ocppResponseService);
33 this.jsonSchemas = new Map<OCPP20RequestCommand, JSONSchemaType<JsonObject>>([
34 [
35 OCPP20RequestCommand.BOOT_NOTIFICATION,
130783a7 36 OCPP20ServiceUtils.parseJsonSchemaFile<OCPP20BootNotificationRequest>(
e9a4164c
JB
37 '../../../assets/json-schemas/ocpp/2.0/BootNotificationRequest.json'
38 ),
d270cc87 39 ],
81533a20
JB
40 [
41 OCPP20RequestCommand.HEARTBEAT,
130783a7 42 OCPP20ServiceUtils.parseJsonSchemaFile<OCPP20HeartbeatRequest>(
e9a4164c
JB
43 '../../../assets/json-schemas/ocpp/2.0/HeartbeatRequest.json'
44 ),
81533a20 45 ],
6e939d9e
JB
46 [
47 OCPP20RequestCommand.STATUS_NOTIFICATION,
130783a7 48 OCPP20ServiceUtils.parseJsonSchemaFile<OCPP20StatusNotificationRequest>(
e9a4164c
JB
49 '../../../assets/json-schemas/ocpp/2.0/StatusNotificationRequest.json'
50 ),
6e939d9e 51 ],
d270cc87 52 ]);
953d6b02 53 this.buildRequestPayload.bind(this);
953d6b02
JB
54 }
55
56 public async requestHandler<RequestType extends JsonType, ResponseType extends JsonType>(
57 chargingStation: ChargingStation,
58 commandName: OCPP20RequestCommand,
59 commandParams?: JsonType,
60 params?: RequestParams
61 ): Promise<ResponseType> {
62 if (OCPP20ServiceUtils.isRequestCommandSupported(chargingStation, commandName) === true) {
953d6b02
JB
63 return (await this.sendMessage(
64 chargingStation,
65 Utils.generateUUID(),
18bf8274 66 this.buildRequestPayload<RequestType>(chargingStation, commandName, commandParams),
953d6b02
JB
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:
36c462a4 88 return commandParams as unknown as Request;
81533a20 89 case OCPP20RequestCommand.HEARTBEAT:
4d20f040 90 return OCPPConstants.OCPP_RESPONSE_EMPTY as unknown as Request;
6e939d9e
JB
91 case OCPP20RequestCommand.STATUS_NOTIFICATION:
92 return {
36c462a4
JB
93 timestamp: new Date(),
94 ...commandParams,
6e939d9e 95 } as unknown as Request;
953d6b02
JB
96 default:
97 // OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError().
98 throw new OCPPError(
99 ErrorType.NOT_SUPPORTED,
100 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
101 `Unsupported OCPP command '${commandName}'`,
102 commandName,
103 commandParams
104 );
105 }
106 }
953d6b02 107}