Commit | Line | Data |
---|---|---|
47e22477 JB |
1 | import * as url from 'url'; |
2 | ||
3 | import { ChangeAvailabilityRequest, ChangeConfigurationRequest, ClearChargingProfileRequest, GetConfigurationRequest, GetDiagnosticsRequest, OCPP16AvailabilityType, OCPP16IncomingRequestCommand, RemoteStartTransactionRequest, RemoteStopTransactionRequest, ResetRequest, SetChargingProfileRequest, UnlockConnectorRequest } from '../../../types/ocpp/1.6/Requests'; | |
4 | import { ChangeAvailabilityResponse, ChangeConfigurationResponse, ClearChargingProfileResponse, GetConfigurationResponse, GetDiagnosticsResponse, SetChargingProfileResponse, UnlockConnectorResponse } from '../../../types/ocpp/1.6/Responses'; | |
c0560973 | 5 | import { ChargingProfilePurposeType, OCPP16ChargingProfile } from '../../../types/ocpp/1.6/ChargingProfile'; |
47e22477 | 6 | import { Client, FTPResponse } from 'basic-ftp'; |
290d006c | 7 | import { IncomingRequestCommand, RequestCommand } from '../../../types/ocpp/Requests'; |
c0560973 JB |
8 | import { OCPP16AuthorizationStatus, OCPP16StopTransactionReason } from '../../../types/ocpp/1.6/Transaction'; |
9 | ||
10 | import Constants from '../../../utils/Constants'; | |
9ccca265 | 11 | import { DefaultResponse } from '../../../types/ocpp/Responses'; |
c0560973 JB |
12 | import { ErrorType } from '../../../types/ocpp/ErrorType'; |
13 | import { MessageType } from '../../../types/ocpp/MessageType'; | |
14 | import { OCPP16ChargePointStatus } from '../../../types/ocpp/1.6/ChargePointStatus'; | |
47e22477 | 15 | import { OCPP16DiagnosticsStatus } from '../../../types/ocpp/1.6/DiagnosticsStatus'; |
c0560973 JB |
16 | import { OCPP16StandardParametersKey } from '../../../types/ocpp/1.6/Configuration'; |
17 | import { OCPPConfigurationKey } from '../../../types/ocpp/Configuration'; | |
7170127d | 18 | import OCPPError from '../../OCPPError'; |
c0560973 JB |
19 | import OCPPIncomingRequestService from '../OCPPIncomingRequestService'; |
20 | import Utils from '../../../utils/Utils'; | |
47e22477 | 21 | import fs from 'fs'; |
c0560973 | 22 | import logger from '../../../utils/Logger'; |
47e22477 JB |
23 | import path from 'path'; |
24 | import tar from 'tar'; | |
c0560973 JB |
25 | |
26 | export default class OCPP16IncomingRequestService extends OCPPIncomingRequestService { | |
27 | public async handleRequest(messageId: string, commandName: OCPP16IncomingRequestCommand, commandPayload: Record<string, unknown>): Promise<void> { | |
28 | let response; | |
35ff6ec4 | 29 | const methodName = `handleRequest${commandName}`; |
c0560973 | 30 | // Call |
35ff6ec4 | 31 | if (typeof this[methodName] === 'function') { |
c0560973 JB |
32 | try { |
33 | // Call the method to build the response | |
35ff6ec4 | 34 | response = await this[methodName](commandPayload); |
c0560973 JB |
35 | } catch (error) { |
36 | // Log | |
37 | logger.error(this.chargingStation.logPrefix() + ' Handle request error: %j', error); | |
a4cc42ea | 38 | // Send back an error response to inform backend |
c0560973 JB |
39 | await this.chargingStation.ocppRequestService.sendError(messageId, error, commandName); |
40 | throw error; | |
41 | } | |
42 | } else { | |
43 | // Throw exception | |
44 | await this.chargingStation.ocppRequestService.sendError(messageId, new OCPPError(ErrorType.NOT_IMPLEMENTED, `${commandName} is not implemented`, {}), commandName); | |
45 | throw new Error(`${commandName} is not implemented to handle payload ${JSON.stringify(commandPayload)}`); | |
46 | } | |
47 | // Send the built response | |
48 | await this.chargingStation.ocppRequestService.sendMessage(messageId, response, MessageType.CALL_RESULT_MESSAGE, commandName); | |
49 | } | |
50 | ||
51 | // Simulate charging station restart | |
52 | private handleRequestReset(commandPayload: ResetRequest): DefaultResponse { | |
71623267 JB |
53 | // eslint-disable-next-line @typescript-eslint/no-misused-promises |
54 | setImmediate(async (): Promise<void> => { | |
c0560973 JB |
55 | await this.chargingStation.stop(commandPayload.type + 'Reset' as OCPP16StopTransactionReason); |
56 | await Utils.sleep(this.chargingStation.stationInfo.resetTime); | |
71623267 | 57 | this.chargingStation.start(); |
c0560973 JB |
58 | }); |
59 | logger.info(`${this.chargingStation.logPrefix()} ${commandPayload.type} reset command received, simulating it. The station will be back online in ${Utils.milliSecondsToHHMMSS(this.chargingStation.stationInfo.resetTime)}`); | |
60 | return Constants.OCPP_RESPONSE_ACCEPTED; | |
61 | } | |
62 | ||
63 | private handleRequestClearCache(): DefaultResponse { | |
64 | return Constants.OCPP_RESPONSE_ACCEPTED; | |
65 | } | |
66 | ||
67 | private async handleRequestUnlockConnector(commandPayload: UnlockConnectorRequest): Promise<UnlockConnectorResponse> { | |
68 | const connectorId = commandPayload.connectorId; | |
69 | if (connectorId === 0) { | |
70 | logger.error(this.chargingStation.logPrefix() + ' Trying to unlock connector ' + connectorId.toString()); | |
71 | return Constants.OCPP_RESPONSE_UNLOCK_NOT_SUPPORTED; | |
72 | } | |
73 | if (this.chargingStation.getConnector(connectorId)?.transactionStarted) { | |
74 | const transactionId = this.chargingStation.getConnector(connectorId).transactionId; | |
6ed92bc1 JB |
75 | const stopResponse = await this.chargingStation.ocppRequestService.sendStopTransaction(transactionId, |
76 | this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId), | |
77 | this.chargingStation.getTransactionIdTag(transactionId), | |
78 | OCPP16StopTransactionReason.UNLOCK_COMMAND); | |
c0560973 JB |
79 | if (stopResponse.idTagInfo?.status === OCPP16AuthorizationStatus.ACCEPTED) { |
80 | return Constants.OCPP_RESPONSE_UNLOCKED; | |
81 | } | |
82 | return Constants.OCPP_RESPONSE_UNLOCK_FAILED; | |
83 | } | |
84 | await this.chargingStation.ocppRequestService.sendStatusNotification(connectorId, OCPP16ChargePointStatus.AVAILABLE); | |
85 | this.chargingStation.getConnector(connectorId).status = OCPP16ChargePointStatus.AVAILABLE; | |
86 | return Constants.OCPP_RESPONSE_UNLOCKED; | |
87 | } | |
88 | ||
89 | private handleRequestGetConfiguration(commandPayload: GetConfigurationRequest): GetConfigurationResponse { | |
90 | const configurationKey: OCPPConfigurationKey[] = []; | |
91 | const unknownKey: string[] = []; | |
92 | if (Utils.isEmptyArray(commandPayload.key)) { | |
93 | for (const configuration of this.chargingStation.configuration.configurationKey) { | |
94 | if (Utils.isUndefined(configuration.visible)) { | |
95 | configuration.visible = true; | |
96 | } | |
97 | if (!configuration.visible) { | |
98 | continue; | |
99 | } | |
100 | configurationKey.push({ | |
101 | key: configuration.key, | |
102 | readonly: configuration.readonly, | |
103 | value: configuration.value, | |
104 | }); | |
105 | } | |
106 | } else { | |
107 | for (const key of commandPayload.key) { | |
108 | const keyFound = this.chargingStation.getConfigurationKey(key); | |
109 | if (keyFound) { | |
110 | if (Utils.isUndefined(keyFound.visible)) { | |
111 | keyFound.visible = true; | |
112 | } | |
113 | if (!keyFound.visible) { | |
114 | continue; | |
115 | } | |
116 | configurationKey.push({ | |
117 | key: keyFound.key, | |
118 | readonly: keyFound.readonly, | |
119 | value: keyFound.value, | |
120 | }); | |
121 | } else { | |
122 | unknownKey.push(key); | |
123 | } | |
124 | } | |
125 | } | |
126 | return { | |
127 | configurationKey, | |
128 | unknownKey, | |
129 | }; | |
130 | } | |
131 | ||
132 | private handleRequestChangeConfiguration(commandPayload: ChangeConfigurationRequest): ChangeConfigurationResponse { | |
133 | // JSON request fields type sanity check | |
134 | if (!Utils.isString(commandPayload.key)) { | |
290d006c | 135 | logger.error(`${this.chargingStation.logPrefix()} ${RequestCommand.CHANGE_CONFIGURATION} request key field is not a string:`, commandPayload); |
c0560973 JB |
136 | } |
137 | if (!Utils.isString(commandPayload.value)) { | |
290d006c | 138 | logger.error(`${this.chargingStation.logPrefix()} ${RequestCommand.CHANGE_CONFIGURATION} request value field is not a string:`, commandPayload); |
c0560973 JB |
139 | } |
140 | const keyToChange = this.chargingStation.getConfigurationKey(commandPayload.key, true); | |
141 | if (!keyToChange) { | |
142 | return Constants.OCPP_CONFIGURATION_RESPONSE_NOT_SUPPORTED; | |
143 | } else if (keyToChange && keyToChange.readonly) { | |
144 | return Constants.OCPP_CONFIGURATION_RESPONSE_REJECTED; | |
145 | } else if (keyToChange && !keyToChange.readonly) { | |
146 | const keyIndex = this.chargingStation.configuration.configurationKey.indexOf(keyToChange); | |
147 | let valueChanged = false; | |
148 | if (this.chargingStation.configuration.configurationKey[keyIndex].value !== commandPayload.value) { | |
149 | this.chargingStation.configuration.configurationKey[keyIndex].value = commandPayload.value; | |
150 | valueChanged = true; | |
151 | } | |
152 | let triggerHeartbeatRestart = false; | |
153 | if (keyToChange.key === OCPP16StandardParametersKey.HeartBeatInterval && valueChanged) { | |
154 | this.chargingStation.setConfigurationKeyValue(OCPP16StandardParametersKey.HeartbeatInterval, commandPayload.value); | |
155 | triggerHeartbeatRestart = true; | |
156 | } | |
157 | if (keyToChange.key === OCPP16StandardParametersKey.HeartbeatInterval && valueChanged) { | |
158 | this.chargingStation.setConfigurationKeyValue(OCPP16StandardParametersKey.HeartBeatInterval, commandPayload.value); | |
159 | triggerHeartbeatRestart = true; | |
160 | } | |
161 | if (triggerHeartbeatRestart) { | |
162 | this.chargingStation.restartHeartbeat(); | |
163 | } | |
164 | if (keyToChange.key === OCPP16StandardParametersKey.WebSocketPingInterval && valueChanged) { | |
165 | this.chargingStation.restartWebSocketPing(); | |
166 | } | |
167 | if (keyToChange.reboot) { | |
168 | return Constants.OCPP_CONFIGURATION_RESPONSE_REBOOT_REQUIRED; | |
169 | } | |
170 | return Constants.OCPP_CONFIGURATION_RESPONSE_ACCEPTED; | |
171 | } | |
172 | } | |
173 | ||
174 | private handleRequestSetChargingProfile(commandPayload: SetChargingProfileRequest): SetChargingProfileResponse { | |
175 | if (!this.chargingStation.getConnector(commandPayload.connectorId)) { | |
a4cc42ea | 176 | logger.error(`${this.chargingStation.logPrefix()} Trying to set charging profile(s) to a non existing connector Id ${commandPayload.connectorId}`); |
c0560973 JB |
177 | return Constants.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED; |
178 | } | |
179 | if (commandPayload.csChargingProfiles.chargingProfilePurpose === ChargingProfilePurposeType.CHARGE_POINT_MAX_PROFILE && commandPayload.connectorId !== 0) { | |
180 | return Constants.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED; | |
181 | } | |
182 | if (commandPayload.csChargingProfiles.chargingProfilePurpose === ChargingProfilePurposeType.TX_PROFILE && (commandPayload.connectorId === 0 || !this.chargingStation.getConnector(commandPayload.connectorId)?.transactionStarted)) { | |
183 | return Constants.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED; | |
184 | } | |
185 | this.chargingStation.setChargingProfile(commandPayload.connectorId, commandPayload.csChargingProfiles); | |
a4cc42ea | 186 | logger.debug(`${this.chargingStation.logPrefix()} Charging profile(s) set, dump their stack: %j`, this.chargingStation.getConnector(commandPayload.connectorId).chargingProfiles); |
c0560973 JB |
187 | return Constants.OCPP_SET_CHARGING_PROFILE_RESPONSE_ACCEPTED; |
188 | } | |
189 | ||
190 | private handleRequestClearChargingProfile(commandPayload: ClearChargingProfileRequest): ClearChargingProfileResponse { | |
191 | if (!this.chargingStation.getConnector(commandPayload.connectorId)) { | |
a4cc42ea | 192 | logger.error(`${this.chargingStation.logPrefix()} Trying to clear a charging profile(s) to a non existing connector Id ${commandPayload.connectorId}`); |
c0560973 JB |
193 | return Constants.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN; |
194 | } | |
195 | if (commandPayload.connectorId && !Utils.isEmptyArray(this.chargingStation.getConnector(commandPayload.connectorId).chargingProfiles)) { | |
196 | this.chargingStation.getConnector(commandPayload.connectorId).chargingProfiles = []; | |
a4cc42ea | 197 | logger.debug(`${this.chargingStation.logPrefix()} Charging profile(s) cleared, dump their stack: %j`, this.chargingStation.getConnector(commandPayload.connectorId).chargingProfiles); |
c0560973 JB |
198 | return Constants.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED; |
199 | } | |
200 | if (!commandPayload.connectorId) { | |
201 | let clearedCP = false; | |
202 | for (const connector in this.chargingStation.connectors) { | |
203 | if (!Utils.isEmptyArray(this.chargingStation.getConnector(Utils.convertToInt(connector)).chargingProfiles)) { | |
6e0964c8 | 204 | this.chargingStation.getConnector(Utils.convertToInt(connector)).chargingProfiles?.forEach((chargingProfile: OCPP16ChargingProfile, index: number) => { |
c0560973 JB |
205 | let clearCurrentCP = false; |
206 | if (chargingProfile.chargingProfileId === commandPayload.id) { | |
207 | clearCurrentCP = true; | |
208 | } | |
209 | if (!commandPayload.chargingProfilePurpose && chargingProfile.stackLevel === commandPayload.stackLevel) { | |
210 | clearCurrentCP = true; | |
211 | } | |
212 | if (!chargingProfile.stackLevel && chargingProfile.chargingProfilePurpose === commandPayload.chargingProfilePurpose) { | |
213 | clearCurrentCP = true; | |
214 | } | |
215 | if (chargingProfile.stackLevel === commandPayload.stackLevel && chargingProfile.chargingProfilePurpose === commandPayload.chargingProfilePurpose) { | |
216 | clearCurrentCP = true; | |
217 | } | |
218 | if (clearCurrentCP) { | |
219 | this.chargingStation.getConnector(commandPayload.connectorId).chargingProfiles[index] = {} as OCPP16ChargingProfile; | |
a4cc42ea | 220 | logger.debug(`${this.chargingStation.logPrefix()} Charging profile(s) cleared, dump their stack: %j`, this.chargingStation.getConnector(commandPayload.connectorId).chargingProfiles); |
c0560973 JB |
221 | clearedCP = true; |
222 | } | |
223 | }); | |
224 | } | |
225 | } | |
226 | if (clearedCP) { | |
227 | return Constants.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED; | |
228 | } | |
229 | } | |
230 | return Constants.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN; | |
231 | } | |
232 | ||
e268356b | 233 | private async handleRequestChangeAvailability(commandPayload: ChangeAvailabilityRequest): Promise<ChangeAvailabilityResponse> { |
c0560973 JB |
234 | const connectorId: number = commandPayload.connectorId; |
235 | if (!this.chargingStation.getConnector(connectorId)) { | |
236 | logger.error(`${this.chargingStation.logPrefix()} Trying to change the availability of a non existing connector Id ${connectorId.toString()}`); | |
237 | return Constants.OCPP_AVAILABILITY_RESPONSE_REJECTED; | |
238 | } | |
239 | const chargePointStatus: OCPP16ChargePointStatus = commandPayload.type === OCPP16AvailabilityType.OPERATIVE ? OCPP16ChargePointStatus.AVAILABLE : OCPP16ChargePointStatus.UNAVAILABLE; | |
240 | if (connectorId === 0) { | |
241 | let response: ChangeAvailabilityResponse = Constants.OCPP_AVAILABILITY_RESPONSE_ACCEPTED; | |
242 | for (const connector in this.chargingStation.connectors) { | |
243 | if (this.chargingStation.getConnector(Utils.convertToInt(connector)).transactionStarted) { | |
244 | response = Constants.OCPP_AVAILABILITY_RESPONSE_SCHEDULED; | |
245 | } | |
246 | this.chargingStation.getConnector(Utils.convertToInt(connector)).availability = commandPayload.type; | |
247 | if (response === Constants.OCPP_AVAILABILITY_RESPONSE_ACCEPTED) { | |
e268356b | 248 | await this.chargingStation.ocppRequestService.sendStatusNotification(Utils.convertToInt(connector), chargePointStatus); |
c0560973 JB |
249 | this.chargingStation.getConnector(Utils.convertToInt(connector)).status = chargePointStatus; |
250 | } | |
251 | } | |
252 | return response; | |
253 | } else if (connectorId > 0 && (this.chargingStation.getConnector(0).availability === OCPP16AvailabilityType.OPERATIVE || (this.chargingStation.getConnector(0).availability === OCPP16AvailabilityType.INOPERATIVE && commandPayload.type === OCPP16AvailabilityType.INOPERATIVE))) { | |
254 | if (this.chargingStation.getConnector(connectorId)?.transactionStarted) { | |
255 | this.chargingStation.getConnector(connectorId).availability = commandPayload.type; | |
256 | return Constants.OCPP_AVAILABILITY_RESPONSE_SCHEDULED; | |
257 | } | |
258 | this.chargingStation.getConnector(connectorId).availability = commandPayload.type; | |
e268356b | 259 | await this.chargingStation.ocppRequestService.sendStatusNotification(connectorId, chargePointStatus); |
c0560973 JB |
260 | this.chargingStation.getConnector(connectorId).status = chargePointStatus; |
261 | return Constants.OCPP_AVAILABILITY_RESPONSE_ACCEPTED; | |
262 | } | |
263 | return Constants.OCPP_AVAILABILITY_RESPONSE_REJECTED; | |
264 | } | |
265 | ||
266 | private async handleRequestRemoteStartTransaction(commandPayload: RemoteStartTransactionRequest): Promise<DefaultResponse> { | |
a7fc8211 JB |
267 | const transactionConnectorId: number = commandPayload.connectorId; |
268 | if (transactionConnectorId) { | |
269 | await this.chargingStation.ocppRequestService.sendStatusNotification(transactionConnectorId, OCPP16ChargePointStatus.PREPARING); | |
270 | this.chargingStation.getConnector(transactionConnectorId).status = OCPP16ChargePointStatus.PREPARING; | |
271 | if (this.chargingStation.isChargingStationAvailable() && this.chargingStation.isConnectorAvailable(transactionConnectorId)) { | |
e060fe58 | 272 | // Check if authorized |
a7fc8211 JB |
273 | if (this.chargingStation.getAuthorizeRemoteTxRequests()) { |
274 | let authorized = false; | |
a7fc8211 JB |
275 | if (this.chargingStation.getLocalAuthListEnabled() && this.chargingStation.hasAuthorizedTags() |
276 | && this.chargingStation.authorizedTags.find((value) => value === commandPayload.idTag)) { | |
36f6a92e JB |
277 | authorized = true; |
278 | } | |
a7fc8211 JB |
279 | if (!authorized || (authorized && this.chargingStation.getMayAuthorizeAtRemoteStart())) { |
280 | const authorizeResponse = await this.chargingStation.ocppRequestService.sendAuthorize(transactionConnectorId, commandPayload.idTag); | |
281 | if (authorizeResponse?.idTagInfo?.status === OCPP16AuthorizationStatus.ACCEPTED) { | |
282 | authorized = true; | |
283 | } else { | |
284 | authorized = false; | |
285 | } | |
286 | } | |
287 | if (authorized) { | |
288 | // Authorization successful, start transaction | |
e060fe58 JB |
289 | if (this.setRemoteStartTransactionChargingProfile(transactionConnectorId, commandPayload.chargingProfile)) { |
290 | if ((await this.chargingStation.ocppRequestService.sendStartTransaction(transactionConnectorId, commandPayload.idTag)).idTagInfo.status === OCPP16AuthorizationStatus.ACCEPTED) { | |
291 | logger.debug(this.chargingStation.logPrefix() + ' Transaction remotely STARTED on ' + this.chargingStation.stationInfo.chargingStationId + '#' + transactionConnectorId.toString() + ' for idTag ' + commandPayload.idTag); | |
292 | return Constants.OCPP_RESPONSE_ACCEPTED; | |
293 | } | |
57939a9d | 294 | return this.notifyRemoteStartTransactionRejected(transactionConnectorId, commandPayload.idTag); |
e060fe58 | 295 | } |
57939a9d | 296 | return this.notifyRemoteStartTransactionRejected(transactionConnectorId, commandPayload.idTag); |
a7fc8211 | 297 | } |
57939a9d | 298 | return this.notifyRemoteStartTransactionRejected(transactionConnectorId, commandPayload.idTag); |
36f6a92e | 299 | } |
a7fc8211 | 300 | // No authorization check required, start transaction |
e060fe58 JB |
301 | if (this.setRemoteStartTransactionChargingProfile(transactionConnectorId, commandPayload.chargingProfile)) { |
302 | if ((await this.chargingStation.ocppRequestService.sendStartTransaction(transactionConnectorId, commandPayload.idTag)).idTagInfo.status === OCPP16AuthorizationStatus.ACCEPTED) { | |
303 | logger.debug(this.chargingStation.logPrefix() + ' Transaction remotely STARTED on ' + this.chargingStation.stationInfo.chargingStationId + '#' + transactionConnectorId.toString() + ' for idTag ' + commandPayload.idTag); | |
304 | return Constants.OCPP_RESPONSE_ACCEPTED; | |
305 | } | |
57939a9d | 306 | return this.notifyRemoteStartTransactionRejected(transactionConnectorId, commandPayload.idTag); |
e060fe58 | 307 | } |
57939a9d | 308 | return this.notifyRemoteStartTransactionRejected(transactionConnectorId, commandPayload.idTag); |
c0560973 | 309 | } |
57939a9d | 310 | return this.notifyRemoteStartTransactionRejected(transactionConnectorId, commandPayload.idTag); |
c0560973 | 311 | } |
57939a9d | 312 | return this.notifyRemoteStartTransactionRejected(transactionConnectorId, commandPayload.idTag); |
a7fc8211 JB |
313 | } |
314 | ||
315 | private async notifyRemoteStartTransactionRejected(connectorId: number, idTag: string): Promise<DefaultResponse> { | |
e060fe58 JB |
316 | if (this.chargingStation.getConnector(connectorId).status !== OCPP16ChargePointStatus.AVAILABLE) { |
317 | await this.chargingStation.ocppRequestService.sendStatusNotification(connectorId, OCPP16ChargePointStatus.AVAILABLE); | |
318 | this.chargingStation.getConnector(connectorId).status = OCPP16ChargePointStatus.AVAILABLE; | |
319 | } | |
320 | logger.warn(this.chargingStation.logPrefix() + ' Remote starting transaction REJECTED on connector Id ' + connectorId.toString() + ', idTag ' + idTag + ', availability ' + this.chargingStation.getConnector(connectorId).availability + ', status ' + this.chargingStation.getConnector(connectorId).status); | |
c0560973 JB |
321 | return Constants.OCPP_RESPONSE_REJECTED; |
322 | } | |
323 | ||
e060fe58 | 324 | private setRemoteStartTransactionChargingProfile(connectorId: number, cp: OCPP16ChargingProfile): boolean { |
a7fc8211 JB |
325 | if (cp && cp.chargingProfilePurpose === ChargingProfilePurposeType.TX_PROFILE) { |
326 | this.chargingStation.setChargingProfile(connectorId, cp); | |
327 | logger.debug(`${this.chargingStation.logPrefix()} Charging profile(s) set at remote start transaction, dump their stack: %j`, this.chargingStation.getConnector(connectorId).chargingProfiles); | |
328 | return true; | |
329 | } else if (cp && cp.chargingProfilePurpose !== ChargingProfilePurposeType.TX_PROFILE) { | |
a7fc8211 JB |
330 | logger.warn(`${this.chargingStation.logPrefix()} Not allowed to set ${cp.chargingProfilePurpose} charging profile(s) at remote start transaction`); |
331 | return false; | |
e060fe58 JB |
332 | } else if (!cp) { |
333 | return true; | |
a7fc8211 JB |
334 | } |
335 | } | |
336 | ||
c0560973 JB |
337 | private async handleRequestRemoteStopTransaction(commandPayload: RemoteStopTransactionRequest): Promise<DefaultResponse> { |
338 | const transactionId = commandPayload.transactionId; | |
339 | for (const connector in this.chargingStation.connectors) { | |
340 | if (Utils.convertToInt(connector) > 0 && this.chargingStation.getConnector(Utils.convertToInt(connector))?.transactionId === transactionId) { | |
341 | await this.chargingStation.ocppRequestService.sendStatusNotification(Utils.convertToInt(connector), OCPP16ChargePointStatus.FINISHING); | |
342 | this.chargingStation.getConnector(Utils.convertToInt(connector)).status = OCPP16ChargePointStatus.FINISHING; | |
6ed92bc1 | 343 | await this.chargingStation.ocppRequestService.sendStopTransaction(transactionId, this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId), |
035742f7 | 344 | this.chargingStation.getTransactionIdTag(transactionId)); |
c0560973 JB |
345 | return Constants.OCPP_RESPONSE_ACCEPTED; |
346 | } | |
347 | } | |
348 | logger.info(this.chargingStation.logPrefix() + ' Trying to remote stop a non existing transaction ' + transactionId.toString()); | |
349 | return Constants.OCPP_RESPONSE_REJECTED; | |
350 | } | |
47e22477 JB |
351 | |
352 | private async handleRequestGetDiagnostics(commandPayload: GetDiagnosticsRequest): Promise<GetDiagnosticsResponse> { | |
353 | logger.debug(this.chargingStation.logPrefix() + ' ' + IncomingRequestCommand.GET_DIAGNOSTICS + ' request received: %j', commandPayload); | |
354 | const uri = new url.URL(commandPayload.location); | |
355 | if (uri.protocol.startsWith('ftp:')) { | |
356 | let ftpClient: Client; | |
357 | try { | |
358 | const logFiles = fs.readdirSync(path.resolve(__dirname, '../../../../')).filter((file) => file.endsWith('.log')).map((file) => path.join('./', file)); | |
359 | const diagnosticsArchive = this.chargingStation.stationInfo.chargingStationId + '_logs.tar.gz'; | |
360 | tar.create({ gzip: true }, logFiles).pipe(fs.createWriteStream(diagnosticsArchive)); | |
361 | ftpClient = new Client(); | |
362 | const accessResponse = await ftpClient.access({ | |
363 | host: uri.host, | |
364 | ...(uri.port !== '') && { port: Utils.convertToInt(uri.port) }, | |
365 | ...(uri.username !== '') && { user: uri.username }, | |
366 | ...(uri.password !== '') && { password: uri.password }, | |
367 | }); | |
368 | let uploadResponse: FTPResponse; | |
369 | if (accessResponse.code === 220) { | |
370 | // eslint-disable-next-line @typescript-eslint/no-misused-promises | |
371 | ftpClient.trackProgress(async (info) => { | |
372 | logger.info(`${this.chargingStation.logPrefix()} ${info.bytes / 1024} bytes transferred from diagnostics archive ${info.name}`); | |
373 | await this.chargingStation.ocppRequestService.sendDiagnosticsStatusNotification(OCPP16DiagnosticsStatus.Uploading); | |
374 | }); | |
375 | uploadResponse = await ftpClient.uploadFrom(path.join(path.resolve(__dirname, '../../../../'), diagnosticsArchive), uri.pathname + diagnosticsArchive); | |
376 | if (uploadResponse.code === 226) { | |
377 | await this.chargingStation.ocppRequestService.sendDiagnosticsStatusNotification(OCPP16DiagnosticsStatus.Uploaded); | |
378 | if (ftpClient) { | |
379 | ftpClient.close(); | |
380 | } | |
381 | return { fileName: diagnosticsArchive }; | |
382 | } | |
290d006c | 383 | throw new Error(`Diagnostics transfer failed with error code ${accessResponse.code.toString()}${uploadResponse?.code && '|' + uploadResponse?.code.toString()}`); |
47e22477 | 384 | } |
290d006c | 385 | throw new Error(`Diagnostics transfer failed with error code ${accessResponse.code.toString()}${uploadResponse?.code && '|' + uploadResponse?.code.toString()}`); |
47e22477 JB |
386 | } catch (error) { |
387 | await this.chargingStation.ocppRequestService.sendDiagnosticsStatusNotification(OCPP16DiagnosticsStatus.UploadFailed); | |
47e22477 JB |
388 | if (ftpClient) { |
389 | ftpClient.close(); | |
390 | } | |
e64c0923 | 391 | return this.handleIncomingRequestError(IncomingRequestCommand.GET_DIAGNOSTICS, error, Constants.OCPP_RESPONSE_EMPTY); |
47e22477 JB |
392 | } |
393 | } else { | |
394 | logger.error(`${this.chargingStation.logPrefix()} Unsupported protocol ${uri.protocol} to transfer the diagnostic logs archive`); | |
395 | await this.chargingStation.ocppRequestService.sendDiagnosticsStatusNotification(OCPP16DiagnosticsStatus.UploadFailed); | |
396 | return Constants.OCPP_RESPONSE_EMPTY; | |
397 | } | |
398 | } | |
c0560973 | 399 | } |