X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;ds=sidebyside;f=src%2Fcharging-station%2FChargingStationWorkerBroadcastChannel.ts;h=c03c4acb81db8aae2a01efee281e5558fbfb3a81;hb=fc28853f03af7666e2f55a244b787157fddadfbc;hp=1528d65c0983580e86fb99b7e0b0f314612dd7a7;hpb=c9a4f9ea603df09541c6837fee50012256fb46ad;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ChargingStationWorkerBroadcastChannel.ts b/src/charging-station/ChargingStationWorkerBroadcastChannel.ts index 1528d65c..c03c4acb 100644 --- a/src/charging-station/ChargingStationWorkerBroadcastChannel.ts +++ b/src/charging-station/ChargingStationWorkerBroadcastChannel.ts @@ -1,51 +1,45 @@ -import BaseError from '../exception/BaseError'; -import type OCPPError from '../exception/OCPPError'; -import { StandardParametersKey } from '../types/ocpp/Configuration'; import { - type BootNotificationRequest, - type DataTransferRequest, - type DiagnosticsStatusNotificationRequest, - type FirmwareStatusNotificationRequest, - type HeartbeatRequest, - type MeterValuesRequest, - RequestCommand, - type StatusNotificationRequest, -} from '../types/ocpp/Requests'; + type ChargingStation, + ChargingStationConfigurationUtils, + WorkerBroadcastChannel, +} from './internal'; +import { OCPP16ServiceUtils } from './ocpp'; +import { BaseError, type OCPPError } from '../exception'; import { + AuthorizationStatus, + type AuthorizeRequest, + type AuthorizeResponse, + type BootNotificationRequest, type BootNotificationResponse, + BroadcastChannelProcedureName, + 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, - type StatusNotificationResponse, -} from '../types/ocpp/Responses'; -import { - AuthorizationStatus, - type AuthorizeRequest, - type AuthorizeResponse, + RequestCommand, + type RequestParams, + ResponseStatus, + StandardParametersKey, type StartTransactionRequest, type StartTransactionResponse, + type StatusNotificationRequest, + type StatusNotificationResponse, type StopTransactionRequest, type StopTransactionResponse, -} from '../types/ocpp/Transaction'; -import { ResponseStatus } from '../types/UIProtocol'; -import { - BroadcastChannelProcedureName, - type BroadcastChannelRequest, - type BroadcastChannelRequestPayload, - type BroadcastChannelResponsePayload, - type MessageEvent, -} from '../types/WorkerBroadcastChannel'; -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'; +} from '../types'; +import { Constants, Utils, logger } from '../utils'; const moduleName = 'ChargingStationWorkerBroadcastChannel'; @@ -65,12 +59,15 @@ type CommandHandler = ( requestPayload?: BroadcastChannelRequestPayload ) => Promise | void; -export default class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChannel { +export class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChannel { private readonly commandHandlers: Map; private readonly chargingStation: ChargingStation; constructor(chargingStation: ChargingStation) { super(); + const requestParams: RequestParams = { + throwError: true, + }; this.commandHandlers = new Map([ [BroadcastChannelProcedureName.START_CHARGING_STATION, () => this.chargingStation.start()], [ @@ -88,12 +85,17 @@ export default class ChargingStationWorkerBroadcastChannel extends WorkerBroadca [ 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, @@ -101,7 +103,7 @@ export default class ChargingStationWorkerBroadcastChannel extends WorkerBroadca this.chargingStation.ocppRequestService.requestHandler< StartTransactionRequest, StartTransactionResponse - >(this.chargingStation, RequestCommand.START_TRANSACTION, requestPayload), + >(this.chargingStation, RequestCommand.START_TRANSACTION, requestPayload, requestParams), ], [ BroadcastChannelProcedureName.STOP_TRANSACTION, @@ -109,13 +111,18 @@ export default class ChargingStationWorkerBroadcastChannel extends WorkerBroadca this.chargingStation.ocppRequestService.requestHandler< StopTransactionRequest, StartTransactionResponse - >(this.chargingStation, RequestCommand.STOP_TRANSACTION, { - meterStop: this.chargingStation.getEnergyActiveImportRegisterByTransactionId( - requestPayload.transactionId, - true - ), - ...requestPayload, - }), + >( + this.chargingStation, + RequestCommand.STOP_TRANSACTION, + { + meterStop: this.chargingStation.getEnergyActiveImportRegisterByTransactionId( + requestPayload.transactionId, + true + ), + ...requestPayload, + }, + requestParams + ), ], [ BroadcastChannelProcedureName.AUTHORIZE, @@ -123,7 +130,7 @@ export default class ChargingStationWorkerBroadcastChannel extends WorkerBroadca this.chargingStation.ocppRequestService.requestHandler< AuthorizeRequest, AuthorizeResponse - >(this.chargingStation, RequestCommand.AUTHORIZE, requestPayload), + >(this.chargingStation, RequestCommand.AUTHORIZE, requestPayload, requestParams), ], [ BroadcastChannelProcedureName.BOOT_NOTIFICATION, @@ -141,6 +148,7 @@ export default class ChargingStationWorkerBroadcastChannel extends WorkerBroadca }, { skipBufferingOnError: true, + throwError: true, } ); return this.chargingStation.bootNotificationResponse; @@ -152,7 +160,12 @@ export default class ChargingStationWorkerBroadcastChannel extends WorkerBroadca this.chargingStation.ocppRequestService.requestHandler< StatusNotificationRequest, StatusNotificationResponse - >(this.chargingStation, RequestCommand.STATUS_NOTIFICATION, requestPayload), + >( + this.chargingStation, + RequestCommand.STATUS_NOTIFICATION, + requestPayload, + requestParams + ), ], [ BroadcastChannelProcedureName.HEARTBEAT, @@ -160,7 +173,7 @@ export default class ChargingStationWorkerBroadcastChannel extends WorkerBroadca this.chargingStation.ocppRequestService.requestHandler< HeartbeatRequest, HeartbeatResponse - >(this.chargingStation, RequestCommand.HEARTBEAT, requestPayload), + >(this.chargingStation, RequestCommand.HEARTBEAT, requestPayload, requestParams), ], [ BroadcastChannelProcedureName.METER_VALUES, @@ -173,19 +186,26 @@ export default class ChargingStationWorkerBroadcastChannel extends WorkerBroadca return this.chargingStation.ocppRequestService.requestHandler< MeterValuesRequest, MeterValuesResponse - >(this.chargingStation, RequestCommand.METER_VALUES, { - meterValue: [ - OCPP16ServiceUtils.buildMeterValue( - this.chargingStation, - requestPayload.connectorId, - this.chargingStation.getConnectorStatus(requestPayload.connectorId)?.transactionId, - configuredMeterValueSampleInterval - ? Utils.convertToInt(configuredMeterValueSampleInterval.value) * 1000 - : Constants.DEFAULT_METER_VALUES_INTERVAL - ), - ], - ...requestPayload, - }); + >( + 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 + ); }, ], [ @@ -194,7 +214,7 @@ export default class ChargingStationWorkerBroadcastChannel extends WorkerBroadca this.chargingStation.ocppRequestService.requestHandler< DataTransferRequest, DataTransferResponse - >(this.chargingStation, RequestCommand.DATA_TRANSFER, requestPayload), + >(this.chargingStation, RequestCommand.DATA_TRANSFER, requestPayload, requestParams), ], [ BroadcastChannelProcedureName.DIAGNOSTICS_STATUS_NOTIFICATION, @@ -202,7 +222,12 @@ export default class ChargingStationWorkerBroadcastChannel extends WorkerBroadca this.chargingStation.ocppRequestService.requestHandler< DiagnosticsStatusNotificationRequest, DiagnosticsStatusNotificationResponse - >(this.chargingStation, RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION, requestPayload), + >( + this.chargingStation, + RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION, + requestPayload, + requestParams + ), ], [ BroadcastChannelProcedureName.FIRMWARE_STATUS_NOTIFICATION, @@ -210,7 +235,12 @@ export default class ChargingStationWorkerBroadcastChannel extends WorkerBroadca this.chargingStation.ocppRequestService.requestHandler< FirmwareStatusNotificationRequest, FirmwareStatusNotificationResponse - >(this.chargingStation, RequestCommand.FIRMWARE_STATUS_NOTIFICATION, requestPayload), + >( + this.chargingStation, + RequestCommand.FIRMWARE_STATUS_NOTIFICATION, + requestPayload, + requestParams + ), ], ]); this.chargingStation = chargingStation; @@ -228,12 +258,12 @@ export default class ChargingStationWorkerBroadcastChannel extends WorkerBroadca } 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' array instead` ); @@ -244,8 +274,7 @@ export default class ChargingStationWorkerBroadcastChannel extends WorkerBroadca try { commandResponse = await this.commandHandler(command, requestPayload); if ( - commandResponse === undefined || - commandResponse === null || + Utils.isNullOrUndefined(commandResponse) || Utils.isEmptyObject(commandResponse as CommandResponse) ) { responsePayload = {