<?php

class Ladestation_Universal extends IPSModule
{
    public function Create()
    {
        parent::Create();

        // Ladestationspezifische Properties
        $this->RegisterPropertyInteger("MinLeistung", 3600);
        $this->RegisterPropertyInteger("MaxLeistung", 11000);
        $this->RegisterPropertyInteger("MinLeistung_1ph", 1400);
        $this->RegisterPropertyInteger("MaxLeistung_1ph", 7800);
        $this->RegisterPropertyString("IP_Adresse", "0.0.0.0");
        $this->RegisterPropertyInteger("Ladestation", 2);
        $this->RegisterPropertyString("ID", "");
        $this->RegisterPropertyString("Seriennummer", "");
        $this->RegisterPropertyString("Username", "");
        $this->RegisterPropertyString("Password", "");
        $this->RegisterPropertyInteger("Interval", 5); // Recheninterval


        // Ladestationspezifische Variabeln
        $this->RegisterVariableBoolean("Ladebereit", "Ladebereit", "~Switch", 1);
        $this->RegisterVariableBoolean("Solarladen", "Solarladen", "~Switch", 0);
        $this->RegisterVariableInteger("Fahrzeugstatus", "Fahrzeugstatus", "", 0);
        $this->RegisterVariableInteger("Lademodus", "Lademodus", "", 0);
        $this->RegisterVariableFloat("Ladeleistung_Effektiv", "Ladeleistung_Effektiv", "", 0);
        $this->RegisterVariableBoolean("Peak", "Peak", "", 0);
        $this->RegisterVariableInteger("Ladestrom", "Ladestrom");
        $this->RegisterVariableInteger("Ladeleistung", "Ladeleistung");
        
        // Variabeln für Kommunkation mit Manager
        $this->RegisterVariableInteger("Sperre_Prio", "Sperre_Prio");
        $this->RegisterVariableInteger("PV_Prio", "PV_Prio");
        $this->RegisterVariableBoolean("Idle", "Idle", "", 0);
        $this->RegisterVariableInteger("Aktuelle_Leistung", "Aktuelle_Leistung", "", 0);
        $this->RegisterVariableFloat("Bezogene_Energie", "Bezogene_Energie", "", 0);
        $this->RegisterVariableString("PowerSteps", "PowerSteps");
        $this->RegisterVariableInteger("Power", "Power");
        $this->RegisterVariableBoolean("Is_Peak_Shaving", "Is_Peak_Shaving");
        $this->RegisterVariableInteger("Leistung_Delta", "Leistung_Delta", "", 0);
        $this->RegisterVariableInteger("Power_Count", "Power_Count", "", 0);

        // Hilfsvariabeln für Idle zustand
        $this->RegisterPropertyInteger("IdleCounterMax", 2);
        $this->RegisterVariableInteger("IdleCounter", "IdleCounter", "", 0);
        $this->SetValue("IdleCounter", 0);

        // Initialisiere Idle
        $this->SetValue("Idle", true);

        $this->RegisterTimer("Timer_Do_UserCalc_EVC",$this->ReadPropertyInteger("Interval")*1000,"IPS_RequestAction(" .$this->InstanceID .', "Do_UserCalc", "");');

    }

    public function ApplyChanges()
    {
        parent::ApplyChanges();
        $this->SetTimerInterval("Timer_Do_UserCalc_EVC",$this->ReadPropertyInteger("Interval")*1000); 

        // Zusätzliche Anpassungen nach Bedarf
    }

    public function RequestAction($Ident, $Value)
    {
        switch ($Ident) {

            case "SetAktuelle_Leistung":
                $this->SetValue("Power", (int)$Value);
                break;
    
            case "GetCurrentData":
                $this->SetValue("Is_Peak_Shaving", (bool)$Value);
                break;
                
            case "Do_UserCalc":

                $this->SetAktuelle_Leistung($this->GetValue("Power"));
                $this->GetCurrentData($this->GetValue("Is_Peak_Shaving"));
                break; 

            default:
                throw new Exception("Invalid Ident");
        }
    }




