Commit | Line | Data |
---|---|---|
edd13439 | 1 | // Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved. |
953d6b02 | 2 | |
d270cc87 JB |
3 | import fs from 'fs'; |
4 | import path from 'path'; | |
5 | import { fileURLToPath } from 'url'; | |
6 | ||
953d6b02 JB |
7 | import type { JSONSchemaType } from 'ajv'; |
8 | ||
9 | import OCPPError from '../../../exception/OCPPError'; | |
10 | import type { JsonObject, JsonType } from '../../../types/JsonType'; | |
d270cc87 | 11 | import { |
96a52d08 | 12 | type OCPP20BootNotificationRequest, |
81533a20 | 13 | type OCPP20HeartbeatRequest, |
d270cc87 | 14 | OCPP20RequestCommand, |
6e939d9e | 15 | type OCPP20StatusNotificationRequest, |
d270cc87 | 16 | } from '../../../types/ocpp/2.0/Requests'; |
953d6b02 | 17 | import { ErrorType } from '../../../types/ocpp/ErrorType'; |
d270cc87 | 18 | import { OCPPVersion } from '../../../types/ocpp/OCPPVersion'; |
953d6b02 | 19 | import type { RequestParams } from '../../../types/ocpp/Requests'; |
953d6b02 JB |
20 | import Utils from '../../../utils/Utils'; |
21 | import type ChargingStation from '../../ChargingStation'; | |
22 | import OCPPRequestService from '../OCPPRequestService'; | |
23 | import type OCPPResponseService from '../OCPPResponseService'; | |
24 | import { OCPP20ServiceUtils } from './OCPP20ServiceUtils'; | |
25 | ||
26 | const moduleName = 'OCPP20RequestService'; | |
27 | ||
28 | export default class OCPP20RequestService extends OCPPRequestService { | |
b3fc3ff5 | 29 | protected jsonSchemas: Map<OCPP20RequestCommand, JSONSchemaType<JsonObject>>; |
953d6b02 JB |
30 | |
31 | public constructor(ocppResponseService: OCPPResponseService) { | |
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, | |
e9a4164c JB |
39 | this.parseJsonSchemaFile<OCPP20BootNotificationRequest>( |
40 | '../../../assets/json-schemas/ocpp/2.0/BootNotificationRequest.json' | |
41 | ), | |
d270cc87 | 42 | ], |
81533a20 JB |
43 | [ |
44 | OCPP20RequestCommand.HEARTBEAT, | |
e9a4164c JB |
45 | this.parseJsonSchemaFile<OCPP20HeartbeatRequest>( |
46 | '../../../assets/json-schemas/ocpp/2.0/HeartbeatRequest.json' | |
47 | ), | |
81533a20 | 48 | ], |
6e939d9e JB |
49 | [ |
50 | OCPP20RequestCommand.STATUS_NOTIFICATION, | |
e9a4164c JB |
51 | this.parseJsonSchemaFile<OCPP20StatusNotificationRequest>( |
52 | '../../../assets/json-schemas/ocpp/2.0/StatusNotificationRequest.json' | |
53 | ), | |
6e939d9e | 54 | ], |
d270cc87 | 55 | ]); |
953d6b02 | 56 | this.buildRequestPayload.bind(this); |
953d6b02 JB |
57 | } |
58 | ||
59 | public async requestHandler<RequestType extends JsonType, ResponseType extends JsonType>( | |
60 | chargingStation: ChargingStation, | |
61 | commandName: OCPP20RequestCommand, | |
62 | commandParams?: JsonType, | |
63 | params?: RequestParams | |
64 | ): Promise<ResponseType> { | |
65 | if (OCPP20ServiceUtils.isRequestCommandSupported(chargingStation, commandName) === true) { | |
66 | const requestPayload = this.buildRequestPayload<RequestType>( | |
67 | chargingStation, | |
68 | commandName, | |
69 | commandParams | |
70 | ); | |
953d6b02 JB |
71 | return (await this.sendMessage( |
72 | chargingStation, | |
73 | Utils.generateUUID(), | |
74 | requestPayload, | |
75 | commandName, | |
76 | params | |
77 | )) as unknown as ResponseType; | |
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, | |
84 | commandParams | |
85 | ); | |
86 | } | |
87 | ||
88 | private buildRequestPayload<Request extends JsonType>( | |
89 | chargingStation: ChargingStation, | |
90 | commandName: OCPP20RequestCommand, | |
91 | commandParams?: JsonType | |
92 | ): Request { | |
93 | commandParams = commandParams as JsonObject; | |
94 | switch (commandName) { | |
d270cc87 | 95 | case OCPP20RequestCommand.BOOT_NOTIFICATION: |
98fc1389 JB |
96 | commandParams.chargingStation = commandParams.chargingStation as JsonObject; |
97 | commandParams.chargingStation.modem = commandParams.chargingStation.modem as JsonObject; | |
d270cc87 JB |
98 | return { |
99 | reason: commandParams?.reason, | |
100 | chargingStation: { | |
98fc1389 JB |
101 | model: commandParams?.chargingStation?.model, |
102 | vendorName: commandParams?.chargingStation?.vendorName, | |
103 | ...(!Utils.isUndefined(commandParams?.chargingStation?.firmwareVersion) && { | |
104 | firmwareVersion: commandParams.chargingStation?.firmwareVersion, | |
d270cc87 | 105 | }), |
98fc1389 JB |
106 | ...(!Utils.isUndefined(commandParams?.chargingStation?.serialNumber) && { |
107 | serialNumber: commandParams.chargingStation?.serialNumber, | |
108 | }), | |
109 | ...(!Utils.isUndefined(commandParams?.chargingStation?.modem) && { | |
110 | modem: { | |
111 | ...(!Utils.isUndefined(commandParams?.chargingStation?.modem?.iccid) && { | |
112 | iccid: commandParams.chargingStation.modem.iccid, | |
113 | }), | |
114 | ...(!Utils.isUndefined(commandParams?.chargingStation?.modem?.imsi) && { | |
115 | imsi: commandParams.chargingStation.modem.imsi, | |
116 | }), | |
117 | }, | |
d270cc87 | 118 | }), |
d270cc87 JB |
119 | }, |
120 | } as unknown as Request; | |
81533a20 JB |
121 | case OCPP20RequestCommand.HEARTBEAT: |
122 | return {} as unknown as Request; | |
6e939d9e JB |
123 | case OCPP20RequestCommand.STATUS_NOTIFICATION: |
124 | return { | |
125 | timestamp: commandParams?.timestamp, | |
126 | connectorStatus: commandParams?.connectorStatus, | |
127 | evseId: commandParams?.evseId, | |
128 | connectorId: commandParams?.connectorId, | |
129 | } as unknown as Request; | |
953d6b02 JB |
130 | default: |
131 | // OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError(). | |
132 | throw new OCPPError( | |
133 | ErrorType.NOT_SUPPORTED, | |
134 | // eslint-disable-next-line @typescript-eslint/restrict-template-expressions | |
135 | `Unsupported OCPP command '${commandName}'`, | |
136 | commandName, | |
137 | commandParams | |
138 | ); | |
139 | } | |
140 | } | |
e9a4164c JB |
141 | |
142 | private parseJsonSchemaFile<T extends JsonType>(relativePath: string): JSONSchemaType<T> { | |
143 | return JSON.parse( | |
144 | fs.readFileSync( | |
145 | path.resolve(path.dirname(fileURLToPath(import.meta.url)), relativePath), | |
146 | 'utf8' | |
147 | ) | |
148 | ) as JSONSchemaType<T>; | |
149 | } | |
953d6b02 | 150 | } |