Skip to content
This repository has been archived by the owner on Jun 12, 2024. It is now read-only.

Commit

Permalink
Use the fixed JsonType definition where appropriate
Browse files Browse the repository at this point in the history
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
  • Loading branch information
Jérôme Benoit committed May 6, 2022
1 parent 94bb13e commit 5cc4b63
Show file tree
Hide file tree
Showing 13 changed files with 66 additions and 71 deletions.
14 changes: 7 additions & 7 deletions src/charging-station/ChargingStation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
6 changes: 3 additions & 3 deletions src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -129,9 +129,9 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
public async incomingRequestHandler(
messageId: string,
commandName: OCPP16IncomingRequestCommand,
commandPayload: JsonObject
commandPayload: JsonType
): Promise<void> {
let response: JsonObject;
let response: JsonType;
if (
this.chargingStation.getOcppStrictCompliance() &&
this.chargingStation.isInPendingState() &&
Expand Down
12 changes: 7 additions & 5 deletions src/charging-station/ocpp/1.6/OCPP16RequestService.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -22,9 +23,9 @@ export default class OCPP16RequestService extends OCPPRequestService {
super(chargingStation, ocppResponseService);
}

public async requestHandler<Request extends JsonObject, Response extends JsonObject>(
public async requestHandler<Request extends JsonType, Response extends JsonType>(
commandName: OCPP16RequestCommand,
commandParams?: JsonObject,
commandParams?: JsonType,
params?: RequestParams
): Promise<Response> {
if (Object.values(OCPP16RequestCommand).includes(commandName)) {
Expand All @@ -43,11 +44,12 @@ export default class OCPP16RequestService extends OCPPRequestService {
);
}

private buildRequestPayload<Request extends JsonObject>(
private buildRequestPayload<Request extends JsonType>(
commandName: OCPP16RequestCommand,
commandParams?: JsonObject
commandParams?: JsonType
): Request {
let connectorId: number;
commandParams = commandParams as JsonObject;
switch (commandName) {
case OCPP16RequestCommand.AUTHORIZE:
return {
Expand Down
6 changes: 3 additions & 3 deletions src/charging-station/ocpp/1.6/OCPP16ResponseService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -60,8 +60,8 @@ export default class OCPP16ResponseService extends OCPPResponseService {

public async responseHandler(
commandName: OCPP16RequestCommand,
payload: JsonObject,
requestPayload: JsonObject
payload: JsonType,
requestPayload: JsonType
): Promise<void> {
if (
this.chargingStation.isRegistered() ||
Expand Down
4 changes: 2 additions & 2 deletions src/charging-station/ocpp/OCPPIncomingRequestService.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -50,6 +50,6 @@ export default abstract class OCPPIncomingRequestService {
public abstract incomingRequestHandler(
messageId: string,
commandName: IncomingRequestCommand,
commandPayload: JsonObject
commandPayload: JsonType
): Promise<void>;
}
28 changes: 14 additions & 14 deletions src/charging-station/ocpp/OCPPRequestService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -56,7 +56,7 @@ export default abstract class OCPPRequestService {

public async sendResponse(
messageId: string,
messagePayload: JsonObject,
messagePayload: JsonType,
commandName: IncomingRequestCommand
): Promise<ResponseType> {
try {
Expand Down Expand Up @@ -92,7 +92,7 @@ export default abstract class OCPPRequestService {

protected async sendMessage(
messageId: string,
messagePayload: JsonObject,
messagePayload: JsonType,
commandName: RequestCommand,
params: RequestParams = {
skipBufferingOnError: false,
Expand All @@ -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 = {
Expand Down Expand Up @@ -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
Expand All @@ -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
);
Expand All @@ -201,8 +201,8 @@ export default abstract class OCPPRequestService {
* @param requestPayload
*/
async function responseCallback(
payload: JsonObject,
requestPayload: JsonObject
payload: JsonType,
requestPayload: JsonType
): Promise<void> {
if (self.chargingStation.getEnableStatistics()) {
self.chargingStation.performanceStatistics.addRequestStatistic(
Expand Down Expand Up @@ -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 &&
Expand All @@ -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<void>,
responseCallback?: (payload: JsonType, requestPayload: JsonType) => Promise<void>,
errorCallback?: (error: OCPPError, requestStatistic?: boolean) => void
): string {
let messageToSend: string;
Expand All @@ -286,7 +286,7 @@ export default abstract class OCPPRequestService {
responseCallback,
errorCallback,
commandName,
messagePayload as JsonObject,
messagePayload as JsonType,
]);
messageToSend = JSON.stringify([
messageType,
Expand Down Expand Up @@ -342,9 +342,9 @@ export default abstract class OCPPRequestService {
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
public abstract requestHandler<Request extends JsonObject, Response extends JsonObject>(
public abstract requestHandler<Request extends JsonType, Response extends JsonType>(
commandName: RequestCommand,
commandParams?: JsonObject,
commandParams?: JsonType,
params?: RequestParams
): Promise<Response>;
}
6 changes: 3 additions & 3 deletions src/charging-station/ocpp/OCPPResponseService.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -26,7 +26,7 @@ export default abstract class OCPPResponseService {

public abstract responseHandler(
commandName: RequestCommand,
payload: JsonObject,
requestPayload: JsonObject
payload: JsonType,
requestPayload: JsonType
): Promise<void>;
}
10 changes: 5 additions & 5 deletions src/charging-station/ui-websocket-services/AbstractUIService.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -16,12 +16,12 @@ export default abstract class AbstractUIService {
]);
}

public async messageHandler(command: ProtocolCommand, payload: JsonObject): Promise<void> {
let messageResponse: JsonObject;
public async messageHandler(command: ProtocolCommand, payload: JsonType): Promise<void> {
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);
Expand All @@ -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]);
}

Expand Down
6 changes: 3 additions & 3 deletions src/charging-station/ui-websocket-services/UIService001.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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 {}
}
6 changes: 3 additions & 3 deletions src/exception/OCPPError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
8 changes: 4 additions & 4 deletions src/types/UIProtocol.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { JsonObject } from './JsonType';
import { JsonType } from './JsonType';

export enum Protocol {
UI = 'ui',
Expand All @@ -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<void> | JsonObject | Promise<JsonObject>;
payload: JsonType
) => void | Promise<void> | JsonType | Promise<JsonType>;
Loading

0 comments on commit 5cc4b63

Please sign in to comment.