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