    public function SetAktuelle_Leistung(int $power)
    {

        $internalPower = GetValue($this->GetIDForIdent("Aktuelle_Leistung"));
        // Aktuelle Leistungsvorgabe setzen
        SetValue($this->GetIDForIdent("Aktuelle_Leistung"), $power);
        $this->SetValue("Bezogene_Energie", ($this->GetValue("Bezogene_Energie") + ($this->GetValue("Aktuelle_Leistung")*($this->ReadPropertyInteger("Interval")/3600))));

        if ($power != $internalPower) {
            // Setze die interne Leistungsvorgabe

            // Idle für 4 Zyklen auf false setzen
            SetValue($this->GetIDForIdent("Idle"), false);
            SetValue(
                $this->GetIDForIdent("IdleCounter"),
                $this->ReadPropertyInteger("IdleCounterMax")
            );
        } else {
            // IdleCallCounter herunterzählen, wenn power == interne Leistungsvorgabe
            $idleCounter = GetValue($this->GetIDForIdent("IdleCounter"));
            if ($idleCounter > 0) {
                $idleCounter--;
                SetValue($this->GetIDForIdent("IdleCounter"), $idleCounter);
                if ($idleCounter == 0) {
                    SetValue($this->GetIDForIdent("Idle"), true);
                }
            }
        }

        // Ladeleistung setzen
        $peak = GetValue($this->GetIDForIdent("Peak"));
        $solarladen = GetValue($this->GetIDForIdent("Solarladen"));
        $Ladebereit = GetValue($this->GetIDForIdent("Ladebereit"));
        if (!$Ladebereit) {
            $this->sendPowerToStation(0);
            SetValue($this->GetIDForIdent("Ladeleistung"), 0);
            SetValue($this->GetIDForIdent("Ladestrom"), 0);
        } elseif (!$peak && !$solarladen) {
            // Wenn weder Peak noch Solarladen aktiv sind, setze Ladeleistung auf MaxLeistung
            SetValue(
                $this->GetIDForIdent("Ladeleistung"),
                $this->ReadPropertyInteger("MaxLeistung")
            );

            if (GetValue($this->GetIDForIdent("Lademodus")) == 0) {
                SetValue(
                    $this->GetIDForIdent("Ladestrom"),
                    $this->ReadPropertyInteger("MaxLeistung_1ph") / 240
                );
                $this->sendPowerToStation(
                    $this->ReadPropertyInteger("MaxLeistung_1ph")
                );
            } elseif (GetValue($this->GetIDForIdent("Lademodus")) == 1) {
                SetValue(
                    $this->GetIDForIdent("Ladestrom"),
                    $this->ReadPropertyInteger("MaxLeistung") / 400 / sqrt(3)
                );
                $this->sendPowerToStation(
                    $this->ReadPropertyInteger("MaxLeistung")
                );
            }
        } else {
            // Ansonsten setze Ladeleistung auf die aktuelle Leistungsvorgabe (Aktuelle_Leistung)
            SetValue($this->GetIDForIdent("Ladeleistung"), $power);

            if (GetValue($this->GetIDForIdent("Lademodus")) == 0) {
                SetValue($this->GetIDForIdent("Ladestrom"), $power / 240);
            } elseif (GetValue($this->GetIDForIdent("Lademodus")) == 1) {
                SetValue(
                    $this->GetIDForIdent("Ladestrom"),
                    $power / 400 / sqrt(3)
                );
            }

            $this->sendPowerToStation($power);
        }
    }

