// Partial Copyright Jerome Benoit. 2021-2024. All Rights Reserved.
import { createWriteStream, readdirSync } from 'node:fs'
-import { dirname, join, resolve } from 'node:path'
+import { dirname, extname, join, resolve } from 'node:path'
import { URL, fileURLToPath } from 'node:url'
import type { ValidateFunction } from 'ajv'
import {
type ChangeConfigurationRequest,
type ChangeConfigurationResponse,
+ ConfigurationSection,
ErrorType,
type GenericResponse,
GenericStatus,
type GetDiagnosticsResponse,
type IncomingRequestHandler,
type JsonType,
+ type LogConfiguration,
OCPP16AuthorizationStatus,
OCPP16AvailabilityType,
type OCPP16BootNotificationRequest,
type UnlockConnectorResponse
} from '../../../types/index.js'
import {
+ Configuration,
Constants,
convertToDate,
convertToInt,
formatDurationMilliSeconds,
getRandomInteger,
+ isAsyncFunction,
isEmptyArray,
isNotEmptyArray,
isNotEmptyString,
.bind(this)
]
])
+ // Handle incoming request events
this.on(
OCPP16IncomingRequestCommand.REMOTE_START_TRANSACTION,
(
}
}
)
+ this.on(
+ OCPP16IncomingRequestCommand.REMOTE_STOP_TRANSACTION,
+ (
+ chargingStation: ChargingStation,
+ request: RemoteStopTransactionRequest,
+ response: GenericResponse
+ ) => {
+ if (response.status === GenericStatus.Accepted) {
+ const { transactionId } = request
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+ const connectorId = chargingStation.getConnectorIdByTransactionId(transactionId)!
+ OCPP16ServiceUtils.remoteStopTransaction(chargingStation, connectorId)
+ .then(response => {
+ if (response.status === GenericStatus.Accepted) {
+ logger.debug(
+ `${chargingStation.logPrefix()} Remote stop transaction ACCEPTED on ${chargingStation.stationInfo?.chargingStationId}#${connectorId} for transaction '${transactionId}'`
+ )
+ } else {
+ logger.debug(
+ `${chargingStation.logPrefix()} Remote stop transaction REJECTED on ${chargingStation.stationInfo?.chargingStationId}#${connectorId} for transaction '${transactionId}'`
+ )
+ }
+ })
+ .catch(error => {
+ logger.error(
+ `${chargingStation.logPrefix()} ${moduleName}.constructor: Remote stop transaction error:`,
+ error
+ )
+ })
+ }
+ }
+ )
this.on(
OCPP16IncomingRequestCommand.TRIGGER_MESSAGE,
(
this.validatePayload(chargingStation, commandName, commandPayload)
// Call the method to build the response
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
- response = (await this.incomingRequestHandlers.get(commandName)!(
- chargingStation,
- commandPayload
- )) as ResType
- this.emit(commandName, chargingStation, commandPayload, response)
+ const incomingRequestHandler = this.incomingRequestHandlers.get(commandName)!
+ if (isAsyncFunction(incomingRequestHandler)) {
+ response = (await incomingRequestHandler(chargingStation, commandPayload)) as ResType
+ } else {
+ response = incomingRequestHandler(chargingStation, commandPayload) as ResType
+ }
} catch (error) {
// Log
logger.error(
// Throw exception
throw new OCPPError(
ErrorType.NOT_IMPLEMENTED,
- `${commandName} is not implemented to handle request PDU ${JSON.stringify(
+ `'${commandName}' is not implemented to handle request PDU ${JSON.stringify(
commandPayload,
undefined,
2
commandPayload,
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,
commandPayload
)
response,
commandName
)
+ // Emit command name event to allow delayed handling
+ this.emit(commandName, chargingStation, commandPayload, response)
}
private validatePayload (
idTag
)
}
+ logger.debug(
+ `${chargingStation.logPrefix()} Remote start transaction ACCEPTED on connector id ${transactionConnectorId}, idTag '${idTag}'`
+ )
return OCPP16Constants.OCPP_RESPONSE_ACCEPTED
}
OCPP16ChargePointStatus.Available
)
}
- logger.warn(
+ logger.debug(
`${chargingStation.logPrefix()} Remote start transaction REJECTED on connector id ${connectorId}, idTag '${idTag}', availability '${connectorStatus?.availability}', status '${connectorStatus?.status}'`
)
return OCPP16Constants.OCPP_RESPONSE_REJECTED
)
return true
}
- logger.warn(
+ logger.debug(
`${chargingStation.logPrefix()} Not allowed to set ${
chargingProfile.chargingProfilePurpose
} charging profile(s) at remote start transaction`
return false
}
- private async handleRequestRemoteStopTransaction (
+ private handleRequestRemoteStopTransaction (
chargingStation: ChargingStation,
commandPayload: RemoteStopTransactionRequest
- ): Promise<GenericResponse> {
+ ): GenericResponse {
const { transactionId } = commandPayload
- if (chargingStation.hasEvses) {
- for (const [evseId, evseStatus] of chargingStation.evses) {
- if (evseId > 0) {
- for (const [connectorId, connectorStatus] of evseStatus.connectors) {
- if (connectorStatus.transactionId === transactionId) {
- return await OCPP16ServiceUtils.remoteStopTransaction(chargingStation, connectorId)
- }
- }
- }
- }
- } else {
- for (const connectorId of chargingStation.connectors.keys()) {
- if (
- connectorId > 0 &&
- chargingStation.getConnectorStatus(connectorId)?.transactionId === transactionId
- ) {
- return await OCPP16ServiceUtils.remoteStopTransaction(chargingStation, connectorId)
- }
- }
+ if (chargingStation.getConnectorIdByTransactionId(transactionId) != null) {
+ logger.debug(
+ `${chargingStation.logPrefix()} Remote stop transaction ACCEPTED for transactionId '${transactionId}'`
+ )
+ return OCPP16Constants.OCPP_RESPONSE_ACCEPTED
}
- logger.warn(
- `${chargingStation.logPrefix()} Trying to remote stop a non existing transaction with id ${transactionId}`
+ logger.debug(
+ `${chargingStation.logPrefix()} Remote stop transaction REJECTED for transactionId '${transactionId}'`
)
return OCPP16Constants.OCPP_RESPONSE_REJECTED
}
if (uri.protocol.startsWith('ftp:')) {
let ftpClient: Client | undefined
try {
+ const logConfiguration = Configuration.getConfigurationSection<LogConfiguration>(
+ ConfigurationSection.log
+ )
const logFiles = readdirSync(resolve(dirname(fileURLToPath(import.meta.url)), '../'))
- .filter(file => file.endsWith('.log'))
- .map(file => join('./', file))
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+ .filter(file => file.endsWith(extname(logConfiguration.file!)))
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+ .map(file => join(dirname(logConfiguration.file!), file))
const diagnosticsArchive = `${chargingStation.stationInfo?.chargingStationId}_logs.tar.gz`
create({ gzip: true }, logFiles).pipe(createWriteStream(diagnosticsArchive))
ftpClient = new Client()
const accessResponse = await ftpClient.access({
- host: uri.host,
+ host: uri.hostname,
...(isNotEmptyString(uri.port) && { port: convertToInt(uri.port) }),
...(isNotEmptyString(uri.username) && { user: uri.username }),
...(isNotEmptyString(uri.password) && { password: uri.password })