feat(simulator): wait for transactions end before simulating firmware
[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 type { JSONSchemaType } from 'ajv';
4
5 import type { ChargingStation } from '../../../charging-station';
6 import { OCPPError } from '../../../exception';
7 import {
8 ErrorType,
9 type JsonObject,
10 type JsonType,
11 type OCPP20BootNotificationRequest,
12 type OCPP20HeartbeatRequest,
13 OCPP20RequestCommand,
14 type OCPP20StatusNotificationRequest,
15 OCPPVersion,
16 type RequestParams,
17 } from '../../../types';
18 import { Utils } from '../../../utils';
19 import {
20 OCPP20ServiceUtils,
21 OCPPConstants,
22 OCPPRequestService,
23 type OCPPResponseService,
24 } from '../internal';
25
26 const moduleName = 'OCPP20RequestService';
27
28 export class OCPP20RequestService extends OCPPRequestService {
29 protected 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 // }
35 super(OCPPVersion.VERSION_20, ocppResponseService);
36 this.jsonSchemas = new Map<OCPP20RequestCommand, JSONSchemaType<JsonObject>>([
37 [
38 OCPP20RequestCommand.BOOT_NOTIFICATION,
39 OCPP20ServiceUtils.parseJsonSchemaFile<OCPP20BootNotificationRequest>(
40 '../../../assets/json-schemas/ocpp/2.0/BootNotificationRequest.json',
41 moduleName,
42 'constructor'
43 ),
44 ],
45 [
46 OCPP20RequestCommand.HEARTBEAT,
47 OCPP20ServiceUtils.parseJsonSchemaFile<OCPP20HeartbeatRequest>(
48 '../../../assets/json-schemas/ocpp/2.0/HeartbeatRequest.json',
49 moduleName,
50 'constructor'
51 ),
52 ],
53 [
54 OCPP20RequestCommand.STATUS_NOTIFICATION,
55 OCPP20ServiceUtils.parseJsonSchemaFile<OCPP20StatusNotificationRequest>(
56 '../../../assets/json-schemas/ocpp/2.0/StatusNotificationRequest.json',
57 moduleName,
58 'constructor'
59 ),
60 ],
61 ]);
62 this.buildRequestPayload.bind(this);
63 }
64
65 public async requestHandler<RequestType extends JsonType, ResponseType extends JsonType>(
66 chargingStation: ChargingStation,
67 commandName: OCPP20RequestCommand,
68 commandParams?: JsonType,
69 params?: RequestParams
70 ): Promise<ResponseType> {
71 // FIXME?: add sanity checks on charging station availability, connector availability, connector status, etc.
72 if (OCPP20ServiceUtils.isRequestCommandSupported(chargingStation, commandName) === true) {
73 return (await this.sendMessage(
74 chargingStation,
75 Utils.generateUUID(),
76 this.buildRequestPayload<RequestType>(chargingStation, commandName, commandParams),
77 commandName,
78 params
79 )) as ResponseType;
80 }
81 // OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError().
82 throw new OCPPError(
83 ErrorType.NOT_SUPPORTED,
84 `Unsupported OCPP command '${commandName}'`,
85 commandName,
86 commandParams
87 );
88 }
89
90 private buildRequestPayload<Request extends JsonType>(
91 chargingStation: ChargingStation,
92 commandName: OCPP20RequestCommand,
93 commandParams?: JsonType
94 ): Request {
95 commandParams = commandParams as JsonObject;
96 switch (commandName) {
97 case OCPP20RequestCommand.BOOT_NOTIFICATION:
98 return commandParams as unknown as Request;
99 case OCPP20RequestCommand.HEARTBEAT:
100 return OCPPConstants.OCPP_RESPONSE_EMPTY as unknown as Request;
101 case OCPP20RequestCommand.STATUS_NOTIFICATION:
102 return {
103 timestamp: new Date(),
104 ...commandParams,
105 } as unknown as Request;
106 default:
107 // OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError().
108 throw new OCPPError(
109 ErrorType.NOT_SUPPORTED,
110 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
111 `Unsupported OCPP command '${commandName}'`,
112 commandName,
113 commandParams
114 );
115 }
116 }
117 }