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