1 // Partial Copyright Jerome Benoit. 2021. All Rights Reserved.
3 import OCPPError from
'../../../exception/OCPPError';
4 import { JsonObject
, JsonType
} from
'../../../types/JsonType';
5 import { OCPP16RequestCommand
} from
'../../../types/ocpp/1.6/Requests';
6 import { ErrorType
} from
'../../../types/ocpp/ErrorType';
7 import { RequestParams
} from
'../../../types/ocpp/Requests';
8 import Constants from
'../../../utils/Constants';
9 import Utils from
'../../../utils/Utils';
10 import type ChargingStation from
'../../ChargingStation';
11 import { ChargingStationUtils
} from
'../../ChargingStationUtils';
12 import OCPPRequestService from
'../OCPPRequestService';
13 import type OCPPResponseService from
'../OCPPResponseService';
14 import { OCPP16ServiceUtils
} from
'./OCPP16ServiceUtils';
16 const moduleName
= 'OCPP16RequestService';
18 export default class OCPP16RequestService
extends OCPPRequestService
{
19 public constructor(ocppResponseService
: OCPPResponseService
) {
20 if (new.target
?.name
=== moduleName
) {
21 throw new TypeError(`Cannot construct ${new.target?.name} instances directly`);
23 super(ocppResponseService
);
26 public async requestHandler
<Request
extends JsonType
, Response
extends JsonType
>(
27 chargingStation
: ChargingStation
,
28 commandName
: OCPP16RequestCommand
,
29 commandParams
?: JsonType
,
30 params
?: RequestParams
31 ): Promise
<Response
> {
32 if (ChargingStationUtils
.isRequestCommandSupported(commandName
, chargingStation
)) {
33 return (await this.sendMessage(
36 this.buildRequestPayload
<Request
>(chargingStation
, commandName
, commandParams
),
39 )) as unknown
as Response
;
42 ErrorType
.NOT_SUPPORTED
,
43 `${moduleName}.requestHandler: Unsupported OCPP command '${commandName}'`,
49 private buildRequestPayload
<Request
extends JsonType
>(
50 chargingStation
: ChargingStation
,
51 commandName
: OCPP16RequestCommand
,
52 commandParams
?: JsonType
54 let connectorId
: number;
55 commandParams
= commandParams
as JsonObject
;
56 switch (commandName
) {
57 case OCPP16RequestCommand
.AUTHORIZE
:
59 ...(!Utils
.isUndefined(commandParams
?.idTag
)
60 ? { idTag
: commandParams
.idTag
}
61 : { idTag
: Constants
.DEFAULT_IDTAG
}),
62 } as unknown
as Request
;
63 case OCPP16RequestCommand
.BOOT_NOTIFICATION
:
65 chargePointModel
: commandParams
?.chargePointModel
,
66 chargePointVendor
: commandParams
?.chargePointVendor
,
67 ...(!Utils
.isUndefined(commandParams
?.chargeBoxSerialNumber
) && {
68 chargeBoxSerialNumber
: commandParams
.chargeBoxSerialNumber
,
70 ...(!Utils
.isUndefined(commandParams
?.chargePointSerialNumber
) && {
71 chargePointSerialNumber
: commandParams
.chargePointSerialNumber
,
73 ...(!Utils
.isUndefined(commandParams
?.firmwareVersion
) && {
74 firmwareVersion
: commandParams
.firmwareVersion
,
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
,
81 ...(!Utils
.isUndefined(commandParams
?.meterType
) && {
82 meterType
: commandParams
.meterType
,
84 } as unknown
as Request
;
85 case OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
:
87 status: commandParams
?.diagnosticsStatus
,
88 } as unknown
as Request
;
89 case OCPP16RequestCommand
.HEARTBEAT
:
90 return {} as unknown
as Request
;
91 case OCPP16RequestCommand
.METER_VALUES
:
93 if (!Array.isArray(commandParams
?.meterValue
)) {
95 ErrorType
.TYPERAINT_VIOLATION
,
96 `${moduleName}.buildRequestPayload ${commandName}: Invalid array type for meterValue payload field`,
102 connectorId
: commandParams
?.connectorId
,
103 transactionId
: commandParams
?.transactionId
,
104 meterValue
: commandParams
?.meterValue
,
105 } as unknown
as Request
;
106 case OCPP16RequestCommand
.STATUS_NOTIFICATION
:
108 connectorId
: commandParams
?.connectorId
,
109 status: commandParams
?.status,
110 errorCode
: commandParams
?.errorCode
,
111 } as unknown
as Request
;
112 case OCPP16RequestCommand
.START_TRANSACTION
:
114 connectorId
: commandParams
?.connectorId
,
115 ...(!Utils
.isUndefined(commandParams
?.idTag
)
116 ? { idTag
: commandParams
?.idTag
}
117 : { idTag
: Constants
.DEFAULT_IDTAG
}),
118 meterStart
: chargingStation
.getEnergyActiveImportRegisterByConnectorId(
119 commandParams
?.connectorId
as number
121 timestamp
: new Date().toISOString(),
122 } as unknown
as Request
;
123 case OCPP16RequestCommand
.STOP_TRANSACTION
:
124 connectorId
= chargingStation
.getConnectorIdByTransactionId(
125 commandParams
?.transactionId
as number
128 transactionId
: commandParams
?.transactionId
,
129 ...(!Utils
.isUndefined(commandParams
?.idTag
) && { idTag
: commandParams
.idTag
}),
130 meterStop
: commandParams
?.meterStop
,
131 timestamp
: new Date().toISOString(),
132 ...(commandParams
?.reason
&& { reason
: commandParams
.reason
}),
133 ...(chargingStation
.getTransactionDataMeterValues() && {
134 transactionData
: OCPP16ServiceUtils
.buildTransactionDataMeterValues(
135 chargingStation
.getConnectorStatus(connectorId
).transactionBeginMeterValue
,
136 OCPP16ServiceUtils
.buildTransactionEndMeterValue(
139 commandParams
?.meterStop
as number
143 } as unknown
as Request
;
146 ErrorType
.NOT_SUPPORTED
,
147 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
148 `${moduleName}.buildRequestPayload: Unsupported OCPP command '${commandName}'`,