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 { |
08f130a0 | 19 | public constructor(ocppResponseService: OCPPResponseService) { |
909dcf2d | 20 | if (new.target?.name === moduleName) { |
06127450 | 21 | throw new TypeError(`Cannot construct ${new.target?.name} instances directly`); |
9f2e3130 | 22 | } |
08f130a0 | 23 | super(ocppResponseService); |
9f2e3130 JB |
24 | } |
25 | ||
5cc4b63b | 26 | public async requestHandler<Request extends JsonType, Response extends JsonType>( |
08f130a0 | 27 | chargingStation: ChargingStation, |
94a464f9 | 28 | commandName: OCPP16RequestCommand, |
5cc4b63b | 29 | commandParams?: JsonType, |
be9b0d50 | 30 | params?: RequestParams |
f22266fd | 31 | ): Promise<Response> { |
94a464f9 | 32 | if (Object.values(OCPP16RequestCommand).includes(commandName)) { |
f22266fd | 33 | return (await this.sendMessage( |
08f130a0 | 34 | chargingStation, |
94a464f9 | 35 | Utils.generateUUID(), |
08f130a0 | 36 | this.buildRequestPayload<Request>(chargingStation, commandName, commandParams), |
94a464f9 JB |
37 | commandName, |
38 | params | |
f22266fd | 39 | )) as unknown as Response; |
94a464f9 JB |
40 | } |
41 | throw new OCPPError( | |
42 | ErrorType.NOT_SUPPORTED, | |
1c959f1f | 43 | `${moduleName}.requestHandler: Unsupported OCPP command ${commandName}`, |
94a464f9 JB |
44 | commandName, |
45 | { commandName } | |
46 | ); | |
c0560973 JB |
47 | } |
48 | ||
5cc4b63b | 49 | private buildRequestPayload<Request extends JsonType>( |
08f130a0 | 50 | chargingStation: ChargingStation, |
78085c42 | 51 | commandName: OCPP16RequestCommand, |
5cc4b63b | 52 | commandParams?: JsonType |
f22266fd | 53 | ): Request { |
68c993d5 | 54 | let connectorId: number; |
5cc4b63b | 55 | commandParams = commandParams as JsonObject; |
78085c42 JB |
56 | switch (commandName) { |
57 | case OCPP16RequestCommand.AUTHORIZE: | |
58 | return { | |
59 | ...(!Utils.isUndefined(commandParams?.idTag) | |
60 | ? { idTag: commandParams.idTag } | |
61 | : { idTag: Constants.DEFAULT_IDTAG }), | |
f22266fd | 62 | } as unknown as Request; |
78085c42 JB |
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 | }), | |
f22266fd | 84 | } as unknown as Request; |
78085c42 JB |
85 | case OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION: |
86 | return { | |
87 | status: commandParams?.diagnosticsStatus, | |
f22266fd | 88 | } as unknown as Request; |
78085c42 | 89 | case OCPP16RequestCommand.HEARTBEAT: |
f22266fd | 90 | return {} as unknown as Request; |
78085c42 JB |
91 | case OCPP16RequestCommand.METER_VALUES: |
92 | return { | |
93 | connectorId: commandParams?.connectorId, | |
94 | transactionId: commandParams?.transactionId, | |
5e62b119 JB |
95 | meterValue: Array.isArray(commandParams?.meterValue) |
96 | ? commandParams?.meterValue | |
78085c42 | 97 | : [commandParams?.meterValue], |
f22266fd | 98 | } as unknown as Request; |
78085c42 JB |
99 | case OCPP16RequestCommand.STATUS_NOTIFICATION: |
100 | return { | |
101 | connectorId: commandParams?.connectorId, | |
78085c42 | 102 | status: commandParams?.status, |
93b4a429 | 103 | errorCode: commandParams?.errorCode, |
f22266fd | 104 | } as unknown as Request; |
78085c42 JB |
105 | case OCPP16RequestCommand.START_TRANSACTION: |
106 | return { | |
107 | connectorId: commandParams?.connectorId, | |
108 | ...(!Utils.isUndefined(commandParams?.idTag) | |
109 | ? { idTag: commandParams?.idTag } | |
110 | : { idTag: Constants.DEFAULT_IDTAG }), | |
08f130a0 | 111 | meterStart: chargingStation.getEnergyActiveImportRegisterByConnectorId( |
78085c42 JB |
112 | commandParams?.connectorId as number |
113 | ), | |
114 | timestamp: new Date().toISOString(), | |
f22266fd | 115 | } as unknown as Request; |
78085c42 | 116 | case OCPP16RequestCommand.STOP_TRANSACTION: |
08f130a0 | 117 | connectorId = chargingStation.getConnectorIdByTransactionId( |
f479a792 JB |
118 | commandParams?.transactionId as number |
119 | ); | |
78085c42 JB |
120 | return { |
121 | transactionId: commandParams?.transactionId, | |
122 | ...(!Utils.isUndefined(commandParams?.idTag) && { idTag: commandParams.idTag }), | |
123 | meterStop: commandParams?.meterStop, | |
124 | timestamp: new Date().toISOString(), | |
125 | ...(commandParams?.reason && { reason: commandParams.reason }), | |
08f130a0 | 126 | ...(chargingStation.getTransactionDataMeterValues() && { |
78085c42 | 127 | transactionData: OCPP16ServiceUtils.buildTransactionDataMeterValues( |
08f130a0 | 128 | chargingStation.getConnectorStatus(connectorId).transactionBeginMeterValue, |
78085c42 | 129 | OCPP16ServiceUtils.buildTransactionEndMeterValue( |
08f130a0 | 130 | chargingStation, |
68c993d5 | 131 | connectorId, |
78085c42 JB |
132 | commandParams?.meterStop as number |
133 | ) | |
134 | ), | |
135 | }), | |
f22266fd | 136 | } as unknown as Request; |
78085c42 JB |
137 | default: |
138 | throw new OCPPError( | |
139 | ErrorType.NOT_SUPPORTED, | |
140 | // eslint-disable-next-line @typescript-eslint/restrict-template-expressions | |
1c959f1f | 141 | `${moduleName}.buildRequestPayload: Unsupported OCPP command: ${commandName}`, |
78085c42 JB |
142 | commandName, |
143 | { commandName } | |
144 | ); | |
145 | } | |
146 | } | |
c0560973 | 147 | } |