1 // Partial Copyright Jerome Benoit. 2021. All Rights Reserved.
3 import { ACElectricUtils
, DCElectricUtils
} from
'../../../utils/ElectricUtils';
4 import { AuthorizeRequest
, OCPP16AuthorizeResponse
, OCPP16StartTransactionResponse
, OCPP16StopTransactionReason
, OCPP16StopTransactionResponse
, StartTransactionRequest
, StopTransactionRequest
} from
'../../../types/ocpp/1.6/Transaction';
5 import { CurrentType
, Voltage
} from
'../../../types/ChargingStationTemplate';
6 import { DiagnosticsStatusNotificationRequest
, HeartbeatRequest
, OCPP16BootNotificationRequest
, OCPP16RequestCommand
, StatusNotificationRequest
} from
'../../../types/ocpp/1.6/Requests';
7 import { MeterValueUnit
, MeterValuesRequest
, OCPP16MeterValue
, OCPP16MeterValueMeasurand
, OCPP16MeterValuePhase
} from
'../../../types/ocpp/1.6/MeterValues';
9 import type ChargingStation from
'../../ChargingStation';
10 import Constants from
'../../../utils/Constants';
11 import { ErrorType
} from
'../../../types/ocpp/ErrorType';
12 import MeasurandPerPhaseSampledValueTemplates from
'../../../types/MeasurandPerPhaseSampledValueTemplates';
13 import MeasurandValues from
'../../../types/MeasurandValues';
14 import { OCPP16BootNotificationResponse
} from
'../../../types/ocpp/1.6/Responses';
15 import { OCPP16ChargePointErrorCode
} from
'../../../types/ocpp/1.6/ChargePointErrorCode';
16 import { OCPP16ChargePointStatus
} from
'../../../types/ocpp/1.6/ChargePointStatus';
17 import { OCPP16DiagnosticsStatus
} from
'../../../types/ocpp/1.6/DiagnosticsStatus';
18 import { OCPP16ServiceUtils
} from
'./OCPP16ServiceUtils';
19 import OCPPError from
'../../../exception/OCPPError';
20 import OCPPRequestService from
'../OCPPRequestService';
21 import type OCPPResponseService from
'../OCPPResponseService';
22 import { SendParams
} from
'../../../types/ocpp/Requests';
23 import Utils from
'../../../utils/Utils';
24 import logger from
'../../../utils/Logger';
26 export default class OCPP16RequestService
extends OCPPRequestService
{
27 public constructor(chargingStation
: ChargingStation
, ocppResponseService
: OCPPResponseService
) {
28 if (new.target
?.name
=== 'OCPP16RequestService') {
29 throw new TypeError(`Cannot construct ${new.target?.name} instances directly`);
31 super(chargingStation
, ocppResponseService
);
34 public async sendHeartbeat(params
?: SendParams
): Promise
<void> {
35 const payload
: HeartbeatRequest
= {};
36 await this.sendMessage(Utils
.generateUUID(), payload
, OCPP16RequestCommand
.HEARTBEAT
, params
);
39 public async sendBootNotification(chargePointModel
: string, chargePointVendor
: string, chargeBoxSerialNumber
?: string, firmwareVersion
?: string,
40 chargePointSerialNumber
?: string, iccid
?: string, imsi
?: string, meterSerialNumber
?: string, meterType
?: string,
41 params
?: SendParams
): Promise
<OCPP16BootNotificationResponse
> {
42 const payload
: OCPP16BootNotificationRequest
= {
45 ...!Utils
.isUndefined(chargeBoxSerialNumber
) && { chargeBoxSerialNumber
},
46 ...!Utils
.isUndefined(chargePointSerialNumber
) && { chargePointSerialNumber
},
47 ...!Utils
.isUndefined(firmwareVersion
) && { firmwareVersion
},
48 ...!Utils
.isUndefined(iccid
) && { iccid
},
49 ...!Utils
.isUndefined(imsi
) && { imsi
},
50 ...!Utils
.isUndefined(meterSerialNumber
) && { meterSerialNumber
},
51 ...!Utils
.isUndefined(meterType
) && { meterType
}
53 return await this.sendMessage(Utils
.generateUUID(), payload
, OCPP16RequestCommand
.BOOT_NOTIFICATION
,
54 { ...params
, skipBufferingOnError
: true }) as OCPP16BootNotificationResponse
;
57 public async sendStatusNotification(connectorId
: number, status: OCPP16ChargePointStatus
,
58 errorCode
: OCPP16ChargePointErrorCode
= OCPP16ChargePointErrorCode
.NO_ERROR
): Promise
<void> {
59 const payload
: StatusNotificationRequest
= {
64 await this.sendMessage(Utils
.generateUUID(), payload
, OCPP16RequestCommand
.STATUS_NOTIFICATION
);
67 public async sendAuthorize(connectorId
: number, idTag
?: string): Promise
<OCPP16AuthorizeResponse
> {
68 const payload
: AuthorizeRequest
= {
69 ...!Utils
.isUndefined(idTag
) ? { idTag
} : { idTag
: Constants
.TRANSACTION_DEFAULT_IDTAG
},
71 this.chargingStation
.getConnectorStatus(connectorId
).authorizeIdTag
= idTag
;
72 return await this.sendMessage(Utils
.generateUUID(), payload
, OCPP16RequestCommand
.AUTHORIZE
) as OCPP16AuthorizeResponse
;
75 public async sendStartTransaction(connectorId
: number, idTag
?: string): Promise
<OCPP16StartTransactionResponse
> {
76 const payload
: StartTransactionRequest
= {
78 ...!Utils
.isUndefined(idTag
) ? { idTag
} : { idTag
: Constants
.TRANSACTION_DEFAULT_IDTAG
},
79 meterStart
: this.chargingStation
.getEnergyActiveImportRegisterByConnectorId(connectorId
),
80 timestamp
: new Date().toISOString(),
82 return await this.sendMessage(Utils
.generateUUID(), payload
, OCPP16RequestCommand
.START_TRANSACTION
) as OCPP16StartTransactionResponse
;
85 public async sendStopTransaction(transactionId
: number, meterStop
: number, idTag
?: string,
86 reason
: OCPP16StopTransactionReason
= OCPP16StopTransactionReason
.NONE
): Promise
<OCPP16StopTransactionResponse
> {
87 let connectorId
: number;
88 for (const id
of this.chargingStation
.connectors
.keys()) {
89 if (id
> 0 && this.chargingStation
.getConnectorStatus(id
)?.transactionId
=== transactionId
) {
94 const transactionEndMeterValue
= OCPP16ServiceUtils
.buildTransactionEndMeterValue(this.chargingStation
, connectorId
, meterStop
);
95 // FIXME: should be a callback, each OCPP commands implementation must do only one job
96 (this.chargingStation
.getBeginEndMeterValues() && this.chargingStation
.getOcppStrictCompliance() && !this.chargingStation
.getOutOfOrderEndMeterValues())
97 && await this.sendTransactionEndMeterValues(connectorId
, transactionId
, transactionEndMeterValue
);
98 const payload
: StopTransactionRequest
= {
100 ...!Utils
.isUndefined(idTag
) && { idTag
},
102 timestamp
: new Date().toISOString(),
103 ...reason
&& { reason
},
104 ...this.chargingStation
.getTransactionDataMeterValues() && { transactionData
: OCPP16ServiceUtils
.buildTransactionDataMeterValues(this.chargingStation
.getConnectorStatus(connectorId
).transactionBeginMeterValue
, transactionEndMeterValue
) },
106 return await this.sendMessage(Utils
.generateUUID(), payload
, OCPP16RequestCommand
.STOP_TRANSACTION
) as OCPP16StartTransactionResponse
;
109 public async sendMeterValues(connectorId
: number, transactionId
: number, interval
: number, debug
= false): Promise
<void> {
110 const meterValue
: OCPP16MeterValue
= {
111 timestamp
: new Date().toISOString(),
114 const connector
= this.chargingStation
.getConnectorStatus(connectorId
);
116 const socSampledValueTemplate
= this.chargingStation
.getSampledValueTemplate(connectorId
, OCPP16MeterValueMeasurand
.STATE_OF_CHARGE
);
117 if (socSampledValueTemplate
) {
118 const socSampledValueTemplateValue
= socSampledValueTemplate
.value
119 ? Utils
.getRandomFloatFluctuatedRounded(parseInt(socSampledValueTemplate
.value
), socSampledValueTemplate
.fluctuationPercent
?? Constants
.DEFAULT_FLUCTUATION_PERCENT
)
120 : Utils
.getRandomInteger(100);
121 meterValue
.sampledValue
.push(OCPP16ServiceUtils
.buildSampledValue(socSampledValueTemplate
, socSampledValueTemplateValue
));
122 const sampledValuesIndex
= meterValue
.sampledValue
.length
- 1;
123 if (Utils
.convertToInt(meterValue
.sampledValue
[sampledValuesIndex
].value
) > 100 || debug
) {
124 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`);
128 const voltageSampledValueTemplate
= this.chargingStation
.getSampledValueTemplate(connectorId
, OCPP16MeterValueMeasurand
.VOLTAGE
);
129 if (voltageSampledValueTemplate
) {
130 const voltageSampledValueTemplateValue
= voltageSampledValueTemplate
.value
? parseInt(voltageSampledValueTemplate
.value
) : this.chargingStation
.getVoltageOut();
131 const fluctuationPercent
= voltageSampledValueTemplate
.fluctuationPercent
?? Constants
.DEFAULT_FLUCTUATION_PERCENT
;
132 const voltageMeasurandValue
= Utils
.getRandomFloatFluctuatedRounded(voltageSampledValueTemplateValue
, fluctuationPercent
);
133 if (this.chargingStation
.getNumberOfPhases() !== 3 || (this.chargingStation
.getNumberOfPhases() === 3 && this.chargingStation
.getMainVoltageMeterValues())) {
134 meterValue
.sampledValue
.push(OCPP16ServiceUtils
.buildSampledValue(voltageSampledValueTemplate
, voltageMeasurandValue
));
136 for (let phase
= 1; this.chargingStation
.getNumberOfPhases() === 3 && phase
<= this.chargingStation
.getNumberOfPhases(); phase
++) {
137 const phaseLineToNeutralValue
= `L${phase}-N`;
138 const voltagePhaseLineToNeutralSampledValueTemplate
= this.chargingStation
.getSampledValueTemplate(connectorId
, OCPP16MeterValueMeasurand
.VOLTAGE
,
139 phaseLineToNeutralValue
as OCPP16MeterValuePhase
);
140 let voltagePhaseLineToNeutralMeasurandValue
: number;
141 if (voltagePhaseLineToNeutralSampledValueTemplate
) {
142 const voltagePhaseLineToNeutralSampledValueTemplateValue
= voltagePhaseLineToNeutralSampledValueTemplate
.value
? parseInt(voltagePhaseLineToNeutralSampledValueTemplate
.value
) : this.chargingStation
.getVoltageOut();
143 const fluctuationPhaseToNeutralPercent
= voltagePhaseLineToNeutralSampledValueTemplate
.fluctuationPercent
?? Constants
.DEFAULT_FLUCTUATION_PERCENT
;
144 voltagePhaseLineToNeutralMeasurandValue
= Utils
.getRandomFloatFluctuatedRounded(voltagePhaseLineToNeutralSampledValueTemplateValue
, fluctuationPhaseToNeutralPercent
);
146 meterValue
.sampledValue
.push(OCPP16ServiceUtils
.buildSampledValue(voltagePhaseLineToNeutralSampledValueTemplate
?? voltageSampledValueTemplate
,
147 voltagePhaseLineToNeutralMeasurandValue
?? voltageMeasurandValue
, null, phaseLineToNeutralValue
as OCPP16MeterValuePhase
));
148 if (this.chargingStation
.getPhaseLineToLineVoltageMeterValues()) {
149 const phaseLineToLineValue
= `L${phase}-L${(phase + 1) % this.chargingStation.getNumberOfPhases() !== 0 ? (phase + 1) % this.chargingStation.getNumberOfPhases() : this.chargingStation.getNumberOfPhases()}`;
150 const voltagePhaseLineToLineSampledValueTemplate
= this.chargingStation
.getSampledValueTemplate(connectorId
, OCPP16MeterValueMeasurand
.VOLTAGE
, phaseLineToLineValue
as OCPP16MeterValuePhase
);
151 let voltagePhaseLineToLineMeasurandValue
: number;
152 if (voltagePhaseLineToLineSampledValueTemplate
) {
153 const voltagePhaseLineToLineSampledValueTemplateValue
= voltagePhaseLineToLineSampledValueTemplate
.value
? parseInt(voltagePhaseLineToLineSampledValueTemplate
.value
) : Voltage
.VOLTAGE_400
;
154 const fluctuationPhaseLineToLinePercent
= voltagePhaseLineToLineSampledValueTemplate
.fluctuationPercent
?? Constants
.DEFAULT_FLUCTUATION_PERCENT
;
155 voltagePhaseLineToLineMeasurandValue
= Utils
.getRandomFloatFluctuatedRounded(voltagePhaseLineToLineSampledValueTemplateValue
, fluctuationPhaseLineToLinePercent
);
157 const defaultVoltagePhaseLineToLineMeasurandValue
= Utils
.getRandomFloatFluctuatedRounded(Voltage
.VOLTAGE_400
, fluctuationPercent
);
158 meterValue
.sampledValue
.push(OCPP16ServiceUtils
.buildSampledValue(voltagePhaseLineToLineSampledValueTemplate
?? voltageSampledValueTemplate
,
159 voltagePhaseLineToLineMeasurandValue
?? defaultVoltagePhaseLineToLineMeasurandValue
, null, phaseLineToLineValue
as OCPP16MeterValuePhase
));
163 // Power.Active.Import measurand
164 const powerSampledValueTemplate
= this.chargingStation
.getSampledValueTemplate(connectorId
, OCPP16MeterValueMeasurand
.POWER_ACTIVE_IMPORT
);
165 let powerPerPhaseSampledValueTemplates
: MeasurandPerPhaseSampledValueTemplates
= {};
166 if (this.chargingStation
.getNumberOfPhases() === 3) {
167 powerPerPhaseSampledValueTemplates
= {
168 L1
: this.chargingStation
.getSampledValueTemplate(connectorId
, OCPP16MeterValueMeasurand
.POWER_ACTIVE_IMPORT
, OCPP16MeterValuePhase
.L1_N
),
169 L2
: this.chargingStation
.getSampledValueTemplate(connectorId
, OCPP16MeterValueMeasurand
.POWER_ACTIVE_IMPORT
, OCPP16MeterValuePhase
.L2_N
),
170 L3
: this.chargingStation
.getSampledValueTemplate(connectorId
, OCPP16MeterValueMeasurand
.POWER_ACTIVE_IMPORT
, OCPP16MeterValuePhase
.L3_N
),
173 if (powerSampledValueTemplate
) {
174 OCPP16ServiceUtils
.checkMeasurandPowerDivider(this.chargingStation
, powerSampledValueTemplate
.measurand
);
175 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`;
176 const powerMeasurandValues
= {} as MeasurandValues
;
177 const unitDivider
= powerSampledValueTemplate
?.unit
=== MeterValueUnit
.KILO_WATT
? 1000 : 1;
178 const maxPower
= Math.round(this.chargingStation
.stationInfo
.maxPower
/ this.chargingStation
.stationInfo
.powerDivider
);
179 const maxPowerPerPhase
= Math.round((this.chargingStation
.stationInfo
.maxPower
/ this.chargingStation
.stationInfo
.powerDivider
) / this.chargingStation
.getNumberOfPhases());
180 switch (this.chargingStation
.getCurrentOutType()) {
182 if (this.chargingStation
.getNumberOfPhases() === 3) {
183 const defaultFluctuatedPowerPerPhase
= powerSampledValueTemplate
.value
184 && Utils
.getRandomFloatFluctuatedRounded(parseInt(powerSampledValueTemplate
.value
) / this.chargingStation
.getNumberOfPhases(), powerSampledValueTemplate
.fluctuationPercent
?? Constants
.DEFAULT_FLUCTUATION_PERCENT
);
185 const phase1FluctuatedValue
= powerPerPhaseSampledValueTemplates
?.L1
?.value
186 && Utils
.getRandomFloatFluctuatedRounded(parseInt(powerPerPhaseSampledValueTemplates
.L1
.value
), powerPerPhaseSampledValueTemplates
.L1
.fluctuationPercent
?? Constants
.DEFAULT_FLUCTUATION_PERCENT
);
187 const phase2FluctuatedValue
= powerPerPhaseSampledValueTemplates
?.L2
?.value
188 && Utils
.getRandomFloatFluctuatedRounded(parseInt(powerPerPhaseSampledValueTemplates
.L2
.value
), powerPerPhaseSampledValueTemplates
.L2
.fluctuationPercent
?? Constants
.DEFAULT_FLUCTUATION_PERCENT
);
189 const phase3FluctuatedValue
= powerPerPhaseSampledValueTemplates
?.L3
?.value
190 && Utils
.getRandomFloatFluctuatedRounded(parseInt(powerPerPhaseSampledValueTemplates
.L3
.value
), powerPerPhaseSampledValueTemplates
.L3
.fluctuationPercent
?? Constants
.DEFAULT_FLUCTUATION_PERCENT
);
191 powerMeasurandValues
.L1
= (phase1FluctuatedValue
?? defaultFluctuatedPowerPerPhase
) ?? Utils
.getRandomFloatRounded(maxPowerPerPhase
/ unitDivider
);
192 powerMeasurandValues
.L2
= (phase2FluctuatedValue
?? defaultFluctuatedPowerPerPhase
) ?? Utils
.getRandomFloatRounded(maxPowerPerPhase
/ unitDivider
);
193 powerMeasurandValues
.L3
= (phase3FluctuatedValue
?? defaultFluctuatedPowerPerPhase
) ?? Utils
.getRandomFloatRounded(maxPowerPerPhase
/ unitDivider
);
195 powerMeasurandValues
.L1
= powerSampledValueTemplate
.value
196 ? Utils
.getRandomFloatFluctuatedRounded(parseInt(powerSampledValueTemplate
.value
), powerSampledValueTemplate
.fluctuationPercent
?? Constants
.DEFAULT_FLUCTUATION_PERCENT
)
197 : Utils
.getRandomFloatRounded(maxPower
/ unitDivider
);
198 powerMeasurandValues
.L2
= 0;
199 powerMeasurandValues
.L3
= 0;
201 powerMeasurandValues
.allPhases
= Utils
.roundTo(powerMeasurandValues
.L1
+ powerMeasurandValues
.L2
+ powerMeasurandValues
.L3
, 2);
204 powerMeasurandValues
.allPhases
= powerSampledValueTemplate
.value
205 ? Utils
.getRandomFloatFluctuatedRounded(parseInt(powerSampledValueTemplate
.value
), powerSampledValueTemplate
.fluctuationPercent
?? Constants
.DEFAULT_FLUCTUATION_PERCENT
)
206 : Utils
.getRandomFloatRounded(maxPower
/ unitDivider
);
209 logger
.error(errMsg
);
210 throw new OCPPError(ErrorType
.INTERNAL_ERROR
, errMsg
, OCPP16RequestCommand
.METER_VALUES
);
212 meterValue
.sampledValue
.push(OCPP16ServiceUtils
.buildSampledValue(powerSampledValueTemplate
, powerMeasurandValues
.allPhases
));
213 const sampledValuesIndex
= meterValue
.sampledValue
.length
- 1;
214 const maxPowerRounded
= Utils
.roundTo(maxPower
/ unitDivider
, 2);
215 if (Utils
.convertToFloat(meterValue
.sampledValue
[sampledValuesIndex
].value
) > maxPowerRounded
|| debug
) {
216 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}`);
218 for (let phase
= 1; this.chargingStation
.getNumberOfPhases() === 3 && phase
<= this.chargingStation
.getNumberOfPhases(); phase
++) {
219 const phaseValue
= `L${phase}-N`;
220 meterValue
.sampledValue
.push(OCPP16ServiceUtils
.buildSampledValue(powerPerPhaseSampledValueTemplates
[`L${phase}`] ?? powerSampledValueTemplate
, powerMeasurandValues
[`L${phase}`], null,
221 phaseValue
as OCPP16MeterValuePhase
));
222 const sampledValuesPerPhaseIndex
= meterValue
.sampledValue
.length
- 1;
223 const maxPowerPerPhaseRounded
= Utils
.roundTo(maxPowerPerPhase
/ unitDivider
, 2);
224 if (Utils
.convertToFloat(meterValue
.sampledValue
[sampledValuesPerPhaseIndex
].value
) > maxPowerPerPhaseRounded
|| debug
) {
225 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}`);
229 // Current.Import measurand
230 const currentSampledValueTemplate
= this.chargingStation
.getSampledValueTemplate(connectorId
, OCPP16MeterValueMeasurand
.CURRENT_IMPORT
);
231 let currentPerPhaseSampledValueTemplates
: MeasurandPerPhaseSampledValueTemplates
= {};
232 if (this.chargingStation
.getNumberOfPhases() === 3) {
233 currentPerPhaseSampledValueTemplates
= {
234 L1
: this.chargingStation
.getSampledValueTemplate(connectorId
, OCPP16MeterValueMeasurand
.CURRENT_IMPORT
, OCPP16MeterValuePhase
.L1
),
235 L2
: this.chargingStation
.getSampledValueTemplate(connectorId
, OCPP16MeterValueMeasurand
.CURRENT_IMPORT
, OCPP16MeterValuePhase
.L2
),
236 L3
: this.chargingStation
.getSampledValueTemplate(connectorId
, OCPP16MeterValueMeasurand
.CURRENT_IMPORT
, OCPP16MeterValuePhase
.L3
),
239 if (currentSampledValueTemplate
) {
240 OCPP16ServiceUtils
.checkMeasurandPowerDivider(this.chargingStation
, currentSampledValueTemplate
.measurand
);
241 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`;
242 const currentMeasurandValues
: MeasurandValues
= {} as MeasurandValues
;
243 let maxAmperage
: number;
244 switch (this.chargingStation
.getCurrentOutType()) {
246 maxAmperage
= ACElectricUtils
.amperagePerPhaseFromPower(this.chargingStation
.getNumberOfPhases(), this.chargingStation
.stationInfo
.maxPower
/ this.chargingStation
.stationInfo
.powerDivider
, this.chargingStation
.getVoltageOut());
247 if (this.chargingStation
.getNumberOfPhases() === 3) {
248 const defaultFluctuatedAmperagePerPhase
= currentSampledValueTemplate
.value
249 && Utils
.getRandomFloatFluctuatedRounded(parseInt(currentSampledValueTemplate
.value
), currentSampledValueTemplate
.fluctuationPercent
?? Constants
.DEFAULT_FLUCTUATION_PERCENT
);
250 const phase1FluctuatedValue
= currentPerPhaseSampledValueTemplates
?.L1
?.value
251 && Utils
.getRandomFloatFluctuatedRounded(parseInt(currentPerPhaseSampledValueTemplates
.L1
.value
), currentPerPhaseSampledValueTemplates
.L1
.fluctuationPercent
?? Constants
.DEFAULT_FLUCTUATION_PERCENT
);
252 const phase2FluctuatedValue
= currentPerPhaseSampledValueTemplates
?.L2
?.value
253 && Utils
.getRandomFloatFluctuatedRounded(parseInt(currentPerPhaseSampledValueTemplates
.L2
.value
), currentPerPhaseSampledValueTemplates
.L2
.fluctuationPercent
?? Constants
.DEFAULT_FLUCTUATION_PERCENT
);
254 const phase3FluctuatedValue
= currentPerPhaseSampledValueTemplates
?.L3
?.value
255 && Utils
.getRandomFloatFluctuatedRounded(parseInt(currentPerPhaseSampledValueTemplates
.L3
.value
), currentPerPhaseSampledValueTemplates
.L3
.fluctuationPercent
?? Constants
.DEFAULT_FLUCTUATION_PERCENT
);
256 currentMeasurandValues
.L1
= (phase1FluctuatedValue
?? defaultFluctuatedAmperagePerPhase
) ?? Utils
.getRandomFloatRounded(maxAmperage
);
257 currentMeasurandValues
.L2
= (phase2FluctuatedValue
?? defaultFluctuatedAmperagePerPhase
) ?? Utils
.getRandomFloatRounded(maxAmperage
);
258 currentMeasurandValues
.L3
= (phase3FluctuatedValue
?? defaultFluctuatedAmperagePerPhase
) ?? Utils
.getRandomFloatRounded(maxAmperage
);
260 currentMeasurandValues
.L1
= currentSampledValueTemplate
.value
261 ? Utils
.getRandomFloatFluctuatedRounded(parseInt(currentSampledValueTemplate
.value
), currentSampledValueTemplate
.fluctuationPercent
?? Constants
.DEFAULT_FLUCTUATION_PERCENT
)
262 : Utils
.getRandomFloatRounded(maxAmperage
);
263 currentMeasurandValues
.L2
= 0;
264 currentMeasurandValues
.L3
= 0;
266 currentMeasurandValues
.allPhases
= Utils
.roundTo((currentMeasurandValues
.L1
+ currentMeasurandValues
.L2
+ currentMeasurandValues
.L3
) / this.chargingStation
.getNumberOfPhases(), 2);
269 maxAmperage
= DCElectricUtils
.amperage(this.chargingStation
.stationInfo
.maxPower
/ this.chargingStation
.stationInfo
.powerDivider
, this.chargingStation
.getVoltageOut());
270 currentMeasurandValues
.allPhases
= currentSampledValueTemplate
.value
271 ? Utils
.getRandomFloatFluctuatedRounded(parseInt(currentSampledValueTemplate
.value
), currentSampledValueTemplate
.fluctuationPercent
?? Constants
.DEFAULT_FLUCTUATION_PERCENT
)
272 : Utils
.getRandomFloatRounded(maxAmperage
);
275 logger
.error(errMsg
);
276 throw new OCPPError(ErrorType
.INTERNAL_ERROR
, errMsg
, OCPP16RequestCommand
.METER_VALUES
);
278 meterValue
.sampledValue
.push(OCPP16ServiceUtils
.buildSampledValue(currentSampledValueTemplate
, currentMeasurandValues
.allPhases
));
279 const sampledValuesIndex
= meterValue
.sampledValue
.length
- 1;
280 if (Utils
.convertToFloat(meterValue
.sampledValue
[sampledValuesIndex
].value
) > maxAmperage
|| debug
) {
281 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}`);
283 for (let phase
= 1; this.chargingStation
.getNumberOfPhases() === 3 && phase
<= this.chargingStation
.getNumberOfPhases(); phase
++) {
284 const phaseValue
= `L${phase}`;
285 meterValue
.sampledValue
.push(OCPP16ServiceUtils
.buildSampledValue(currentPerPhaseSampledValueTemplates
[phaseValue
] ?? currentSampledValueTemplate
,
286 currentMeasurandValues
[phaseValue
], null, phaseValue
as OCPP16MeterValuePhase
));
287 const sampledValuesPerPhaseIndex
= meterValue
.sampledValue
.length
- 1;
288 if (Utils
.convertToFloat(meterValue
.sampledValue
[sampledValuesPerPhaseIndex
].value
) > maxAmperage
|| debug
) {
289 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}`);
293 // Energy.Active.Import.Register measurand (default)
294 const energySampledValueTemplate
= this.chargingStation
.getSampledValueTemplate(connectorId
);
295 if (energySampledValueTemplate
) {
296 OCPP16ServiceUtils
.checkMeasurandPowerDivider(this.chargingStation
, energySampledValueTemplate
.measurand
);
297 const unitDivider
= energySampledValueTemplate
?.unit
=== MeterValueUnit
.KILO_WATT_HOUR
? 1000 : 1;
298 const maxEnergyRounded
= Utils
.roundTo(((this.chargingStation
.stationInfo
.maxPower
/ this.chargingStation
.stationInfo
.powerDivider
) * interval
) / (3600 * 1000), 2);
299 const energyValueRounded
= energySampledValueTemplate
.value
300 // Cumulate the fluctuated value around the static one
301 ? Utils
.getRandomFloatFluctuatedRounded(parseInt(energySampledValueTemplate
.value
), energySampledValueTemplate
.fluctuationPercent
?? Constants
.DEFAULT_FLUCTUATION_PERCENT
)
302 : Utils
.getRandomFloatRounded(maxEnergyRounded
);
303 // Persist previous value on connector
304 if (connector
&& !Utils
.isNullOrUndefined(connector
.energyActiveImportRegisterValue
) && connector
.energyActiveImportRegisterValue
>= 0 &&
305 !Utils
.isNullOrUndefined(connector
.transactionEnergyActiveImportRegisterValue
) && connector
.transactionEnergyActiveImportRegisterValue
>= 0) {
306 connector
.energyActiveImportRegisterValue
+= energyValueRounded
;
307 connector
.transactionEnergyActiveImportRegisterValue
+= energyValueRounded
;
309 connector
.energyActiveImportRegisterValue
= 0;
310 connector
.transactionEnergyActiveImportRegisterValue
= 0;
312 meterValue
.sampledValue
.push(OCPP16ServiceUtils
.buildSampledValue(energySampledValueTemplate
,
313 Utils
.roundTo(this.chargingStation
.getEnergyActiveImportRegisterByTransactionId(transactionId
) / unitDivider
, 2)));
314 const sampledValuesIndex
= meterValue
.sampledValue
.length
- 1;
315 if (energyValueRounded
> maxEnergyRounded
|| debug
) {
316 logger
.error(`${this.chargingStation.logPrefix()} MeterValues measurand ${meterValue.sampledValue[sampledValuesIndex].measurand ?? OCPP16MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER}: connectorId ${connectorId}, transaction ${connector.transactionId}, value: ${energyValueRounded}/${maxEnergyRounded}, duration: ${Utils.roundTo(interval / (3600 * 1000), 4)}h`);
319 const payload
: MeterValuesRequest
= {
322 meterValue
: [meterValue
],
324 await this.sendMessage(Utils
.generateUUID(), payload
, OCPP16RequestCommand
.METER_VALUES
);
327 public async sendTransactionBeginMeterValues(connectorId
: number, transactionId
: number, beginMeterValue
: OCPP16MeterValue
): Promise
<void> {
328 const payload
: MeterValuesRequest
= {
331 meterValue
: [beginMeterValue
],
333 await this.sendMessage(Utils
.generateUUID(), payload
, OCPP16RequestCommand
.METER_VALUES
);
336 public async sendTransactionEndMeterValues(connectorId
: number, transactionId
: number, endMeterValue
: OCPP16MeterValue
): Promise
<void> {
337 const payload
: MeterValuesRequest
= {
340 meterValue
: [endMeterValue
],
342 await this.sendMessage(Utils
.generateUUID(), payload
, OCPP16RequestCommand
.METER_VALUES
);
345 public async sendDiagnosticsStatusNotification(diagnosticsStatus
: OCPP16DiagnosticsStatus
): Promise
<void> {
346 const payload
: DiagnosticsStatusNotificationRequest
= {
347 status: diagnosticsStatus
349 await this.sendMessage(Utils
.generateUUID(), payload
, OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
);