1 // Partial Copyright Jerome Benoit. 2021. All Rights Reserved.
5 StartTransactionRequest
,
6 StopTransactionRequest
,
7 } from
'../../../types/ocpp/1.6/Transaction';
9 DiagnosticsStatusNotificationRequest
,
11 OCPP16BootNotificationRequest
,
13 StatusNotificationRequest
,
14 } from
'../../../types/ocpp/1.6/Requests';
15 import { MeterValuesRequest
, OCPP16MeterValue
} from
'../../../types/ocpp/1.6/MeterValues';
16 import { ResponseType
, SendParams
} from
'../../../types/ocpp/Requests';
18 import type ChargingStation from
'../../ChargingStation';
19 import Constants from
'../../../utils/Constants';
20 import { ErrorType
} from
'../../../types/ocpp/ErrorType';
21 import { JsonType
} from
'../../../types/JsonType';
22 import { OCPP16DiagnosticsStatus
} from
'../../../types/ocpp/1.6/DiagnosticsStatus';
23 import { OCPP16ServiceUtils
} from
'./OCPP16ServiceUtils';
24 import OCPPError from
'../../../exception/OCPPError';
25 import OCPPRequestService from
'../OCPPRequestService';
26 import type OCPPResponseService from
'../OCPPResponseService';
27 import Utils from
'../../../utils/Utils';
29 const moduleName
= 'OCPP16RequestService';
31 export default class OCPP16RequestService
extends OCPPRequestService
{
32 public constructor(chargingStation
: ChargingStation
, ocppResponseService
: OCPPResponseService
) {
33 if (new.target
?.name
=== moduleName
) {
34 throw new TypeError(`Cannot construct ${new.target?.name} instances directly`);
36 super(chargingStation
, ocppResponseService
);
39 public async sendMessageHandler(
40 commandName
: OCPP16RequestCommand
,
41 commandParams
?: JsonType
,
43 ): Promise
<ResponseType
> {
44 if (Object.values(OCPP16RequestCommand
).includes(commandName
)) {
45 return this.sendMessage(
47 this.buildCommandPayload(commandName
, commandParams
),
53 ErrorType
.NOT_SUPPORTED
,
54 `${moduleName}.sendMessageHandler: Unsupported OCPP command ${commandName}`,
60 public async sendTransactionBeginMeterValues(
62 transactionId
: number,
63 beginMeterValue
: OCPP16MeterValue
65 const payload
: MeterValuesRequest
= {
68 meterValue
: [beginMeterValue
],
70 await this.sendMessage(Utils
.generateUUID(), payload
, OCPP16RequestCommand
.METER_VALUES
);
73 public async sendTransactionEndMeterValues(
75 transactionId
: number,
76 endMeterValue
: OCPP16MeterValue
78 const payload
: MeterValuesRequest
= {
81 meterValue
: [endMeterValue
],
83 await this.sendMessage(Utils
.generateUUID(), payload
, OCPP16RequestCommand
.METER_VALUES
);
86 public async sendDiagnosticsStatusNotification(
87 diagnosticsStatus
: OCPP16DiagnosticsStatus
89 const payload
: DiagnosticsStatusNotificationRequest
= {
90 status: diagnosticsStatus
,
92 await this.sendMessage(
95 OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
99 private buildCommandPayload(
100 commandName
: OCPP16RequestCommand
,
101 commandParams
?: JsonType
103 let connectorId
: number;
104 switch (commandName
) {
105 case OCPP16RequestCommand
.AUTHORIZE
:
107 ...(!Utils
.isUndefined(commandParams
?.idTag
)
108 ? { idTag
: commandParams
.idTag
}
109 : { idTag
: Constants
.DEFAULT_IDTAG
}),
110 } as AuthorizeRequest
;
111 case OCPP16RequestCommand
.BOOT_NOTIFICATION
:
113 chargePointModel
: commandParams
?.chargePointModel
,
114 chargePointVendor
: commandParams
?.chargePointVendor
,
115 ...(!Utils
.isUndefined(commandParams
?.chargeBoxSerialNumber
) && {
116 chargeBoxSerialNumber
: commandParams
.chargeBoxSerialNumber
,
118 ...(!Utils
.isUndefined(commandParams
?.chargePointSerialNumber
) && {
119 chargePointSerialNumber
: commandParams
.chargePointSerialNumber
,
121 ...(!Utils
.isUndefined(commandParams
?.firmwareVersion
) && {
122 firmwareVersion
: commandParams
.firmwareVersion
,
124 ...(!Utils
.isUndefined(commandParams
?.iccid
) && { iccid
: commandParams
.iccid
}),
125 ...(!Utils
.isUndefined(commandParams
?.imsi
) && { imsi
: commandParams
.imsi
}),
126 ...(!Utils
.isUndefined(commandParams
?.meterSerialNumber
) && {
127 meterSerialNumber
: commandParams
.meterSerialNumber
,
129 ...(!Utils
.isUndefined(commandParams
?.meterType
) && {
130 meterType
: commandParams
.meterType
,
132 } as OCPP16BootNotificationRequest
;
133 case OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
:
135 status: commandParams
?.diagnosticsStatus
,
136 } as DiagnosticsStatusNotificationRequest
;
137 case OCPP16RequestCommand
.HEARTBEAT
:
138 return {} as HeartbeatRequest
;
139 case OCPP16RequestCommand
.METER_VALUES
:
141 connectorId
: commandParams
?.connectorId
,
142 transactionId
: commandParams
?.transactionId
,
143 meterValue
: Array.isArray(commandParams
?.meterValues
)
144 ? commandParams
?.meterValues
145 : [commandParams
?.meterValue
],
146 } as MeterValuesRequest
;
147 case OCPP16RequestCommand
.STATUS_NOTIFICATION
:
149 connectorId
: commandParams
?.connectorId
,
150 status: commandParams
?.status,
151 errorCode
: commandParams
?.errorCode
,
152 } as StatusNotificationRequest
;
153 case OCPP16RequestCommand
.START_TRANSACTION
:
155 connectorId
: commandParams
?.connectorId
,
156 ...(!Utils
.isUndefined(commandParams
?.idTag
)
157 ? { idTag
: commandParams
?.idTag
}
158 : { idTag
: Constants
.DEFAULT_IDTAG
}),
159 meterStart
: this.chargingStation
.getEnergyActiveImportRegisterByConnectorId(
160 commandParams
?.connectorId
as number
162 timestamp
: new Date().toISOString(),
163 } as StartTransactionRequest
;
164 case OCPP16RequestCommand
.STOP_TRANSACTION
:
165 for (const id
of this.chargingStation
.connectors
.keys()) {
168 this.chargingStation
.getConnectorStatus(id
)?.transactionId
===
169 commandParams
?.transactionId
176 transactionId
: commandParams
?.transactionId
,
177 ...(!Utils
.isUndefined(commandParams
?.idTag
) && { idTag
: commandParams
.idTag
}),
178 meterStop
: commandParams
?.meterStop
,
179 timestamp
: new Date().toISOString(),
180 ...(commandParams
?.reason
&& { reason
: commandParams
.reason
}),
181 ...(this.chargingStation
.getTransactionDataMeterValues() && {
182 transactionData
: OCPP16ServiceUtils
.buildTransactionDataMeterValues(
183 this.chargingStation
.getConnectorStatus(connectorId
).transactionBeginMeterValue
,
184 OCPP16ServiceUtils
.buildTransactionEndMeterValue(
185 this.chargingStation
,
187 commandParams
?.meterStop
as number
191 } as StopTransactionRequest
;
194 ErrorType
.NOT_SUPPORTED
,
195 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
196 `${moduleName}.buildCommandPayload: Unsupported OCPP command: ${commandName}`,