From: Jérôme Benoit Date: Mon, 29 Jan 2024 16:50:46 +0000 (+0100) Subject: refactor: move OCPP event emission ops to the promises queue X-Git-Tag: v1.2.33~10^2 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=ceb9a9009db1987092c2bdf8266118913a6965a4;p=e-mobility-charging-stations-simulator.git refactor: move OCPP event emission ops to the promises queue Signed-off-by: Jérôme Benoit --- 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