    // Methode zum Abrufen der aktuellen Daten
    public function GetCurrentData(bool $Peak)
    {
        // Aktuelle Properties abrufen
        $ladebereit = GetValue($this->GetIDForIdent("Ladebereit"));
        $solarladen = GetValue($this->GetIDForIdent("Solarladen"));

        if (GetValue($this->GetIDForIdent("Lademodus")) == 0) {
            $minLeistung = $this->ReadPropertyInteger("MinLeistung_1ph");
            $maxLeistung = $this->ReadPropertyInteger("MaxLeistung_1ph");
        } elseif (GetValue($this->GetIDForIdent("Lademodus")) == 1) {
            $minLeistung = $this->ReadPropertyInteger("MinLeistung");
            $maxLeistung = $this->ReadPropertyInteger("MaxLeistung");
        }



        $ch = curl_init();

        // Setze die URL
        if($this->ReadPropertyInteger("Ladestation")==2){
                   curl_setopt(
            $ch,
            CURLOPT_URL,
            "http://" . $this->ReadPropertyString("IP_Adresse") . "/api/status"
        ); 
                // Setze die Option, die Antwort als String zurückzugeben
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

                // Führe die Anfrage aus und speichere die Antwort
                $response = curl_exec($ch);
        
                // Schließe cURL
                curl_close($ch);
        }elseif($this->ReadPropertyInteger("Ladestation")==1){
            curl_setopt(
                $ch,
                CURLOPT_URL,
                "http://" . $this->ReadPropertyString("IP_Adresse") . "/mqtt?payload="
            ); 
                    // Setze die Option, die Antwort als String zurückzugeben
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        // Führe die Anfrage aus und speichere die Antwort
        $response = curl_exec($ch);

        // Schließe cURL
        curl_close($ch);
        }
        elseif($this->ReadPropertyInteger("Ladestation")==3){
            curl_setopt(
                $ch,
                CURLOPT_URL,
                "https://api.smart-me.com/pico/charging/". $this->ReadPropertyString("ID")
            ); 
            curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
            curl_setopt($ch, CURLOPT_USERPWD, $this->ReadPropertyString("Username") . ":" . $this->ReadPropertyString("Password"));
                    // Setze die Option, die Antwort als String zurückzugeben
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        // Führe die Anfrage aus und speichere die Antwort
        $response = curl_exec($ch);

        // Schließe cURL
        curl_close($ch);
        }
        elseif($this->ReadPropertyInteger("Ladestation")==4){

            $response = true; 

        }


        // Überprüfe, ob die Antwort nicht leer ist
        if ($response) {
            // Dekodiere die JSON-Antwort
            $data = json_decode($response, true);

            if($this->ReadPropertyInteger("Ladestation")==2){
            // Überprüfe, ob das JSON-Dekodieren erfolgreich war und der Schlüssel "car" existiert
            if (json_last_error() === JSON_ERROR_NONE && isset($data["car"])) {
                // Speichere den Wert von "car" in der Variable $status

                SetValue(
                    $this->GetIDForIdent("Ladeleistung_Effektiv"),
                    $data["nrg"][11]
                );

                SetValue($this->GetIDForIdent("Fahrzeugstatus"), $data["car"]);

                if ($data["nrg"][6] > 1 && $data["car"] == 2) {
                    SetValue($this->GetIDForIdent("Lademodus"), 1);
                } elseif ($data["nrg"][6] <= 1 && $data["car"] == 2) {
                    SetValue($this->GetIDForIdent("Lademodus"), 0);
                }
            }
        }
        if($this->ReadPropertyInteger("Ladestation")==1){
            // Überprüfe, ob das JSON-Dekodieren erfolgreich war und der Schlüssel "car" existiert
            if (json_last_error() === JSON_ERROR_NONE && isset($data["car"])) {
                // Speichere den Wert von "car" in der Variable $status

                SetValue(
                    $this->GetIDForIdent("Ladeleistung_Effektiv"),
                    $data["nrg"][11]*10
                );

                SetValue($this->GetIDForIdent("Fahrzeugstatus"), $data["car"]);

                if ($data["nrg"][6] > 1 && $data["car"] == 2) {
                    SetValue($this->GetIDForIdent("Lademodus"), 1);
                } elseif ($data["nrg"][6] <= 1 && $data["car"] == 2) {
                    SetValue($this->GetIDForIdent("Lademodus"), 0);
                }
            }
        }

        if($this->ReadPropertyInteger("Ladestation")==3){
            // Überprüfe, ob das JSON-Dekodieren erfolgreich war und der Schlüssel "car" existiert
            if (json_last_error() === JSON_ERROR_NONE && isset($data["State"])) {
                // Speichere den Wert von "car" in der Variable $status

                SetValue(
                    $this->GetIDForIdent("Ladeleistung_Effektiv"),
                    round($data["ActiveChargingPower"]*1000)
                );

                SetValue($this->GetIDForIdent("Fahrzeugstatus"), $data["State"]);
                
                if(($this->GetValue("Ladeleistung_Effektiv")>0)&& ($data["MaxAllowedChargingCurrent"]>0)){
                if ($data["ActiveChargingPower"]/$data["MaxAllowedChargingCurrent"] > 0.4/* && $data["State"] != 1*/) {
                    SetValue($this->GetIDForIdent("Lademodus"), 1);
                } elseif ($data["State"] != 1) {
                    SetValue($this->GetIDForIdent("Lademodus"), 0);
                }
            }else{
                //SetValue($this->GetIDForIdent("Lademodus"), 1);

            }
        }
        }
        if($this->ReadPropertyInteger("Ladestation")==4){

            // Nichts zu tun hier...
        
            
        }
        

        }
        
        // Peak-Wert speichern
        $this->SetValue("Peak", $Peak);

        
        IPS_LogMessage("Lades", "Hier");

        $akt = $this->GetValue("Ladeleistung_Effektiv");
        $pow = $this->GetValue("Aktuelle_Leistung");
        IPS_LogMessage("akt", $akt);
        IPS_LogMessage("pow", $pow);

        $timeout = $this->ReadPropertyInteger("Interval")*$this->ReadPropertyInteger("IdleCounterMax")*2;
        if(($akt<(0.8*$pow)) && ($pow>0)){
            IPS_LogMessage("Lades", "Hier2");

            $dummy = $this->GetValue("Power_Count")+1;
            $this->SetValue("Power_Count", $dummy);
        
        }elseif($this->GetValue("Power_Count")<=$timeout) {
            IPS_LogMessage("Lades", "Hier3");

            $this->SetValue("Power_Count", 0);
        
                }
                IPS_LogMessage("Lades", "Hier4");





        // Array für die Powersteps initialisieren
        $powerSteps = [0];

        if (GetValue($this->GetIDForIdent("Fahrzeugstatus")) != 1) {
            // Konfiguration des powerSteps-Arrays basierend auf den Properties
            if (!$ladebereit) {
                $powerSteps = [0];
            } elseif (!$Peak && !$solarladen) {
                $powerSteps = [$maxLeistung];
            } elseif (!$Peak && $solarladen) {
                $powerSteps = array_merge(
                    $powerSteps,
                    $this->getRangeLimits(
                        $minLeistung,
                        $maxLeistung,
                        GetValue($this->GetIDForIdent("Lademodus"))
                    )
                );
            } elseif ($solarladen && $Peak) {
                $powerSteps = [0];
            } else {
                if($this->GetValue("Power_Count")>$timeout){

                    $maxLeistung = 1.05 * $this->GetValue("Ladeleistung_Effektiv"); 
                    IPS_LogMessage("Lades", "Hier5");
        
                }

                $powerSteps += $this->getRangeLimits(
                    $minLeistung,
                    $maxLeistung,
                    GetValue($this->GetIDForIdent("Lademodus"))
                );
            }
        }else{

            $this->SetValue("Power_Count", 0); 
        }
        // PowerSteps in der RegisterVariable speichern
        SetValue($this->GetIDForIdent("PowerSteps"), json_encode($powerSteps));
        $this->SetValue("Leistung_Delta", $this->GetValue("Ladeleistung_Effektiv")-$this->GetValue("Ladeleistung")); 
        // Rückgabe der Powersteps
        return $powerSteps;
    }

