1 // Partial Copyright Jerome Benoit. 2021. All Rights Reserved.
3 import { JsonObject
, JsonType
} from
'../../../types/JsonType';
5 import type ChargingStation from
'../../ChargingStation';
6 import Constants from
'../../../utils/Constants';
7 import { ErrorType
} from
'../../../types/ocpp/ErrorType';
8 import { OCPP16RequestCommand
} from
'../../../types/ocpp/1.6/Requests';
9 import { OCPP16ServiceUtils
} from
'./OCPP16ServiceUtils';
10 import OCPPError from
'../../../exception/OCPPError';
11 import OCPPRequestService from
'../OCPPRequestService';
12 import type OCPPResponseService from
'../OCPPResponseService';
13 import { RequestParams
} from
'../../../types/ocpp/Requests';
14 import Utils from
'../../../utils/Utils';
16 const moduleName
= 'OCPP16RequestService';
18 export default class OCPP16RequestService
extends OCPPRequestService
{
19 public constructor(chargingStation
: ChargingStation
, ocppResponseService
: OCPPResponseService
) {
20 if (new.target
?.name
=== moduleName
) {
21 throw new TypeError(`Cannot construct ${new.target?.name} instances directly`);
23 super(chargingStation
, ocppResponseService
);
26 public async requestHandler
<Request
extends JsonType
, Response
extends JsonType
>(
27 commandName
: OCPP16RequestCommand
,
28 commandParams
?: JsonType
,
29 params
?: RequestParams
30 ): Promise
<Response
> {
31 if (Object.values(OCPP16RequestCommand
).includes(commandName
)) {
32 return (await this.sendMessage(
34 this.buildRequestPayload
<Request
>(commandName
, commandParams
),
37 )) as unknown
as Response
;
40 ErrorType
.NOT_SUPPORTED
,
41 `${moduleName}.requestHandler: Unsupported OCPP command ${commandName}`,
47 private buildRequestPayload
<Request
extends JsonType
>(
48 commandName
: OCPP16RequestCommand
,
49 commandParams
?: JsonType
51 let connectorId
: number;
52 commandParams
= commandParams
as JsonObject
;
53 switch (commandName
) {
54 case OCPP16RequestCommand
.AUTHORIZE
:
56 ...(!Utils
.isUndefined(commandParams
?.idTag
)
57 ? { idTag
: commandParams
.idTag
}
58 : { idTag
: Constants
.DEFAULT_IDTAG
}),
59 } as unknown
as Request
;
60 case OCPP16RequestCommand
.BOOT_NOTIFICATION
:
62 chargePointModel
: commandParams
?.chargePointModel
,
63 chargePointVendor
: commandParams
?.chargePointVendor
,
64 ...(!Utils
.isUndefined(commandParams
?.chargeBoxSerialNumber
) && {
65 chargeBoxSerialNumber
: commandParams
.chargeBoxSerialNumber
,
67 ...(!Utils
.isUndefined(commandParams
?.chargePointSerialNumber
) && {
68 chargePointSerialNumber
: commandParams
.chargePointSerialNumber
,
70 ...(!Utils
.isUndefined(commandParams
?.firmwareVersion
) && {
71 firmwareVersion
: commandParams
.firmwareVersion
,
73 ...(!Utils
.isUndefined(commandParams
?.iccid
) && { iccid
: commandParams
.iccid
}),
74 ...(!Utils
.isUndefined(commandParams
?.imsi
) && { imsi
: commandParams
.imsi
}),
75 ...(!Utils
.isUndefined(commandParams
?.meterSerialNumber
) && {
76 meterSerialNumber
: commandParams
.meterSerialNumber
,
78 ...(!Utils
.isUndefined(commandParams
?.meterType
) && {
79 meterType
: commandParams
.meterType
,
81 } as unknown
as Request
;
82 case OCPP16RequestCommand
.DIAGNOSTICS_STATUS_NOTIFICATION
:
84 status: commandParams
?.diagnosticsStatus
,
85 } as unknown
as Request
;
86 case OCPP16RequestCommand
.HEARTBEAT
:
87 return {} as unknown
as Request
;
88 case OCPP16RequestCommand
.METER_VALUES
:
90 connectorId
: commandParams
?.connectorId
,
91 transactionId
: commandParams
?.transactionId
,
92 meterValue
: Array.isArray(commandParams
?.meterValue
)
93 ? commandParams
?.meterValue
94 : [commandParams
?.meterValue
],
95 } as unknown
as Request
;
96 case OCPP16RequestCommand
.STATUS_NOTIFICATION
:
98 connectorId
: commandParams
?.connectorId
,
99 status: commandParams
?.status,
100 errorCode
: commandParams
?.errorCode
,
101 } as unknown
as Request
;
102 case OCPP16RequestCommand
.START_TRANSACTION
:
104 connectorId
: commandParams
?.connectorId
,
105 ...(!Utils
.isUndefined(commandParams
?.idTag
)
106 ? { idTag
: commandParams
?.idTag
}
107 : { idTag
: Constants
.DEFAULT_IDTAG
}),
108 meterStart
: this.chargingStation
.getEnergyActiveImportRegisterByConnectorId(
109 commandParams
?.connectorId
as number
111 timestamp
: new Date().toISOString(),
112 } as unknown
as Request
;
113 case OCPP16RequestCommand
.STOP_TRANSACTION
:
114 connectorId
= this.chargingStation
.getConnectorIdByTransactionId(
115 commandParams
?.transactionId
as number
118 transactionId
: commandParams
?.transactionId
,
119 ...(!Utils
.isUndefined(commandParams
?.idTag
) && { idTag
: commandParams
.idTag
}),
120 meterStop
: commandParams
?.meterStop
,
121 timestamp
: new Date().toISOString(),
122 ...(commandParams
?.reason
&& { reason
: commandParams
.reason
}),
123 ...(this.chargingStation
.getTransactionDataMeterValues() && {
124 transactionData
: OCPP16ServiceUtils
.buildTransactionDataMeterValues(
125 this.chargingStation
.getConnectorStatus(connectorId
).transactionBeginMeterValue
,
126 OCPP16ServiceUtils
.buildTransactionEndMeterValue(
127 this.chargingStation
,
129 commandParams
?.meterStop
as number
133 } as unknown
as Request
;
136 ErrorType
.NOT_SUPPORTED
,
137 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
138 `${moduleName}.buildRequestPayload: Unsupported OCPP command: ${commandName}`,