Commit | Line | Data |
---|---|---|
c8eeb62b JB |
1 | // Partial Copyright Jerome Benoit. 2021. All Rights Reserved. |
2 | ||
4a1857a2 | 3 | import { ACElectricUtils, DCElectricUtils } from '../../../utils/ElectricUtils'; |
c0560973 | 4 | import { AuthorizeRequest, OCPP16AuthorizeResponse, OCPP16StartTransactionResponse, OCPP16StopTransactionReason, OCPP16StopTransactionResponse, StartTransactionRequest, StopTransactionRequest } from '../../../types/ocpp/1.6/Transaction'; |
4c2b4904 | 5 | import { CurrentType, Voltage } from '../../../types/ChargingStationTemplate'; |
47e22477 | 6 | import { DiagnosticsStatusNotificationRequest, HeartbeatRequest, OCPP16BootNotificationRequest, OCPP16IncomingRequestCommand, OCPP16RequestCommand, StatusNotificationRequest } from '../../../types/ocpp/1.6/Requests'; |
9ccca265 | 7 | import { MeterValueUnit, MeterValuesRequest, OCPP16MeterValue, OCPP16MeterValueMeasurand, OCPP16MeterValuePhase } from '../../../types/ocpp/1.6/MeterValues'; |
c0560973 JB |
8 | |
9 | import Constants from '../../../utils/Constants'; | |
14763b46 | 10 | import { ErrorType } from '../../../types/ocpp/ErrorType'; |
9ccca265 | 11 | import MeasurandPerPhaseSampledValueTemplates from '../../../types/MeasurandPerPhaseSampledValueTemplates'; |
c0560973 JB |
12 | import MeasurandValues from '../../../types/MeasurandValues'; |
13 | import { MessageType } from '../../../types/ocpp/MessageType'; | |
efa43e52 | 14 | import { OCPP16BootNotificationResponse } from '../../../types/ocpp/1.6/Responses'; |
c0560973 JB |
15 | import { OCPP16ChargePointErrorCode } from '../../../types/ocpp/1.6/ChargePointErrorCode'; |
16 | import { OCPP16ChargePointStatus } from '../../../types/ocpp/1.6/ChargePointStatus'; | |
47e22477 | 17 | import { OCPP16DiagnosticsStatus } from '../../../types/ocpp/1.6/DiagnosticsStatus'; |
6ed92bc1 | 18 | import { OCPP16ServiceUtils } from './OCPP16ServiceUtils'; |
14763b46 | 19 | import OCPPError from '../OCPPError'; |
c0560973 | 20 | import OCPPRequestService from '../OCPPRequestService'; |
c0560973 JB |
21 | import Utils from '../../../utils/Utils'; |
22 | import logger from '../../../utils/Logger'; | |
23 | ||
24 | export default class OCPP16RequestService extends OCPPRequestService { | |
25 | public async sendHeartbeat(): Promise<void> { | |
26 | try { | |
27 | const payload: HeartbeatRequest = {}; | |
28 | await this.sendMessage(Utils.generateUUID(), payload, MessageType.CALL_MESSAGE, OCPP16RequestCommand.HEARTBEAT); | |
29 | } catch (error) { | |
30 | this.handleRequestError(OCPP16RequestCommand.HEARTBEAT, error); | |
31 | } | |
32 | } | |
33 | ||
fd0c36fa JB |
34 | public async sendBootNotification(chargePointModel: string, chargePointVendor: string, chargeBoxSerialNumber?: string, firmwareVersion?: string, |
35 | chargePointSerialNumber?: string, iccid?: string, imsi?: string, meterSerialNumber?: string, meterType?: string): Promise<OCPP16BootNotificationResponse> { | |
c0560973 JB |
36 | try { |
37 | const payload: OCPP16BootNotificationRequest = { | |
38 | chargePointModel, | |
39 | chargePointVendor, | |
40 | ...!Utils.isUndefined(chargeBoxSerialNumber) && { chargeBoxSerialNumber }, | |
41 | ...!Utils.isUndefined(chargePointSerialNumber) && { chargePointSerialNumber }, | |
42 | ...!Utils.isUndefined(firmwareVersion) && { firmwareVersion }, | |
43 | ...!Utils.isUndefined(iccid) && { iccid }, | |
44 | ...!Utils.isUndefined(imsi) && { imsi }, | |
45 | ...!Utils.isUndefined(meterSerialNumber) && { meterSerialNumber }, | |
46 | ...!Utils.isUndefined(meterType) && { meterType } | |
47 | }; | |
48 | return await this.sendMessage(Utils.generateUUID(), payload, MessageType.CALL_MESSAGE, OCPP16RequestCommand.BOOT_NOTIFICATION) as OCPP16BootNotificationResponse; | |
49 | } catch (error) { | |
50 | this.handleRequestError(OCPP16RequestCommand.BOOT_NOTIFICATION, error); | |
51 | } | |
52 | } | |
53 | ||
035742f7 JB |
54 | public async sendStatusNotification(connectorId: number, status: OCPP16ChargePointStatus, |
55 | errorCode: OCPP16ChargePointErrorCode = OCPP16ChargePointErrorCode.NO_ERROR): Promise<void> { | |
c0560973 JB |
56 | try { |
57 | const payload: StatusNotificationRequest = { | |
58 | connectorId, | |
59 | errorCode, | |
60 | status, | |
61 | }; | |
62 | await this.sendMessage(Utils.generateUUID(), payload, MessageType.CALL_MESSAGE, OCPP16RequestCommand.STATUS_NOTIFICATION); | |
63 | } catch (error) { | |
64 | this.handleRequestError(OCPP16RequestCommand.STATUS_NOTIFICATION, error); | |
65 | } | |
66 | } | |
67 | ||
163547b1 | 68 | public async sendAuthorize(connectorId: number, idTag?: string): Promise<OCPP16AuthorizeResponse> { |
c0560973 JB |
69 | try { |
70 | const payload: AuthorizeRequest = { | |
bb3343f9 | 71 | ...!Utils.isUndefined(idTag) ? { idTag } : { idTag: Constants.TRANSACTION_DEFAULT_IDTAG }, |
c0560973 | 72 | }; |
163547b1 | 73 | this.chargingStation.getConnector(connectorId).authorizeIdTag = idTag; |
c0560973 JB |
74 | return await this.sendMessage(Utils.generateUUID(), payload, MessageType.CALL_MESSAGE, OCPP16RequestCommand.AUTHORIZE) as OCPP16AuthorizeResponse; |
75 | } catch (error) { | |
76 | this.handleRequestError(OCPP16RequestCommand.AUTHORIZE, error); | |
77 | } | |
78 | } | |
79 | ||
80 | public async sendStartTransaction(connectorId: number, idTag?: string): Promise<OCPP16StartTransactionResponse> { | |
81 | try { | |
82 | const payload: StartTransactionRequest = { | |
83 | connectorId, | |
bb3343f9 | 84 | ...!Utils.isUndefined(idTag) ? { idTag } : { idTag: Constants.TRANSACTION_DEFAULT_IDTAG }, |
6ed92bc1 | 85 | meterStart: this.chargingStation.getEnergyActiveImportRegisterByConnectorId(connectorId), |
c0560973 JB |
86 | timestamp: new Date().toISOString(), |
87 | }; | |
6ed92bc1 | 88 | return await this.sendMessage(Utils.generateUUID(), payload, MessageType.CALL_MESSAGE, OCPP16RequestCommand.START_TRANSACTION) as OCPP16StartTransactionResponse; |
c0560973 JB |
89 | } catch (error) { |
90 | this.handleRequestError(OCPP16RequestCommand.START_TRANSACTION, error); | |
91 | } | |
92 | } | |
93 | ||
035742f7 JB |
94 | public async sendStopTransaction(transactionId: number, meterStop: number, idTag?: string, |
95 | reason: OCPP16StopTransactionReason = OCPP16StopTransactionReason.NONE): Promise<OCPP16StopTransactionResponse> { | |
c0560973 | 96 | try { |
326f6e38 JB |
97 | let connectorId: number; |
98 | for (const connector in this.chargingStation.connectors) { | |
99 | if (Utils.convertToInt(connector) > 0 && this.chargingStation.getConnector(Utils.convertToInt(connector))?.transactionId === transactionId) { | |
100 | connectorId = Utils.convertToInt(connector); | |
163547b1 | 101 | break; |
326f6e38 JB |
102 | } |
103 | } | |
fd0c36fa JB |
104 | const transactionEndMeterValue = OCPP16ServiceUtils.buildTransactionEndMeterValue(this.chargingStation, connectorId, meterStop); |
105 | // FIXME: should be a callback, each OCPP commands implementation must do only one job | |
6ed92bc1 | 106 | (this.chargingStation.getBeginEndMeterValues() && !this.chargingStation.getOutOfOrderEndMeterValues()) |
fd0c36fa JB |
107 | && await this.sendTransactionEndMeterValues(connectorId, transactionId, transactionEndMeterValue); |
108 | const payload: StopTransactionRequest = { | |
109 | transactionId, | |
110 | ...!Utils.isUndefined(idTag) && { idTag }, | |
111 | meterStop, | |
112 | timestamp: new Date().toISOString(), | |
113 | ...reason && { reason }, | |
114 | ...this.chargingStation.getTransactionDataMeterValues() && { transactionData: OCPP16ServiceUtils.buildTransactionDataMeterValues(this.chargingStation.getConnector(connectorId).transactionBeginMeterValue, transactionEndMeterValue) }, | |
115 | }; | |
c0560973 JB |
116 | return await this.sendMessage(Utils.generateUUID(), payload, MessageType.CALL_MESSAGE, OCPP16RequestCommand.STOP_TRANSACTION) as OCPP16StartTransactionResponse; |
117 | } catch (error) { | |
118 | this.handleRequestError(OCPP16RequestCommand.STOP_TRANSACTION, error); | |
119 | } | |
120 | } | |
121 | ||
122 | // eslint-disable-next-line consistent-this | |
aef1b33a | 123 | public async sendMeterValues(connectorId: number, transactionId: number, interval: number, debug = false): Promise<void> { |
c0560973 | 124 | try { |
fd0c36fa | 125 | const meterValue: OCPP16MeterValue = { |
c0560973 JB |
126 | timestamp: new Date().toISOString(), |
127 | sampledValue: [], | |
128 | }; | |
aef1b33a | 129 | const connector = this.chargingStation.getConnector(connectorId); |
9ccca265 | 130 | // SoC measurand |
aef1b33a | 131 | const socSampledValueTemplate = this.chargingStation.getSampledValueTemplate(connectorId, OCPP16MeterValueMeasurand.STATE_OF_CHARGE); |
9ccca265 | 132 | if (socSampledValueTemplate) { |
561d966b JB |
133 | const socSampledValueTemplateValue = socSampledValueTemplate.value |
134 | ? Utils.getRandomFloatFluctuatedRounded(parseInt(socSampledValueTemplate.value), socSampledValueTemplate.fluctuationPercent ?? Constants.DEFAULT_FLUCTUATION_PERCENT) | |
135 | : Utils.getRandomInt(100); | |
136 | meterValue.sampledValue.push(OCPP16ServiceUtils.buildSampledValue(socSampledValueTemplate, socSampledValueTemplateValue)); | |
9ccca265 JB |
137 | const sampledValuesIndex = meterValue.sampledValue.length - 1; |
138 | if (Utils.convertToInt(meterValue.sampledValue[sampledValuesIndex].value) > 100 || debug) { | |
aef1b33a | 139 | logger.error(`${this.chargingStation.logPrefix()} MeterValues measurand ${meterValue.sampledValue[sampledValuesIndex].measurand ?? OCPP16MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER}: connectorId ${connectorId}, transaction ${connector.transactionId}, value: ${meterValue.sampledValue[sampledValuesIndex].value}/100`); |
9ccca265 JB |
140 | } |
141 | } | |
142 | // Voltage measurand | |
aef1b33a | 143 | const voltageSampledValueTemplate = this.chargingStation.getSampledValueTemplate(connectorId, OCPP16MeterValueMeasurand.VOLTAGE); |
9ccca265 | 144 | if (voltageSampledValueTemplate) { |
aef1b33a | 145 | const voltageSampledValueTemplateValue = voltageSampledValueTemplate.value ? parseInt(voltageSampledValueTemplate.value) : this.chargingStation.getVoltageOut(); |
9ccca265 JB |
146 | const fluctuationPercent = voltageSampledValueTemplate.fluctuationPercent ?? Constants.DEFAULT_FLUCTUATION_PERCENT; |
147 | const voltageMeasurandValue = Utils.getRandomFloatFluctuatedRounded(voltageSampledValueTemplateValue, fluctuationPercent); | |
aef1b33a | 148 | if (this.chargingStation.getNumberOfPhases() !== 3 || (this.chargingStation.getNumberOfPhases() === 3 && this.chargingStation.getMainVoltageMeterValues())) { |
9ccca265 JB |
149 | meterValue.sampledValue.push(OCPP16ServiceUtils.buildSampledValue(voltageSampledValueTemplate, voltageMeasurandValue)); |
150 | } | |
aef1b33a | 151 | for (let phase = 1; this.chargingStation.getNumberOfPhases() === 3 && phase <= this.chargingStation.getNumberOfPhases(); phase++) { |
9ccca265 | 152 | const phaseLineToNeutralValue = `L${phase}-N`; |
aef1b33a | 153 | const voltagePhaseLineToNeutralSampledValueTemplate = this.chargingStation.getSampledValueTemplate(connectorId, OCPP16MeterValueMeasurand.VOLTAGE, |
9ccca265 JB |
154 | phaseLineToNeutralValue as OCPP16MeterValuePhase); |
155 | let voltagePhaseLineToNeutralMeasurandValue: number; | |
156 | if (voltagePhaseLineToNeutralSampledValueTemplate) { | |
aef1b33a | 157 | const voltagePhaseLineToNeutralSampledValueTemplateValue = voltagePhaseLineToNeutralSampledValueTemplate.value ? parseInt(voltagePhaseLineToNeutralSampledValueTemplate.value) : this.chargingStation.getVoltageOut(); |
9ccca265 JB |
158 | const fluctuationPhaseToNeutralPercent = voltagePhaseLineToNeutralSampledValueTemplate.fluctuationPercent ?? Constants.DEFAULT_FLUCTUATION_PERCENT; |
159 | voltagePhaseLineToNeutralMeasurandValue = Utils.getRandomFloatFluctuatedRounded(voltagePhaseLineToNeutralSampledValueTemplateValue, fluctuationPhaseToNeutralPercent); | |
c0560973 | 160 | } |
9ccca265 JB |
161 | meterValue.sampledValue.push(OCPP16ServiceUtils.buildSampledValue(voltagePhaseLineToNeutralSampledValueTemplate ?? voltageSampledValueTemplate, |
162 | voltagePhaseLineToNeutralMeasurandValue ?? voltageMeasurandValue, null, phaseLineToNeutralValue as OCPP16MeterValuePhase)); | |
aef1b33a JB |
163 | if (this.chargingStation.getPhaseLineToLineVoltageMeterValues()) { |
164 | const phaseLineToLineValue = `L${phase}-L${(phase + 1) % this.chargingStation.getNumberOfPhases() !== 0 ? (phase + 1) % this.chargingStation.getNumberOfPhases() : this.chargingStation.getNumberOfPhases()}`; | |
165 | const voltagePhaseLineToLineSampledValueTemplate = this.chargingStation.getSampledValueTemplate(connectorId, OCPP16MeterValueMeasurand.VOLTAGE, phaseLineToLineValue as OCPP16MeterValuePhase); | |
9bd87386 JB |
166 | let voltagePhaseLineToLineMeasurandValue: number; |
167 | if (voltagePhaseLineToLineSampledValueTemplate) { | |
4c2b4904 | 168 | const voltagePhaseLineToLineSampledValueTemplateValue = voltagePhaseLineToLineSampledValueTemplate.value ? parseInt(voltagePhaseLineToLineSampledValueTemplate.value) : Voltage.VOLTAGE_400; |
9bd87386 JB |
169 | const fluctuationPhaseLineToLinePercent = voltagePhaseLineToLineSampledValueTemplate.fluctuationPercent ?? Constants.DEFAULT_FLUCTUATION_PERCENT; |
170 | voltagePhaseLineToLineMeasurandValue = Utils.getRandomFloatFluctuatedRounded(voltagePhaseLineToLineSampledValueTemplateValue, fluctuationPhaseLineToLinePercent); | |
171 | } | |
4c2b4904 | 172 | const defaultVoltagePhaseLineToLineMeasurandValue = Utils.getRandomFloatFluctuatedRounded(Voltage.VOLTAGE_400, fluctuationPercent); |
9bd87386 JB |
173 | meterValue.sampledValue.push(OCPP16ServiceUtils.buildSampledValue(voltagePhaseLineToLineSampledValueTemplate ?? voltageSampledValueTemplate, |
174 | voltagePhaseLineToLineMeasurandValue ?? defaultVoltagePhaseLineToLineMeasurandValue, null, phaseLineToLineValue as OCPP16MeterValuePhase)); | |
c0560973 | 175 | } |
9ccca265 JB |
176 | } |
177 | } | |
178 | // Power.Active.Import measurand | |
aef1b33a | 179 | const powerSampledValueTemplate = this.chargingStation.getSampledValueTemplate(connectorId, OCPP16MeterValueMeasurand.POWER_ACTIVE_IMPORT); |
9ccca265 | 180 | let powerPerPhaseSampledValueTemplates: MeasurandPerPhaseSampledValueTemplates = {}; |
aef1b33a | 181 | if (this.chargingStation.getNumberOfPhases() === 3) { |
9ccca265 | 182 | powerPerPhaseSampledValueTemplates = { |
aef1b33a JB |
183 | L1: this.chargingStation.getSampledValueTemplate(connectorId, OCPP16MeterValueMeasurand.POWER_ACTIVE_IMPORT, OCPP16MeterValuePhase.L1_N), |
184 | L2: this.chargingStation.getSampledValueTemplate(connectorId, OCPP16MeterValueMeasurand.POWER_ACTIVE_IMPORT, OCPP16MeterValuePhase.L2_N), | |
185 | L3: this.chargingStation.getSampledValueTemplate(connectorId, OCPP16MeterValueMeasurand.POWER_ACTIVE_IMPORT, OCPP16MeterValuePhase.L3_N), | |
9ccca265 JB |
186 | }; |
187 | } | |
188 | if (powerSampledValueTemplate) { | |
aef1b33a JB |
189 | OCPP16ServiceUtils.checkMeasurandPowerDivider(this.chargingStation, powerSampledValueTemplate.measurand); |
190 | const errMsg = `${this.chargingStation.logPrefix()} MeterValues measurand ${powerSampledValueTemplate.measurand ?? OCPP16MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER}: Unknown ${this.chargingStation.getCurrentOutType()} currentOutType in template file ${this.chargingStation.stationTemplateFile}, cannot calculate ${powerSampledValueTemplate.measurand ?? OCPP16MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER} measurand value`; | |
9ccca265 JB |
191 | const powerMeasurandValues = {} as MeasurandValues; |
192 | const unitDivider = powerSampledValueTemplate?.unit === MeterValueUnit.KILO_WATT ? 1000 : 1; | |
aef1b33a JB |
193 | const maxPower = Math.round(this.chargingStation.stationInfo.maxPower / this.chargingStation.stationInfo.powerDivider); |
194 | const maxPowerPerPhase = Math.round((this.chargingStation.stationInfo.maxPower / this.chargingStation.stationInfo.powerDivider) / this.chargingStation.getNumberOfPhases()); | |
195 | switch (this.chargingStation.getCurrentOutType()) { | |
4c2b4904 | 196 | case CurrentType.AC: |
aef1b33a | 197 | if (this.chargingStation.getNumberOfPhases() === 3) { |
9ccca265 | 198 | const defaultFluctuatedPowerPerPhase = powerSampledValueTemplate.value |
aef1b33a | 199 | && Utils.getRandomFloatFluctuatedRounded(parseInt(powerSampledValueTemplate.value) / this.chargingStation.getNumberOfPhases(), powerSampledValueTemplate.fluctuationPercent ?? Constants.DEFAULT_FLUCTUATION_PERCENT); |
9ccca265 JB |
200 | const phase1FluctuatedValue = powerPerPhaseSampledValueTemplates?.L1?.value |
201 | && Utils.getRandomFloatFluctuatedRounded(parseInt(powerPerPhaseSampledValueTemplates.L1.value), powerPerPhaseSampledValueTemplates.L1.fluctuationPercent ?? Constants.DEFAULT_FLUCTUATION_PERCENT); | |
202 | const phase2FluctuatedValue = powerPerPhaseSampledValueTemplates?.L2?.value | |
203 | && Utils.getRandomFloatFluctuatedRounded(parseInt(powerPerPhaseSampledValueTemplates.L2.value), powerPerPhaseSampledValueTemplates.L2.fluctuationPercent ?? Constants.DEFAULT_FLUCTUATION_PERCENT); | |
204 | const phase3FluctuatedValue = powerPerPhaseSampledValueTemplates?.L3?.value | |
205 | && Utils.getRandomFloatFluctuatedRounded(parseInt(powerPerPhaseSampledValueTemplates.L3.value), powerPerPhaseSampledValueTemplates.L3.fluctuationPercent ?? Constants.DEFAULT_FLUCTUATION_PERCENT); | |
206 | powerMeasurandValues.L1 = (phase1FluctuatedValue ?? defaultFluctuatedPowerPerPhase) ?? Utils.getRandomFloatRounded(maxPowerPerPhase / unitDivider); | |
207 | powerMeasurandValues.L2 = (phase2FluctuatedValue ?? defaultFluctuatedPowerPerPhase) ?? Utils.getRandomFloatRounded(maxPowerPerPhase / unitDivider); | |
208 | powerMeasurandValues.L3 = (phase3FluctuatedValue ?? defaultFluctuatedPowerPerPhase) ?? Utils.getRandomFloatRounded(maxPowerPerPhase / unitDivider); | |
c0560973 | 209 | } else { |
9ccca265 JB |
210 | powerMeasurandValues.L1 = powerSampledValueTemplate.value |
211 | ? Utils.getRandomFloatFluctuatedRounded(parseInt(powerSampledValueTemplate.value), powerSampledValueTemplate.fluctuationPercent ?? Constants.DEFAULT_FLUCTUATION_PERCENT) | |
212 | : Utils.getRandomFloatRounded(maxPower / unitDivider); | |
213 | powerMeasurandValues.L2 = 0; | |
214 | powerMeasurandValues.L3 = 0; | |
c0560973 | 215 | } |
9ccca265 JB |
216 | powerMeasurandValues.allPhases = Utils.roundTo(powerMeasurandValues.L1 + powerMeasurandValues.L2 + powerMeasurandValues.L3, 2); |
217 | break; | |
4c2b4904 | 218 | case CurrentType.DC: |
9ccca265 JB |
219 | powerMeasurandValues.allPhases = powerSampledValueTemplate.value |
220 | ? Utils.getRandomFloatFluctuatedRounded(parseInt(powerSampledValueTemplate.value), powerSampledValueTemplate.fluctuationPercent ?? Constants.DEFAULT_FLUCTUATION_PERCENT) | |
221 | : Utils.getRandomFloatRounded(maxPower / unitDivider); | |
222 | break; | |
223 | default: | |
224 | logger.error(errMsg); | |
14763b46 | 225 | throw new OCPPError(ErrorType.INTERNAL_ERROR, errMsg); |
9ccca265 JB |
226 | } |
227 | meterValue.sampledValue.push(OCPP16ServiceUtils.buildSampledValue(powerSampledValueTemplate, powerMeasurandValues.allPhases)); | |
228 | const sampledValuesIndex = meterValue.sampledValue.length - 1; | |
229 | const maxPowerRounded = Utils.roundTo(maxPower / unitDivider, 2); | |
230 | if (Utils.convertToFloat(meterValue.sampledValue[sampledValuesIndex].value) > maxPowerRounded || debug) { | |
aef1b33a | 231 | logger.error(`${this.chargingStation.logPrefix()} MeterValues measurand ${meterValue.sampledValue[sampledValuesIndex].measurand ?? OCPP16MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER}: connectorId ${connectorId}, transaction ${connector.transactionId}, value: ${meterValue.sampledValue[sampledValuesIndex].value}/${maxPowerRounded}`); |
9ccca265 | 232 | } |
aef1b33a | 233 | for (let phase = 1; this.chargingStation.getNumberOfPhases() === 3 && phase <= this.chargingStation.getNumberOfPhases(); phase++) { |
9ccca265 JB |
234 | const phaseValue = `L${phase}-N`; |
235 | meterValue.sampledValue.push(OCPP16ServiceUtils.buildSampledValue(powerPerPhaseSampledValueTemplates[`L${phase}`] ?? powerSampledValueTemplate, powerMeasurandValues[`L${phase}`], null, | |
236 | phaseValue as OCPP16MeterValuePhase)); | |
237 | const sampledValuesPerPhaseIndex = meterValue.sampledValue.length - 1; | |
238 | const maxPowerPerPhaseRounded = Utils.roundTo(maxPowerPerPhase / unitDivider, 2); | |
239 | if (Utils.convertToFloat(meterValue.sampledValue[sampledValuesPerPhaseIndex].value) > maxPowerPerPhaseRounded || debug) { | |
aef1b33a | 240 | logger.error(`${this.chargingStation.logPrefix()} MeterValues measurand ${meterValue.sampledValue[sampledValuesPerPhaseIndex].measurand ?? OCPP16MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER}: phase ${meterValue.sampledValue[sampledValuesPerPhaseIndex].phase}, connectorId ${connectorId}, transaction ${connector.transactionId}, value: ${meterValue.sampledValue[sampledValuesPerPhaseIndex].value}/${maxPowerPerPhaseRounded}`); |
c0560973 | 241 | } |
9ccca265 JB |
242 | } |
243 | } | |
244 | // Current.Import measurand | |
aef1b33a | 245 | const currentSampledValueTemplate = this.chargingStation.getSampledValueTemplate(connectorId, OCPP16MeterValueMeasurand.CURRENT_IMPORT); |
9ccca265 | 246 | let currentPerPhaseSampledValueTemplates: MeasurandPerPhaseSampledValueTemplates = {}; |
aef1b33a | 247 | if (this.chargingStation.getNumberOfPhases() === 3) { |
9ccca265 | 248 | currentPerPhaseSampledValueTemplates = { |
aef1b33a JB |
249 | L1: this.chargingStation.getSampledValueTemplate(connectorId, OCPP16MeterValueMeasurand.CURRENT_IMPORT, OCPP16MeterValuePhase.L1), |
250 | L2: this.chargingStation.getSampledValueTemplate(connectorId, OCPP16MeterValueMeasurand.CURRENT_IMPORT, OCPP16MeterValuePhase.L2), | |
251 | L3: this.chargingStation.getSampledValueTemplate(connectorId, OCPP16MeterValueMeasurand.CURRENT_IMPORT, OCPP16MeterValuePhase.L3), | |
9ccca265 JB |
252 | }; |
253 | } | |
254 | if (currentSampledValueTemplate) { | |
aef1b33a JB |
255 | OCPP16ServiceUtils.checkMeasurandPowerDivider(this.chargingStation, currentSampledValueTemplate.measurand); |
256 | const errMsg = `${this.chargingStation.logPrefix()} MeterValues measurand ${currentSampledValueTemplate.measurand ?? OCPP16MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER}: Unknown ${this.chargingStation.getCurrentOutType()} currentOutType in template file ${this.chargingStation.stationTemplateFile}, cannot calculate ${currentSampledValueTemplate.measurand ?? OCPP16MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER} measurand value`; | |
9ccca265 JB |
257 | const currentMeasurandValues: MeasurandValues = {} as MeasurandValues; |
258 | let maxAmperage: number; | |
aef1b33a | 259 | switch (this.chargingStation.getCurrentOutType()) { |
4c2b4904 | 260 | case CurrentType.AC: |
aef1b33a JB |
261 | maxAmperage = ACElectricUtils.amperagePerPhaseFromPower(this.chargingStation.getNumberOfPhases(), this.chargingStation.stationInfo.maxPower / this.chargingStation.stationInfo.powerDivider, this.chargingStation.getVoltageOut()); |
262 | if (this.chargingStation.getNumberOfPhases() === 3) { | |
9ccca265 JB |
263 | const defaultFluctuatedAmperagePerPhase = currentSampledValueTemplate.value |
264 | && Utils.getRandomFloatFluctuatedRounded(parseInt(currentSampledValueTemplate.value), currentSampledValueTemplate.fluctuationPercent ?? Constants.DEFAULT_FLUCTUATION_PERCENT); | |
265 | const phase1FluctuatedValue = currentPerPhaseSampledValueTemplates?.L1?.value | |
266 | && Utils.getRandomFloatFluctuatedRounded(parseInt(currentPerPhaseSampledValueTemplates.L1.value), currentPerPhaseSampledValueTemplates.L1.fluctuationPercent ?? Constants.DEFAULT_FLUCTUATION_PERCENT); | |
267 | const phase2FluctuatedValue = currentPerPhaseSampledValueTemplates?.L2?.value | |
268 | && Utils.getRandomFloatFluctuatedRounded(parseInt(currentPerPhaseSampledValueTemplates.L2.value), currentPerPhaseSampledValueTemplates.L2.fluctuationPercent ?? Constants.DEFAULT_FLUCTUATION_PERCENT); | |
269 | const phase3FluctuatedValue = currentPerPhaseSampledValueTemplates?.L3?.value | |
270 | && Utils.getRandomFloatFluctuatedRounded(parseInt(currentPerPhaseSampledValueTemplates.L3.value), currentPerPhaseSampledValueTemplates.L3.fluctuationPercent ?? Constants.DEFAULT_FLUCTUATION_PERCENT); | |
271 | currentMeasurandValues.L1 = (phase1FluctuatedValue ?? defaultFluctuatedAmperagePerPhase) ?? Utils.getRandomFloatRounded(maxAmperage); | |
272 | currentMeasurandValues.L2 = (phase2FluctuatedValue ?? defaultFluctuatedAmperagePerPhase) ?? Utils.getRandomFloatRounded(maxAmperage); | |
273 | currentMeasurandValues.L3 = (phase3FluctuatedValue ?? defaultFluctuatedAmperagePerPhase) ?? Utils.getRandomFloatRounded(maxAmperage); | |
274 | } else { | |
275 | currentMeasurandValues.L1 = currentSampledValueTemplate.value | |
276 | ? Utils.getRandomFloatFluctuatedRounded(parseInt(currentSampledValueTemplate.value), currentSampledValueTemplate.fluctuationPercent ?? Constants.DEFAULT_FLUCTUATION_PERCENT) | |
277 | : Utils.getRandomFloatRounded(maxAmperage); | |
278 | currentMeasurandValues.L2 = 0; | |
279 | currentMeasurandValues.L3 = 0; | |
280 | } | |
aef1b33a | 281 | currentMeasurandValues.allPhases = Utils.roundTo((currentMeasurandValues.L1 + currentMeasurandValues.L2 + currentMeasurandValues.L3) / this.chargingStation.getNumberOfPhases(), 2); |
9ccca265 | 282 | break; |
4c2b4904 | 283 | case CurrentType.DC: |
aef1b33a | 284 | maxAmperage = DCElectricUtils.amperage(this.chargingStation.stationInfo.maxPower / this.chargingStation.stationInfo.powerDivider, this.chargingStation.getVoltageOut()); |
9ccca265 JB |
285 | currentMeasurandValues.allPhases = currentSampledValueTemplate.value |
286 | ? Utils.getRandomFloatFluctuatedRounded(parseInt(currentSampledValueTemplate.value), currentSampledValueTemplate.fluctuationPercent ?? Constants.DEFAULT_FLUCTUATION_PERCENT) | |
287 | : Utils.getRandomFloatRounded(maxAmperage); | |
288 | break; | |
289 | default: | |
290 | logger.error(errMsg); | |
14763b46 | 291 | throw new OCPPError(ErrorType.INTERNAL_ERROR, errMsg); |
9ccca265 JB |
292 | } |
293 | meterValue.sampledValue.push(OCPP16ServiceUtils.buildSampledValue(currentSampledValueTemplate, currentMeasurandValues.allPhases)); | |
294 | const sampledValuesIndex = meterValue.sampledValue.length - 1; | |
295 | if (Utils.convertToFloat(meterValue.sampledValue[sampledValuesIndex].value) > maxAmperage || debug) { | |
aef1b33a | 296 | logger.error(`${this.chargingStation.logPrefix()} MeterValues measurand ${meterValue.sampledValue[sampledValuesIndex].measurand ?? OCPP16MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER}: connectorId ${connectorId}, transaction ${connector.transactionId}, value: ${meterValue.sampledValue[sampledValuesIndex].value}/${maxAmperage}`); |
9ccca265 | 297 | } |
aef1b33a | 298 | for (let phase = 1; this.chargingStation.getNumberOfPhases() === 3 && phase <= this.chargingStation.getNumberOfPhases(); phase++) { |
9ccca265 JB |
299 | const phaseValue = `L${phase}`; |
300 | meterValue.sampledValue.push(OCPP16ServiceUtils.buildSampledValue(currentPerPhaseSampledValueTemplates[phaseValue] ?? currentSampledValueTemplate, | |
301 | currentMeasurandValues[phaseValue], null, phaseValue as OCPP16MeterValuePhase)); | |
302 | const sampledValuesPerPhaseIndex = meterValue.sampledValue.length - 1; | |
303 | if (Utils.convertToFloat(meterValue.sampledValue[sampledValuesPerPhaseIndex].value) > maxAmperage || debug) { | |
aef1b33a | 304 | logger.error(`${this.chargingStation.logPrefix()} MeterValues measurand ${meterValue.sampledValue[sampledValuesPerPhaseIndex].measurand ?? OCPP16MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER}: phase ${meterValue.sampledValue[sampledValuesPerPhaseIndex].phase}, connectorId ${connectorId}, transaction ${connector.transactionId}, value: ${meterValue.sampledValue[sampledValuesPerPhaseIndex].value}/${maxAmperage}`); |
c0560973 | 305 | } |
9ccca265 JB |
306 | } |
307 | } | |
308 | // Energy.Active.Import.Register measurand (default) | |
aef1b33a | 309 | const energySampledValueTemplate = this.chargingStation.getSampledValueTemplate(connectorId); |
9ccca265 | 310 | if (energySampledValueTemplate) { |
aef1b33a | 311 | OCPP16ServiceUtils.checkMeasurandPowerDivider(this.chargingStation, energySampledValueTemplate.measurand); |
9ccca265 JB |
312 | const unitDivider = energySampledValueTemplate?.unit === MeterValueUnit.KILO_WATT_HOUR ? 1000 : 1; |
313 | const energyMeasurandValue = energySampledValueTemplate.value | |
314 | // Cumulate the fluctuated value around the static one | |
315 | ? Utils.getRandomFloatFluctuatedRounded(parseInt(energySampledValueTemplate.value), energySampledValueTemplate.fluctuationPercent ?? Constants.DEFAULT_FLUCTUATION_PERCENT) | |
aef1b33a | 316 | : Utils.getRandomInt(this.chargingStation.stationInfo.maxPower / (this.chargingStation.stationInfo.powerDivider * 3600000) * interval); |
9ccca265 JB |
317 | // Persist previous value on connector |
318 | if (connector && !Utils.isNullOrUndefined(connector.energyActiveImportRegisterValue) && connector.energyActiveImportRegisterValue >= 0 && | |
319 | !Utils.isNullOrUndefined(connector.transactionEnergyActiveImportRegisterValue) && connector.transactionEnergyActiveImportRegisterValue >= 0) { | |
320 | connector.energyActiveImportRegisterValue += energyMeasurandValue; | |
321 | connector.transactionEnergyActiveImportRegisterValue += energyMeasurandValue; | |
c0560973 | 322 | } else { |
9ccca265 JB |
323 | connector.energyActiveImportRegisterValue = 0; |
324 | connector.transactionEnergyActiveImportRegisterValue = 0; | |
325 | } | |
326 | meterValue.sampledValue.push(OCPP16ServiceUtils.buildSampledValue(energySampledValueTemplate, | |
aef1b33a | 327 | Utils.roundTo(this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId) / unitDivider, 4))); |
9ccca265 | 328 | const sampledValuesIndex = meterValue.sampledValue.length - 1; |
aef1b33a | 329 | const maxEnergy = Math.round(this.chargingStation.stationInfo.maxPower * 3600 / (this.chargingStation.stationInfo.powerDivider * interval)); |
9ccca265 JB |
330 | const maxEnergyRounded = Utils.roundTo(maxEnergy / unitDivider, 4); |
331 | if (Utils.convertToFloat(meterValue.sampledValue[sampledValuesIndex].value) > maxEnergyRounded || debug) { | |
aef1b33a | 332 | logger.error(`${this.chargingStation.logPrefix()} MeterValues measurand ${meterValue.sampledValue[sampledValuesIndex].measurand ?? OCPP16MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER}: connectorId ${connectorId}, transaction ${connector.transactionId}, value: ${meterValue.sampledValue[sampledValuesIndex].value}/${maxEnergyRounded}`); |
c0560973 JB |
333 | } |
334 | } | |
335 | const payload: MeterValuesRequest = { | |
336 | connectorId, | |
337 | transactionId, | |
b02ab215 | 338 | meterValue: [meterValue], |
c0560973 | 339 | }; |
aef1b33a | 340 | await this.sendMessage(Utils.generateUUID(), payload, MessageType.CALL_MESSAGE, OCPP16RequestCommand.METER_VALUES); |
c0560973 | 341 | } catch (error) { |
aef1b33a | 342 | this.handleRequestError(OCPP16RequestCommand.METER_VALUES, error); |
c0560973 JB |
343 | } |
344 | } | |
345 | ||
fd0c36fa | 346 | public async sendTransactionBeginMeterValues(connectorId: number, transactionId: number, beginMeterValue: OCPP16MeterValue): Promise<void> { |
290d006c JB |
347 | try { |
348 | const payload: MeterValuesRequest = { | |
349 | connectorId, | |
350 | transactionId, | |
b02ab215 | 351 | meterValue: [beginMeterValue], |
290d006c JB |
352 | }; |
353 | await this.sendMessage(Utils.generateUUID(), payload, MessageType.CALL_MESSAGE, OCPP16RequestCommand.METER_VALUES); | |
354 | } catch (error) { | |
355 | this.handleRequestError(OCPP16RequestCommand.METER_VALUES, error); | |
356 | } | |
326f6e38 JB |
357 | } |
358 | ||
fd0c36fa | 359 | public async sendTransactionEndMeterValues(connectorId: number, transactionId: number, endMeterValue: OCPP16MeterValue): Promise<void> { |
290d006c JB |
360 | try { |
361 | const payload: MeterValuesRequest = { | |
362 | connectorId, | |
363 | transactionId, | |
b02ab215 | 364 | meterValue: [endMeterValue], |
290d006c JB |
365 | }; |
366 | await this.sendMessage(Utils.generateUUID(), payload, MessageType.CALL_MESSAGE, OCPP16RequestCommand.METER_VALUES); | |
367 | } catch (error) { | |
368 | this.handleRequestError(OCPP16RequestCommand.METER_VALUES, error); | |
369 | } | |
326f6e38 | 370 | } |
6ed92bc1 | 371 | |
47e22477 | 372 | public async sendDiagnosticsStatusNotification(diagnosticsStatus: OCPP16DiagnosticsStatus): Promise<void> { |
290d006c JB |
373 | try { |
374 | const payload: DiagnosticsStatusNotificationRequest = { | |
375 | status: diagnosticsStatus | |
376 | }; | |
377 | await this.sendMessage(Utils.generateUUID(), payload, MessageType.CALL_MESSAGE, OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION); | |
378 | } catch (error) { | |
379 | this.handleRequestError(OCPP16RequestCommand.METER_VALUES, error); | |
380 | } | |
47e22477 JB |
381 | } |
382 | ||
6ed92bc1 | 383 | public async sendError(messageId: string, error: OCPPError, commandName: OCPP16RequestCommand | OCPP16IncomingRequestCommand): Promise<unknown> { |
290d006c JB |
384 | try { |
385 | // Send error | |
2891f761 | 386 | return await this.sendMessage(messageId, error, MessageType.CALL_ERROR_MESSAGE, commandName); |
290d006c JB |
387 | } catch (err) { |
388 | this.handleRequestError(commandName as OCPP16RequestCommand, err); | |
389 | } | |
6ed92bc1 | 390 | } |
c0560973 | 391 | } |