    public function sendPowerToStation($value)
    {
        IPS_LogMessage("value", "bei sendpower" . $value);

        if($this->ReadPropertyInteger("Ladestation")==2){
            $baseUrl ="http://" . $this->ReadPropertyString("IP_Adresse") . "/api/set?";

         }elseif($this->ReadPropertyInteger("Ladestation")==1){
         $baseUrl ="http://" . $this->ReadPropertyString("IP_Adresse") . "/mqtt?payload=";

        }elseif($this->ReadPropertyInteger("Ladestation")==3){
            $baseUrl ="https://api.smart-me.com/pico/loadmanagementgroup/current/" . $this->ReadPropertyString("Seriennummer") . "?current=";

           }
        // Base URL

        $value = $this->convertPowerToCurrent(
            $value,
            GetValue($this->GetIDForIdent("Lademodus"))
        );
        // Initialize a cURL session
        $ch = curl_init();
        IPS_LogMessage("value", "nach konvertierung" . $value);

        // If value is 0, make a single request
        if ($value == 0) {
            if($this->ReadPropertyInteger("Ladestation")==2){            
                $url = $baseUrl . "frc=1";
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                $response = curl_exec($ch);
                curl_close($ch);
                return $response;

             }elseif($this->ReadPropertyInteger("Ladestation")==1){           
                 $url = $baseUrl . "alw=0";
                 curl_setopt($ch, CURLOPT_URL, $url);
                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                 $response = curl_exec($ch);
                 curl_close($ch);
                 return $response;

        }elseif($this->ReadPropertyInteger("Ladestation")==3){           
            $url = $baseUrl . "0";
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
            curl_setopt($ch, CURLOPT_USERPWD, $this->ReadPropertyString("Username") . ":" . $this->ReadPropertyString("Password"));
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            $response = curl_exec($ch);
            curl_close($ch);
            return $response;

   }
          
       
        }
        // For value between 1 and 32, make two requests
        elseif ($value >= 1 && $value <= 32) {
            // First request
                if($this->ReadPropertyInteger("Ladestation")==2){            
                    $url = $baseUrl . "frc=2";
                    curl_setopt($ch, CURLOPT_URL, $url);
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                    $response1 = curl_exec($ch);
        
                    // Check for errors
                    if (curl_errno($ch)) {
                        curl_close($ch);
                        return "Error:" . curl_error($ch);
                    }
                 }elseif($this->ReadPropertyInteger("Ladestation")==1){           
                     $url = $baseUrl . "alw=1";
                     curl_setopt($ch, CURLOPT_URL, $url);
                     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                     $response1 = curl_exec($ch);
         
                     // Check for errors
                     if (curl_errno($ch)) {
                         curl_close($ch);
                         return "Error:" . curl_error($ch);
                     }
    
         }elseif($this->ReadPropertyInteger("Ladestation")==3){           
            // Nichts zu tun für Smart-Me station
}         }elseif($this->ReadPropertyInteger("Ladestation")==4){          
       // Nichts zu tun für Dummy station
}


        

            // Second request


                if($this->ReadPropertyInteger("Ladestation")==2){            
                    $url2 = $baseUrl . "amp=$value";
                    curl_setopt($ch, CURLOPT_URL, $url2);
                    $response2 = curl_exec($ch);
        
                    // Check for errors
                    if (curl_errno($ch)) {
                        curl_close($ch);
                        return "Error:" . curl_error($ch);
                    }
        
                    // Close cURL session
                    curl_close($ch);
        
                    // Return responses
                    return ;
                }elseif($this->ReadPropertyInteger("Ladestation")==1){           
                    $url2 = $baseUrl . "amp=$value";
                    curl_setopt($ch, CURLOPT_URL, $url2);
                    $response2 = curl_exec($ch);
        
                    // Check for errors
                    if (curl_errno($ch)) {
                        curl_close($ch);
                        return "Error:" . curl_error($ch);
                    }
        
                    // Close cURL session
                    curl_close($ch);
        
                    // Return responses
                    return;
    
         }
         elseif($this->ReadPropertyInteger("Ladestation")==3){     
            IPS_LogMessage("value", "value ist " . $value);

            $value2 = $value * 1000; 
            IPS_LogMessage("value", "value2 ist " . $value2);

            $url2 = $baseUrl . $value2;
            IPS_LogMessage("Ladestation", "Aufgerufene ip" . $url2);

            curl_setopt($ch, CURLOPT_URL, $url2);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
            curl_setopt($ch, CURLOPT_USERPWD, $this->ReadPropertyString("Username") . ":" . $this->ReadPropertyString("Password"));
            $response2 = curl_exec($ch);
            IPS_LogMessage("Ladestation", "Antwort" . (curl_getinfo($ch, CURLINFO_HTTP_CODE)));

            // Check for errors
            if (curl_errno($ch)) {
                curl_close($ch);
                return "Error:" . curl_error($ch);
            }

            // Close cURL session
            curl_close($ch);

            // Return responses
            return;

 }elseif($this->ReadPropertyInteger("Ladestation")==4){          
    // Nichts zu tun für Dummy station
}

       

        // If value is out of range
        else {
            return "Invalid value. Must be between 0 and 32.";
        }
    
    }

