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 sendDiagnosticsStatusNotification(
61 diagnosticsStatus
: OCPP16DiagnosticsStatus
63 const payload
: DiagnosticsStatusNotificationRequest
= {
64 status: diagnosticsStatus
,
66 await this.sendMessage(
69 OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
73 private buildCommandPayload(
74 commandName
: OCPP16RequestCommand
,
75 commandParams
?: JsonType
77 let connectorId
: number;
78 switch (commandName
) {
79 case OCPP16RequestCommand
.AUTHORIZE
:
81 ...(!Utils
.isUndefined(commandParams
?.idTag
)
82 ? { idTag
: commandParams
.idTag
}
83 : { idTag
: Constants
.DEFAULT_IDTAG
}),
84 } as AuthorizeRequest
;
85 case OCPP16RequestCommand
.BOOT_NOTIFICATION
:
87 chargePointModel
: commandParams
?.chargePointModel
,
88 chargePointVendor
: commandParams
?.chargePointVendor
,
89 ...(!Utils
.isUndefined(commandParams
?.chargeBoxSerialNumber
) && {
90 chargeBoxSerialNumber
: commandParams
.chargeBoxSerialNumber
,
92 ...(!Utils
.isUndefined(commandParams
?.chargePointSerialNumber
) && {
93 chargePointSerialNumber
: commandParams
.chargePointSerialNumber
,
95 ...(!Utils
.isUndefined(commandParams
?.firmwareVersion
) && {
96 firmwareVersion
: commandParams
.firmwareVersion
,
98 ...(!Utils
.isUndefined(commandParams
?.iccid
) && { iccid
: commandParams
.iccid
}),
99 ...(!Utils
.isUndefined(commandParams
?.imsi
) && { imsi
: commandParams
.imsi
}),
100 ...(!Utils
.isUndefined(commandParams
?.meterSerialNumber
) && {
101 meterSerialNumber
: commandParams
.meterSerialNumber
,
103 ...(!Utils
.isUndefined(commandParams
?.meterType
) && {
104 meterType
: commandParams
.meterType
,
106 } as OCPP16BootNotificationRequest
;
107 case OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
:
109 status: commandParams
?.diagnosticsStatus
,
110 } as DiagnosticsStatusNotificationRequest
;
111 case OCPP16RequestCommand
.HEARTBEAT
:
112 return {} as HeartbeatRequest
;
113 case OCPP16RequestCommand
.METER_VALUES
:
115 connectorId
: commandParams
?.connectorId
,
116 transactionId
: commandParams
?.transactionId
,
117 meterValue
: Array.isArray(commandParams
?.meterValues
)
118 ? commandParams
?.meterValues
119 : [commandParams
?.meterValue
],
120 } as MeterValuesRequest
;
121 case OCPP16RequestCommand
.STATUS_NOTIFICATION
:
123 connectorId
: commandParams
?.connectorId
,
124 status: commandParams
?.status,
125 errorCode
: commandParams
?.errorCode
,
126 } as StatusNotificationRequest
;
127 case OCPP16RequestCommand
.START_TRANSACTION
:
129 connectorId
: commandParams
?.connectorId
,
130 ...(!Utils
.isUndefined(commandParams
?.idTag
)
131 ? { idTag
: commandParams
?.idTag
}
132 : { idTag
: Constants
.DEFAULT_IDTAG
}),
133 meterStart
: this.chargingStation
.getEnergyActiveImportRegisterByConnectorId(
134 commandParams
?.connectorId
as number
136 timestamp
: new Date().toISOString(),
137 } as StartTransactionRequest
;
138 case OCPP16RequestCommand
.STOP_TRANSACTION
:
139 for (const id
of this.chargingStation
.connectors
.keys()) {
142 this.chargingStation
.getConnectorStatus(id
)?.transactionId
===
143 commandParams
?.transactionId
150 transactionId
: commandParams
?.transactionId
,
151 ...(!Utils
.isUndefined(commandParams
?.idTag
) && { idTag
: commandParams
.idTag
}),
152 meterStop
: commandParams
?.meterStop
,
153 timestamp
: new Date().toISOString(),
154 ...(commandParams
?.reason
&& { reason
: commandParams
.reason
}),
155 ...(this.chargingStation
.getTransactionDataMeterValues() && {
156 transactionData
: OCPP16ServiceUtils
.buildTransactionDataMeterValues(
157 this.chargingStation
.getConnectorStatus(connectorId
).transactionBeginMeterValue
,
158 OCPP16ServiceUtils
.buildTransactionEndMeterValue(
159 this.chargingStation
,
161 commandParams
?.meterStop
as number
165 } as StopTransactionRequest
;
168 ErrorType
.NOT_SUPPORTED
,
169 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
170 `${moduleName}.buildCommandPayload: Unsupported OCPP command: ${commandName}`,