refactor(simulator): switch to named exports
[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 { OCPP20ServiceUtils } from './OCPP20ServiceUtils';
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/Utils';
19 import type { ChargingStation } from '../../ChargingStation';
20 import { OCPPConstants } from '../OCPPConstants';
21 import { OCPPRequestService } from '../OCPPRequestService';
22 import type { OCPPResponseService } from '../OCPPResponseService';
23
24 const moduleName = 'OCPP20RequestService';
25
26 export class OCPP20RequestService extends OCPPRequestService {
27 protected jsonSchemas: Map<OCPP20RequestCommand, JSONSchemaType<JsonObject>>;
28
29 public constructor(ocppResponseService: OCPPResponseService) {
30 if (new.target?.name === moduleName) {
31 throw new TypeError(`Cannot construct ${new.target?.name} instances directly`);
32 }
33 super(OCPPVersion.VERSION_20, ocppResponseService);
34 this.jsonSchemas = new Map<OCPP20RequestCommand, JSONSchemaType<JsonObject>>([
35 [
36 OCPP20RequestCommand.BOOT_NOTIFICATION,
37 OCPP20ServiceUtils.parseJsonSchemaFile<OCPP20BootNotificationRequest>(
38 '../../../assets/json-schemas/ocpp/2.0/BootNotificationRequest.json',
39 moduleName,
40 'constructor'
41 ),
42 ],
43 [
44 OCPP20RequestCommand.HEARTBEAT,
45 OCPP20ServiceUtils.parseJsonSchemaFile<OCPP20HeartbeatRequest>(
46 '../../../assets/json-schemas/ocpp/2.0/HeartbeatRequest.json',
47 moduleName,
48 'constructor'
49 ),
50 ],
51 [
52 OCPP20RequestCommand.STATUS_NOTIFICATION,
53 OCPP20ServiceUtils.parseJsonSchemaFile<OCPP20StatusNotificationRequest>(
54 '../../../assets/json-schemas/ocpp/2.0/StatusNotificationRequest.json',
55 moduleName,
56 'constructor'
57 ),
58 ],
59 ]);
60 this.buildRequestPayload.bind(this);
61 }
62
63 public async requestHandler<RequestType extends JsonType, ResponseType extends JsonType>(
64 chargingStation: ChargingStation,
65 commandName: OCPP20RequestCommand,
66 commandParams?: JsonType,
67 params?: RequestParams
68 ): Promise<ResponseType> {
69 if (OCPP20ServiceUtils.isRequestCommandSupported(chargingStation, commandName) === true) {
70 return (await this.sendMessage(
71 chargingStation,
72 Utils.generateUUID(),
73 this.buildRequestPayload<RequestType>(chargingStation, commandName, commandParams),
74 commandName,
75 params
76 )) as unknown as ResponseType;
77 }
78 // OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError().
79 throw new OCPPError(
80 ErrorType.NOT_SUPPORTED,
81 `Unsupported OCPP command '${commandName}'`,
82 commandName,
83 commandParams
84 );
85 }
86
87 private buildRequestPayload<Request extends JsonType>(
88 chargingStation: ChargingStation,
89 commandName: OCPP20RequestCommand,
90 commandParams?: JsonType
91 ): Request {
92 commandParams = commandParams as JsonObject;
93 switch (commandName) {
94 case OCPP20RequestCommand.BOOT_NOTIFICATION:
95 return commandParams as unknown as Request;
96 case OCPP20RequestCommand.HEARTBEAT:
97 return OCPPConstants.OCPP_RESPONSE_EMPTY as unknown as Request;
98 case OCPP20RequestCommand.STATUS_NOTIFICATION:
99 return {
100 timestamp: new Date(),
101 ...commandParams,
102 } as unknown as Request;
103 default:
104 // OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError().
105 throw new OCPPError(
106 ErrorType.NOT_SUPPORTED,
107 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
108 `Unsupported OCPP command '${commandName}'`,
109 commandName,
110 commandParams
111 );
112 }
113 }
114 }