From 5bd15d76859f5ac6b04a610f055145642e53e400 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Fri, 1 Jan 2021 19:53:53 +0100 Subject: [PATCH] Type all requests MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/charging-station/ChargingStation.ts | 16 ++++++++-------- src/types/ocpp/1.6/Requests.ts | 7 ++++++- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 20d8db73..20058769 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -6,7 +6,7 @@ import ChargingStationTemplate, { PowerOutType } from '../types/ChargingStationT import Connectors, { Connector } from '../types/Connectors'; import { MeterValue, MeterValueLocation, MeterValueMeasurand, MeterValuePhase, MeterValueUnit, MeterValuesRequest, MeterValuesResponse, SampledValue } from '../types/ocpp/1.6/MeterValues'; import { PerformanceObserver, performance } from 'perf_hooks'; -import Requests, { BootNotificationRequest, ChangeConfigurationRequest, GetConfigurationRequest, HeartbeatRequest, IncomingRequestCommand, RemoteStartTransactionRequest, RemoteStopTransactionRequest, RequestCommand, ResetRequest, SetChargingProfileRequest, StatusNotificationRequest, UnlockConnectorRequest } from '../types/ocpp/1.6/Requests'; +import Requests, { BootNotificationRequest, ChangeConfigurationRequest, GetConfigurationRequest, HeartbeatRequest, IncomingRequest, IncomingRequestCommand, RemoteStartTransactionRequest, RemoteStopTransactionRequest, Request, RequestCommand, ResetRequest, SetChargingProfileRequest, StatusNotificationRequest, UnlockConnectorRequest } from '../types/ocpp/1.6/Requests'; import WebSocket, { MessageEvent } from 'ws'; import AutomaticTransactionGenerator from './AutomaticTransactionGenerator'; @@ -687,20 +687,20 @@ export default class ChargingStation { } async onMessage(messageEvent: MessageEvent): Promise { - let [messageType, messageId, commandName, commandPayload, errorDetails] = [0, '', Constants.ENTITY_CHARGING_STATION, '', '']; + let [messageType, messageId, commandName, commandPayload, errorDetails]: IncomingRequest = [0, '', '' as IncomingRequestCommand, '', '']; try { // Parse the message - [messageType, messageId, commandName, commandPayload, errorDetails] = JSON.parse(messageEvent.toString()); + [messageType, messageId, commandName, commandPayload, errorDetails] = JSON.parse(messageEvent.toString()) as IncomingRequest; // Check the Type of message switch (messageType) { // Incoming Message case MessageType.CALL_MESSAGE: if (this.getEnableStatistics()) { - this._statistics.addMessage(commandName as IncomingRequestCommand, messageType); + this._statistics.addMessage(commandName , messageType); } // Process the call - await this.handleRequest(messageId, commandName as IncomingRequestCommand, commandPayload); + await this.handleRequest(messageId, commandName , commandPayload); break; // Outcome Message case MessageType.CALL_RESULT_MESSAGE: @@ -744,9 +744,9 @@ export default class ChargingStation { } } catch (error) { // Log - logger.error('%s Incoming message %j processing error %s on request content type %s', this._logPrefix(), messageEvent, error, this._requests[messageId]); + logger.error('%s Incoming message %j processing error %j on request content type %j', this._logPrefix(), messageEvent, error, this._requests[messageId]); // Send error - messageType !== MessageType.CALL_ERROR_MESSAGE && await this.sendError(messageId, error, commandName as IncomingRequestCommand); + messageType !== MessageType.CALL_ERROR_MESSAGE && await this.sendError(messageId, error, commandName); } } @@ -1049,7 +1049,7 @@ export default class ChargingStation { // Request case MessageType.CALL_MESSAGE: // Build request - this._requests[messageId] = [responseCallback, rejectCallback, commandParams]; + this._requests[messageId] = [responseCallback, rejectCallback, commandParams] as Request; messageToSend = JSON.stringify([messageType, messageId, commandName, commandParams]); break; // Response diff --git a/src/types/ocpp/1.6/Requests.ts b/src/types/ocpp/1.6/Requests.ts index bbcf5145..d318dafb 100644 --- a/src/types/ocpp/1.6/Requests.ts +++ b/src/types/ocpp/1.6/Requests.ts @@ -1,12 +1,17 @@ import { ChargePointErrorCode } from './ChargePointErrorCode'; import { ChargePointStatus } from './ChargePointStatus'; import { ChargingProfile } from './ChargingProfile'; +import { MessageType } from '../MessageType'; import OCPPError from '../../../charging-station/OcppError'; export default interface Requests { - [id: string]: [(payload?, requestPayload?) => void, (error?: OCPPError) => void, Record]; + [id: string]: Request; } +export type Request = [(payload?, requestPayload?) => void, (error?: OCPPError) => void, Record]; + +export type IncomingRequest = [MessageType, string, IncomingRequestCommand, string, string]; + export enum RequestCommand { BOOT_NOTIFICATION = 'BootNotification', HEARTBEAT = 'Heartbeat', -- 2.34.1