]> Piment Noir Git Repositories - e-mobility-charging-stations-simulator.git/blame - src/charging-station/ocpp/2.0/OCPP20RequestService.ts
chore(deps-dev): apply updates
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / 2.0 / OCPP20RequestService.ts
CommitLineData
a19b897d 1// Partial Copyright Jerome Benoit. 2021-2024. All Rights Reserved.
953d6b02 2
24d15716 3import type { ValidateFunction } from 'ajv'
953d6b02 4
66a7748d 5import type { ChargingStation } from '../../../charging-station/index.js'
0749233f
JB
6import type { OCPPResponseService } from '../OCPPResponseService.js'
7
66a7748d 8import { OCPPError } from '../../../exception/index.js'
d270cc87 9import {
268a74bb
JB
10 ErrorType,
11 type JsonObject,
12 type JsonType,
96a52d08 13 type OCPP20BootNotificationRequest,
81533a20 14 type OCPP20HeartbeatRequest,
d270cc87 15 OCPP20RequestCommand,
6e939d9e 16 type OCPP20StatusNotificationRequest,
268a74bb 17 OCPPVersion,
d1f5bfd8 18 type RequestParams,
66a7748d
JB
19} from '../../../types/index.js'
20import { generateUUID } from '../../../utils/index.js'
21import { OCPPRequestService } from '../OCPPRequestService.js'
4c3f6c20
JB
22import { OCPP20Constants } from './OCPP20Constants.js'
23import { OCPP20ServiceUtils } from './OCPP20ServiceUtils.js'
953d6b02 24
66a7748d 25const moduleName = 'OCPP20RequestService'
953d6b02 26
268a74bb 27export class OCPP20RequestService extends OCPPRequestService {
d5490a13 28 protected payloadValidateFunctions: Map<OCPP20RequestCommand, ValidateFunction<JsonType>>
953d6b02 29
66a7748d 30 public constructor (ocppResponseService: OCPPResponseService) {
5199f9fd
JB
31 // if (new.target.name === moduleName) {
32 // throw new TypeError(`Cannot construct ${new.target.name} instances directly`)
b768993d 33 // }
1feac591 34 super(OCPPVersion.VERSION_201, ocppResponseService)
d5490a13 35 this.payloadValidateFunctions = new Map<OCPP20RequestCommand, ValidateFunction<JsonType>>([
d270cc87
JB
36 [
37 OCPP20RequestCommand.BOOT_NOTIFICATION,
d712a9a3
JB
38 this.ajv.compile(
39 OCPP20ServiceUtils.parseJsonSchemaFile<OCPP20BootNotificationRequest>(
40 'assets/json-schemas/ocpp/2.0/BootNotificationRequest.json',
41 moduleName,
42 'constructor'
43 )
44 ),
d270cc87 45 ],
81533a20
JB
46 [
47 OCPP20RequestCommand.HEARTBEAT,
d712a9a3
JB
48 this.ajv.compile(
49 OCPP20ServiceUtils.parseJsonSchemaFile<OCPP20HeartbeatRequest>(
50 'assets/json-schemas/ocpp/2.0/HeartbeatRequest.json',
51 moduleName,
52 'constructor'
53 )
54 ),
81533a20 55 ],
6e939d9e
JB
56 [
57 OCPP20RequestCommand.STATUS_NOTIFICATION,
d712a9a3
JB
58 this.ajv.compile(
59 OCPP20ServiceUtils.parseJsonSchemaFile<OCPP20StatusNotificationRequest>(
60 'assets/json-schemas/ocpp/2.0/StatusNotificationRequest.json',
61 moduleName,
62 'constructor'
63 )
64 ),
d1f5bfd8 65 ],
66a7748d 66 ])
ba9a56a6 67 this.buildRequestPayload = this.buildRequestPayload.bind(this)
953d6b02
JB
68 }
69
c4a89082
JB
70 // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-parameters
71 public async requestHandler<RequestType extends JsonType, ResponseType extends JsonType>(
72 chargingStation: ChargingStation,
73 commandName: OCPP20RequestCommand,
74 commandParams?: RequestType,
75 params?: RequestParams
76 ): Promise<ResponseType> {
77 // FIXME?: add sanity checks on charging station availability, connector availability, connector status, etc.
78 if (OCPP20ServiceUtils.isRequestCommandSupported(chargingStation, commandName)) {
79 // TODO: pre request actions hook
80 return (await this.sendMessage(
81 chargingStation,
82 generateUUID(),
83 this.buildRequestPayload<RequestType>(chargingStation, commandName, commandParams),
84 commandName,
85 params
86 )) as ResponseType
87 }
88 // OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError().
89 throw new OCPPError(
90 ErrorType.NOT_SUPPORTED,
91 `Unsupported OCPP command ${commandName}`,
92 commandName,
93 commandParams
94 )
95 }
96
01841aad 97 // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-parameters
953d6b02
JB
98 private buildRequestPayload<Request extends JsonType>(
99 chargingStation: ChargingStation,
100 commandName: OCPP20RequestCommand,
66a7748d 101 commandParams?: JsonType
953d6b02 102 ): Request {
66a7748d 103 commandParams = commandParams as JsonObject
953d6b02 104 switch (commandName) {
d270cc87 105 case OCPP20RequestCommand.BOOT_NOTIFICATION:
66a7748d 106 return commandParams as unknown as Request
81533a20 107 case OCPP20RequestCommand.HEARTBEAT:
66a7748d 108 return OCPP20Constants.OCPP_RESPONSE_EMPTY as unknown as Request
6e939d9e
JB
109 case OCPP20RequestCommand.STATUS_NOTIFICATION:
110 return {
36c462a4 111 timestamp: new Date(),
d1f5bfd8 112 ...commandParams,
66a7748d 113 } as unknown as Request
953d6b02
JB
114 default:
115 // OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError().
116 throw new OCPPError(
117 ErrorType.NOT_SUPPORTED,
118 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
3024d5b2 119 `Unsupported OCPP command ${commandName}`,
953d6b02 120 commandName,
66a7748d
JB
121 commandParams
122 )
953d6b02
JB
123 }
124 }
953d6b02 125}