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 OCPPRequestService from
'../OCPPRequestService';
12 import type OCPPResponseService from
'../OCPPResponseService';
13 import { OCPP16ServiceUtils
} from
'./OCPP16ServiceUtils';
15 const moduleName
= 'OCPP16RequestService';
17 export default class OCPP16RequestService
extends OCPPRequestService
{
18 public constructor(ocppResponseService
: OCPPResponseService
) {
19 if (new.target
?.name
=== moduleName
) {
20 throw new TypeError(`Cannot construct ${new.target?.name} instances directly`);
22 super(ocppResponseService
);
25 public async requestHandler
<Request
extends JsonType
, Response
extends JsonType
>(
26 chargingStation
: ChargingStation
,
27 commandName
: OCPP16RequestCommand
,
28 commandParams
?: JsonType
,
29 params
?: RequestParams
30 ): Promise
<Response
> {
31 if (Object.values(OCPP16RequestCommand
).includes(commandName
)) {
32 return (await this.sendMessage(
35 this.buildRequestPayload
<Request
>(chargingStation
, commandName
, commandParams
),
38 )) as unknown
as Response
;
41 ErrorType
.NOT_SUPPORTED
,
42 `${moduleName}.requestHandler: Unsupported OCPP command ${commandName}`,
48 private buildRequestPayload
<Request
extends JsonType
>(
49 chargingStation
: ChargingStation
,
50 commandName
: OCPP16RequestCommand
,
51 commandParams
?: JsonType
53 let connectorId
: number;
54 commandParams
= commandParams
as JsonObject
;
55 switch (commandName
) {
56 case OCPP16RequestCommand
.AUTHORIZE
:
58 ...(!Utils
.isUndefined(commandParams
?.idTag
)
59 ? { idTag
: commandParams
.idTag
}
60 : { idTag
: Constants
.DEFAULT_IDTAG
}),
61 } as unknown
as Request
;
62 case OCPP16RequestCommand
.BOOT_NOTIFICATION
:
64 chargePointModel
: commandParams
?.chargePointModel
,
65 chargePointVendor
: commandParams
?.chargePointVendor
,
66 ...(!Utils
.isUndefined(commandParams
?.chargeBoxSerialNumber
) && {
67 chargeBoxSerialNumber
: commandParams
.chargeBoxSerialNumber
,
69 ...(!Utils
.isUndefined(commandParams
?.chargePointSerialNumber
) && {
70 chargePointSerialNumber
: commandParams
.chargePointSerialNumber
,
72 ...(!Utils
.isUndefined(commandParams
?.firmwareVersion
) && {
73 firmwareVersion
: commandParams
.firmwareVersion
,
75 ...(!Utils
.isUndefined(commandParams
?.iccid
) && { iccid
: commandParams
.iccid
}),
76 ...(!Utils
.isUndefined(commandParams
?.imsi
) && { imsi
: commandParams
.imsi
}),
77 ...(!Utils
.isUndefined(commandParams
?.meterSerialNumber
) && {
78 meterSerialNumber
: commandParams
.meterSerialNumber
,
80 ...(!Utils
.isUndefined(commandParams
?.meterType
) && {
81 meterType
: commandParams
.meterType
,
83 } as unknown
as Request
;
84 case OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
:
86 status: commandParams
?.diagnosticsStatus
,
87 } as unknown
as Request
;
88 case OCPP16RequestCommand
.HEARTBEAT
:
89 return {} as unknown
as Request
;
90 case OCPP16RequestCommand
.METER_VALUES
:
92 connectorId
: commandParams
?.connectorId
,
93 transactionId
: commandParams
?.transactionId
,
94 meterValue
: Array.isArray(commandParams
?.meterValue
)
95 ? commandParams
?.meterValue
96 : [commandParams
?.meterValue
],
97 } as unknown
as Request
;
98 case OCPP16RequestCommand
.STATUS_NOTIFICATION
:
100 connectorId
: commandParams
?.connectorId
,
101 status: commandParams
?.status,
102 errorCode
: commandParams
?.errorCode
,
103 } as unknown
as Request
;
104 case OCPP16RequestCommand
.START_TRANSACTION
:
106 connectorId
: commandParams
?.connectorId
,
107 ...(!Utils
.isUndefined(commandParams
?.idTag
)
108 ? { idTag
: commandParams
?.idTag
}
109 : { idTag
: Constants
.DEFAULT_IDTAG
}),
110 meterStart
: chargingStation
.getEnergyActiveImportRegisterByConnectorId(
111 commandParams
?.connectorId
as number
113 timestamp
: new Date().toISOString(),
114 } as unknown
as Request
;
115 case OCPP16RequestCommand
.STOP_TRANSACTION
:
116 connectorId
= chargingStation
.getConnectorIdByTransactionId(
117 commandParams
?.transactionId
as number
120 transactionId
: commandParams
?.transactionId
,
121 ...(!Utils
.isUndefined(commandParams
?.idTag
) && { idTag
: commandParams
.idTag
}),
122 meterStop
: commandParams
?.meterStop
,
123 timestamp
: new Date().toISOString(),
124 ...(commandParams
?.reason
&& { reason
: commandParams
.reason
}),
125 ...(chargingStation
.getTransactionDataMeterValues() && {
126 transactionData
: OCPP16ServiceUtils
.buildTransactionDataMeterValues(
127 chargingStation
.getConnectorStatus(connectorId
).transactionBeginMeterValue
,
128 OCPP16ServiceUtils
.buildTransactionEndMeterValue(
131 commandParams
?.meterStop
as number
135 } as unknown
as Request
;
138 ErrorType
.NOT_SUPPORTED
,
139 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
140 `${moduleName}.buildRequestPayload: Unsupported OCPP command: ${commandName}`,