refactor(simulator): switch to named exports
[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';
268a74bb 6import { OCPPError } from '../../../exception';
d270cc87 7import {
268a74bb
JB
8 ErrorType,
9 type JsonObject,
10 type JsonType,
96a52d08 11 type OCPP20BootNotificationRequest,
81533a20 12 type OCPP20HeartbeatRequest,
d270cc87 13 OCPP20RequestCommand,
6e939d9e 14 type OCPP20StatusNotificationRequest,
268a74bb
JB
15 OCPPVersion,
16 type RequestParams,
17} from '../../../types';
18import { Utils } from '../../../utils/Utils';
19import type { ChargingStation } from '../../ChargingStation';
20import { OCPPConstants } from '../OCPPConstants';
21import { OCPPRequestService } from '../OCPPRequestService';
22import type { OCPPResponseService } from '../OCPPResponseService';
953d6b02
JB
23
24const moduleName = 'OCPP20RequestService';
25
268a74bb 26export class OCPP20RequestService extends OCPPRequestService {
b3fc3ff5 27 protected jsonSchemas: Map<OCPP20RequestCommand, JSONSchemaType<JsonObject>>;
953d6b02
JB
28
29 public constructor(ocppResponseService: OCPPResponseService) {
30 if (new.target?.name === moduleName) {
31 throw new TypeError(`Cannot construct ${new.target?.name} instances directly`);
32 }
d270cc87
JB
33 super(OCPPVersion.VERSION_20, ocppResponseService);
34 this.jsonSchemas = new Map<OCPP20RequestCommand, JSONSchemaType<JsonObject>>([
35 [
36 OCPP20RequestCommand.BOOT_NOTIFICATION,
130783a7 37 OCPP20ServiceUtils.parseJsonSchemaFile<OCPP20BootNotificationRequest>(
1b271a54
JB
38 '../../../assets/json-schemas/ocpp/2.0/BootNotificationRequest.json',
39 moduleName,
40 'constructor'
e9a4164c 41 ),
d270cc87 42 ],
81533a20
JB
43 [
44 OCPP20RequestCommand.HEARTBEAT,
130783a7 45 OCPP20ServiceUtils.parseJsonSchemaFile<OCPP20HeartbeatRequest>(
1b271a54
JB
46 '../../../assets/json-schemas/ocpp/2.0/HeartbeatRequest.json',
47 moduleName,
48 'constructor'
e9a4164c 49 ),
81533a20 50 ],
6e939d9e
JB
51 [
52 OCPP20RequestCommand.STATUS_NOTIFICATION,
130783a7 53 OCPP20ServiceUtils.parseJsonSchemaFile<OCPP20StatusNotificationRequest>(
1b271a54
JB
54 '../../../assets/json-schemas/ocpp/2.0/StatusNotificationRequest.json',
55 moduleName,
56 'constructor'
e9a4164c 57 ),
6e939d9e 58 ],
d270cc87 59 ]);
953d6b02 60 this.buildRequestPayload.bind(this);
953d6b02
JB
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) {
953d6b02
JB
70 return (await this.sendMessage(
71 chargingStation,
72 Utils.generateUUID(),
18bf8274 73 this.buildRequestPayload<RequestType>(chargingStation, commandName, commandParams),
953d6b02
JB
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) {
d270cc87 94 case OCPP20RequestCommand.BOOT_NOTIFICATION:
36c462a4 95 return commandParams as unknown as Request;
81533a20 96 case OCPP20RequestCommand.HEARTBEAT:
4d20f040 97 return OCPPConstants.OCPP_RESPONSE_EMPTY as unknown as Request;
6e939d9e
JB
98 case OCPP20RequestCommand.STATUS_NOTIFICATION:
99 return {
36c462a4
JB
100 timestamp: new Date(),
101 ...commandParams,
6e939d9e 102 } as unknown as Request;
953d6b02
JB
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 }
953d6b02 114}