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