refactor: factor out change availability helper
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / 1.6 / OCPP16ServiceUtils.ts
index 00594e1c7ed664809d195ff412f6eab9e48a851e..cf055beb949d05f5844c86a450fd6cfc27c7f8ee 100644 (file)
@@ -2,6 +2,7 @@
 
 import type { JSONSchemaType } from 'ajv';
 
+import { OCPP16Constants } from './OCPP16Constants';
 import { type ChargingStation, hasFeatureProfile } from '../../../charging-station';
 import { OCPPError } from '../../../exception';
 import {
@@ -14,6 +15,9 @@ import {
   MeterValueContext,
   MeterValueLocation,
   MeterValueUnit,
+  OCPP16AvailabilityType,
+  type OCPP16ChangeAvailabilityResponse,
+  OCPP16ChargePointStatus,
   type OCPP16ChargingProfile,
   type OCPP16IncomingRequestCommand,
   type OCPP16MeterValue,
@@ -788,6 +792,29 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils {
     return meterValues;
   }
 
+  public static changeAvailability = async (
+    chargingStation: ChargingStation,
+    connectorId: number,
+    chargePointStatus: OCPP16ChargePointStatus,
+    availabilityType: OCPP16AvailabilityType,
+  ): Promise<OCPP16ChangeAvailabilityResponse> => {
+    let response: OCPP16ChangeAvailabilityResponse =
+      OCPP16Constants.OCPP_AVAILABILITY_RESPONSE_ACCEPTED;
+    const connectorStatus = chargingStation.getConnectorStatus(connectorId)!;
+    if (connectorStatus?.transactionStarted === true) {
+      response = OCPP16Constants.OCPP_AVAILABILITY_RESPONSE_SCHEDULED;
+    }
+    connectorStatus.availability = availabilityType;
+    if (response === OCPP16Constants.OCPP_AVAILABILITY_RESPONSE_ACCEPTED) {
+      await OCPP16ServiceUtils.sendAndSetConnectorStatus(
+        chargingStation,
+        connectorId,
+        chargePointStatus,
+      );
+    }
+    return response;
+  };
+
   public static setChargingProfile(
     chargingStation: ChargingStation,
     connectorId: number,