Commit | Line | Data |
---|---|---|
edd13439 | 1 | // Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved. |
953d6b02 JB |
2 | |
3 | import type { JSONSchemaType } from 'ajv'; | |
4 | ||
2896e06d | 5 | import type { ChargingStation } from '../../../charging-station'; |
268a74bb | 6 | import { OCPPError } from '../../../exception'; |
d270cc87 | 7 | import { |
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'; | |
60a74391 | 18 | import { Utils } from '../../../utils'; |
2896e06d | 19 | import { |
d8b1fab1 | 20 | OCPP20Constants, |
2896e06d | 21 | OCPP20ServiceUtils, |
2896e06d JB |
22 | OCPPRequestService, |
23 | type OCPPResponseService, | |
24 | } from '../internal'; | |
953d6b02 JB |
25 | |
26 | const moduleName = 'OCPP20RequestService'; | |
27 | ||
268a74bb | 28 | export class OCPP20RequestService extends OCPPRequestService { |
b3fc3ff5 | 29 | protected jsonSchemas: Map<OCPP20RequestCommand, JSONSchemaType<JsonObject>>; |
953d6b02 JB |
30 | |
31 | public constructor(ocppResponseService: OCPPResponseService) { | |
b768993d JB |
32 | // if (new.target?.name === moduleName) { |
33 | // throw new TypeError(`Cannot construct ${new.target?.name} instances directly`); | |
34 | // } | |
d270cc87 JB |
35 | super(OCPPVersion.VERSION_20, ocppResponseService); |
36 | this.jsonSchemas = new Map<OCPP20RequestCommand, JSONSchemaType<JsonObject>>([ | |
37 | [ | |
38 | OCPP20RequestCommand.BOOT_NOTIFICATION, | |
130783a7 | 39 | OCPP20ServiceUtils.parseJsonSchemaFile<OCPP20BootNotificationRequest>( |
1b271a54 JB |
40 | '../../../assets/json-schemas/ocpp/2.0/BootNotificationRequest.json', |
41 | moduleName, | |
42 | 'constructor' | |
e9a4164c | 43 | ), |
d270cc87 | 44 | ], |
81533a20 JB |
45 | [ |
46 | OCPP20RequestCommand.HEARTBEAT, | |
130783a7 | 47 | OCPP20ServiceUtils.parseJsonSchemaFile<OCPP20HeartbeatRequest>( |
1b271a54 JB |
48 | '../../../assets/json-schemas/ocpp/2.0/HeartbeatRequest.json', |
49 | moduleName, | |
50 | 'constructor' | |
e9a4164c | 51 | ), |
81533a20 | 52 | ], |
6e939d9e JB |
53 | [ |
54 | OCPP20RequestCommand.STATUS_NOTIFICATION, | |
130783a7 | 55 | OCPP20ServiceUtils.parseJsonSchemaFile<OCPP20StatusNotificationRequest>( |
1b271a54 JB |
56 | '../../../assets/json-schemas/ocpp/2.0/StatusNotificationRequest.json', |
57 | moduleName, | |
58 | 'constructor' | |
e9a4164c | 59 | ), |
6e939d9e | 60 | ], |
d270cc87 | 61 | ]); |
31f59c6d JB |
62 | this.buildRequestPayload = this.buildRequestPayload.bind(this) as <Request extends JsonType>( |
63 | chargingStation: ChargingStation, | |
64 | commandName: OCPP20RequestCommand, | |
65 | commandParams?: JsonType | |
66 | ) => Request; | |
953d6b02 JB |
67 | } |
68 | ||
69 | public async requestHandler<RequestType extends JsonType, ResponseType extends JsonType>( | |
70 | chargingStation: ChargingStation, | |
71 | commandName: OCPP20RequestCommand, | |
72 | commandParams?: JsonType, | |
73 | params?: RequestParams | |
74 | ): Promise<ResponseType> { | |
62340a29 | 75 | // FIXME?: add sanity checks on charging station availability, connector availability, connector status, etc. |
953d6b02 | 76 | if (OCPP20ServiceUtils.isRequestCommandSupported(chargingStation, commandName) === true) { |
953d6b02 JB |
77 | return (await this.sendMessage( |
78 | chargingStation, | |
79 | Utils.generateUUID(), | |
18bf8274 | 80 | this.buildRequestPayload<RequestType>(chargingStation, commandName, commandParams), |
953d6b02 JB |
81 | commandName, |
82 | params | |
617cad0c | 83 | )) as ResponseType; |
953d6b02 JB |
84 | } |
85 | // OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError(). | |
86 | throw new OCPPError( | |
87 | ErrorType.NOT_SUPPORTED, | |
88 | `Unsupported OCPP command '${commandName}'`, | |
89 | commandName, | |
90 | commandParams | |
91 | ); | |
92 | } | |
93 | ||
94 | private buildRequestPayload<Request extends JsonType>( | |
95 | chargingStation: ChargingStation, | |
96 | commandName: OCPP20RequestCommand, | |
97 | commandParams?: JsonType | |
98 | ): Request { | |
99 | commandParams = commandParams as JsonObject; | |
100 | switch (commandName) { | |
d270cc87 | 101 | case OCPP20RequestCommand.BOOT_NOTIFICATION: |
36c462a4 | 102 | return commandParams as unknown as Request; |
81533a20 | 103 | case OCPP20RequestCommand.HEARTBEAT: |
d8b1fab1 | 104 | return OCPP20Constants.OCPP_RESPONSE_EMPTY as unknown as Request; |
6e939d9e JB |
105 | case OCPP20RequestCommand.STATUS_NOTIFICATION: |
106 | return { | |
36c462a4 JB |
107 | timestamp: new Date(), |
108 | ...commandParams, | |
6e939d9e | 109 | } as unknown as Request; |
953d6b02 JB |
110 | default: |
111 | // OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError(). | |
112 | throw new OCPPError( | |
113 | ErrorType.NOT_SUPPORTED, | |
114 | // eslint-disable-next-line @typescript-eslint/restrict-template-expressions | |
115 | `Unsupported OCPP command '${commandName}'`, | |
116 | commandName, | |
117 | commandParams | |
118 | ); | |
119 | } | |
120 | } | |
953d6b02 | 121 | } |