From: Jérôme Benoit Date: Fri, 6 May 2022 19:56:24 +0000 (+0200) Subject: Use the fixed JsonType definition where appropriate X-Git-Tag: v1.1.59~8 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=5cc4b63bd363814ddc657bd4c9b407e5eb3150cb;p=e-mobility-charging-stations-simulator.git Use the fixed JsonType definition where appropriate Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index d0707043..e5759c33 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -64,7 +64,7 @@ import Constants from '../utils/Constants'; import { ErrorType } from '../types/ocpp/ErrorType'; import { FileType } from '../types/FileType'; import FileUtils from '../utils/FileUtils'; -import { JsonObject } from '../types/JsonType'; +import { JsonType } from '../types/JsonType'; import { MessageType } from '../types/ocpp/MessageType'; import OCPP16IncomingRequestService from './ocpp/1.6/OCPP16IncomingRequestService'; import OCPP16RequestService from './ocpp/1.6/OCPP16RequestService'; @@ -1572,14 +1572,14 @@ export default class ChargingStation { let messageType: number; let messageId: string; let commandName: IncomingRequestCommand; - let commandPayload: JsonObject; + let commandPayload: JsonType; let errorType: ErrorType; let errorMessage: string; - let errorDetails: JsonObject; - let responseCallback: (payload: JsonObject, requestPayload: JsonObject) => void; + let errorDetails: JsonType; + let responseCallback: (payload: JsonType, requestPayload: JsonType) => void; let errorCallback: (error: OCPPError, requestStatistic?: boolean) => void; let requestCommandName: RequestCommand | IncomingRequestCommand; - let requestPayload: JsonObject; + let requestPayload: JsonType; let cachedRequest: CachedRequest; let errMsg: string; try { @@ -1627,7 +1627,7 @@ export default class ChargingStation { ErrorType.PROTOCOL_ERROR, `Cached request for message id ${messageId} response is not iterable`, null, - cachedRequest as unknown as JsonObject + cachedRequest as unknown as JsonType ); } logger.debug( @@ -1657,7 +1657,7 @@ export default class ChargingStation { ErrorType.PROTOCOL_ERROR, `Cached request for message id ${messageId} error response is not iterable`, null, - cachedRequest as unknown as JsonObject + cachedRequest as unknown as JsonType ); } logger.debug( diff --git a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts index 0241e177..8dc9135f 100644 --- a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts @@ -64,7 +64,7 @@ import Constants from '../../../utils/Constants'; import { DefaultResponse } from '../../../types/ocpp/Responses'; import { ErrorType } from '../../../types/ocpp/ErrorType'; import { IncomingRequestHandler } from '../../../types/ocpp/Requests'; -import { JsonObject } from '../../../types/JsonType'; +import { JsonType } from '../../../types/JsonType'; import { OCPP16ChargePointErrorCode } from '../../../types/ocpp/1.6/ChargePointErrorCode'; import { OCPP16ChargePointStatus } from '../../../types/ocpp/1.6/ChargePointStatus'; import { OCPP16DiagnosticsStatus } from '../../../types/ocpp/1.6/DiagnosticsStatus'; @@ -129,9 +129,9 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer public async incomingRequestHandler( messageId: string, commandName: OCPP16IncomingRequestCommand, - commandPayload: JsonObject + commandPayload: JsonType ): Promise { - let response: JsonObject; + let response: JsonType; if ( this.chargingStation.getOcppStrictCompliance() && this.chargingStation.isInPendingState() && diff --git a/src/charging-station/ocpp/1.6/OCPP16RequestService.ts b/src/charging-station/ocpp/1.6/OCPP16RequestService.ts index 2a4165c4..eb347480 100644 --- a/src/charging-station/ocpp/1.6/OCPP16RequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16RequestService.ts @@ -1,9 +1,10 @@ // Partial Copyright Jerome Benoit. 2021. All Rights Reserved. +import { JsonObject, JsonType } from '../../../types/JsonType'; + import type ChargingStation from '../../ChargingStation'; import Constants from '../../../utils/Constants'; import { ErrorType } from '../../../types/ocpp/ErrorType'; -import { JsonObject } from '../../../types/JsonType'; import { OCPP16RequestCommand } from '../../../types/ocpp/1.6/Requests'; import { OCPP16ServiceUtils } from './OCPP16ServiceUtils'; import OCPPError from '../../../exception/OCPPError'; @@ -22,9 +23,9 @@ export default class OCPP16RequestService extends OCPPRequestService { super(chargingStation, ocppResponseService); } - public async requestHandler( + public async requestHandler( commandName: OCPP16RequestCommand, - commandParams?: JsonObject, + commandParams?: JsonType, params?: RequestParams ): Promise { if (Object.values(OCPP16RequestCommand).includes(commandName)) { @@ -43,11 +44,12 @@ export default class OCPP16RequestService extends OCPPRequestService { ); } - private buildRequestPayload( + private buildRequestPayload( commandName: OCPP16RequestCommand, - commandParams?: JsonObject + commandParams?: JsonType ): Request { let connectorId: number; + commandParams = commandParams as JsonObject; switch (commandName) { case OCPP16RequestCommand.AUTHORIZE: return { diff --git a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts index c8946d43..5a60af63 100644 --- a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts @@ -26,7 +26,7 @@ import { import type ChargingStation from '../../ChargingStation'; import { ErrorType } from '../../../types/ocpp/ErrorType'; -import { JsonObject } from '../../../types/JsonType'; +import { JsonType } from '../../../types/JsonType'; import { OCPP16ChargePointErrorCode } from '../../../types/ocpp/1.6/ChargePointErrorCode'; import { OCPP16ChargePointStatus } from '../../../types/ocpp/1.6/ChargePointStatus'; import { OCPP16ServiceUtils } from './OCPP16ServiceUtils'; @@ -60,8 +60,8 @@ export default class OCPP16ResponseService extends OCPPResponseService { public async responseHandler( commandName: OCPP16RequestCommand, - payload: JsonObject, - requestPayload: JsonObject + payload: JsonType, + requestPayload: JsonType ): Promise { if ( this.chargingStation.isRegistered() || diff --git a/src/charging-station/ocpp/OCPPIncomingRequestService.ts b/src/charging-station/ocpp/OCPPIncomingRequestService.ts index d7d14c2e..b5f607f5 100644 --- a/src/charging-station/ocpp/OCPPIncomingRequestService.ts +++ b/src/charging-station/ocpp/OCPPIncomingRequestService.ts @@ -1,7 +1,7 @@ import type ChargingStation from '../ChargingStation'; import { HandleErrorParams } from '../../types/Error'; import { IncomingRequestCommand } from '../../types/ocpp/Requests'; -import { JsonObject } from '../../types/JsonType'; +import { JsonType } from '../../types/JsonType'; import logger from '../../utils/Logger'; export default abstract class OCPPIncomingRequestService { @@ -50,6 +50,6 @@ export default abstract class OCPPIncomingRequestService { public abstract incomingRequestHandler( messageId: string, commandName: IncomingRequestCommand, - commandPayload: JsonObject + commandPayload: JsonType ): Promise; } diff --git a/src/charging-station/ocpp/OCPPRequestService.ts b/src/charging-station/ocpp/OCPPRequestService.ts index c19944ba..f8b6b227 100644 --- a/src/charging-station/ocpp/OCPPRequestService.ts +++ b/src/charging-station/ocpp/OCPPRequestService.ts @@ -6,13 +6,13 @@ import { RequestParams, ResponseType, } from '../../types/ocpp/Requests'; +import { JsonObject, JsonType } from '../../types/JsonType'; import type ChargingStation from '../ChargingStation'; import Constants from '../../utils/Constants'; import { EmptyObject } from '../../types/EmptyObject'; import { ErrorType } from '../../types/ocpp/ErrorType'; import { HandleErrorParams } from '../../types/Error'; -import { JsonObject } from '../../types/JsonType'; import { MessageType } from '../../types/ocpp/MessageType'; import OCPPError from '../../exception/OCPPError'; import type OCPPResponseService from './OCPPResponseService'; @@ -56,7 +56,7 @@ export default abstract class OCPPRequestService { public async sendResponse( messageId: string, - messagePayload: JsonObject, + messagePayload: JsonType, commandName: IncomingRequestCommand ): Promise { try { @@ -92,7 +92,7 @@ export default abstract class OCPPRequestService { protected async sendMessage( messageId: string, - messagePayload: JsonObject, + messagePayload: JsonType, commandName: RequestCommand, params: RequestParams = { skipBufferingOnError: false, @@ -114,7 +114,7 @@ export default abstract class OCPPRequestService { private async internalSendMessage( messageId: string, - messagePayload: JsonObject | OCPPError, + messagePayload: JsonType | OCPPError, messageType: MessageType, commandName?: RequestCommand | IncomingRequestCommand, params: RequestParams = { @@ -169,7 +169,7 @@ export default abstract class OCPPRequestService { ErrorType.GENERIC_ERROR, `WebSocket closed for buffered message id '${messageId}' with content '${messageToSend}'`, commandName, - (messagePayload?.details as JsonObject) ?? {} + (messagePayload as JsonObject)?.details ?? {} ); if (messageType === MessageType.CALL_MESSAGE) { // Reject it but keep the request in the cache @@ -183,7 +183,7 @@ export default abstract class OCPPRequestService { ErrorType.GENERIC_ERROR, `WebSocket closed for non buffered message id '${messageId}' with content '${messageToSend}'`, commandName, - (messagePayload?.details as JsonObject) ?? {} + (messagePayload as JsonObject)?.details ?? {} ), false ); @@ -201,8 +201,8 @@ export default abstract class OCPPRequestService { * @param requestPayload */ async function responseCallback( - payload: JsonObject, - requestPayload: JsonObject + payload: JsonType, + requestPayload: JsonType ): Promise { if (self.chargingStation.getEnableStatistics()) { self.chargingStation.performanceStatistics.addRequestStatistic( @@ -253,7 +253,7 @@ export default abstract class OCPPRequestService { ErrorType.GENERIC_ERROR, `Timeout for message id '${messageId}'`, commandName, - (messagePayload?.details as JsonObject) ?? {} + (messagePayload as JsonObject)?.details ?? {} ), () => { messageType === MessageType.CALL_MESSAGE && @@ -270,10 +270,10 @@ export default abstract class OCPPRequestService { private buildMessageToSend( messageId: string, - messagePayload: JsonObject | OCPPError, + messagePayload: JsonType | OCPPError, messageType: MessageType, commandName?: RequestCommand | IncomingRequestCommand, - responseCallback?: (payload: JsonObject, requestPayload: JsonObject) => Promise, + responseCallback?: (payload: JsonType, requestPayload: JsonType) => Promise, errorCallback?: (error: OCPPError, requestStatistic?: boolean) => void ): string { let messageToSend: string; @@ -286,7 +286,7 @@ export default abstract class OCPPRequestService { responseCallback, errorCallback, commandName, - messagePayload as JsonObject, + messagePayload as JsonType, ]); messageToSend = JSON.stringify([ messageType, @@ -342,9 +342,9 @@ export default abstract class OCPPRequestService { } // eslint-disable-next-line @typescript-eslint/no-unused-vars - public abstract requestHandler( + public abstract requestHandler( commandName: RequestCommand, - commandParams?: JsonObject, + commandParams?: JsonType, params?: RequestParams ): Promise; } diff --git a/src/charging-station/ocpp/OCPPResponseService.ts b/src/charging-station/ocpp/OCPPResponseService.ts index f45d1b43..e838bd9f 100644 --- a/src/charging-station/ocpp/OCPPResponseService.ts +++ b/src/charging-station/ocpp/OCPPResponseService.ts @@ -1,5 +1,5 @@ import type ChargingStation from '../ChargingStation'; -import { JsonObject } from '../../types/JsonType'; +import { JsonType } from '../../types/JsonType'; import { RequestCommand } from '../../types/ocpp/Requests'; export default abstract class OCPPResponseService { @@ -26,7 +26,7 @@ export default abstract class OCPPResponseService { public abstract responseHandler( commandName: RequestCommand, - payload: JsonObject, - requestPayload: JsonObject + payload: JsonType, + requestPayload: JsonType ): Promise; } diff --git a/src/charging-station/ui-websocket-services/AbstractUIService.ts b/src/charging-station/ui-websocket-services/AbstractUIService.ts index 9fd577cd..42cfa428 100644 --- a/src/charging-station/ui-websocket-services/AbstractUIService.ts +++ b/src/charging-station/ui-websocket-services/AbstractUIService.ts @@ -1,7 +1,7 @@ import { ProtocolCommand, ProtocolRequestHandler } from '../../types/UIProtocol'; import BaseError from '../../exception/BaseError'; -import { JsonObject } from '../../types/JsonType'; +import { JsonType } from '../../types/JsonType'; import UIWebSocketServer from '../UIWebSocketServer'; import logger from '../../utils/Logger'; @@ -16,12 +16,12 @@ export default abstract class AbstractUIService { ]); } - public async messageHandler(command: ProtocolCommand, payload: JsonObject): Promise { - let messageResponse: JsonObject; + public async messageHandler(command: ProtocolCommand, payload: JsonType): Promise { + let messageResponse: JsonType; if (this.messageHandlers.has(command)) { try { // Call the method to build the message response - messageResponse = (await this.messageHandlers.get(command)(payload)) as JsonObject; + messageResponse = (await this.messageHandlers.get(command)(payload)) as JsonType; } catch (error) { // Log logger.error(this.uiWebSocketServer.logPrefix() + ' Handle message error: %j', error); @@ -41,7 +41,7 @@ export default abstract class AbstractUIService { this.uiWebSocketServer.broadcastToClients(this.buildProtocolMessage(command, messageResponse)); } - protected buildProtocolMessage(command: ProtocolCommand, payload: JsonObject): string { + protected buildProtocolMessage(command: ProtocolCommand, payload: JsonType): string { return JSON.stringify([command, payload]); } diff --git a/src/charging-station/ui-websocket-services/UIService001.ts b/src/charging-station/ui-websocket-services/UIService001.ts index 82bafd78..18fb66a9 100644 --- a/src/charging-station/ui-websocket-services/UIService001.ts +++ b/src/charging-station/ui-websocket-services/UIService001.ts @@ -1,7 +1,7 @@ import { ProtocolCommand, ProtocolRequestHandler } from '../../types/UIProtocol'; import AbstractUIService from './AbstractUIService'; -import { JsonObject } from '../../types/JsonType'; +import { JsonType } from '../../types/JsonType'; import UIWebSocketServer from '../UIWebSocketServer'; export default class UIService001 extends AbstractUIService { @@ -17,6 +17,6 @@ export default class UIService001 extends AbstractUIService { ); } - private handleStartTransaction(payload: JsonObject): void {} - private handleStopTransaction(payload: JsonObject): void {} + private handleStartTransaction(payload: JsonType): void {} + private handleStopTransaction(payload: JsonType): void {} } diff --git a/src/exception/OCPPError.ts b/src/exception/OCPPError.ts index bfe64fb4..e67f3c95 100644 --- a/src/exception/OCPPError.ts +++ b/src/exception/OCPPError.ts @@ -4,18 +4,18 @@ import { IncomingRequestCommand, RequestCommand } from '../types/ocpp/Requests'; import BaseError from './BaseError'; import { ErrorType } from '../types/ocpp/ErrorType'; -import { JsonObject } from '../types/JsonType'; +import { JsonType } from '../types/JsonType'; export default class OCPPError extends BaseError { code: ErrorType; command?: RequestCommand | IncomingRequestCommand; - details?: JsonObject; + details?: JsonType; constructor( code: ErrorType, message: string, command?: RequestCommand | IncomingRequestCommand, - details?: JsonObject + details?: JsonType ) { super(message); diff --git a/src/types/UIProtocol.ts b/src/types/UIProtocol.ts index 4ff499b4..6d247892 100644 --- a/src/types/UIProtocol.ts +++ b/src/types/UIProtocol.ts @@ -1,4 +1,4 @@ -import { JsonObject } from './JsonType'; +import { JsonType } from './JsonType'; export enum Protocol { UI = 'ui', @@ -15,8 +15,8 @@ export enum ProtocolCommand { UNKNOWN = 'unknown', } -export type ProtocolRequest = [ProtocolCommand, JsonObject]; +export type ProtocolRequest = [ProtocolCommand, JsonType]; export type ProtocolRequestHandler = ( - payload: JsonObject -) => void | Promise | JsonObject | Promise; + payload: JsonType +) => void | Promise | JsonType | Promise; diff --git a/src/types/ocpp/Requests.ts b/src/types/ocpp/Requests.ts index 60724254..16a79c05 100644 --- a/src/types/ocpp/Requests.ts +++ b/src/types/ocpp/Requests.ts @@ -7,33 +7,26 @@ import { OCPP16StatusNotificationRequest, } from './1.6/Requests'; -import { JsonObject } from '../JsonType'; +import { JsonType } from '../JsonType'; import { MessageType } from './MessageType'; import { OCPP16DiagnosticsStatus } from './1.6/DiagnosticsStatus'; import { OCPP16MeterValuesRequest } from './1.6/MeterValues'; import OCPPError from '../../exception/OCPPError'; -export type OutgoingRequest = [MessageType.CALL_MESSAGE, string, RequestCommand, JsonObject]; +export type OutgoingRequest = [MessageType.CALL_MESSAGE, string, RequestCommand, JsonType]; -export type IncomingRequest = [ - MessageType.CALL_MESSAGE, - string, - IncomingRequestCommand, - JsonObject -]; +export type IncomingRequest = [MessageType.CALL_MESSAGE, string, IncomingRequestCommand, JsonType]; export type CachedRequest = [ - (payload: JsonObject, requestPayload: JsonObject) => void, + (payload: JsonType, requestPayload: JsonType) => void, (error: OCPPError, requestStatistic?: boolean) => void, RequestCommand | IncomingRequestCommand, - JsonObject + JsonType ]; -export type IncomingRequestHandler = ( - commandPayload: JsonObject -) => JsonObject | Promise; +export type IncomingRequestHandler = (commandPayload: JsonType) => JsonType | Promise; -export type ResponseType = JsonObject | OCPPError; +export type ResponseType = JsonType | OCPPError; export interface RequestParams { skipBufferingOnError?: boolean; diff --git a/src/types/ocpp/Responses.ts b/src/types/ocpp/Responses.ts index db44800c..baefc637 100644 --- a/src/types/ocpp/Responses.ts +++ b/src/types/ocpp/Responses.ts @@ -12,17 +12,17 @@ import { } from './1.6/Responses'; import { ErrorType } from './ErrorType'; -import { JsonObject } from '../JsonType'; +import { JsonType } from '../JsonType'; import { MessageType } from './MessageType'; import { OCPP16MeterValuesResponse } from './1.6/MeterValues'; -export type Response = [MessageType.CALL_RESULT_MESSAGE, string, JsonObject]; +export type Response = [MessageType.CALL_RESULT_MESSAGE, string, JsonType]; -export type ErrorResponse = [MessageType.CALL_ERROR_MESSAGE, string, ErrorType, string, JsonObject]; +export type ErrorResponse = [MessageType.CALL_ERROR_MESSAGE, string, ErrorType, string, JsonType]; export type ResponseHandler = ( - payload: JsonObject, - requestPayload?: JsonObject + payload: JsonType, + requestPayload?: JsonType ) => void | Promise; export type BootNotificationResponse = OCPP16BootNotificationResponse;