Untitled

mail@pastecode.io avatar
unknown
php
2 years ago
3.5 kB
2
Indexable
Never
public function dynamic_mode(){
        header('Access-Control-Allow-Origin: *');
        header("Content-type: application/json; charset=utf-8");        
        $doors  = $this->db->where(array('dynamic_mode' => 1, 'status' => 1))->get('mdl_tech_door_definitions')->result();
        $day_of_week = date('N', strtotime(date('Y-m-d')));
        $day_in_array =  $day_of_week - 1;
        foreach ($doors as $key => $record) {

            $current_time   = new DateTime();
            $start_time     = new DateTime(json_decode($record->start_time)[$day_in_array]);
            $end_time       = new DateTime(json_decode($record->end_time)[$day_in_array]);   

            // Başlangıç ve bitiş aynı ise pas
            if ($start_time == $end_time) {
                continue;
            }
            
            $now = $current_time->format('H:i');
            $start = $start_time->format('H:i');
            $finish = $end_time->format('H:i');

            $door_definition_id = array(
                "door_definition_id" => $record->door_definition_id
            ); 
                       
            if ($start <= $now && $now <= $finish && $start != $finish) {
                $param = 'G1';
                $action = 'on';
                $door_status = array(
                    "door_status" => 1
                );

            } else {
                $param = 'G0';
                $action = 'off';
                $door_status = array(
                    "door_status" => 0
                );
            }   
        // Trigger Door - Start
            $ch = curl_init(); 
            curl_setopt($ch, CURLOPT_URL, 'http://'.$record->ip.'/'.$param); 
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_TIMEOUT, 10);
            curl_setopt($ch, CURLOPT_HEADER, 1); 
            curl_setopt($ch, CURLOPT_NOBODY, 1);
            curl_exec($ch); 
            $header = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            if (!curl_errno($ch)) {
                if ($header == '200') {
                  $this->Mdoordefinitions->update($door_definition_id, $door_status);            
                  $data = array('time' => date('Y-m-d H:i:s'), 'door' => $record->door_definition_id, 'action' => $action, 'type' => 'success -header:200');
                } else {
                  $data = array('time' => date('Y-m-d H:i:s'), 'door' => $record->door_definition_id, 'action' => $action, 'type' => 'error -header:not 200');
                }          
            } else {
            $data = array('time' => date('Y-m-d H:i:s'), 'door' => $record->door_definition_id, 'action' => $action, 'type' => 'error /curl');
            }

            if ($header == '0') {
            $data = array('time' => date('Y-m-d H:i:s'), 'door' => $record->door_definition_id, 'action' => $action, 'type' => 'error -header:0');
            }
            curl_close($ch);

            // Add Log
            $result = array(
                'door_id' => $record->door_definition_id,
                'door_name' => $record->door_name,
                'result' => json_encode($data),
                'record_date_time' => date('Y-m-d H:i:s'),
                'created_time' => date('Y-m-d H:i:s'),
            );
            $this->Moperator->add_dynamic_mode_log($result);            
            // if ($header == '0') {
            //     $this->single_dynamic($record, $param, $action);
            // }
            echo json_encode($data);

        }        

    }