<?php
    error_reporting(E_ALL);
    ini_set('display_errors', 1);

    ini_set('log_errors', 1);
    ini_set('error_log', "../logs/log_getlivedataController.txt");

    if (isset($_GET['key'])) {
        // if (($_GET['key'] === 'brt6k11t')&&($_POST['action']=='get')) {
        if (($_GET['key'] === 'brt6k11t')) {
            require('../connect.php');
            require('../times.php');

            $conn = new mysqli($host, $user, $pass, $database);

            $sql = "SELECT * FROM `last_data`";

            $result = mysqli_query($conn, $sql);

            if (mysqli_num_rows($result) > 0) {
                $row = mysqli_fetch_assoc($result);

                if (($current_datetime >= $start_time && $current_datetime <= $end_time)&&(file_get_contents("../allowWork.txt")=="yes")) {
                    if($row['r201']=='3'||$row['r201']=='2'){
                        if((time() - file_get_contents('action_time.txt')) > 4){
                            if(((($row['r232']<'300')&&($row['r229']<'90'))||($row['r204']>'100'))&&((time() - file_get_contents('action_time.txt')) > 3)) {
                            // if((($row['r232']<'300')||($row['r204']>'100'))&&((time() - file_get_contents('action_time.txt')) > 3)) {
                                $response = "less2";
                                $time = time()-3;
                                file_put_contents('action_time.txt', $time);
                            }elseif(((($row['r232']<'380')&&($row['r229']<'90'))||(($row['r232']<'0')&&($row['r229']>'90'))||($row['r204']>'100'))&&((time() - file_get_contents('action_time.txt')) > 3)){
                            // }elseif((($row['r232']<'380')||($row['r204']>'100'))&&((time() - file_get_contents('action_time.txt')) > 3)){
                                $response = "less";
                                $time = time()-3;
                                file_put_contents('action_time.txt', $time);
                            // }elseif(($current_datetime >= $morePower_start_time && $current_datetime <= $morePower_end_time)&&($row['r219']>'2700')&&($row['r216']>'-15')){
                            //     $response = "more";
                            //     $time = time()+3;
                            //     file_put_contents('action_time.txt', $time);
                            // }elseif((($row['r219']>'3020')&&($row['r216']>'330'))||(($row['r204']<'100')&&$row['r201']=='2')){
                            }elseif((($row['r219']>'3020')&&($row['r216']>'330'))||(($row['r219']>'3050')&&($row['r229']>'90'))||(($row['r204']<'100')&&$row['r201']=='2')){
                                $response = "more";
                                $time = time()+3;
                                file_put_contents('action_time.txt', $time);
                            }elseif(($row['r232']  > '-10')&&($row['r229'] > '95')){
                            // }elseif((($row['r217']  <'-170'))||(($row['r232'] <'-100')&&($row['r229'] < 40))){
                                $response = "ok";
                                $time = time()+5;
                                file_put_contents('action_time.txt', $time);
                            // }elseif(($row['r232']  <'200')&&($row['r229'] < '92')){
                            //     $response = "less";
                            //     $time = time()+5;
                            //     file_put_contents('action_time.txt', $time);
                            }else{
                                $response = "ok";
                                $time = time();
                                file_put_contents('action_time.txt', $time);
                            }
                        }elseif((($row['r232']  < '100')&&($row['r229'] < 90))||(($row['r204']>'150'))){
                        // }elseif((($row['r232']  < '100'))||(($row['r204']>'150'))){
                        // }elseif((($row['r217']  <'-200'))||(($row['r232'] <'-100')&&($row['r229'] < 40))){
                            $response = "less2";
                            // $response = "stop";
                            $time = time()+5;
                            file_put_contents('action_time.txt', $time);
                        }else{
                            $response = "ok";
                            // $response = file_get_contents('action_time.txt')." ".time();
                            $time = time(); 
                            // file_put_contents('action_time.txt', $time);
                        }
                    }else{
                        $response = "stop";
                    }
                }else{
                    $response = "stop";
                }
            }else{
                $response = "Błąd serwera";
            }

            $conn->close();

            // header('Content-Type: application/json');
            echo $response;
            // echo "more";
            file_put_contents("../logs/response.txt", date("Y-m-d H:i:s")." - ".$response."\n", FILE_APPEND);
        }
    }else{
        echo "Błąd weryfikacji";
    }

?>