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'; | |
b3fc3ff5 JB |
11 | import { |
12 | OCPP20IncomingRequestCommand, | |
13 | OCPP20RequestCommand, | |
14 | } from '../../../types/ocpp/2.0/Requests'; | |
02887891 JB |
15 | import type { |
16 | OCPP20BootNotificationResponse, | |
17 | OCPP20ClearCacheResponse, | |
18 | } from '../../../types/ocpp/2.0/Responses'; | |
953d6b02 | 19 | import { ErrorType } from '../../../types/ocpp/ErrorType'; |
d270cc87 JB |
20 | import { OCPPVersion } from '../../../types/ocpp/OCPPVersion'; |
21 | import { RegistrationStatusEnumType, ResponseHandler } from '../../../types/ocpp/Responses'; | |
953d6b02 JB |
22 | import logger from '../../../utils/Logger'; |
23 | import type ChargingStation from '../../ChargingStation'; | |
24 | import OCPPResponseService from '../OCPPResponseService'; | |
25 | import { OCPP20ServiceUtils } from './OCPP20ServiceUtils'; | |
26 | ||
27 | const moduleName = 'OCPP20ResponseService'; | |
28 | ||
29 | export default class OCPP20ResponseService extends OCPPResponseService { | |
b3fc3ff5 JB |
30 | public jsonIncomingRequestResponseSchemas: Map< |
31 | OCPP20IncomingRequestCommand, | |
32 | JSONSchemaType<JsonObject> | |
33 | >; | |
34 | ||
953d6b02 JB |
35 | private responseHandlers: Map<OCPP20RequestCommand, ResponseHandler>; |
36 | private jsonSchemas: Map<OCPP20RequestCommand, JSONSchemaType<JsonObject>>; | |
37 | ||
38 | public constructor() { | |
39 | if (new.target?.name === moduleName) { | |
40 | throw new TypeError(`Cannot construct ${new.target?.name} instances directly`); | |
41 | } | |
d270cc87 JB |
42 | super(OCPPVersion.VERSION_20); |
43 | this.responseHandlers = new Map<OCPP20RequestCommand, ResponseHandler>([ | |
44 | [OCPP20RequestCommand.BOOT_NOTIFICATION, this.handleResponseBootNotification.bind(this)], | |
45 | ]); | |
46 | this.jsonSchemas = new Map<OCPP20RequestCommand, JSONSchemaType<JsonObject>>([ | |
47 | [ | |
48 | OCPP20RequestCommand.BOOT_NOTIFICATION, | |
49 | JSON.parse( | |
50 | fs.readFileSync( | |
51 | path.resolve( | |
52 | path.dirname(fileURLToPath(import.meta.url)), | |
53 | '../../../assets/json-schemas/ocpp/2.0/BootNotificationResponse.json' | |
54 | ), | |
55 | 'utf8' | |
56 | ) | |
57 | ) as JSONSchemaType<OCPP20BootNotificationResponse>, | |
58 | ], | |
59 | ]); | |
02887891 JB |
60 | this.jsonIncomingRequestResponseSchemas = new Map([ |
61 | [ | |
62 | OCPP20IncomingRequestCommand.CLEAR_CACHE, | |
63 | JSON.parse( | |
64 | fs.readFileSync( | |
65 | path.resolve( | |
66 | path.dirname(fileURLToPath(import.meta.url)), | |
67 | '../../../assets/json-schemas/ocpp/2.0/ClearCacheResponse.json' | |
68 | ), | |
69 | 'utf8' | |
70 | ) | |
71 | ) as JSONSchemaType<OCPP20ClearCacheResponse>, | |
72 | ], | |
73 | ]); | |
953d6b02 JB |
74 | this.validatePayload.bind(this); |
75 | } | |
76 | ||
77 | public async responseHandler( | |
78 | chargingStation: ChargingStation, | |
79 | commandName: OCPP20RequestCommand, | |
80 | payload: JsonType, | |
81 | requestPayload: JsonType | |
82 | ): Promise<void> { | |
83 | if ( | |
d270cc87 JB |
84 | chargingStation.isRegistered() === true || |
85 | commandName === OCPP20RequestCommand.BOOT_NOTIFICATION | |
953d6b02 JB |
86 | ) { |
87 | if ( | |
88 | this.responseHandlers.has(commandName) === true && | |
89 | OCPP20ServiceUtils.isRequestCommandSupported(chargingStation, commandName) === true | |
90 | ) { | |
91 | try { | |
92 | this.validatePayload(chargingStation, commandName, payload); | |
93 | await this.responseHandlers.get(commandName)(chargingStation, payload, requestPayload); | |
94 | } catch (error) { | |
95 | logger.error( | |
96 | `${chargingStation.logPrefix()} ${moduleName}.responseHandler: Handle response error:`, | |
97 | error | |
98 | ); | |
99 | throw error; | |
100 | } | |
101 | } else { | |
102 | // Throw exception | |
103 | throw new OCPPError( | |
104 | ErrorType.NOT_IMPLEMENTED, | |
105 | `${commandName} is not implemented to handle response PDU ${JSON.stringify( | |
106 | payload, | |
107 | null, | |
108 | 2 | |
109 | )}`, | |
110 | commandName, | |
111 | payload | |
112 | ); | |
113 | } | |
114 | } else { | |
115 | throw new OCPPError( | |
116 | ErrorType.SECURITY_ERROR, | |
117 | `${commandName} cannot be issued to handle response PDU ${JSON.stringify( | |
118 | payload, | |
119 | null, | |
120 | 2 | |
439fc71b | 121 | )} while the charging station is not registered on the central server.`, |
953d6b02 JB |
122 | commandName, |
123 | payload | |
124 | ); | |
125 | } | |
126 | } | |
127 | ||
128 | private validatePayload( | |
129 | chargingStation: ChargingStation, | |
130 | commandName: OCPP20RequestCommand, | |
131 | payload: JsonType | |
132 | ): boolean { | |
45988780 | 133 | if (this.jsonSchemas.has(commandName) === true) { |
953d6b02 JB |
134 | return this.validateResponsePayload( |
135 | chargingStation, | |
136 | commandName, | |
137 | this.jsonSchemas.get(commandName), | |
138 | payload | |
139 | ); | |
140 | } | |
141 | logger.warn( | |
b3fc3ff5 | 142 | `${chargingStation.logPrefix()} ${moduleName}.validatePayload: No JSON schema found for command '${commandName}' PDU validation` |
953d6b02 JB |
143 | ); |
144 | return false; | |
145 | } | |
d270cc87 JB |
146 | |
147 | private handleResponseBootNotification( | |
148 | chargingStation: ChargingStation, | |
149 | payload: OCPP20BootNotificationResponse | |
150 | ): void { | |
151 | if (payload.status === RegistrationStatusEnumType.ACCEPTED) { | |
152 | // ChargingStationConfigurationUtils.addConfigurationKey( | |
153 | // chargingStation, | |
154 | // OCPP16StandardParametersKey.HeartbeatInterval, | |
155 | // payload.interval.toString(), | |
156 | // {}, | |
157 | // { overwrite: true, save: true } | |
158 | // ); | |
159 | // ChargingStationConfigurationUtils.addConfigurationKey( | |
160 | // chargingStation, | |
161 | // OCPP16StandardParametersKey.HeartBeatInterval, | |
162 | // payload.interval.toString(), | |
163 | // { visible: false }, | |
164 | // { overwrite: true, save: true } | |
165 | // ); | |
166 | chargingStation.heartbeatSetInterval | |
167 | ? chargingStation.restartHeartbeat() | |
168 | : chargingStation.startHeartbeat(); | |
169 | } | |
170 | if (Object.values(RegistrationStatusEnumType).includes(payload.status)) { | |
171 | const logMsg = `${chargingStation.logPrefix()} Charging station in '${ | |
172 | payload.status | |
173 | }' state on the central server`; | |
174 | payload.status === RegistrationStatusEnumType.REJECTED | |
175 | ? logger.warn(logMsg) | |
176 | : logger.info(logMsg); | |
177 | } else { | |
178 | logger.error( | |
179 | chargingStation.logPrefix() + | |
180 | ' Charging station boot notification response received: %j with undefined registration status', | |
181 | payload | |
182 | ); | |
183 | } | |
184 | } | |
953d6b02 | 185 | } |