| 1 | // Partial Copyright Jerome Benoit. 2021. All Rights Reserved. |
| 2 | |
| 3 | import OCPPError from '../../../exception/OCPPError'; |
| 4 | import { JsonObject, JsonType } from '../../../types/JsonType'; |
| 5 | import { OCPP16RequestCommand } from '../../../types/ocpp/1.6/Requests'; |
| 6 | import { ErrorType } from '../../../types/ocpp/ErrorType'; |
| 7 | import { RequestParams } from '../../../types/ocpp/Requests'; |
| 8 | import Constants from '../../../utils/Constants'; |
| 9 | import Utils from '../../../utils/Utils'; |
| 10 | import type ChargingStation from '../../ChargingStation'; |
| 11 | import { ChargingStationUtils } from '../../ChargingStationUtils'; |
| 12 | import OCPPRequestService from '../OCPPRequestService'; |
| 13 | import type OCPPResponseService from '../OCPPResponseService'; |
| 14 | import { OCPP16ServiceUtils } from './OCPP16ServiceUtils'; |
| 15 | |
| 16 | const moduleName = 'OCPP16RequestService'; |
| 17 | |
| 18 | export default class OCPP16RequestService extends OCPPRequestService { |
| 19 | public constructor(ocppResponseService: OCPPResponseService) { |
| 20 | if (new.target?.name === moduleName) { |
| 21 | throw new TypeError(`Cannot construct ${new.target?.name} instances directly`); |
| 22 | } |
| 23 | super(ocppResponseService); |
| 24 | } |
| 25 | |
| 26 | public async requestHandler<Request extends JsonType, Response extends JsonType>( |
| 27 | chargingStation: ChargingStation, |
| 28 | commandName: OCPP16RequestCommand, |
| 29 | commandParams?: JsonType, |
| 30 | params?: RequestParams |
| 31 | ): Promise<Response> { |
| 32 | if (ChargingStationUtils.isRequestCommandSupported(commandName, chargingStation)) { |
| 33 | return (await this.sendMessage( |
| 34 | chargingStation, |
| 35 | Utils.generateUUID(), |
| 36 | this.buildRequestPayload<Request>(chargingStation, commandName, commandParams), |
| 37 | commandName, |
| 38 | params |
| 39 | )) as unknown as Response; |
| 40 | } |
| 41 | throw new OCPPError( |
| 42 | ErrorType.NOT_SUPPORTED, |
| 43 | `${moduleName}.requestHandler: Unsupported OCPP command '${commandName}'`, |
| 44 | commandName, |
| 45 | commandParams |
| 46 | ); |
| 47 | } |
| 48 | |
| 49 | private buildRequestPayload<Request extends JsonType>( |
| 50 | chargingStation: ChargingStation, |
| 51 | commandName: OCPP16RequestCommand, |
| 52 | commandParams?: JsonType |
| 53 | ): Request { |
| 54 | let connectorId: number; |
| 55 | commandParams = commandParams as JsonObject; |
| 56 | switch (commandName) { |
| 57 | case OCPP16RequestCommand.AUTHORIZE: |
| 58 | return { |
| 59 | ...(!Utils.isUndefined(commandParams?.idTag) |
| 60 | ? { idTag: commandParams.idTag } |
| 61 | : { idTag: Constants.DEFAULT_IDTAG }), |
| 62 | } as unknown as Request; |
| 63 | case OCPP16RequestCommand.BOOT_NOTIFICATION: |
| 64 | return { |
| 65 | chargePointModel: commandParams?.chargePointModel, |
| 66 | chargePointVendor: commandParams?.chargePointVendor, |
| 67 | ...(!Utils.isUndefined(commandParams?.chargeBoxSerialNumber) && { |
| 68 | chargeBoxSerialNumber: commandParams.chargeBoxSerialNumber, |
| 69 | }), |
| 70 | ...(!Utils.isUndefined(commandParams?.chargePointSerialNumber) && { |
| 71 | chargePointSerialNumber: commandParams.chargePointSerialNumber, |
| 72 | }), |
| 73 | ...(!Utils.isUndefined(commandParams?.firmwareVersion) && { |
| 74 | firmwareVersion: commandParams.firmwareVersion, |
| 75 | }), |
| 76 | ...(!Utils.isUndefined(commandParams?.iccid) && { iccid: commandParams.iccid }), |
| 77 | ...(!Utils.isUndefined(commandParams?.imsi) && { imsi: commandParams.imsi }), |
| 78 | ...(!Utils.isUndefined(commandParams?.meterSerialNumber) && { |
| 79 | meterSerialNumber: commandParams.meterSerialNumber, |
| 80 | }), |
| 81 | ...(!Utils.isUndefined(commandParams?.meterType) && { |
| 82 | meterType: commandParams.meterType, |
| 83 | }), |
| 84 | } as unknown as Request; |
| 85 | case OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION: |
| 86 | return { |
| 87 | status: commandParams?.diagnosticsStatus, |
| 88 | } as unknown as Request; |
| 89 | case OCPP16RequestCommand.HEARTBEAT: |
| 90 | return {} as unknown as Request; |
| 91 | case OCPP16RequestCommand.METER_VALUES: |
| 92 | // Sanity check |
| 93 | if (!Array.isArray(commandParams?.meterValue)) { |
| 94 | throw new OCPPError( |
| 95 | ErrorType.TYPE_CONSTRAINT_VIOLATION, |
| 96 | `${moduleName}.buildRequestPayload ${commandName}: Invalid array type for meterValue PDU field`, |
| 97 | commandName, |
| 98 | commandParams |
| 99 | ); |
| 100 | } |
| 101 | return { |
| 102 | connectorId: commandParams?.connectorId, |
| 103 | transactionId: commandParams?.transactionId, |
| 104 | meterValue: commandParams?.meterValue, |
| 105 | } as unknown as Request; |
| 106 | case OCPP16RequestCommand.STATUS_NOTIFICATION: |
| 107 | return { |
| 108 | connectorId: commandParams?.connectorId, |
| 109 | status: commandParams?.status, |
| 110 | errorCode: commandParams?.errorCode, |
| 111 | } as unknown as Request; |
| 112 | case OCPP16RequestCommand.START_TRANSACTION: |
| 113 | return { |
| 114 | connectorId: commandParams?.connectorId, |
| 115 | ...(!Utils.isUndefined(commandParams?.idTag) |
| 116 | ? { idTag: commandParams?.idTag } |
| 117 | : { idTag: Constants.DEFAULT_IDTAG }), |
| 118 | meterStart: chargingStation.getEnergyActiveImportRegisterByConnectorId( |
| 119 | commandParams?.connectorId as number |
| 120 | ), |
| 121 | timestamp: new Date().toISOString(), |
| 122 | } as unknown as Request; |
| 123 | case OCPP16RequestCommand.STOP_TRANSACTION: |
| 124 | connectorId = chargingStation.getConnectorIdByTransactionId( |
| 125 | commandParams?.transactionId as number |
| 126 | ); |
| 127 | return { |
| 128 | transactionId: commandParams?.transactionId, |
| 129 | ...(!Utils.isUndefined(commandParams?.idTag) && { idTag: commandParams.idTag }), |
| 130 | meterStop: commandParams?.meterStop, |
| 131 | timestamp: new Date().toISOString(), |
| 132 | ...(commandParams?.reason && { reason: commandParams.reason }), |
| 133 | ...(chargingStation.getTransactionDataMeterValues() && { |
| 134 | transactionData: OCPP16ServiceUtils.buildTransactionDataMeterValues( |
| 135 | chargingStation.getConnectorStatus(connectorId).transactionBeginMeterValue, |
| 136 | OCPP16ServiceUtils.buildTransactionEndMeterValue( |
| 137 | chargingStation, |
| 138 | connectorId, |
| 139 | commandParams?.meterStop as number |
| 140 | ) |
| 141 | ), |
| 142 | }), |
| 143 | } as unknown as Request; |
| 144 | default: |
| 145 | throw new OCPPError( |
| 146 | ErrorType.NOT_SUPPORTED, |
| 147 | // eslint-disable-next-line @typescript-eslint/restrict-template-expressions |
| 148 | `${moduleName}.buildRequestPayload: Unsupported OCPP command '${commandName}'`, |
| 149 | commandName, |
| 150 | commandParams |
| 151 | ); |
| 152 | } |
| 153 | } |
| 154 | } |