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 { ResponseType
, SendParams
} from
'../../../types/ocpp/Requests';
17 import type ChargingStation from
'../../ChargingStation';
18 import Constants from
'../../../utils/Constants';
19 import { ErrorType
} from
'../../../types/ocpp/ErrorType';
20 import { JsonType
} from
'../../../types/JsonType';
21 import { MeterValuesRequest
} from
'../../../types/ocpp/1.6/MeterValues';
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 private buildCommandPayload(
61 commandName
: OCPP16RequestCommand
,
62 commandParams
?: JsonType
64 let connectorId
: number;
65 switch (commandName
) {
66 case OCPP16RequestCommand
.AUTHORIZE
:
68 ...(!Utils
.isUndefined(commandParams
?.idTag
)
69 ? { idTag
: commandParams
.idTag
}
70 : { idTag
: Constants
.DEFAULT_IDTAG
}),
71 } as AuthorizeRequest
;
72 case OCPP16RequestCommand
.BOOT_NOTIFICATION
:
74 chargePointModel
: commandParams
?.chargePointModel
,
75 chargePointVendor
: commandParams
?.chargePointVendor
,
76 ...(!Utils
.isUndefined(commandParams
?.chargeBoxSerialNumber
) && {
77 chargeBoxSerialNumber
: commandParams
.chargeBoxSerialNumber
,
79 ...(!Utils
.isUndefined(commandParams
?.chargePointSerialNumber
) && {
80 chargePointSerialNumber
: commandParams
.chargePointSerialNumber
,
82 ...(!Utils
.isUndefined(commandParams
?.firmwareVersion
) && {
83 firmwareVersion
: commandParams
.firmwareVersion
,
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
,
90 ...(!Utils
.isUndefined(commandParams
?.meterType
) && {
91 meterType
: commandParams
.meterType
,
93 } as OCPP16BootNotificationRequest
;
94 case OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
:
96 status: commandParams
?.diagnosticsStatus
,
97 } as DiagnosticsStatusNotificationRequest
;
98 case OCPP16RequestCommand
.HEARTBEAT
:
99 return {} as HeartbeatRequest
;
100 case OCPP16RequestCommand
.METER_VALUES
:
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
:
110 connectorId
: commandParams
?.connectorId
,
111 status: commandParams
?.status,
112 errorCode
: commandParams
?.errorCode
,
113 } as StatusNotificationRequest
;
114 case OCPP16RequestCommand
.START_TRANSACTION
:
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
123 timestamp
: new Date().toISOString(),
124 } as StartTransactionRequest
;
125 case OCPP16RequestCommand
.STOP_TRANSACTION
:
126 for (const id
of this.chargingStation
.connectors
.keys()) {
129 this.chargingStation
.getConnectorStatus(id
)?.transactionId
===
130 commandParams
?.transactionId
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(
144 this.chargingStation
.getConnectorStatus(connectorId
).transactionBeginMeterValue
,
145 OCPP16ServiceUtils
.buildTransactionEndMeterValue(
146 this.chargingStation
,
148 commandParams
?.meterStop
as number
152 } as StopTransactionRequest
;
155 ErrorType
.NOT_SUPPORTED
,
156 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
157 `${moduleName}.buildCommandPayload: Unsupported OCPP command: ${commandName}`,