    public function convertPowerToCurrent($value, $Lademodus)
    {
        if ($Lademodus == 1) {
            if ($value == 0) {
                return 0;
            } elseif ($value < 4150) {
                return 0;
            } elseif ($value < 4850) {
                return 6;
            } elseif ($value < 5550) {
                return 7;
            } elseif ($value < 6250) {
                return 8;
            } elseif ($value < 6950) {
                return 9;
            } elseif ($value < 7600) {
                return 10;
            } elseif ($value < 8300) {
                return 11;
            } elseif ($value < 9000) {
                return 12;
            } elseif ($value < 9700) {
                return 13;
            } elseif ($value < 10300) {
                return 14;
            } elseif ($value < 11000) {
                return 15;
            } elseif ($value < 11750) {
                return 16;
            } elseif ($value < 12450) {
                return 17;
            } elseif ($value < 13150) {
                return 18;
            } elseif ($value < 13850) {
                return 19;
            } elseif ($value < 14550) {
                return 20;
            } elseif ($value < 15250) {
                return 21;
            } elseif ($value < 15900) {
                return 22;
            } elseif ($value < 16600) {
                return 23;
            } elseif ($value < 17300) {
                return 24;
            } elseif ($value < 18000) {
                return 25;
            } elseif ($value < 18700) {
                return 26;
            } elseif ($value < 19400) {
                return 27;
            } elseif ($value < 20100) {
                return 28;
            } elseif ($value < 20800) {
                return 29;
            } elseif ($value < 21500) {
                return 30;
            } elseif ($value < 22000) {
                return 31;
            } elseif ($value >= 22000) {
                return 32;
            }
            return 0;
        } elseif ($Lademodus == 0) {
            if ($value == 0) {
                return 0;
            } elseif ($value < 1450) {
                return 0;
            } elseif ($value < 1690) {
                return 6;
            } elseif ($value < 1930) {
                return 7;
            } elseif ($value < 2170) {
                return 8;
            } elseif ($value < 2410) {
                return 9;
            } elseif ($value < 2650) {
                return 10;
            } elseif ($value < 2890) {
                return 11;
            } elseif ($value < 3130) {
                return 12;
            } elseif ($value < 3370) {
                return 13;
            } elseif ($value < 3610) {
                return 14;
            } elseif ($value < 3850) {
                return 15;
            } elseif ($value < 4090) {
                return 16;
            } elseif ($value < 4330) {
                return 17;
            } elseif ($value < 4570) {
                return 18;
            } elseif ($value < 4810) {
                return 19;
            } elseif ($value < 5050) {
                return 20;
            } elseif ($value < 5290) {
                return 21;
            } elseif ($value < 5530) {
                return 22;
            } elseif ($value < 5770) {
                return 23;
            } elseif ($value < 6010) {
                return 24;
            } elseif ($value < 6250) {
                return 25;
            } elseif ($value < 6490) {
                return 26;
            } elseif ($value < 6730) {
                return 27;
            } elseif ($value < 6970) {
                return 28;
            } elseif ($value < 7210) {
                return 29;
            } elseif ($value < 7450) {
                return 30;
            } elseif ($value < 7690) {
                return 31;
            } elseif ($value >= 7690) {
                return 32;
            }
            return 0;
        }
    }

    public function getRangeLimits($min, $max, $Lademodus)
    {
        if ($Lademodus == 1) {
            $limits = [
                4150,
                4850,
                5550,
                6250,
                6950,
                7600,
                8300,
                9000,
                9700,
                10300,
                11000,
                11750,
                12450,
                13150,
                13850,
                14550,
                15250,
                15900,
                16600,
                17300,
                18000,
                18700,
                19400,
                20100,
                20800,
                21500,
                22000,
            ];
        } elseif ($Lademodus == 0) {
            $limits = [
                1450,
                1690,
                1930,
                2170,
                2410,
                2650,
                2890,
                3130,
                3370,
                3610,
                3850,
                4090,
                4330,
                4570,
                4810,
                5050,
                5290,
                5530,
                5770,
                6010,
                6250,
                6490,
                6730,
                6970,
                7210,
                7450,
                7690,
            ];
        }

        $result = [];

        foreach ($limits as $limit) {
            if ($limit >= $min && $limit <= $max) {
                $result[] = $limit;
            }
        }

        return $result;
    }

}
?>
