Release 1.0.14
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / 1.6 / OCPP16ResponseService.ts
1 import { AuthorizeRequest, OCPP16AuthorizationStatus, OCPP16AuthorizeResponse, OCPP16StartTransactionResponse, OCPP16StopTransactionResponse, StartTransactionRequest, StopTransactionRequest } from '../../../types/ocpp/1.6/Transaction';
2 import { HeartbeatRequest, OCPP16BootNotificationRequest, OCPP16RequestCommand, StatusNotificationRequest } from '../../../types/ocpp/1.6/Requests';
3 import { HeartbeatResponse, OCPP16BootNotificationResponse, OCPP16RegistrationStatus, StatusNotificationResponse } from '../../../types/ocpp/1.6/Responses';
4 import { MeterValuesRequest, MeterValuesResponse } from '../../../types/ocpp/1.6/MeterValues';
5
6 import { OCPP16ChargePointStatus } from '../../../types/ocpp/1.6/ChargePointStatus';
7 import { OCPP16StandardParametersKey } from '../../../types/ocpp/1.6/Configuration';
8 import OCPPResponseService from '../OCPPResponseService';
9 import Utils from '../../../utils/Utils';
10 import logger from '../../../utils/Logger';
11
12 export default class OCPP16ResponseService extends OCPPResponseService {
13 public async handleResponse(commandName: OCPP16RequestCommand, payload: Record<string, unknown> | string, requestPayload: Record<string, unknown>): Promise<void> {
14 const responseCallbackMethodName = `handleResponse${commandName}`;
15 if (typeof this[responseCallbackMethodName] === 'function') {
16 await this[responseCallbackMethodName](payload, requestPayload);
17 } else {
18 logger.error(this.chargingStation.logPrefix() + ' Trying to call an undefined response callback method: ' + responseCallbackMethodName);
19 }
20 }
21
22 private handleResponseBootNotification(payload: OCPP16BootNotificationResponse, requestPayload: OCPP16BootNotificationRequest): void {
23 if (payload.status === OCPP16RegistrationStatus.ACCEPTED) {
24 this.chargingStation.addConfigurationKey(OCPP16StandardParametersKey.HeartBeatInterval, payload.interval.toString());
25 this.chargingStation.addConfigurationKey(OCPP16StandardParametersKey.HeartbeatInterval, payload.interval.toString(), false, false);
26 this.chargingStation.heartbeatSetInterval ? this.chargingStation.restartHeartbeat() : this.chargingStation.startHeartbeat();
27 } else if (payload.status === OCPP16RegistrationStatus.PENDING) {
28 logger.info(this.chargingStation.logPrefix() + ' Charging station in pending state on the central server');
29 } else {
30 logger.warn(this.chargingStation.logPrefix() + ' Charging station rejected by the central server');
31 }
32 }
33
34 private async handleResponseStartTransaction(payload: OCPP16StartTransactionResponse, requestPayload: StartTransactionRequest): Promise<void> {
35 const connectorId = requestPayload.connectorId;
36
37 let transactionConnectorId: number;
38 for (const connector in this.chargingStation.connectors) {
39 if (Utils.convertToInt(connector) > 0 && Utils.convertToInt(connector) === connectorId) {
40 transactionConnectorId = Utils.convertToInt(connector);
41 break;
42 }
43 }
44 if (!transactionConnectorId) {
45 logger.error(this.chargingStation.logPrefix() + ' Trying to start a transaction on a non existing connector Id ' + connectorId.toString());
46 return;
47 }
48 if (this.chargingStation.getConnector(connectorId)?.transactionStarted) {
49 logger.debug(this.chargingStation.logPrefix() + ' Trying to start a transaction on an already used connector ' + connectorId.toString() + ': %j', this.chargingStation.getConnector(connectorId));
50 return;
51 }
52
53 if (payload.idTagInfo?.status === OCPP16AuthorizationStatus.ACCEPTED) {
54 this.chargingStation.getConnector(connectorId).transactionStarted = true;
55 this.chargingStation.getConnector(connectorId).transactionId = payload.transactionId;
56 this.chargingStation.getConnector(connectorId).idTag = requestPayload.idTag;
57 this.chargingStation.getConnector(connectorId).lastEnergyActiveImportRegisterValue = 0;
58 await this.chargingStation.ocppRequestService.sendStatusNotification(connectorId, OCPP16ChargePointStatus.CHARGING);
59 this.chargingStation.getConnector(connectorId).status = OCPP16ChargePointStatus.CHARGING;
60 logger.info(this.chargingStation.logPrefix() + ' Transaction ' + payload.transactionId.toString() + ' STARTED on ' + this.chargingStation.stationInfo.chargingStationId + '#' + connectorId.toString() + ' for idTag ' + requestPayload.idTag);
61 if (this.chargingStation.stationInfo.powerSharedByConnectors) {
62 this.chargingStation.stationInfo.powerDivider++;
63 }
64 const configuredMeterValueSampleInterval = this.chargingStation.getConfigurationKey(OCPP16StandardParametersKey.MeterValueSampleInterval);
65 this.chargingStation.startMeterValues(connectorId, configuredMeterValueSampleInterval ? Utils.convertToInt(configuredMeterValueSampleInterval.value) * 1000 : 60000);
66 } else {
67 logger.warn(this.chargingStation.logPrefix() + ' Starting transaction id ' + payload.transactionId.toString() + ' REJECTED with status ' + payload?.idTagInfo?.status + ', idTag ' + requestPayload.idTag);
68 this.chargingStation.resetTransactionOnConnector(connectorId);
69 await this.chargingStation.ocppRequestService.sendStatusNotification(connectorId, OCPP16ChargePointStatus.AVAILABLE);
70 this.chargingStation.getConnector(connectorId).status = OCPP16ChargePointStatus.AVAILABLE;
71 }
72 }
73
74 private async handleResponseStopTransaction(payload: OCPP16StopTransactionResponse, requestPayload: StopTransactionRequest): Promise<void> {
75 let transactionConnectorId: number;
76 for (const connector in this.chargingStation.connectors) {
77 if (Utils.convertToInt(connector) > 0 && this.chargingStation.getConnector(Utils.convertToInt(connector))?.transactionId === requestPayload.transactionId) {
78 transactionConnectorId = Utils.convertToInt(connector);
79 break;
80 }
81 }
82 if (!transactionConnectorId) {
83 logger.error(this.chargingStation.logPrefix() + ' Trying to stop a non existing transaction ' + requestPayload.transactionId.toString());
84 return;
85 }
86 if (payload.idTagInfo?.status === OCPP16AuthorizationStatus.ACCEPTED) {
87 if (!this.chargingStation.isChargingStationAvailable() || !this.chargingStation.isConnectorAvailable(transactionConnectorId)) {
88 await this.chargingStation.ocppRequestService.sendStatusNotification(transactionConnectorId, OCPP16ChargePointStatus.UNAVAILABLE);
89 this.chargingStation.getConnector(transactionConnectorId).status = OCPP16ChargePointStatus.UNAVAILABLE;
90 } else {
91 await this.chargingStation.ocppRequestService.sendStatusNotification(transactionConnectorId, OCPP16ChargePointStatus.AVAILABLE);
92 this.chargingStation.getConnector(transactionConnectorId).status = OCPP16ChargePointStatus.AVAILABLE;
93 }
94 if (this.chargingStation.stationInfo.powerSharedByConnectors) {
95 this.chargingStation.stationInfo.powerDivider--;
96 }
97 logger.info(this.chargingStation.logPrefix() + ' Transaction ' + requestPayload.transactionId.toString() + ' STOPPED on ' + this.chargingStation.stationInfo.chargingStationId + '#' + transactionConnectorId.toString());
98 this.chargingStation.resetTransactionOnConnector(transactionConnectorId);
99 } else {
100 logger.warn(this.chargingStation.logPrefix() + ' Stopping transaction id ' + requestPayload.transactionId.toString() + ' REJECTED with status ' + payload.idTagInfo?.status);
101 }
102 }
103
104 private handleResponseStatusNotification(payload: StatusNotificationRequest, requestPayload: StatusNotificationResponse): void {
105 logger.debug(this.chargingStation.logPrefix() + ' Status notification response received: %j to StatusNotification request: %j', payload, requestPayload);
106 }
107
108 private handleResponseMeterValues(payload: MeterValuesRequest, requestPayload: MeterValuesResponse): void {
109 logger.debug(this.chargingStation.logPrefix() + ' MeterValues response received: %j to MeterValues request: %j', payload, requestPayload);
110 }
111
112 private handleResponseHeartbeat(payload: HeartbeatResponse, requestPayload: HeartbeatRequest): void {
113 logger.debug(this.chargingStation.logPrefix() + ' Heartbeat response received: %j to Heartbeat request: %j', payload, requestPayload);
114 }
115
116 private handleResponseAuthorize(payload: OCPP16AuthorizeResponse, requestPayload: AuthorizeRequest): void {
117 logger.debug(this.chargingStation.logPrefix() + ' Authorize response received: %j to Authorize request: %j', payload, requestPayload);
118 }
119 }