1 // Partial Copyright Jerome Benoit. 2021. All Rights Reserved.
3 import type ChargingStation from
'../../ChargingStation';
4 import Constants from
'../../../utils/Constants';
5 import { ErrorType
} from
'../../../types/ocpp/ErrorType';
6 import { JsonType
} from
'../../../types/JsonType';
7 import { OCPP16RequestCommand
} from
'../../../types/ocpp/1.6/Requests';
8 import { OCPP16ServiceUtils
} from
'./OCPP16ServiceUtils';
9 import OCPPError from
'../../../exception/OCPPError';
10 import OCPPRequestService from
'../OCPPRequestService';
11 import type OCPPResponseService from
'../OCPPResponseService';
12 import { RequestParams
} from
'../../../types/ocpp/Requests';
13 import Utils from
'../../../utils/Utils';
15 const moduleName
= 'OCPP16RequestService';
17 export default class OCPP16RequestService
extends OCPPRequestService
{
18 public constructor(chargingStation
: ChargingStation
, ocppResponseService
: OCPPResponseService
) {
19 if (new.target
?.name
=== moduleName
) {
20 throw new TypeError(`Cannot construct ${new.target?.name} instances directly`);
22 super(chargingStation
, ocppResponseService
);
25 public async requestHandler
<Request
extends JsonType
, Response
extends JsonType
>(
26 commandName
: OCPP16RequestCommand
,
27 commandParams
?: JsonType
,
28 params
?: RequestParams
29 ): Promise
<Response
> {
30 if (Object.values(OCPP16RequestCommand
).includes(commandName
)) {
31 return (await this.sendMessage(
33 this.buildRequestPayload
<Request
>(commandName
, commandParams
),
36 )) as unknown
as Response
;
39 ErrorType
.NOT_SUPPORTED
,
40 `${moduleName}.requestHandler: Unsupported OCPP command ${commandName}`,
46 private buildRequestPayload
<Request
extends JsonType
>(
47 commandName
: OCPP16RequestCommand
,
48 commandParams
?: JsonType
50 let connectorId
: number;
51 switch (commandName
) {
52 case OCPP16RequestCommand
.AUTHORIZE
:
54 ...(!Utils
.isUndefined(commandParams
?.idTag
)
55 ? { idTag
: commandParams
.idTag
}
56 : { idTag
: Constants
.DEFAULT_IDTAG
}),
57 } as unknown
as Request
;
58 case OCPP16RequestCommand
.BOOT_NOTIFICATION
:
60 chargePointModel
: commandParams
?.chargePointModel
,
61 chargePointVendor
: commandParams
?.chargePointVendor
,
62 ...(!Utils
.isUndefined(commandParams
?.chargeBoxSerialNumber
) && {
63 chargeBoxSerialNumber
: commandParams
.chargeBoxSerialNumber
,
65 ...(!Utils
.isUndefined(commandParams
?.chargePointSerialNumber
) && {
66 chargePointSerialNumber
: commandParams
.chargePointSerialNumber
,
68 ...(!Utils
.isUndefined(commandParams
?.firmwareVersion
) && {
69 firmwareVersion
: commandParams
.firmwareVersion
,
71 ...(!Utils
.isUndefined(commandParams
?.iccid
) && { iccid
: commandParams
.iccid
}),
72 ...(!Utils
.isUndefined(commandParams
?.imsi
) && { imsi
: commandParams
.imsi
}),
73 ...(!Utils
.isUndefined(commandParams
?.meterSerialNumber
) && {
74 meterSerialNumber
: commandParams
.meterSerialNumber
,
76 ...(!Utils
.isUndefined(commandParams
?.meterType
) && {
77 meterType
: commandParams
.meterType
,
79 } as unknown
as Request
;
80 case OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
:
82 status: commandParams
?.diagnosticsStatus
,
83 } as unknown
as Request
;
84 case OCPP16RequestCommand
.HEARTBEAT
:
85 return {} as unknown
as Request
;
86 case OCPP16RequestCommand
.METER_VALUES
:
88 connectorId
: commandParams
?.connectorId
,
89 transactionId
: commandParams
?.transactionId
,
90 meterValue
: Array.isArray(commandParams
?.meterValue
)
91 ? commandParams
?.meterValue
92 : [commandParams
?.meterValue
],
93 } as unknown
as Request
;
94 case OCPP16RequestCommand
.STATUS_NOTIFICATION
:
96 connectorId
: commandParams
?.connectorId
,
97 status: commandParams
?.status,
98 errorCode
: commandParams
?.errorCode
,
99 } as unknown
as Request
;
100 case OCPP16RequestCommand
.START_TRANSACTION
:
102 connectorId
: commandParams
?.connectorId
,
103 ...(!Utils
.isUndefined(commandParams
?.idTag
)
104 ? { idTag
: commandParams
?.idTag
}
105 : { idTag
: Constants
.DEFAULT_IDTAG
}),
106 meterStart
: this.chargingStation
.getEnergyActiveImportRegisterByConnectorId(
107 commandParams
?.connectorId
as number
109 timestamp
: new Date().toISOString(),
110 } as unknown
as Request
;
111 case OCPP16RequestCommand
.STOP_TRANSACTION
:
112 connectorId
= this.chargingStation
.getConnectorIdByTransactionId(
113 commandParams
?.transactionId
as number
116 transactionId
: commandParams
?.transactionId
,
117 ...(!Utils
.isUndefined(commandParams
?.idTag
) && { idTag
: commandParams
.idTag
}),
118 meterStop
: commandParams
?.meterStop
,
119 timestamp
: new Date().toISOString(),
120 ...(commandParams
?.reason
&& { reason
: commandParams
.reason
}),
121 ...(this.chargingStation
.getTransactionDataMeterValues() && {
122 transactionData
: OCPP16ServiceUtils
.buildTransactionDataMeterValues(
123 this.chargingStation
.getConnectorStatus(connectorId
).transactionBeginMeterValue
,
124 OCPP16ServiceUtils
.buildTransactionEndMeterValue(
125 this.chargingStation
,
127 commandParams
?.meterStop
as number
131 } as unknown
as Request
;
134 ErrorType
.NOT_SUPPORTED
,
135 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
136 `${moduleName}.buildRequestPayload: Unsupported OCPP command: ${commandName}`,