<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/*
 * 그외 스케쥴
 */
class cron03 extends CI_Controller {


	#---------------------------------------------------------------------------
    # PURPOSE :	티날씨 스케쥴
    # CREATE  :	2018-03-30
    #---------------------------------------------------------------------------
	function t_weather_proc() {

	    if (!is_dir('./logs/cron03/'.date("Ym")))   mkdir('./logs/cron03/'.date("Ym"), 0777, TRUE);

        # file helper
        $this->load->helper('file');
        $contents = '['.date("Y-m-d H:i:s").'] t_weather_proc start'.chr(10);

	    # t 날씨 가져오기
        require_once ("./static_data/main_code.php");

        $arr_public_weather = array();
        foreach ($def_arr_position as $val) {
            $latitude   = $val['latitude'];
            $longitude  = $val['longitude'];

            $url = 'https://apis.sktelecom.com/v1/weather/status?latitude='.$latitude.'&longitude='.$longitude;
            $ch = curl_init(); //curl 사용 전 초기화 필수(curl handle)
            curl_setopt($ch, CURLOPT_URL, $url); //URL 지정하기
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json', 'Content-Type: application/json', 'TDCProjectKey: 5cceabfc-f5a3-4a1c-a2c6-3e98e72c90bd')); //header 지정하기
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_TIMEOUT, 30);
            $response = curl_exec ($ch);
            if($response!='') {

                $result = json_decode($response, true);

                if(isset($result['weatherStatusCode'])==true){
                    switch ($result['weatherModifyCode']) {
                        case '0':  $temperatures = '-'; break;
                        case '1':  $temperatures = '한파'; break;
                        case '2':  $temperatures = '추움'; break;
                        case '3':  $temperatures = '쌀쌀'; break;
                        case '4':  $temperatures = '포근'; break;
                        case '5':  $temperatures = '따뜻'; break;
                        case '6':  $temperatures = '선선'; break;
                        case '7':  $temperatures = '더움'; break;
                        case '8':  $temperatures = '폭염'; break;
                        default : $temperatures = ''; break;
                    }

                    if($result['weatherStatusDescription']=='알수없음') $result['weatherStatusDescription'] = '-';
                    $weather_img = '/img/web/pweather_icon_'.$result['weatherStatusCode'].'.png';
                    $arr_public_weather[] = array('location'=>$val['position_name'], 'weather_img'=>$weather_img, 'weather_status'=>$result['weatherStatusDescription'], 'temperatures'=>$temperatures);
                }

            }
            curl_close($ch);

        }
        $now_date       = date("Y-m-d H:i:s");
        if(count($arr_public_weather) > 0) {

            $this->load->model('admin/comm/m_t_weather');
            $this->m_t_weather->t_weather_tuncate();

            foreach ($arr_public_weather as $val) {
                $req_data = array();
                $req_data['location']       = $val['location'];
                $req_data['weather_img']    = $val['weather_img'];
                $req_data['weather_status'] = $val['weather_status'];
                $req_data['temperatures']   = $val['temperatures'];
                $req_data['reg_id']         = 'system';
                $req_data['reg_date']       = $now_date;


                $this->m_t_weather->t_weather_insert($req_data);
            }
        }


        $contents.= '['.date("Y-m-d H:i:s").'] t_weather_proc end'.chr(10);
        $contents.= '-------------------------------------------------------------------------------------'.chr(10);
        write_file('./logs/cron03/'.date("Ym").'/'.date("Ymd").'.php', $contents, 'a+');


	}




}

?>