Add template section to list OCPP commands supported (#129)
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / 1.6 / OCPP16RequestService.ts
CommitLineData
c8eeb62b
JB
1// Partial Copyright Jerome Benoit. 2021. All Rights Reserved.
2
8114d10e 3import OCPPError from '../../../exception/OCPPError';
5cc4b63b 4import { JsonObject, JsonType } from '../../../types/JsonType';
f22266fd 5import { OCPP16RequestCommand } from '../../../types/ocpp/1.6/Requests';
8114d10e 6import { ErrorType } from '../../../types/ocpp/ErrorType';
be9b0d50 7import { RequestParams } from '../../../types/ocpp/Requests';
8114d10e 8import Constants from '../../../utils/Constants';
c0560973 9import Utils from '../../../utils/Utils';
8114d10e 10import type ChargingStation from '../../ChargingStation';
65554cc3 11import { ChargingStationUtils } from '../../ChargingStationUtils';
8114d10e
JB
12import OCPPRequestService from '../OCPPRequestService';
13import type OCPPResponseService from '../OCPPResponseService';
14import { OCPP16ServiceUtils } from './OCPP16ServiceUtils';
c0560973 15
909dcf2d
JB
16const moduleName = 'OCPP16RequestService';
17
c0560973 18export default class OCPP16RequestService extends OCPPRequestService {
08f130a0 19 public constructor(ocppResponseService: OCPPResponseService) {
909dcf2d 20 if (new.target?.name === moduleName) {
06127450 21 throw new TypeError(`Cannot construct ${new.target?.name} instances directly`);
9f2e3130 22 }
08f130a0 23 super(ocppResponseService);
9f2e3130
JB
24 }
25
5cc4b63b 26 public async requestHandler<Request extends JsonType, Response extends JsonType>(
08f130a0 27 chargingStation: ChargingStation,
94a464f9 28 commandName: OCPP16RequestCommand,
5cc4b63b 29 commandParams?: JsonType,
be9b0d50 30 params?: RequestParams
f22266fd 31 ): Promise<Response> {
65554cc3
JB
32 if (
33 Object.values(OCPP16RequestCommand).includes(commandName) &&
34 ChargingStationUtils.isCommandSupported(commandName, chargingStation.stationInfo)
35 ) {
f22266fd 36 return (await this.sendMessage(
08f130a0 37 chargingStation,
94a464f9 38 Utils.generateUUID(),
08f130a0 39 this.buildRequestPayload<Request>(chargingStation, commandName, commandParams),
94a464f9
JB
40 commandName,
41 params
f22266fd 42 )) as unknown as Response;
94a464f9
JB
43 }
44 throw new OCPPError(
45 ErrorType.NOT_SUPPORTED,
1c959f1f 46 `${moduleName}.requestHandler: Unsupported OCPP command ${commandName}`,
94a464f9 47 commandName,
7369e417 48 commandParams
94a464f9 49 );
c0560973
JB
50 }
51
5cc4b63b 52 private buildRequestPayload<Request extends JsonType>(
08f130a0 53 chargingStation: ChargingStation,
78085c42 54 commandName: OCPP16RequestCommand,
5cc4b63b 55 commandParams?: JsonType
f22266fd 56 ): Request {
68c993d5 57 let connectorId: number;
5cc4b63b 58 commandParams = commandParams as JsonObject;
78085c42
JB
59 switch (commandName) {
60 case OCPP16RequestCommand.AUTHORIZE:
61 return {
62 ...(!Utils.isUndefined(commandParams?.idTag)
63 ? { idTag: commandParams.idTag }
64 : { idTag: Constants.DEFAULT_IDTAG }),
f22266fd 65 } as unknown as Request;
78085c42
JB
66 case OCPP16RequestCommand.BOOT_NOTIFICATION:
67 return {
68 chargePointModel: commandParams?.chargePointModel,
69 chargePointVendor: commandParams?.chargePointVendor,
70 ...(!Utils.isUndefined(commandParams?.chargeBoxSerialNumber) && {
71 chargeBoxSerialNumber: commandParams.chargeBoxSerialNumber,
72 }),
73 ...(!Utils.isUndefined(commandParams?.chargePointSerialNumber) && {
74 chargePointSerialNumber: commandParams.chargePointSerialNumber,
75 }),
76 ...(!Utils.isUndefined(commandParams?.firmwareVersion) && {
77 firmwareVersion: commandParams.firmwareVersion,
78 }),
79 ...(!Utils.isUndefined(commandParams?.iccid) && { iccid: commandParams.iccid }),
80 ...(!Utils.isUndefined(commandParams?.imsi) && { imsi: commandParams.imsi }),
81 ...(!Utils.isUndefined(commandParams?.meterSerialNumber) && {
82 meterSerialNumber: commandParams.meterSerialNumber,
83 }),
84 ...(!Utils.isUndefined(commandParams?.meterType) && {
85 meterType: commandParams.meterType,
86 }),
f22266fd 87 } as unknown as Request;
78085c42
JB
88 case OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION:
89 return {
90 status: commandParams?.diagnosticsStatus,
f22266fd 91 } as unknown as Request;
78085c42 92 case OCPP16RequestCommand.HEARTBEAT:
f22266fd 93 return {} as unknown as Request;
78085c42 94 case OCPP16RequestCommand.METER_VALUES:
7369e417
JB
95 // Sanity check
96 if (!Array.isArray(commandParams?.meterValue)) {
97 throw new OCPPError(
98 ErrorType.TYPERAINT_VIOLATION,
99 `${moduleName}.buildRequestPayload ${commandName}: Invalid array type for meterValue payload field`,
100 commandName,
101 commandParams
102 );
103 }
78085c42
JB
104 return {
105 connectorId: commandParams?.connectorId,
106 transactionId: commandParams?.transactionId,
7369e417 107 meterValue: commandParams?.meterValue,
f22266fd 108 } as unknown as Request;
78085c42
JB
109 case OCPP16RequestCommand.STATUS_NOTIFICATION:
110 return {
111 connectorId: commandParams?.connectorId,
78085c42 112 status: commandParams?.status,
93b4a429 113 errorCode: commandParams?.errorCode,
f22266fd 114 } as unknown as Request;
78085c42
JB
115 case OCPP16RequestCommand.START_TRANSACTION:
116 return {
117 connectorId: commandParams?.connectorId,
118 ...(!Utils.isUndefined(commandParams?.idTag)
119 ? { idTag: commandParams?.idTag }
120 : { idTag: Constants.DEFAULT_IDTAG }),
08f130a0 121 meterStart: chargingStation.getEnergyActiveImportRegisterByConnectorId(
78085c42
JB
122 commandParams?.connectorId as number
123 ),
124 timestamp: new Date().toISOString(),
f22266fd 125 } as unknown as Request;
78085c42 126 case OCPP16RequestCommand.STOP_TRANSACTION:
08f130a0 127 connectorId = chargingStation.getConnectorIdByTransactionId(
f479a792
JB
128 commandParams?.transactionId as number
129 );
78085c42
JB
130 return {
131 transactionId: commandParams?.transactionId,
132 ...(!Utils.isUndefined(commandParams?.idTag) && { idTag: commandParams.idTag }),
133 meterStop: commandParams?.meterStop,
134 timestamp: new Date().toISOString(),
135 ...(commandParams?.reason && { reason: commandParams.reason }),
08f130a0 136 ...(chargingStation.getTransactionDataMeterValues() && {
78085c42 137 transactionData: OCPP16ServiceUtils.buildTransactionDataMeterValues(
08f130a0 138 chargingStation.getConnectorStatus(connectorId).transactionBeginMeterValue,
78085c42 139 OCPP16ServiceUtils.buildTransactionEndMeterValue(
08f130a0 140 chargingStation,
68c993d5 141 connectorId,
78085c42
JB
142 commandParams?.meterStop as number
143 )
144 ),
145 }),
f22266fd 146 } as unknown as Request;
78085c42
JB
147 default:
148 throw new OCPPError(
149 ErrorType.NOT_SUPPORTED,
150 // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
1c959f1f 151 `${moduleName}.buildRequestPayload: Unsupported OCPP command: ${commandName}`,
78085c42 152 commandName,
7369e417 153 commandParams
78085c42
JB
154 );
155 }
156 }
c0560973 157}