import type { ValidateFunction } from 'ajv'
import { secondsToMilliseconds } from 'date-fns'
-import { OCPP16ServiceUtils } from './OCPP16ServiceUtils.js'
import {
- type ChargingStation,
addConfigurationKey,
+ type ChargingStation,
getConfigurationKey,
hasReservationExpired,
resetConnectorStatus
type SetChargingProfileResponse,
type UnlockConnectorResponse
} from '../../../types/index.js'
-import { Constants, convertToInt, logger } from '../../../utils/index.js'
+import { Constants, convertToInt, isAsyncFunction, logger } from '../../../utils/index.js'
import { OCPPResponseService } from '../OCPPResponseService.js'
+import { OCPP16ServiceUtils } from './OCPP16ServiceUtils.js'
const moduleName = 'OCPP16ResponseService'
try {
this.validatePayload(chargingStation, commandName, payload)
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
- await this.responseHandlers.get(commandName)!(chargingStation, payload, requestPayload)
+ const responseHandler = this.responseHandlers.get(commandName)!
+ if (isAsyncFunction(responseHandler)) {
+ await responseHandler(chargingStation, payload, requestPayload)
+ } else {
+ (
+ responseHandler as (
+ chargingStation: ChargingStation,
+ payload: JsonType,
+ requestPayload?: JsonType
+ ) => void
+ )(chargingStation, payload, requestPayload)
+ }
} catch (error) {
logger.error(
`${chargingStation.logPrefix()} ${moduleName}.responseHandler: Handle response error:`,
// Throw exception
throw new OCPPError(
ErrorType.NOT_IMPLEMENTED,
- `${commandName} is not implemented to handle response PDU ${JSON.stringify(
+ `'${commandName}' is not implemented to handle response PDU ${JSON.stringify(
payload,
undefined,
2
payload,
undefined,
2
- )} while the charging station is not registered on the central server.`,
+ )} while the charging station is not registered on the central server`,
commandName,
payload
)
logger.debug(
`${chargingStation.logPrefix()} idTag '${requestPayload.idTag}' rejected with status '${
payload.idTagInfo.status
- }`
+ }'`
)
}
} else {
const connectorStatus = chargingStation.getConnectorStatus(connectorId)
resetConnectorStatus(connectorStatus)
chargingStation.stopMeterValues(connectorId)
- if (connectorStatus?.status !== OCPP16ChargePointStatus.Available) {
+ if (chargingStation.getReservationBy('connectorId', connectorId) != null) {
+ await OCPP16ServiceUtils.sendAndSetConnectorStatus(
+ chargingStation,
+ connectorId,
+ OCPP16ChargePointStatus.Reserved
+ )
+ } else if (connectorStatus?.status !== OCPP16ChargePointStatus.Available) {
await OCPP16ServiceUtils.sendAndSetConnectorStatus(
chargingStation,
connectorId,