Commit | Line | Data |
---|---|---|
c8eeb62b JB |
1 | // Partial Copyright Jerome Benoit. 2021. All Rights Reserved. |
2 | ||
c0560973 | 3 | import { AuthorizeRequest, OCPP16AuthorizationStatus, OCPP16AuthorizeResponse, OCPP16StartTransactionResponse, OCPP16StopTransactionResponse, StartTransactionRequest, StopTransactionRequest } from '../../../types/ocpp/1.6/Transaction'; |
a3868ec4 | 4 | import { HeartbeatRequest, OCPP16RequestCommand, StatusNotificationRequest } from '../../../types/ocpp/1.6/Requests'; |
efa43e52 | 5 | import { HeartbeatResponse, OCPP16BootNotificationResponse, OCPP16RegistrationStatus, StatusNotificationResponse } from '../../../types/ocpp/1.6/Responses'; |
c0560973 JB |
6 | import { MeterValuesRequest, MeterValuesResponse } from '../../../types/ocpp/1.6/MeterValues'; |
7 | ||
73b9adec | 8 | import type ChargingStation from '../../ChargingStation'; |
a4bc2942 | 9 | import { ErrorType } from '../../../types/ocpp/ErrorType'; |
d1888640 | 10 | import { JsonType } from '../../../types/JsonType'; |
c0560973 | 11 | import { OCPP16ChargePointStatus } from '../../../types/ocpp/1.6/ChargePointStatus'; |
fd0c36fa | 12 | import { OCPP16ServiceUtils } from './OCPP16ServiceUtils'; |
c0560973 | 13 | import { OCPP16StandardParametersKey } from '../../../types/ocpp/1.6/Configuration'; |
e58068fd | 14 | import OCPPError from '../../../exception/OCPPError'; |
c0560973 | 15 | import OCPPResponseService from '../OCPPResponseService'; |
74c6a954 | 16 | import { ResponseHandler } from '../../../types/ocpp/Responses'; |
c0560973 | 17 | import Utils from '../../../utils/Utils'; |
9f2e3130 | 18 | import logger from '../../../utils/Logger'; |
c0560973 | 19 | |
909dcf2d JB |
20 | const moduleName = 'OCPP16ResponseService'; |
21 | ||
c0560973 | 22 | export default class OCPP16ResponseService extends OCPPResponseService { |
58144adb JB |
23 | private responseHandlers: Map<OCPP16RequestCommand, ResponseHandler>; |
24 | ||
9f2e3130 | 25 | public constructor(chargingStation: ChargingStation) { |
909dcf2d | 26 | if (new.target?.name === moduleName) { |
06127450 | 27 | throw new TypeError(`Cannot construct ${new.target?.name} instances directly`); |
9f2e3130 | 28 | } |
58144adb JB |
29 | super(chargingStation); |
30 | this.responseHandlers = new Map<OCPP16RequestCommand, ResponseHandler>([ | |
31 | [OCPP16RequestCommand.BOOT_NOTIFICATION, this.handleResponseBootNotification.bind(this)], | |
32 | [OCPP16RequestCommand.HEARTBEAT, this.handleResponseHeartbeat.bind(this)], | |
33 | [OCPP16RequestCommand.AUTHORIZE, this.handleResponseAuthorize.bind(this)], | |
34 | [OCPP16RequestCommand.START_TRANSACTION, this.handleResponseStartTransaction.bind(this)], | |
35 | [OCPP16RequestCommand.STOP_TRANSACTION, this.handleResponseStopTransaction.bind(this)], | |
36 | [OCPP16RequestCommand.STATUS_NOTIFICATION, this.handleResponseStatusNotification.bind(this)], | |
331e20bb | 37 | [OCPP16RequestCommand.METER_VALUES, this.handleResponseMeterValues.bind(this)] |
58144adb JB |
38 | ]); |
39 | } | |
40 | ||
d1888640 | 41 | public async handleResponse(commandName: OCPP16RequestCommand, payload: JsonType | string, requestPayload: JsonType): Promise<void> { |
124f3553 JB |
42 | if (this.chargingStation.isRegistered() || commandName === OCPP16RequestCommand.BOOT_NOTIFICATION) { |
43 | if (this.responseHandlers.has(commandName)) { | |
44 | try { | |
45 | await this.responseHandlers.get(commandName)(payload, requestPayload); | |
46 | } catch (error) { | |
9f2e3130 | 47 | logger.error(this.chargingStation.logPrefix() + ' Handle request response error: %j', error); |
124f3553 JB |
48 | throw error; |
49 | } | |
50 | } else { | |
51 | // Throw exception | |
52 | throw new OCPPError(ErrorType.NOT_IMPLEMENTED, `${commandName} is not implemented to handle request response payload ${JSON.stringify(payload, null, 2)}`, commandName); | |
887fef76 | 53 | } |
c0560973 | 54 | } else { |
672fed6e | 55 | throw new OCPPError(ErrorType.SECURITY_ERROR, `${commandName} cannot be issued to handle request response payload ${JSON.stringify(payload, null, 2)} while the charging station is not registered on the central server. `, commandName); |
c0560973 JB |
56 | } |
57 | } | |
58 | ||
624df199 | 59 | private handleResponseBootNotification(payload: OCPP16BootNotificationResponse): void { |
c0560973 | 60 | if (payload.status === OCPP16RegistrationStatus.ACCEPTED) { |
c0560973 | 61 | this.chargingStation.addConfigurationKey(OCPP16StandardParametersKey.HeartBeatInterval, payload.interval.toString()); |
12fc74d6 | 62 | this.chargingStation.addConfigurationKey(OCPP16StandardParametersKey.HeartbeatInterval, payload.interval.toString(), { visible: false }); |
54b1efe0 | 63 | this.chargingStation.heartbeatSetInterval ? this.chargingStation.restartHeartbeat() : this.chargingStation.startHeartbeat(); |
672fed6e | 64 | } |
74c6a954 | 65 | if (Object.values(OCPP16RegistrationStatus).includes(payload.status)) { |
672fed6e | 66 | const logMsg = `${this.chargingStation.logPrefix()} Charging station in '${payload.status}' state on the central server`; |
9f2e3130 | 67 | payload.status === OCPP16RegistrationStatus.REJECTED ? logger.warn(logMsg) : logger.info(logMsg); |
c0560973 | 68 | } else { |
9f2e3130 | 69 | logger.error(this.chargingStation.logPrefix() + ' Charging station boot notification response received: %j with undefined registration status', payload); |
c0560973 JB |
70 | } |
71 | } | |
72 | ||
58144adb | 73 | private handleResponseHeartbeat(payload: HeartbeatResponse, requestPayload: HeartbeatRequest): void { |
9f2e3130 | 74 | logger.debug(this.chargingStation.logPrefix() + ' Heartbeat response received: %j to Heartbeat request: %j', payload, requestPayload); |
58144adb JB |
75 | } |
76 | ||
77 | private handleResponseAuthorize(payload: OCPP16AuthorizeResponse, requestPayload: AuthorizeRequest): void { | |
78 | let authorizeConnectorId: number; | |
734d790d JB |
79 | for (const connectorId of this.chargingStation.connectors.keys()) { |
80 | if (connectorId > 0 && this.chargingStation.getConnectorStatus(connectorId)?.authorizeIdTag === requestPayload.idTag) { | |
81 | authorizeConnectorId = connectorId; | |
58144adb JB |
82 | break; |
83 | } | |
84 | } | |
85 | if (payload.idTagInfo.status === OCPP16AuthorizationStatus.ACCEPTED) { | |
a2653482 | 86 | this.chargingStation.getConnectorStatus(authorizeConnectorId).idTagAuthorized = true; |
9f2e3130 | 87 | logger.debug(`${this.chargingStation.logPrefix()} IdTag ${requestPayload.idTag} authorized on connector ${authorizeConnectorId}`); |
58144adb | 88 | } else { |
a2653482 | 89 | this.chargingStation.getConnectorStatus(authorizeConnectorId).idTagAuthorized = false; |
734d790d | 90 | delete this.chargingStation.getConnectorStatus(authorizeConnectorId).authorizeIdTag; |
9f2e3130 | 91 | logger.debug(`${this.chargingStation.logPrefix()} IdTag ${requestPayload.idTag} refused with status ${payload.idTagInfo.status} on connector ${authorizeConnectorId}`); |
58144adb JB |
92 | } |
93 | } | |
94 | ||
c0560973 JB |
95 | private async handleResponseStartTransaction(payload: OCPP16StartTransactionResponse, requestPayload: StartTransactionRequest): Promise<void> { |
96 | const connectorId = requestPayload.connectorId; | |
97 | ||
98 | let transactionConnectorId: number; | |
734d790d JB |
99 | for (const id of this.chargingStation.connectors.keys()) { |
100 | if (id > 0 && id === connectorId) { | |
101 | transactionConnectorId = id; | |
c0560973 JB |
102 | break; |
103 | } | |
104 | } | |
105 | if (!transactionConnectorId) { | |
9f2e3130 | 106 | logger.error(this.chargingStation.logPrefix() + ' Trying to start a transaction on a non existing connector Id ' + connectorId.toString()); |
c0560973 JB |
107 | return; |
108 | } | |
a2653482 JB |
109 | if (this.chargingStation.getConnectorStatus(connectorId).transactionRemoteStarted && this.chargingStation.getAuthorizeRemoteTxRequests() |
110 | && this.chargingStation.getLocalAuthListEnabled() && this.chargingStation.hasAuthorizedTags() && !this.chargingStation.getConnectorStatus(connectorId).idTagLocalAuthorized) { | |
9f2e3130 | 111 | logger.error(this.chargingStation.logPrefix() + ' Trying to start a transaction with a not local authorized idTag ' + this.chargingStation.getConnectorStatus(connectorId).localAuthorizeIdTag + ' on connector Id ' + connectorId.toString()); |
a2653482 JB |
112 | await this.resetConnectorOnStartTransactionError(connectorId); |
113 | return; | |
114 | } | |
115 | if (this.chargingStation.getConnectorStatus(connectorId).transactionRemoteStarted && this.chargingStation.getAuthorizeRemoteTxRequests() | |
116 | && this.chargingStation.getMayAuthorizeAtRemoteStart() && !this.chargingStation.getConnectorStatus(connectorId).idTagLocalAuthorized | |
117 | && !this.chargingStation.getConnectorStatus(connectorId).idTagAuthorized) { | |
9f2e3130 | 118 | logger.error(this.chargingStation.logPrefix() + ' Trying to start a transaction with a not authorized idTag ' + this.chargingStation.getConnectorStatus(connectorId).authorizeIdTag + ' on connector Id ' + connectorId.toString()); |
a2653482 JB |
119 | await this.resetConnectorOnStartTransactionError(connectorId); |
120 | return; | |
121 | } | |
122 | if (this.chargingStation.getConnectorStatus(connectorId).idTagAuthorized && this.chargingStation.getConnectorStatus(connectorId).authorizeIdTag !== requestPayload.idTag) { | |
9f2e3130 | 123 | logger.error(this.chargingStation.logPrefix() + ' Trying to start a transaction with an idTag ' + requestPayload.idTag + ' different from the authorize request one ' + this.chargingStation.getConnectorStatus(connectorId).authorizeIdTag + ' on connector Id ' + connectorId.toString()); |
a2653482 JB |
124 | await this.resetConnectorOnStartTransactionError(connectorId); |
125 | return; | |
126 | } | |
127 | if (this.chargingStation.getConnectorStatus(connectorId).idTagLocalAuthorized | |
128 | && this.chargingStation.getConnectorStatus(connectorId).localAuthorizeIdTag !== requestPayload.idTag) { | |
9f2e3130 | 129 | logger.error(this.chargingStation.logPrefix() + ' Trying to start a transaction with an idTag ' + requestPayload.idTag + ' different from the local authorized one ' + this.chargingStation.getConnectorStatus(connectorId).localAuthorizeIdTag + ' on connector Id ' + connectorId.toString()); |
a2653482 | 130 | await this.resetConnectorOnStartTransactionError(connectorId); |
163547b1 JB |
131 | return; |
132 | } | |
734d790d | 133 | if (this.chargingStation.getConnectorStatus(connectorId)?.transactionStarted) { |
9f2e3130 | 134 | logger.debug(this.chargingStation.logPrefix() + ' Trying to start a transaction on an already used connector ' + connectorId.toString() + ': %j', this.chargingStation.getConnectorStatus(connectorId)); |
c0560973 JB |
135 | return; |
136 | } | |
734d790d JB |
137 | if (this.chargingStation.getConnectorStatus(connectorId)?.status !== OCPP16ChargePointStatus.AVAILABLE |
138 | && this.chargingStation.getConnectorStatus(connectorId)?.status !== OCPP16ChargePointStatus.PREPARING) { | |
9f2e3130 | 139 | logger.error(`${this.chargingStation.logPrefix()} Trying to start a transaction on connector ${connectorId.toString()} with status ${this.chargingStation.getConnectorStatus(connectorId)?.status}`); |
290d006c JB |
140 | return; |
141 | } | |
f3a6f63b | 142 | if (!Number.isInteger(payload.transactionId)) { |
9f2e3130 | 143 | logger.warn(`${this.chargingStation.logPrefix()} Trying to start a transaction on connector ${connectorId.toString()} with a non integer transaction Id ${payload.transactionId}, converting to integer`); |
f3a6f63b JB |
144 | payload.transactionId = Utils.convertToInt(payload.transactionId); |
145 | } | |
c0560973 | 146 | |
f0c6ed89 | 147 | if (payload.idTagInfo?.status === OCPP16AuthorizationStatus.ACCEPTED) { |
734d790d JB |
148 | this.chargingStation.getConnectorStatus(connectorId).transactionStarted = true; |
149 | this.chargingStation.getConnectorStatus(connectorId).transactionId = payload.transactionId; | |
150 | this.chargingStation.getConnectorStatus(connectorId).transactionIdTag = requestPayload.idTag; | |
151 | this.chargingStation.getConnectorStatus(connectorId).transactionEnergyActiveImportRegisterValue = 0; | |
152 | this.chargingStation.getConnectorStatus(connectorId).transactionBeginMeterValue = OCPP16ServiceUtils.buildTransactionBeginMeterValue(this.chargingStation, connectorId, | |
fd0c36fa | 153 | requestPayload.meterStart); |
6ed92bc1 | 154 | this.chargingStation.getBeginEndMeterValues() && await this.chargingStation.ocppRequestService.sendTransactionBeginMeterValues(connectorId, payload.transactionId, |
734d790d | 155 | this.chargingStation.getConnectorStatus(connectorId).transactionBeginMeterValue); |
c0560973 | 156 | await this.chargingStation.ocppRequestService.sendStatusNotification(connectorId, OCPP16ChargePointStatus.CHARGING); |
734d790d | 157 | this.chargingStation.getConnectorStatus(connectorId).status = OCPP16ChargePointStatus.CHARGING; |
9f2e3130 | 158 | logger.info(this.chargingStation.logPrefix() + ' Transaction ' + payload.transactionId.toString() + ' STARTED on ' + this.chargingStation.stationInfo.chargingStationId + '#' + connectorId.toString() + ' for idTag ' + requestPayload.idTag); |
c0560973 JB |
159 | if (this.chargingStation.stationInfo.powerSharedByConnectors) { |
160 | this.chargingStation.stationInfo.powerDivider++; | |
161 | } | |
162 | const configuredMeterValueSampleInterval = this.chargingStation.getConfigurationKey(OCPP16StandardParametersKey.MeterValueSampleInterval); | |
163 | this.chargingStation.startMeterValues(connectorId, configuredMeterValueSampleInterval ? Utils.convertToInt(configuredMeterValueSampleInterval.value) * 1000 : 60000); | |
164 | } else { | |
9f2e3130 | 165 | logger.warn(this.chargingStation.logPrefix() + ' Starting transaction id ' + payload.transactionId.toString() + ' REJECTED with status ' + payload?.idTagInfo?.status + ', idTag ' + requestPayload.idTag); |
a2653482 JB |
166 | await this.resetConnectorOnStartTransactionError(connectorId); |
167 | } | |
168 | } | |
169 | ||
170 | private async resetConnectorOnStartTransactionError(connectorId: number): Promise<void> { | |
171 | this.chargingStation.resetConnectorStatus(connectorId); | |
172 | if (this.chargingStation.getConnectorStatus(connectorId).status !== OCPP16ChargePointStatus.AVAILABLE) { | |
173 | await this.chargingStation.ocppRequestService.sendStatusNotification(connectorId, OCPP16ChargePointStatus.AVAILABLE); | |
174 | this.chargingStation.getConnectorStatus(connectorId).status = OCPP16ChargePointStatus.AVAILABLE; | |
c0560973 JB |
175 | } |
176 | } | |
177 | ||
178 | private async handleResponseStopTransaction(payload: OCPP16StopTransactionResponse, requestPayload: StopTransactionRequest): Promise<void> { | |
179 | let transactionConnectorId: number; | |
734d790d JB |
180 | for (const connectorId of this.chargingStation.connectors.keys()) { |
181 | if (connectorId > 0 && this.chargingStation.getConnectorStatus(connectorId)?.transactionId === requestPayload.transactionId) { | |
182 | transactionConnectorId = connectorId; | |
c0560973 JB |
183 | break; |
184 | } | |
185 | } | |
186 | if (!transactionConnectorId) { | |
9f2e3130 | 187 | logger.error(this.chargingStation.logPrefix() + ' Trying to stop a non existing transaction ' + requestPayload.transactionId.toString()); |
c0560973 JB |
188 | return; |
189 | } | |
190 | if (payload.idTagInfo?.status === OCPP16AuthorizationStatus.ACCEPTED) { | |
672fed6e | 191 | (this.chargingStation.getBeginEndMeterValues() && !this.chargingStation.getOcppStrictCompliance() && this.chargingStation.getOutOfOrderEndMeterValues()) |
fd0c36fa JB |
192 | && await this.chargingStation.ocppRequestService.sendTransactionEndMeterValues(transactionConnectorId, requestPayload.transactionId, |
193 | OCPP16ServiceUtils.buildTransactionEndMeterValue(this.chargingStation, transactionConnectorId, requestPayload.meterStop)); | |
c0560973 JB |
194 | if (!this.chargingStation.isChargingStationAvailable() || !this.chargingStation.isConnectorAvailable(transactionConnectorId)) { |
195 | await this.chargingStation.ocppRequestService.sendStatusNotification(transactionConnectorId, OCPP16ChargePointStatus.UNAVAILABLE); | |
734d790d | 196 | this.chargingStation.getConnectorStatus(transactionConnectorId).status = OCPP16ChargePointStatus.UNAVAILABLE; |
c0560973 JB |
197 | } else { |
198 | await this.chargingStation.ocppRequestService.sendStatusNotification(transactionConnectorId, OCPP16ChargePointStatus.AVAILABLE); | |
734d790d | 199 | this.chargingStation.getConnectorStatus(transactionConnectorId).status = OCPP16ChargePointStatus.AVAILABLE; |
c0560973 JB |
200 | } |
201 | if (this.chargingStation.stationInfo.powerSharedByConnectors) { | |
202 | this.chargingStation.stationInfo.powerDivider--; | |
203 | } | |
9f2e3130 | 204 | logger.info(this.chargingStation.logPrefix() + ' Transaction ' + requestPayload.transactionId.toString() + ' STOPPED on ' + this.chargingStation.stationInfo.chargingStationId + '#' + transactionConnectorId.toString()); |
a2653482 | 205 | this.chargingStation.resetConnectorStatus(transactionConnectorId); |
c0560973 | 206 | } else { |
9f2e3130 | 207 | logger.warn(this.chargingStation.logPrefix() + ' Stopping transaction id ' + requestPayload.transactionId.toString() + ' REJECTED with status ' + payload.idTagInfo?.status); |
c0560973 JB |
208 | } |
209 | } | |
210 | ||
211 | private handleResponseStatusNotification(payload: StatusNotificationRequest, requestPayload: StatusNotificationResponse): void { | |
9f2e3130 | 212 | logger.debug(this.chargingStation.logPrefix() + ' Status notification response received: %j to StatusNotification request: %j', payload, requestPayload); |
c0560973 JB |
213 | } |
214 | ||
215 | private handleResponseMeterValues(payload: MeterValuesRequest, requestPayload: MeterValuesResponse): void { | |
9f2e3130 | 216 | logger.debug(this.chargingStation.logPrefix() + ' MeterValues response received: %j to MeterValues request: %j', payload, requestPayload); |
c0560973 | 217 | } |
c0560973 | 218 | } |