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