From ceb9a9009db1987092c2bdf8266118913a6965a4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Mon, 29 Jan 2024 17:50:46 +0100 Subject: [PATCH] refactor: move OCPP event emission ops to the promises queue MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .../ocpp/1.6/OCPP16IncomingRequestService.ts | 43 +++++++++++-------- 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts index f5bc60ea..7f932cb0 100644 --- a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts @@ -2,7 +2,6 @@ import { createWriteStream, readdirSync } from 'node:fs' import { dirname, join, resolve } from 'node:path' -import { nextTick } from 'node:process' import { URL, fileURLToPath } from 'node:url' import type { ValidateFunction } from 'ajv' @@ -1146,14 +1145,16 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { idTag ) } - nextTick(() => - this.emit( - OCPP16IncomingRequestCommand.REMOTE_START_TRANSACTION, - chargingStation, - transactionConnectorId, - idTag + Promise.resolve() + .then(() => + this.emit( + OCPP16IncomingRequestCommand.REMOTE_START_TRANSACTION, + chargingStation, + transactionConnectorId, + idTag + ) ) - ) + .catch(Constants.EMPTY_FUNCTION) return OCPP16Constants.OCPP_RESPONSE_ACCEPTED } @@ -1555,21 +1556,27 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { try { switch (requestedMessage) { case OCPP16MessageTrigger.BootNotification: - nextTick(() => - this.emit(`Trigger${OCPP16MessageTrigger.BootNotification}`, chargingStation) - ) + Promise.resolve() + .then(() => + this.emit(`Trigger${OCPP16MessageTrigger.BootNotification}`, chargingStation) + ) + .catch(Constants.EMPTY_FUNCTION) return OCPP16Constants.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED case OCPP16MessageTrigger.Heartbeat: - nextTick(() => this.emit(`Trigger${OCPP16MessageTrigger.Heartbeat}`, chargingStation)) + Promise.resolve() + .then(() => this.emit(`Trigger${OCPP16MessageTrigger.Heartbeat}`, chargingStation)) + .catch(Constants.EMPTY_FUNCTION) return OCPP16Constants.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED case OCPP16MessageTrigger.StatusNotification: - nextTick(() => - this.emit( - `Trigger${OCPP16MessageTrigger.StatusNotification}`, - chargingStation, - connectorId + Promise.resolve() + .then(() => + this.emit( + `Trigger${OCPP16MessageTrigger.StatusNotification}`, + chargingStation, + connectorId + ) ) - ) + .catch(Constants.EMPTY_FUNCTION) return OCPP16Constants.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED default: return OCPP16Constants.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED -- 2.34.1