Ankündigung

Einklappen
Keine Ankündigung bisher.

Hilfe bei LBS-Debugging - LBS 19000935

Einklappen
X
 
  • Filter
  • Zeit
  • Anzeigen
Alles löschen
neue Beiträge

    Hilfe bei LBS-Debugging - LBS 19000935

    Hallo Zusammen,

    ich hätte mal eine Frage an die PHP-Experten hier im Forum. Ich habe einen LBS (um)geschrieben um die API von Husqvarna nutzen zu können.
    http://service.knx-user-forum.de/?co...ad&id=19000935

    Jetzt habe ich festgestellt, dass sich nach und nach der RAM füllt und der EXEC-Prozess des LBS dafür verantwortlich ist. Da ich das ganze nur auf Basis eines anderen LBS von adaptiert habe, weiß ich nicht so recht, an welcher stelle es hapert.

    Wer hat eine Idee wodurch der RAM vollläuft?

    Hier der EXEC-Teil:

    PHP-Code:
    <?php
    require (dirname(__FILE__) . "/../../../../main/include/php/incl_lbsexec.php");
    set_time_limit(0);
    //Wichtig! Script soll endlos laufen
    sql_connect();
    logging($id, "Husqvarna Automower Connect Daemon started", null, 5);
    $E = logic_getInputs($id);
    $cyclecounter = $E[11]['value'];
    // start with max value, so a cycle is immidiatelly triggerd
    while (getSysInfo(1) >= 1 && getLogicElementVar($id, 99) == 0) {
        
    $E = logic_getInputs($id);
        
    $cmd = getLogicElementVar($id, 5);
        
    setLogicElementVar($id, 5, 0);

        if (
    $cyclecounter < $E[11]['value'] && $cmd == 0) {
            
    sleep(1);
            
    $cyclecounter++;
        } else {
            
    $cyclecounter = 0;
            
    logging($id, "Husqvarna Automower Connect Cycle started", null, 8);

            if (
    $E) {

                
    $username = $E[2]['value'];
                
    $password = $E[3]['value'];
                
    $mower_num = $E[4]['value'];

                
    error_off();
                
    $session_husqvarna = new husqvarna_api();
                
    $session_husqvarna->login($username, $password);
                
    error_on();
                
    logging($id, "new husqvarna_api", null, 8);
                if (
    $session_husqvarna == NULL) {
                    
    logging($id, '$session_husqvarna is NULL', null, 1);
                } else {
                    
    error_off();
                    
    $mowers = $session_husqvarna->list_robots();
                    
    $mower = NULL;
                    if (
    count($mowers) < $mower_num)
                        
    logging($id, "mower not found - looking for mower number $mower_num, total found " . count($mowers), null, 3);
                    else {
                        
    $keys = array_keys($mowers);
                        
    $mower = $mowers[$keys[$mower_num - 1]];
                    }
                    
    error_on();
                    
    logging($id, "mower found", null, 8);
                }

                if (
    is_null($mower)) {
                    
    $cyclecounter = $E[11]['value'];
                    
    logging($id, "restart cycle", null, 8);
                    
    sleep(10);
                } else {
                    switch (
    $cmd) {
                        case 
    1 :
                            
    $session_husqvarna->control($mower->id, 'START');
                            
    logging($id, "Send command START", null, 5);
                            break;
                        case 
    2 :
                            
    $session_husqvarna->control($mower->id, 'STOP');
                            
    logging($id, "Send command STOP", null, 5);
                            break;
                        case 
    3 :
                            
    $session_husqvarna->control($mower->id, 'PARK');
                            
    logging($id, "Send command PARK", null, 5);
                            break;
                    }

                    
    $mower_is_valid = true;
                    if (
    $cmd > 0) {
                        
    logging($id, "get mower after command", null, 5);
                        
    error_off();
                        
    $session_husqvarna = new husqvarna_api();
                        
    $session_husqvarna->login($username, $password);
                        
    error_on();
                        if (
    $session_husqvarna == NULL) {
                            
    logging($id, "husqvarna is NULL", null, 1);
                        } else {
                            
    error_off();
                            
    $mowers = $session_husqvarna->list_robots();
                            
    $mower = NULL;
                            if (
    count($mowers) < $mower_num) {
                                
    logging($id, "mower not found - looking for mower number $mower_num, total found " . count($mowers), null, 3);
                            } else {
                                
    $keys = array_keys($mowers);
                                
    $mower = $mowers[$keys[$mower_num - 1]];
                            }
                            
    error_on();
                            
    logging($id, "mower found v2", null, 8);
                        }

                        if (
    is_null($mower)) {
                            
    logging($id, "mower is NULL", null, 1);
                            
    $cyclecounter = $E[11]['value'];
                            
    sleep(10);
                            
    $mower_is_valid = false;
                        }
                    }

                    if (
    $mower_is_valid) {

                        
    logic_setOutput($id, 1, $mower->name);

                        
    $mowerstate = $session_husqvarna->get_status($mower->id);

                        
    logic_setOutput($id, 2, $mowerstate->valueFound);
                        
    logic_setOutput($id, 3, $mowerstate->batteryPercent);
                        
    logic_setOutput($id, 4, $mowerstate->connected);
                        
    logic_setOutput($id, 5, $mowerstate->lastErrorCode);
                        
    logic_setOutput($id, 6, $mowerstate->lastErrorCodeTimestamp);
                        
    logic_setOutput($id, 7, $mowerstate->mowerStatus);
                        
    logic_setOutput($id, 8, $mowerstate->nextStartSource);
                        
    logic_setOutput($id, 9, $mowerstate->nextStartTimestamp);
                        
    logic_setOutput($id, 10, $mowerstate->operatingMode);
                        
    logic_setOutput($id, 11, round($mowerstate->storedTimestamp / 1000, 0));
                        
    logic_setOutput($id, 12, json_encode($mowerstate->lastLocations));
                    }

                    
    logging($id, "Husqvarna Automower Connect Cycle exit", null, 8);
                }
            }
        }
    }
    sql_disconnect();
    logging($id, "Husqvarna Automower Connect Daemon exit", null, 5);

    function 
    myErrorHandler($errno, $errstr, $errfile, $errline) {
        global 
    $id;
        
    logging($id, "File: $errfile | Error: $errno | Line: $errline | $errstr ");
    }

    function 
    error_off() {
        
    $error_handler = set_error_handler("myErrorHandler");
        
    error_reporting(0);
    }

    function 
    error_on() {
        
    restore_error_handler();
        
    error_reporting(E_ALL);
    }

    function 
    logging($id, $msg, $var = NULL, $priority = 8) {
        
    $E = getLogicEingangDataAll($id);
        
    $logLevel = getLogicElementVar($id, 103);
        if (
    is_int($priority) && $priority <= $logLevel && $priority > 0) {
            
    $logLevelNames = array('none', 'emerg', 'alert', 'crit', 'err', 'warning', 'notice', 'info', 'debug');
            
    $version = getLogicElementVar($id, 100);
            
    $lbsNo = getLogicElementVar($id, 101);
            
    $logName = getLogicElementVar($id, 102) . ' --- LBS' . $lbsNo;
            
    strpos($_SERVER['SCRIPT_NAME'], $lbsNo) ? $scriptname = 'EXE' . $lbsNo : $scriptname = 'LBS' . $lbsNo;
            
    writeToCustomLog($logName, str_pad($logLevelNames[$logLevel], 7), $scriptname . " [v$version]:\t" . $msg);

            if (
    is_object($var))
                
    $var = get_object_vars($var);
    // transfer object into array
            
    if (is_array($var)) {// print out array
                
    writeToCustomLog($logName, str_pad($logLevelNames[$logLevel], 7), $scriptname . " [v$version]:\t================ ARRAY/OBJECT START ================");
                foreach (
    $var as $index => $line)
                    
    writeToCustomLog($logName, str_pad($logLevelNames[$logLevel], 7), $scriptname . " [v$version]:\t" . $index . " => " . $line);
                
    writeToCustomLog($logName, str_pad($logLevelNames[$logLevel], 7), $scriptname . " [v$version]:\t================ ARRAY/OBJECT END ================");
            }
        }
    }

    class 
    husqvarna_api {

        protected 
    $url_api_im = 'https://iam-api.dss.husqvarnagroup.net/api/v3/';
        protected 
    $url_api_track = 'https://amc-api.dss.husqvarnagroup.net/v1/';
        protected 
    $username;
        protected 
    $password;
        protected 
    $token;
        protected 
    $provider;

        function 
    login($username, $password) {
            
    $this->username = $username;
            
    $this->password = $password;
            
    $fields["data"]["attributes"]["username"] = $this->username;
            
    $fields["data"]["attributes"]["password"] = $this->password;
            
    $fields["data"]["type"] = "token";
            
    $result = $this->post_api("token", $fields);
            if (
    $result !== false) {
                
    $this->token = $result->data->id;
                
    $this->provider = $result->data->attributes->provider;
                return 
    true;
            }
            return 
    false;
        }

        private function 
    get_headers($fields = null) {
            if (isset(
    $this->token)) {
                
    $generique_headers = array('Content-type: application/json', 'Accept: application/json', 'Authorization: Bearer ' . $this->token, 'Authorization-Provider: ' . $this->provider);
            } else {
                
    $generique_headers = array('Content-type: application/json', 'Accept: application/json');
            }
            if (isset(
    $fields)) {
                
    $custom_headers = array('Content-Length: ' . strlen(json_encode($fields)));
            } else {
                
    $custom_headers = array();
            }
            return 
    array_merge($generique_headers, $custom_headers);
        }

        private function 
    post_api($page, $fields = null) {
            
    $session = curl_init();

            
    curl_setopt($session, CURLOPT_URL, $this->url_api_im . $page);
            
    curl_setopt($session, CURLOPT_HTTPHEADER, $this->get_headers($fields));
            
    curl_setopt($session, CURLOPT_POST, true);
            
    curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
            if (isset(
    $fields)) {
                
    curl_setopt($session, CURLOPT_POSTFIELDS, json_encode($fields));
            }
            
    $json = curl_exec($session);
            
    curl_close($session);
    // throw new Exception(__('La livebox ne repond pas a la demande de cookie.', __FILE__));
            
    return json_decode($json);
        }

        private function 
    get_api($page, $fields = null) {
            
    $session = curl_init();

            
    curl_setopt($session, CURLOPT_URL, $this->url_api_track . $page);
            
    curl_setopt($session, CURLOPT_HTTPHEADER, $this->get_headers($fields));
            
    curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
            if (isset(
    $fields)) {
                
    curl_setopt($session, CURLOPT_POSTFIELDS, json_encode($fields));
            }
            
    $json = curl_exec($session);
            
    curl_close($session);
    // throw new Exception(__('La livebox ne repond pas a la demande de cookie.', __FILE__));
            
    return json_decode($json);
        }

        private function 
    del_api($page) {
            
    $session = curl_init();

            
    curl_setopt($session, CURLOPT_URL, $this->url_api_im . $page);
            
    curl_setopt($session, CURLOPT_HTTPHEADER, $this->get_headers());
            
    curl_setopt($session, CURLOPT_CUSTOMREQUEST, "DELETE");
            
    curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
            
    $json = curl_exec($session);
            
    curl_close($session);
    // throw new Exception(__('La livebox ne repond pas a la demande de cookie.', __FILE__));
            
    return json_decode($json);
        }

        function 
    logout() {
            
    $result = $this->del_api("token/" . $this->token);
            if (
    $result !== false) {
                unset(
    $this->token);
                unset(
    $this->provider);
                return 
    true;
            }
            return 
    false;
        }

        function 
    list_robots() {
            
    $list_robot = array();
            foreach (
    $this->get_api("mowers") as $robot) {
                
    $list_robot[$robot->id] = $robot;
            }
            return 
    $list_robot;
        }

        function 
    get_status($mover_id) {

            return 
    $this->get_api("mowers/" . $mover_id . "/status");
        }

        function 
    get_geofence($mover_id) {

            return 
    $this->get_api("mowers/" . $mover_id . "/geofence");
        }

        function 
    control($mover_id, $command) {
            if (
    in_array($command, array('PARK', 'STOP', 'START'))) {
                return 
    $this->get_api("mowers/" . $mover_id . "/control", array("action" => $command));
            }
        }

    }
    Gruß
    Stefan

    #2
    Ist bestimmt curl dran schuld. Such mal im Forum, ich hab irgendwo beschrieben wie man eine aktuellere Version nachinstallieren kann...

    Kommentar

    Lädt...
    X