-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,
- MeterValuesResponse,
- StatusNotificationResponse,
-} from '../types/ocpp/Responses';
+ type ChargingStation,
+ ChargingStationConfigurationUtils,
+ WorkerBroadcastChannel,
+} from './internal';
+import { OCPP16ServiceUtils } from './ocpp';
+import { BaseError, type OCPPError } from '../exception';
import {
AuthorizationStatus,
- AuthorizeRequest,
- AuthorizeResponse,
- StartTransactionRequest,
- StartTransactionResponse,
- StopTransactionRequest,
- StopTransactionResponse,
-} from '../types/ocpp/Transaction';
-import {
+ type AuthorizeRequest,
+ type AuthorizeResponse,
+ type BootNotificationRequest,
+ type BootNotificationResponse,
BroadcastChannelProcedureName,
- BroadcastChannelRequest,
- BroadcastChannelRequestPayload,
- BroadcastChannelResponsePayload,
- 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';
+ type BroadcastChannelRequest,
+ type BroadcastChannelRequestPayload,
+ type BroadcastChannelResponsePayload,
+ type DataTransferRequest,
+ type DataTransferResponse,
+ DataTransferStatus,
+ type DiagnosticsStatusNotificationRequest,
+ type DiagnosticsStatusNotificationResponse,
+ type FirmwareStatusNotificationRequest,
+ type FirmwareStatusNotificationResponse,
+ type HeartbeatRequest,
+ type HeartbeatResponse,
+ type MessageEvent,
+ type MeterValuesRequest,
+ type MeterValuesResponse,
+ RegistrationStatusEnumType,
+ RequestCommand,
+ type RequestParams,
+ ResponseStatus,
+ StandardParametersKey,
+ type StartTransactionRequest,
+ type StartTransactionResponse,
+ type StatusNotificationRequest,
+ type StatusNotificationResponse,
+ type StopTransactionRequest,
+ type StopTransactionResponse,
+} from '../types';
+import { Constants, Utils, logger } from '../utils';
const moduleName = 'ChargingStationWorkerBroadcastChannel';
| StartTransactionResponse
| StopTransactionResponse
| AuthorizeResponse
+ | BootNotificationResponse
| StatusNotificationResponse
| HeartbeatResponse
- | MeterValuesResponse;
+ | MeterValuesResponse
+ | DataTransferResponse
+ | DiagnosticsStatusNotificationResponse
+ | FirmwareStatusNotificationResponse;
type CommandHandler = (
requestPayload?: BroadcastChannelRequestPayload
) => Promise<CommandResponse | void> | void;
-export default class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChannel {
+export class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChannel {
private readonly commandHandlers: Map<BroadcastChannelProcedureName, CommandHandler>;
private readonly chargingStation: ChargingStation;
constructor(chargingStation: ChargingStation) {
super();
+ const requestParams: RequestParams = {
+ throwError: true,
+ };
this.commandHandlers = new Map<BroadcastChannelProcedureName, CommandHandler>([
[BroadcastChannelProcedureName.START_CHARGING_STATION, () => this.chargingStation.start()],
[
[
BroadcastChannelProcedureName.START_AUTOMATIC_TRANSACTION_GENERATOR,
(requestPayload?: BroadcastChannelRequestPayload) =>
- this.chargingStation.startAutomaticTransactionGenerator(requestPayload.connectorIds),
+ this.chargingStation.startAutomaticTransactionGenerator(requestPayload?.connectorIds),
],
[
BroadcastChannelProcedureName.STOP_AUTOMATIC_TRANSACTION_GENERATOR,
(requestPayload?: BroadcastChannelRequestPayload) =>
- this.chargingStation.stopAutomaticTransactionGenerator(requestPayload.connectorIds),
+ this.chargingStation.stopAutomaticTransactionGenerator(requestPayload?.connectorIds),
+ ],
+ [
+ BroadcastChannelProcedureName.SET_SUPERVISION_URL,
+ (requestPayload?: BroadcastChannelRequestPayload) =>
+ this.chargingStation.setSupervisionUrl(requestPayload?.url as string),
],
[
BroadcastChannelProcedureName.START_TRANSACTION,
this.chargingStation.ocppRequestService.requestHandler<
StartTransactionRequest,
StartTransactionResponse
- >(this.chargingStation, RequestCommand.START_TRANSACTION, requestPayload),
+ >(this.chargingStation, RequestCommand.START_TRANSACTION, requestPayload, requestParams),
],
[
BroadcastChannelProcedureName.STOP_TRANSACTION,
this.chargingStation.ocppRequestService.requestHandler<
StopTransactionRequest,
StartTransactionResponse
- >(this.chargingStation, RequestCommand.STOP_TRANSACTION, {
- ...requestPayload,
- meterStop:
- requestPayload.meterStop ??
- this.chargingStation.getEnergyActiveImportRegisterByTransactionId(
+ >(
+ this.chargingStation,
+ RequestCommand.STOP_TRANSACTION,
+ {
+ meterStop: this.chargingStation.getEnergyActiveImportRegisterByTransactionId(
requestPayload.transactionId,
true
),
- }),
+ ...requestPayload,
+ },
+ requestParams
+ ),
],
[
BroadcastChannelProcedureName.AUTHORIZE,
this.chargingStation.ocppRequestService.requestHandler<
AuthorizeRequest,
AuthorizeResponse
- >(this.chargingStation, RequestCommand.AUTHORIZE, requestPayload),
+ >(this.chargingStation, RequestCommand.AUTHORIZE, requestPayload, requestParams),
+ ],
+ [
+ BroadcastChannelProcedureName.BOOT_NOTIFICATION,
+ async (requestPayload?: BroadcastChannelRequestPayload) => {
+ this.chargingStation.bootNotificationResponse =
+ await this.chargingStation.ocppRequestService.requestHandler<
+ BootNotificationRequest,
+ BootNotificationResponse
+ >(
+ this.chargingStation,
+ RequestCommand.BOOT_NOTIFICATION,
+ {
+ ...this.chargingStation.bootNotificationRequest,
+ ...requestPayload,
+ },
+ {
+ skipBufferingOnError: true,
+ throwError: true,
+ }
+ );
+ return this.chargingStation.bootNotificationResponse;
+ },
],
[
BroadcastChannelProcedureName.STATUS_NOTIFICATION,
this.chargingStation.ocppRequestService.requestHandler<
StatusNotificationRequest,
StatusNotificationResponse
- >(this.chargingStation, RequestCommand.STATUS_NOTIFICATION, requestPayload),
+ >(
+ this.chargingStation,
+ RequestCommand.STATUS_NOTIFICATION,
+ requestPayload,
+ requestParams
+ ),
],
[
BroadcastChannelProcedureName.HEARTBEAT,
this.chargingStation.ocppRequestService.requestHandler<
HeartbeatRequest,
HeartbeatResponse
- >(this.chargingStation, RequestCommand.HEARTBEAT, requestPayload),
+ >(this.chargingStation, RequestCommand.HEARTBEAT, requestPayload, requestParams),
],
[
BroadcastChannelProcedureName.METER_VALUES,
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,
+ RequestCommand.METER_VALUES,
+ {
+ meterValue: [
+ // FIXME: Implement OCPP version agnostic helpers
+ OCPP16ServiceUtils.buildMeterValue(
+ this.chargingStation,
+ requestPayload.connectorId,
+ this.chargingStation.getConnectorStatus(requestPayload.connectorId)
+ ?.transactionId,
+ configuredMeterValueSampleInterval
+ ? Utils.convertToInt(configuredMeterValueSampleInterval.value) * 1000
+ : Constants.DEFAULT_METER_VALUES_INTERVAL
+ ),
+ ],
+ ...requestPayload,
+ },
+ requestParams
+ );
},
],
+ [
+ BroadcastChannelProcedureName.DATA_TRANSFER,
+ async (requestPayload?: BroadcastChannelRequestPayload) =>
+ this.chargingStation.ocppRequestService.requestHandler<
+ DataTransferRequest,
+ DataTransferResponse
+ >(this.chargingStation, RequestCommand.DATA_TRANSFER, requestPayload, requestParams),
+ ],
+ [
+ BroadcastChannelProcedureName.DIAGNOSTICS_STATUS_NOTIFICATION,
+ async (requestPayload?: BroadcastChannelRequestPayload) =>
+ this.chargingStation.ocppRequestService.requestHandler<
+ DiagnosticsStatusNotificationRequest,
+ DiagnosticsStatusNotificationResponse
+ >(
+ this.chargingStation,
+ RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION,
+ requestPayload,
+ requestParams
+ ),
+ ],
+ [
+ BroadcastChannelProcedureName.FIRMWARE_STATUS_NOTIFICATION,
+ async (requestPayload?: BroadcastChannelRequestPayload) =>
+ this.chargingStation.ocppRequestService.requestHandler<
+ FirmwareStatusNotificationRequest,
+ FirmwareStatusNotificationResponse
+ >(
+ this.chargingStation,
+ RequestCommand.FIRMWARE_STATUS_NOTIFICATION,
+ requestPayload,
+ requestParams
+ ),
+ ],
]);
this.chargingStation = chargingStation;
this.onmessage = this.requestHandler.bind(this) as (message: MessageEvent) => void;
}
const [uuid, command, requestPayload] = validatedMessageEvent.data as BroadcastChannelRequest;
if (
- requestPayload?.hashIds !== undefined &&
+ !Utils.isNullOrUndefined(requestPayload?.hashIds) &&
requestPayload?.hashIds?.includes(this.chargingStation.stationInfo.hashId) === false
) {
return;
}
- if (requestPayload?.hashId !== undefined) {
+ if (!Utils.isNullOrUndefined(requestPayload?.hashId)) {
logger.error(
- `${this.chargingStation.logPrefix()} ${moduleName}.requestHandler: 'hashId' field usage in PDU is deprecated, use 'hashIds' instead`
+ `${this.chargingStation.logPrefix()} ${moduleName}.requestHandler: 'hashId' field usage in PDU is deprecated, use 'hashIds' array instead`
);
return;
}
let commandResponse: CommandResponse | void;
try {
commandResponse = await this.commandHandler(command, requestPayload);
- if (commandResponse === undefined || commandResponse === null) {
+ if (
+ Utils.isNullOrUndefined(commandResponse) ||
+ Utils.isEmptyObject(commandResponse as CommandResponse)
+ ) {
responsePayload = {
hashId: this.chargingStation.stationInfo.hashId,
status: ResponseStatus.SUCCESS,
return ResponseStatus.SUCCESS;
}
return ResponseStatus.FAILURE;
+ case BroadcastChannelProcedureName.BOOT_NOTIFICATION:
+ if (commandResponse?.status === RegistrationStatusEnumType.ACCEPTED) {
+ return ResponseStatus.SUCCESS;
+ }
+ return ResponseStatus.FAILURE;
+ case BroadcastChannelProcedureName.DATA_TRANSFER:
+ if (commandResponse?.status === DataTransferStatus.ACCEPTED) {
+ return ResponseStatus.SUCCESS;
+ }
+ return ResponseStatus.FAILURE;
case BroadcastChannelProcedureName.STATUS_NOTIFICATION:
case BroadcastChannelProcedureName.METER_VALUES:
if (Utils.isEmptyObject(commandResponse) === true) {