| 1 | // Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved. |
| 2 | |
| 3 | import fs from 'fs'; |
| 4 | import path from 'path'; |
| 5 | import { fileURLToPath } from 'url'; |
| 6 | |
| 7 | import type { JSONSchemaType } from 'ajv'; |
| 8 | |
| 9 | import OCPPError from '../../../exception/OCPPError'; |
| 10 | import type { JsonObject, JsonType } from '../../../types/JsonType'; |
| 11 | import { |
| 12 | OCPP20ClearCacheRequest, |
| 13 | OCPP20IncomingRequestCommand, |
| 14 | } from '../../../types/ocpp/2.0/Requests'; |
| 15 | import type { OCPP20ClearCacheResponse } from '../../../types/ocpp/2.0/Responses'; |
| 16 | import { ErrorType } from '../../../types/ocpp/ErrorType'; |
| 17 | import { OCPPVersion } from '../../../types/ocpp/OCPPVersion'; |
| 18 | import type { IncomingRequestHandler } from '../../../types/ocpp/Requests'; |
| 19 | import Constants from '../../../utils/Constants'; |
| 20 | import logger from '../../../utils/Logger'; |
| 21 | import type ChargingStation from '../../ChargingStation'; |
| 22 | import { ChargingStationUtils } from '../../ChargingStationUtils'; |
| 23 | import OCPPIncomingRequestService from '../OCPPIncomingRequestService'; |
| 24 | import { OCPP20ServiceUtils } from './OCPP20ServiceUtils'; |
| 25 | |
| 26 | const moduleName = 'OCPP20IncomingRequestService'; |
| 27 | |
| 28 | export default class OCPP20IncomingRequestService extends OCPPIncomingRequestService { |
| 29 | private incomingRequestHandlers: Map<OCPP20IncomingRequestCommand, IncomingRequestHandler>; |
| 30 | private jsonSchemas: Map<OCPP20IncomingRequestCommand, JSONSchemaType<JsonObject>>; |
| 31 | |
| 32 | public constructor() { |
| 33 | if (new.target?.name === moduleName) { |
| 34 | throw new TypeError(`Cannot construct ${new.target?.name} instances directly`); |
| 35 | } |
| 36 | super(OCPPVersion.VERSION_20); |
| 37 | this.incomingRequestHandlers = new Map<OCPP20IncomingRequestCommand, IncomingRequestHandler>([ |
| 38 | [OCPP20IncomingRequestCommand.CLEAR_CACHE, this.handleRequestClearCache.bind(this)], |
| 39 | ]); |
| 40 | this.jsonSchemas = new Map<OCPP20IncomingRequestCommand, JSONSchemaType<JsonObject>>([ |
| 41 | [ |
| 42 | OCPP20IncomingRequestCommand.CLEAR_CACHE, |
| 43 | JSON.parse( |
| 44 | fs.readFileSync( |
| 45 | path.resolve( |
| 46 | path.dirname(fileURLToPath(import.meta.url)), |
| 47 | '../../../assets/json-schemas/ocpp/2.0/ClearCacheRequest.json' |
| 48 | ), |
| 49 | 'utf8' |
| 50 | ) |
| 51 | ) as JSONSchemaType<OCPP20ClearCacheRequest>, |
| 52 | ], |
| 53 | ]); |
| 54 | this.validatePayload.bind(this); |
| 55 | } |
| 56 | |
| 57 | public async incomingRequestHandler( |
| 58 | chargingStation: ChargingStation, |
| 59 | messageId: string, |
| 60 | commandName: OCPP20IncomingRequestCommand, |
| 61 | commandPayload: JsonType |
| 62 | ): Promise<void> { |
| 63 | let response: JsonType; |
| 64 | if ( |
| 65 | chargingStation.getOcppStrictCompliance() === true && |
| 66 | chargingStation.isInPendingState() === true /* && |
| 67 | (commandName === OCPP20IncomingRequestCommand.REMOTE_START_TRANSACTION || |
| 68 | commandName === OCPP20IncomingRequestCommand.REMOTE_STOP_TRANSACTION ) */ |
| 69 | ) { |
| 70 | throw new OCPPError( |
| 71 | ErrorType.SECURITY_ERROR, |
| 72 | `${commandName} cannot be issued to handle request PDU ${JSON.stringify( |
| 73 | commandPayload, |
| 74 | null, |
| 75 | 2 |
| 76 | )} while the charging station is in pending state on the central server`, |
| 77 | commandName, |
| 78 | commandPayload |
| 79 | ); |
| 80 | } |
| 81 | if ( |
| 82 | chargingStation.isRegistered() === true || |
| 83 | (chargingStation.getOcppStrictCompliance() === false && |
| 84 | chargingStation.isInUnknownState() === true) |
| 85 | ) { |
| 86 | if ( |
| 87 | this.incomingRequestHandlers.has(commandName) === true && |
| 88 | OCPP20ServiceUtils.isIncomingRequestCommandSupported(chargingStation, commandName) === true |
| 89 | ) { |
| 90 | try { |
| 91 | this.validatePayload(chargingStation, commandName, commandPayload); |
| 92 | // Call the method to build the response |
| 93 | response = await this.incomingRequestHandlers.get(commandName)( |
| 94 | chargingStation, |
| 95 | commandPayload |
| 96 | ); |
| 97 | } catch (error) { |
| 98 | // Log |
| 99 | logger.error( |
| 100 | `${chargingStation.logPrefix()} ${moduleName}.incomingRequestHandler: Handle incoming request error:`, |
| 101 | error |
| 102 | ); |
| 103 | throw error; |
| 104 | } |
| 105 | } else { |
| 106 | // Throw exception |
| 107 | throw new OCPPError( |
| 108 | ErrorType.NOT_IMPLEMENTED, |
| 109 | `${commandName} is not implemented to handle request PDU ${JSON.stringify( |
| 110 | commandPayload, |
| 111 | null, |
| 112 | 2 |
| 113 | )}`, |
| 114 | commandName, |
| 115 | commandPayload |
| 116 | ); |
| 117 | } |
| 118 | } else { |
| 119 | throw new OCPPError( |
| 120 | ErrorType.SECURITY_ERROR, |
| 121 | `${commandName} cannot be issued to handle request PDU ${JSON.stringify( |
| 122 | commandPayload, |
| 123 | null, |
| 124 | 2 |
| 125 | )} while the charging station is not registered on the central server.`, |
| 126 | commandName, |
| 127 | commandPayload |
| 128 | ); |
| 129 | } |
| 130 | // Send the built response |
| 131 | await chargingStation.ocppRequestService.sendResponse( |
| 132 | chargingStation, |
| 133 | messageId, |
| 134 | response, |
| 135 | commandName |
| 136 | ); |
| 137 | } |
| 138 | |
| 139 | private validatePayload( |
| 140 | chargingStation: ChargingStation, |
| 141 | commandName: OCPP20IncomingRequestCommand, |
| 142 | commandPayload: JsonType |
| 143 | ): boolean { |
| 144 | if (this.jsonSchemas.has(commandName)) { |
| 145 | return this.validateIncomingRequestPayload( |
| 146 | chargingStation, |
| 147 | commandName, |
| 148 | this.jsonSchemas.get(commandName), |
| 149 | commandPayload |
| 150 | ); |
| 151 | } |
| 152 | logger.warn( |
| 153 | `${chargingStation.logPrefix()} ${moduleName}.validatePayload: No JSON schema found for command ${commandName} PDU validation` |
| 154 | ); |
| 155 | return false; |
| 156 | } |
| 157 | |
| 158 | private handleRequestClearCache(chargingStation: ChargingStation): OCPP20ClearCacheResponse { |
| 159 | chargingStation.authorizedTagsCache.deleteAuthorizedTags( |
| 160 | ChargingStationUtils.getAuthorizationFile(chargingStation.stationInfo) |
| 161 | ); |
| 162 | return Constants.OCPP_RESPONSE_ACCEPTED; |
| 163 | } |
| 164 | } |