}
} else {
// Throw exception
- const errMsg = `${commandName} is not implemented to handle payload ${JSON.stringify(commandPayload, null, 2)}`;
- await this.chargingStation.ocppRequestService.sendError(messageId, new OCPPError(ErrorType.NOT_IMPLEMENTED, errMsg), commandName);
- throw new OCPPError(ErrorType.NOT_IMPLEMENTED, errMsg);
+ const error = new OCPPError(ErrorType.NOT_IMPLEMENTED, `${commandName} is not implemented to handle payload ${JSON.stringify(commandPayload, null, 2)}`);
+ await this.chargingStation.ocppRequestService.sendError(messageId, error, commandName);
+ throw error;
}
// Send the built response
await this.chargingStation.ocppRequestService.sendMessage(messageId, response, MessageType.CALL_RESULT_MESSAGE, commandName);
this.ocppResponseService = ocppResponseService;
}
- public async sendMessage(messageId: string, commandParams: any, messageType: MessageType, commandName: RequestCommand | IncomingRequestCommand): Promise<any> {
+ public async sendMessage(messageId: string, commandParams: Record<string, unknown>, messageType: MessageType,
+ commandName: RequestCommand | IncomingRequestCommand): Promise<unknown> {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const self = this;
// Send a message through wsConnection
// Request
case MessageType.CALL_MESSAGE:
// Build request
- this.chargingStation.requests[messageId] = [responseCallback, rejectCallback, commandParams as Record<string, unknown>];
+ this.chargingStation.requests[messageId] = [responseCallback, rejectCallback, commandParams];
messageToSend = JSON.stringify([messageType, messageId, commandName, commandParams]);
break;
// Response
// Buffer it
this.chargingStation.addToMessageQueue(messageToSend);
// Reject it
- return rejectCallback(new OCPPError(commandParams.code ? commandParams.code : ErrorType.GENERIC_ERROR, commandParams.message ? commandParams.message : `WebSocket closed for message id '${messageId}' with content '${messageToSend}', message buffered`, commandParams.details ? commandParams.details : {}));
+ return rejectCallback(new OCPPError(commandParams.code ? commandParams.code as ErrorType : ErrorType.GENERIC_ERROR, commandParams.message ? commandParams.message as string : `WebSocket closed for message id '${messageId}' with content '${messageToSend}', message buffered`, commandParams.details ? commandParams.details : {}));
}
// Response?
if (messageType === MessageType.CALL_RESULT_MESSAGE) {
resolve();
} else if (messageType === MessageType.CALL_ERROR_MESSAGE) {
// Send timeout
- setTimeout(() => rejectCallback(new OCPPError(commandParams.code ? commandParams.code : ErrorType.GENERIC_ERROR, commandParams.message ? commandParams.message : `Timeout for message id '${messageId}' with content '${messageToSend}'`, commandParams.details ? commandParams.details : {})), Constants.OCPP_ERROR_TIMEOUT);
+ setTimeout(() => rejectCallback(new OCPPError(commandParams.code ? commandParams.code as ErrorType : ErrorType.GENERIC_ERROR, commandParams.message ? commandParams.message as string : `Timeout for message id '${messageId}' with content '${messageToSend}'`, commandParams.details ? commandParams.details : {})), Constants.OCPP_ERROR_TIMEOUT);
}
/**
export default class WorkerSet<T> extends WorkerAbstract {
public readonly maxElementsPerWorker: number;
- private readonly messageHandler: (message: any) => void | Promise<void>;
+ private readonly messageHandler: (message: unknown) => void | Promise<void>;
private workerSet: Set<WorkerSetElement>;
/**
*/
private startWorker(): void {
const worker = new Worker(this.workerScript);
- worker.on('message', this.messageHandler);
+ worker.on('message', (msg) => {
+ (async () => {
+ await this.messageHandler(msg);
+ })().catch(() => {});
+ });
worker.on('error', () => { /* This is intentional */ });
worker.on('exit', (code) => {
WorkerUtils.defaultExitHandler(code);