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