import BaseError from '../exception/BaseError';
import type OCPPError from '../exception/OCPPError';
+import { StandardParametersKey } from '../types/ocpp/Configuration';
import {
HeartbeatRequest,
+ MeterValuesRequest,
RequestCommand,
type StatusNotificationRequest,
} from '../types/ocpp/Requests';
-import type { HeartbeatResponse, StatusNotificationResponse } from '../types/ocpp/Responses';
+import type {
+ HeartbeatResponse,
+ MeterValuesResponse,
+ StatusNotificationResponse,
+} from '../types/ocpp/Responses';
import {
AuthorizationStatus,
AuthorizeRequest,
MessageEvent,
} from '../types/WorkerBroadcastChannel';
import { ResponseStatus } from '../ui/web/src/types/UIProtocol';
+import Constants from '../utils/Constants';
import logger from '../utils/Logger';
import Utils from '../utils/Utils';
import type ChargingStation from './ChargingStation';
+import { ChargingStationConfigurationUtils } from './ChargingStationConfigurationUtils';
+import { OCPP16ServiceUtils } from './ocpp/1.6/OCPP16ServiceUtils';
import WorkerBroadcastChannel from './WorkerBroadcastChannel';
const moduleName = 'ChargingStationWorkerBroadcastChannel';
| StopTransactionResponse
| AuthorizeResponse
| StatusNotificationResponse
- | HeartbeatResponse;
+ | HeartbeatResponse
+ | MeterValuesResponse;
type CommandHandler = (
requestPayload?: BroadcastChannelRequestPayload
BroadcastChannelProcedureName.CLOSE_CONNECTION,
() => this.chargingStation.closeWSConnection(),
],
+ [
+ BroadcastChannelProcedureName.START_AUTOMATIC_TRANSACTION_GENERATOR,
+ (requestPayload?: BroadcastChannelRequestPayload) =>
+ this.chargingStation.startAutomaticTransactionGenerator(requestPayload.connectorIds),
+ ],
+ [
+ BroadcastChannelProcedureName.STOP_AUTOMATIC_TRANSACTION_GENERATOR,
+ (requestPayload?: BroadcastChannelRequestPayload) =>
+ this.chargingStation.stopAutomaticTransactionGenerator(requestPayload.connectorIds),
+ ],
[
BroadcastChannelProcedureName.START_TRANSACTION,
async (requestPayload?: BroadcastChannelRequestPayload) =>
StartTransactionResponse
>(this.chargingStation, RequestCommand.STOP_TRANSACTION, {
...requestPayload,
- meterStop: this.chargingStation.getEnergyActiveImportRegisterByTransactionId(
- requestPayload.transactionId,
- true
- ),
+ meterStop:
+ requestPayload.meterStop ??
+ this.chargingStation.getEnergyActiveImportRegisterByTransactionId(
+ requestPayload.transactionId,
+ true
+ ),
}),
],
- [
- BroadcastChannelProcedureName.START_AUTOMATIC_TRANSACTION_GENERATOR,
- (requestPayload?: BroadcastChannelRequestPayload) =>
- this.chargingStation.startAutomaticTransactionGenerator(requestPayload.connectorIds),
- ],
- [
- BroadcastChannelProcedureName.STOP_AUTOMATIC_TRANSACTION_GENERATOR,
- (requestPayload?: BroadcastChannelRequestPayload) =>
- this.chargingStation.stopAutomaticTransactionGenerator(requestPayload.connectorIds),
- ],
[
BroadcastChannelProcedureName.AUTHORIZE,
async (requestPayload?: BroadcastChannelRequestPayload) =>
HeartbeatResponse
>(this.chargingStation, RequestCommand.HEARTBEAT, requestPayload),
],
+ [
+ BroadcastChannelProcedureName.METER_VALUES,
+ async (requestPayload?: BroadcastChannelRequestPayload) => {
+ const configuredMeterValueSampleInterval =
+ ChargingStationConfigurationUtils.getConfigurationKey(
+ chargingStation,
+ StandardParametersKey.MeterValueSampleInterval
+ );
+ return this.chargingStation.ocppRequestService.requestHandler<
+ MeterValuesRequest,
+ MeterValuesResponse
+ >(this.chargingStation, RequestCommand.METER_VALUES, {
+ ...requestPayload,
+ meterValue: requestPayload.meterValue ?? [
+ OCPP16ServiceUtils.buildMeterValue(
+ this.chargingStation,
+ requestPayload.connectorId,
+ this.chargingStation.getConnectorStatus(requestPayload.connectorId)?.transactionId,
+ configuredMeterValueSampleInterval
+ ? Utils.convertToInt(configuredMeterValueSampleInterval.value) * 1000
+ : Constants.DEFAULT_METER_VALUES_INTERVAL
+ ),
+ ],
+ });
+ },
+ ],
]);
this.chargingStation = chargingStation;
this.onmessage = this.requestHandler.bind(this) as (message: MessageEvent) => void;
);
return;
}
-
let responsePayload: BroadcastChannelResponsePayload;
let commandResponse: CommandResponse | void;
try {
status: ResponseStatus.SUCCESS,
};
} else {
- const responseStatus = this.commandResponseToResponseStatus(
+ responsePayload = this.commandResponseToResponsePayload(
command,
+ requestPayload,
commandResponse as CommandResponse
);
- if (responseStatus === ResponseStatus.SUCCESS) {
- responsePayload = {
- hashId: this.chargingStation.stationInfo.hashId,
- status: responseStatus,
- };
- } else {
- responsePayload = {
- hashId: this.chargingStation.stationInfo.hashId,
- status: responseStatus,
- command,
- requestPayload,
- commandResponse: commandResponse as CommandResponse,
- };
- }
}
} catch (error) {
logger.error(
errorStack: (error as Error).stack,
errorDetails: (error as OCPPError).details,
};
+ } finally {
+ this.sendResponse([uuid, responsePayload]);
}
- this.sendResponse([uuid, responsePayload]);
}
private messageErrorHandler(messageEvent: MessageEvent): void {
].includes(command) === false && delete requestPayload.connectorIds;
}
+ private commandResponseToResponsePayload(
+ command: BroadcastChannelProcedureName,
+ requestPayload: BroadcastChannelRequestPayload,
+ commandResponse: CommandResponse
+ ): BroadcastChannelResponsePayload {
+ const responseStatus = this.commandResponseToResponseStatus(command, commandResponse);
+ if (responseStatus === ResponseStatus.SUCCESS) {
+ return {
+ hashId: this.chargingStation.stationInfo.hashId,
+ status: responseStatus,
+ };
+ }
+ return {
+ hashId: this.chargingStation.stationInfo.hashId,
+ status: responseStatus,
+ command,
+ requestPayload,
+ commandResponse,
+ };
+ }
+
private commandResponseToResponseStatus(
command: BroadcastChannelProcedureName,
commandResponse: CommandResponse
}
return ResponseStatus.FAILURE;
case BroadcastChannelProcedureName.STATUS_NOTIFICATION:
+ case BroadcastChannelProcedureName.METER_VALUES:
if (Utils.isEmptyObject(commandResponse) === true) {
return ResponseStatus.SUCCESS;
}