From cf654bf46161fd4e6f3d760ba7fc9c4fe5a5f9fb Mon Sep 17 00:00:00 2001 From: "altob.rd" Date: Mon, 16 Oct 2017 15:33:08 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9F=A5=E8=A9=A2=E6=A9=9F=20qcar2.html?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controllers/Pks.php | 541 +++---- controllers/Qcar2.php | 112 ++ models/Carpark_model.php | 1192 +++++++-------- models/Pks_model.php | 715 ++++----- models/Qcar2_model.php | 144 ++ views/carpark/main_page.php | 3572 +++++++++++++++++++++---------------------- views/qcar2/main_page.php | 454 ++++++ 7 files changed, 3724 insertions(+), 3006 deletions(-) create mode 100644 controllers/Qcar2.php create mode 100644 models/Qcar2_model.php create mode 100644 views/qcar2/main_page.php diff --git a/controllers/Pks.php b/controllers/Pks.php index 5198d6f..4a37a38 100644 --- a/controllers/Pks.php +++ b/controllers/Pks.php @@ -1,268 +1,273 @@ - 車號, 影像 -鼎高IVS傳送車號及影像檔 -http://203.75.167.89/pks.html/cameras/sno/12112/ivsno/3/pksno/2016/io/KI/type/C/lpr/ABC1234/color/red/sq/5236 -http://203.75.167.89/pks.html/cameras/sno/12119/ivsno/3/pksno/195/io/KO/type/C/lpr/NONE/color/red/sq/5236 -sno: 場站編號(新北市圖書館:12118) -ivsno: ivs編號, 每一支都是獨立編號(序號) -pksno: 車位編號 -io: KI:進車格, KO:出車格, KL:車牌 -type: C:汽車, H:重機, M:機車 -lpr: ABC1234(車號), 無:NONE -color: red(紅色), 若無請用NONE(4個字) -sq: 序號(查詢時參考用) - -http設定說明: -method: POST -上傳圖檔名英數字, 副檔名為gif/jpg/png均可 -上傳圖檔欄位名稱為cars -*/ - -require_once(MQ_CLASS_FILE); - -class Pks extends CI_Controller -{ - var $vars = array(); // 共用變數 - - function __construct() - { - // $this->time_start = microtime(true); - parent::__construct(); - - ignore_user_abort(); // 接受client斷線, 繼續run - - $method_name = $this->router->fetch_method(); - if ($method_name == 'cameras') - { - ob_end_clean(); - ignore_user_abort(); - ob_start(); - header('Connection: close'); - header('Content-Length: ' . ob_get_length()); - ob_end_flush(); - flush(); - } - - $this->vars['date_time'] = date('Y-m-d H:i:s'); // 格式化時間(2015-10-12 14:36:21) - $this->vars['time_num'] = str_replace(array('-', ':', ' '), '', $this->vars['date_time']); //數字化時間(20151012143621) - $this->vars['date_num'] = substr($this->vars['time_num'], 0, 8); // 數字化日期(20151012) - $this->vars['station_no'] = STATION_NO; // 本站編號 - - // ----- 程式開發階段log設定 ----- - if (@ENVIRONMENT == 'development') - { - ini_set('display_errors', '1'); - //error_reporting(E_ALL ^ E_NOTICE); - error_reporting(E_ALL); - } - set_error_handler(array($this, 'error_handler'), E_ALL); // 資料庫異動需做log - - // mqtt subscribe - $this->vars['mqtt'] = new phpMQTT(MQ_HOST, MQ_PORT, uniqid()); - - if(!$this->vars['mqtt']->connect()){ die ('Could not connect mqtt'); } - - // ----- 定義常數(路徑, cache秒數) ----- - define('APP_VERSION', '100'); // 版本號 - - define('MAX_AGE', 604800); // cache秒數, 此定義1個月 - define('APP_NAME', 'pks'); // 應用系統名稱 - - define('PAGE_PATH', APP_BASE.'ci_application/views/'.APP_NAME.'/'); // path of views - - define('SERVER_URL', 'http://'.(isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'localhost').'/'); // URL - define('APP_URL', SERVER_URL.APP_NAME.'.html/'); // controller路徑 - define('WEB_URL', SERVER_URL.APP_NAME.'/'); // 網頁路徑 - define('WEB_LIB', SERVER_URL.'/libs/'); // 網頁lib - define('BOOTSTRAPS', WEB_LIB.'bootstrap_sb/'); // bootstrap lib - define('LOG_PATH', FILE_BASE.APP_NAME.'/logs/'); // log path name - define('LOG_FILE', FILE_BASE.APP_NAME.'/logs/pks.'); // log file name - - $this->load->model('pks_model'); - $this->pks_model->init($this->vars); - } - - - - // 發生錯誤時集中在此處理 - public function error_handler($errno, $errstr, $errfile, $errline, $errcontext) - { - $log_msg = explode('://', $errstr); - if (count($log_msg) > 1) - { - $log_file = $log_msg[0]; - $str = date('H:i:s')."|{$log_msg[1]}|{$errfile}|{$errline}|{$errno}\n"; - } - else - { - $log_file = APP_NAME; - $str = date('H:i:s')."|{$errstr}|{$errfile}|{$errline}|{$errno}\n"; - } - - error_log($str, 3, LOG_PATH.$log_file . '.' . date('Ymd').'.log.txt'); // 3代表參考後面的檔名 - } - - - // 顯示靜態網頁(html檔) - protected function show_page($page_name, &$data = null) - { - $page_file = PAGE_PATH.$page_name.'.php'; - $last_modified_time = filemtime($page_file); - - // 若檔案修改時間沒有異動, 或版本無異動, 通知瀏覽器使用cache, 不再下傳網頁 - // header('Cache-Control:max-age='.MAX_AGE); // cache 1個月 - header('Last-Modified: '.gmdate('D, d M Y H:i:s', $last_modified_time).' GMT'); - header('Etag: '. APP_VERSION); - header('Cache-Control: public'); - - if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH'] == APP_VERSION && @strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $last_modified_time) - { - header('HTTP/1.1 304 Not Modified'); - } - else - { - $this->load->view(APP_NAME.'/'.$page_name, $data); - } - } - - - public function parked() - { - $data['group_id'] = $this->uri->segment(3); - $data['init_value'] = $this->uri->segment(4); - // $data['client_id'] = uniqid(); - // $data['mqtt_ip'] = '192.168.10.201'; - // $data['port_no'] = 8000; - $this->load->view(APP_NAME.'/parked', $data); - } - - // 樓層平面圖 - // http://203.75.167.89/parkingquery.html/floor_map - public function floor_map() - { - /* - header('Access-Control-Allow-Origin: *'); - header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS'); - header('Access-Control-Allow-Headers: X-Requested-With, Content-Type, Accept'); - */ - - $this->load->view("parkingquery/floor_map"); - } - - - // response http - protected function http_return($return_code, $type) - { - if ($type == 'text') echo $return_code; - else echo json_encode($return_code, JSON_UNESCAPED_UNICODE); - } - - // 顯示logs - public function show_logs() - { - $lines = $this->uri->segment(3); // 顯示行數 - if (empty($lines)) $lines = 40; // 無行數參數, 預設為40行 - - // echo '
';
-        echo '
';
-        if (PHP_OS == 'Linux')
-			passthru('/usr/bin/tail -n ' . $lines . '  ' . LOG_FILE);		// 利用linux指令顯示倒數幾行的logs內容
-        else 
-			passthru('d:/afiles/bin/unix_cmd/tail.exe -n ' . $lines . '  ' . LOG_FILE);	
-        echo "\n----- " . LOG_FILE . ' -----';   
-        echo '
'; - } - - - // IVS -> 車號, 影像 - /* - IVS -> 車號, 影像 - 鼎高IVS傳送車號及影像檔 - http://203.75.167.89/pks.html/cameras/sno/12119/ivsno/3/pksno/102/io/KI/type/C/lpr/ABC1234/color/red/sq/5236 - sno: 場站編號(新北市圖書館:12118) - ivsno: ivs編號, 每一支都是獨立編號(序號) - pksno: 車位編號 - io: KI:進車格, KO:出車格, KL:車牌辨識 - type: C:汽車, H:重機, M:機車 - lpr: ABC1234(車號) - color: red(紅色), 若無請用NONE(4個字) - sq: 序號(查詢時參考用) - - http設定說明: - method: POST - 上傳圖檔名英數字, 副檔名為gif/jpg/png均可 - 上傳圖檔欄位名稱為cars - */ - public function cameras() - { - $parms = $this->uri->uri_to_assoc(3); - trigger_error('在席參數傳入:'.print_r($parms, true)); - - // array_map('unlink', glob(PKS_PIC."pks-{$parms['pksno']}-*")); - - /* - // 車入格後的車牌辨識(lpr), 傅送圖檔 - if ($parms['io'] == 'KL') - { - array_map('unlink', glob(PKS_PIC."pks-{$parms['pksno']}-*.jpg")); // 刪除舊照片 - $config['upload_path'] = PKS_PIC; - $config['allowed_types'] = 'gif|jpg|png'; - // ex. pks-2016-1625AB-1-2015080526.jpg -> pks-車位編號-車號-設備編號-時間.jpg - $config['file_name'] = "pks-{$parms['pksno']}-{$parms['lpr']}-{$parms['ivsno']}-{$this->vars['time_num']}.jpg"; - $this->load->library('upload', $config); - - $parms['pic_name'] = $config['file_name']; - if($this->upload->do_upload('cars')) - { - // 若無錯誤,則上傳檔案 - $file = $this->upload->data('cars'); - } - else - { - trigger_error('入席傳檔錯誤:'. print_r($parms, true)); - } - } - */ - $this->pks_model->pksio($parms); // 車輛進出車格資料庫處理 - exit; - } - - - // 重新計算 - // http://203.75.167.89/pks.html/reculc/ - public function reculc() - { - $this->pks_model->reculc(); - } - - - - // 取得所有車位狀態資訊 - // http://203.75.167.89/pks.html/query_station_status/12112 - public function query_station_status() - { - $station_no = $this->uri->segment(3); - $data = $this->pks_model->query_station_status($station_no); - echo json_encode($data, JSON_UNESCAPED_UNICODE); - } - - // 取得車位資訊 - // http://203.75.167.89/pks.html/query_station_pks/12112/2021 - public function query_station_pks(){ - $station_no = $this->uri->segment(3); - $pksno = $this->uri->segment(4); - $data = $this->pks_model->query_station_pks($station_no, $pksno); - echo json_encode($data, JSON_UNESCAPED_UNICODE); - } - - // 車位狀態資訊圖 - // http://203.75.167.89/pks.html/status_map - public function status_map() - { - $this->show_page("status_map"); - } - -} + 車號, 影像 +鼎高IVS傳送車號及影像檔 +http://203.75.167.89/pks.html/cameras/sno/12112/ivsno/3/pksno/2016/io/KI/type/C/lpr/ABC1234/color/red/sq/5236 +http://203.75.167.89/pks.html/cameras/sno/12119/ivsno/3/pksno/195/io/KO/type/C/lpr/NONE/color/red/sq/5236 +sno: 場站編號(新北市圖書館:12118) +ivsno: ivs編號, 每一支都是獨立編號(序號) +pksno: 車位編號 +io: KI:進車格, KO:出車格, KL:車牌 +type: C:汽車, H:重機, M:機車 +lpr: ABC1234(車號), 無:NONE +color: red(紅色), 若無請用NONE(4個字) +sq: 序號(查詢時參考用) + +http設定說明: +method: POST +上傳圖檔名英數字, 副檔名為gif/jpg/png均可 +上傳圖檔欄位名稱為cars +*/ + +require_once(MQ_CLASS_FILE); + +class Pks extends CI_Controller +{ + var $vars = array(); // 共用變數 + + function __construct() + { + // $this->time_start = microtime(true); + parent::__construct(); + + ignore_user_abort(); // 接受client斷線, 繼續run + + $method_name = $this->router->fetch_method(); + if ($method_name == 'cameras') + { + ob_end_clean(); + ignore_user_abort(); + ob_start(); + header('Connection: close'); + header('Content-Length: ' . ob_get_length()); + ob_end_flush(); + flush(); + } + + $this->vars['date_time'] = date('Y-m-d H:i:s'); // 格式化時間(2015-10-12 14:36:21) + $this->vars['time_num'] = str_replace(array('-', ':', ' '), '', $this->vars['date_time']); //數字化時間(20151012143621) + $this->vars['date_num'] = substr($this->vars['time_num'], 0, 8); // 數字化日期(20151012) + $this->vars['station_no'] = STATION_NO; // 本站編號 + + // ----- 程式開發階段log設定 ----- + if (@ENVIRONMENT == 'development') + { + ini_set('display_errors', '1'); + //error_reporting(E_ALL ^ E_NOTICE); + error_reporting(E_ALL); + } + set_error_handler(array($this, 'error_handler'), E_ALL); // 資料庫異動需做log + + // mqtt subscribe + $this->vars['mqtt'] = new phpMQTT(MQ_HOST, MQ_PORT, uniqid()); + + if(!$this->vars['mqtt']->connect()){ die ('Could not connect mqtt'); } + + // ----- 定義常數(路徑, cache秒數) ----- + define('APP_VERSION', '100'); // 版本號 + + define('MAX_AGE', 604800); // cache秒數, 此定義1個月 + define('APP_NAME', 'pks'); // 應用系統名稱 + + define('PAGE_PATH', APP_BASE.'ci_application/views/'.APP_NAME.'/'); // path of views + + define('SERVER_URL', 'http://'.(isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'localhost').'/'); // URL + define('APP_URL', SERVER_URL.APP_NAME.'.html/'); // controller路徑 + define('WEB_URL', SERVER_URL.APP_NAME.'/'); // 網頁路徑 + define('WEB_LIB', SERVER_URL.'/libs/'); // 網頁lib + define('BOOTSTRAPS', WEB_LIB.'bootstrap_sb/'); // bootstrap lib + define('LOG_PATH', FILE_BASE.APP_NAME.'/logs/'); // log path name + define('LOG_FILE', FILE_BASE.APP_NAME.'/logs/pks.'); // log file name + + $this->load->model('pks_model'); + $this->pks_model->init($this->vars); + } + + + + // 發生錯誤時集中在此處理 + public function error_handler($errno, $errstr, $errfile, $errline, $errcontext) + { + $log_msg = explode('://', $errstr); + if (count($log_msg) > 1) + { + $log_file = $log_msg[0]; + $str = date('H:i:s')."|{$log_msg[1]}|{$errfile}|{$errline}|{$errno}\n"; + } + else + { + $log_file = APP_NAME; + $str = date('H:i:s')."|{$errstr}|{$errfile}|{$errline}|{$errno}\n"; + } + + error_log($str, 3, LOG_PATH.$log_file . '.' . date('Ymd').'.log.txt'); // 3代表參考後面的檔名 + } + + + // 顯示靜態網頁(html檔) + protected function show_page($page_name, &$data = null) + { + $page_file = PAGE_PATH.$page_name.'.php'; + $last_modified_time = filemtime($page_file); + + // 若檔案修改時間沒有異動, 或版本無異動, 通知瀏覽器使用cache, 不再下傳網頁 + // header('Cache-Control:max-age='.MAX_AGE); // cache 1個月 + header('Last-Modified: '.gmdate('D, d M Y H:i:s', $last_modified_time).' GMT'); + header('Etag: '. APP_VERSION); + header('Cache-Control: public'); + + if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH'] == APP_VERSION && @strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $last_modified_time) + { + header('HTTP/1.1 304 Not Modified'); + } + else + { + $this->load->view(APP_NAME.'/'.$page_name, $data); + } + } + + + public function parked() + { + $data['group_id'] = $this->uri->segment(3); + $data['init_value'] = $this->uri->segment(4); + // $data['client_id'] = uniqid(); + // $data['mqtt_ip'] = '192.168.10.201'; + // $data['port_no'] = 8000; + $this->load->view(APP_NAME.'/parked', $data); + } + + // 樓層平面圖 + // http://203.75.167.89/parkingquery.html/floor_map + public function floor_map() + { + /* + header('Access-Control-Allow-Origin: *'); + header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS'); + header('Access-Control-Allow-Headers: X-Requested-With, Content-Type, Accept'); + */ + + $this->load->view("parkingquery/floor_map"); + } + + + // response http + protected function http_return($return_code, $type) + { + if ($type == 'text') echo $return_code; + else echo json_encode($return_code, JSON_UNESCAPED_UNICODE); + } + + // 顯示logs + public function show_logs() + { + $lines = $this->uri->segment(3); // 顯示行數 + if (empty($lines)) $lines = 40; // 無行數參數, 預設為40行 + + // echo '
';
+        echo '
';
+        if (PHP_OS == 'Linux')
+			passthru('/usr/bin/tail -n ' . $lines . '  ' . LOG_FILE);		// 利用linux指令顯示倒數幾行的logs內容
+        else 
+			passthru('d:/afiles/bin/unix_cmd/tail.exe -n ' . $lines . '  ' . LOG_FILE);	
+        echo "\n----- " . LOG_FILE . ' -----';   
+        echo '
'; + } + + + // IVS -> 車號, 影像 + /* + IVS -> 車號, 影像 + 鼎高IVS傳送車號及影像檔 + http://203.75.167.89/pks.html/cameras/sno/12119/ivsno/3/pksno/102/io/KI/type/C/lpr/ABC1234/color/red/sq/5236 + sno: 場站編號(新北市圖書館:12118) + ivsno: ivs編號, 每一支都是獨立編號(序號) + pksno: 車位編號 + io: KI:進車格, KO:出車格, KL:車牌辨識 + type: C:汽車, H:重機, M:機車 + lpr: ABC1234(車號) + color: red(紅色), 若無請用NONE(4個字) + sq: 序號(查詢時參考用) + + http設定說明: + method: POST + 上傳圖檔名英數字, 副檔名為gif/jpg/png均可 + 上傳圖檔欄位名稱為cars + */ + public function cameras() + { + $parms = $this->uri->uri_to_assoc(3); + + // 調整 pksno 為 pks 格式 + $parms['pksno'] = intval(implode('', explode('F_', $parms['pksno']))); + + trigger_error('在席參數傳入:'.print_r($parms, true)); + + // array_map('unlink', glob(PKS_PIC."pks-{$parms['pksno']}-*")); + + /* + // 車入格後的車牌辨識(lpr), 傅送圖檔 + if ($parms['io'] == 'KL') + { + array_map('unlink', glob(PKS_PIC."pks-{$parms['pksno']}-*.jpg")); // 刪除舊照片 + $config['upload_path'] = PKS_PIC; + $config['allowed_types'] = 'gif|jpg|png'; + // ex. pks-2016-1625AB-1-2015080526.jpg -> pks-車位編號-車號-設備編號-時間.jpg + $config['file_name'] = "pks-{$parms['pksno']}-{$parms['lpr']}-{$parms['ivsno']}-{$this->vars['time_num']}.jpg"; + $this->load->library('upload', $config); + + $parms['pic_name'] = $config['file_name']; + if($this->upload->do_upload('cars')) + { + // 若無錯誤,則上傳檔案 + $file = $this->upload->data('cars'); + } + else + { + trigger_error('入席傳檔錯誤:'. print_r($parms, true)); + } + } + */ + + $this->pks_model->pksio($parms); // 車輛進出車格資料庫處理 + exit; + } + + + // 重新計算 + // http://203.75.167.89/pks.html/reculc/ + public function reculc() + { + $this->pks_model->reculc(); + } + + + + // 取得所有車位狀態資訊 + // http://203.75.167.89/pks.html/query_station_status/12112 + public function query_station_status() + { + $station_no = $this->uri->segment(3); + $data = $this->pks_model->query_station_status($station_no); + echo json_encode($data, JSON_UNESCAPED_UNICODE); + } + + // 取得車位資訊 + // http://203.75.167.89/pks.html/query_station_pks/12112/2021 + public function query_station_pks(){ + $station_no = $this->uri->segment(3); + $pksno = $this->uri->segment(4); + $data = $this->pks_model->query_station_pks($station_no, $pksno); + echo json_encode($data, JSON_UNESCAPED_UNICODE); + } + + // 車位狀態資訊圖 + // http://203.75.167.89/pks.html/status_map + public function status_map() + { + $this->show_page("status_map"); + } + +} diff --git a/controllers/Qcar2.php b/controllers/Qcar2.php new file mode 100644 index 0000000..392a943 --- /dev/null +++ b/controllers/Qcar2.php @@ -0,0 +1,112 @@ +load->model('qcar2_model'); + } + + + // 發生錯誤時集中在此處理 + public function error_handler($errno, $errstr, $errfile, $errline, $errcontext) + { + $str = date('H:i:s')."|{$errstr}|{$errfile}|{$errline}|{$errno}\n"; + //error_log($str, 3, $log_file . '.' . date('Ymd').'.log.txt'); // 3代表參考後面的檔名 + error_log($str, 3, LOG_PATH.APP_NAME . '.' . date('Ymd').'.log.txt'); // 3代表參考後面的檔名 + } + + + // 顯示靜態網頁(html檔) + protected function show_page($page_name, &$data = null) + { + $page_file = PAGE_PATH.$page_name.'.php'; + $last_modified_time = filemtime($page_file); + + // 若檔案修改時間沒有異動, 或版本無異動, 通知瀏覽器使用cache, 不再下傳網頁 + // header('Cache-Control:max-age='.MAX_AGE); // cache 1個月 + header('Last-Modified: '.gmdate('D, d M Y H:i:s', $last_modified_time).' GMT'); + header('Etag: '. APP_VERSION); + header('Cache-Control: public'); + + if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH'] == APP_VERSION && @strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $last_modified_time) + { + header('HTTP/1.1 304 Not Modified'); + } + else + { + $this->load->view(APP_NAME.'/'.$page_name, $data); + } + } + + + public function index() + { + $this->show_page('main_page'); + } + + + + // 顯示logs + public function show_logs() + { + $lines = $this->uri->segment(3); // 顯示行數 + if (empty($lines)) $lines = 40; // 無行數參數, 預設為40行 + + // echo '
';
+        echo '
';
+		passthru('/usr/bin/tail -n ' . $lines . '  ' . LOG_FILE);		// 利用linux指令顯示倒數幾行的logs內容 
+        echo "\n----- " . LOG_FILE . ' -----';   
+        echo '
'; + } + + + + // 車位查詢 + public function q_pks() + { + $lpr = $this->input->post('lpr', true); + $data = $this->qcar2_model->q_pks($lpr); + echo json_encode($data, JSON_UNESCAPED_UNICODE); + } + + // 取得進場資訊 (模糊比對) + public function q_fuzzy_pks() + { + $input = $this->input->post('fuzzy_input', true); + $data = $this->qcar2_model->q_fuzzy_pks($input); + echo json_encode($data, JSON_UNESCAPED_UNICODE); + } + +} diff --git a/models/Carpark_model.php b/models/Carpark_model.php index 211ede3..c8e4015 100644 --- a/models/Carpark_model.php +++ b/models/Carpark_model.php @@ -1,596 +1,596 @@ -load->database(); - } - - public function init($vars) - { - $this->vars = $vars; - } - - // 月租會員加入 - public function member_add($data) - { - // 會員車輛基本資料檔 - // $data['start_date'] = "{$data['start_date']} 00:00:00"; - // $data['end_date'] = "{$data['end_date']} 23:59:59"; - $old_lpr = $data['old_lpr']; - unset($data['old_lpr']); - $data_car = array - ( - 'lpr' => $data['lpr'], - 'lpr_correct' => $data['lpr'], - 'etag' => $data['etag'], - 'station_no' => $data['station_no'], - 'start_time' => $data['start_date'], - 'end_time' => $data['end_date'] - ); - - $check_member_no = $data['member_no']; - unset($data['member_no']); - trigger_error("members:".print_r($data, true)."car:".print_r($data_car, true)); - if ($check_member_no == 0) // 新增一筆會員資料 - { - $this->db->insert('members', $data); - $data_car['member_no'] = $this->db->insert_id(); - $this->db->insert('member_car', $data_car); - } - else // update會員資料 - { - $this->db->update('members', $data, array('member_no' => $check_member_no)); - if ($old_lpr == $data['lpr']) // 沒有異動到車牌, 使用update, 否則重建一筆 - { - unset($data_car['lpr']); - unset($data_car['lpr_correct']); - - $this->db->update('member_car', $data_car, array('member_no' => $check_member_no)); - } - else - { - $this->db->delete('member_car', array('member_no' => $check_member_no)); - $data_car['member_no'] = $check_member_no; - $this->db->insert('member_car', $data_car); - } - } - - return true; - } - - - // 查詢車牌是否重複 - public function check_lpr($lpr) - { - $rows = $this->db->select('count(*) as counts') - ->from('members') - ->where(array('lpr' => $lpr)) - ->get() - ->row_array(); - - return $rows['counts']; - } - - - // 月租會員查詢 - public function member_query() - { - $results = $this->db->select('member_no, lpr, etag, member_name, mobile_no, start_date, end_date, contract_no, amt, member_id, tel_h, tel_o, addr, valid_time, station_no') - ->from('members') - ->order_by('station_no, lpr', 'asc') - ->get() - ->result_array(); - - return $results; - } - - - // 刪除月租會員 - public function member_delete($member_no) - { - $this->db->delete('members', array('member_no' => $member_no)); - $this->db->delete('member_car', array('member_no' => $member_no)); - - return true; - } - - - // 進出場現況表 - public function cario_list() - { - /* - $data_cario = $this->db - ->select('c.cario_no, c.in_out, in_lane, out_lane, c.in_time, c.out_time, c.minutes, c.obj_id as lpr, c.etag, c.in_pic_name, c.out_pic_name, m.member_name as owner') - ->from('cario c') - ->join('members m', 'c.member_no = m.member_no', 'left') - ->where(array('c.err' => 0)) - ->order_by('c.update_time', 'desc') - ->limit(10) - ->get() - ->result_array(); - */ - $sql = '(select c.cario_no, c.in_out, in_lane, out_lane, c.in_time, c.out_time, c.minutes, c.obj_id as lpr, c.etag, c.in_pic_name, c.out_pic_name, m.member_name as owner, c.in_time as time_order - from cario c left join members m on c.obj_id = m.lpr - where c.err = 0 and c.out_time is null) - union - (select c.cario_no, c.in_out, in_lane, out_lane, c.in_time, c.out_time, c.minutes, c.obj_id as lpr, c.etag, c.in_pic_name, c.out_pic_name, m.member_name as owner, c.out_time as time_order - from cario c left join members m on c.obj_id = m.lpr - where c.err = 0 and c.out_time is not null) - order by time_order desc limit 10;'; - $data_cario = $this->db->query($sql)->result_array(); - - // $lane_arr = array(0 => '入1', 1 => '入2', 3 => '出3', 3 => '出4'); - $idx = 0; - foreach($data_cario as $rows) - { - ++$rows['in_lane']; - ++$rows['out_lane']; - - //$lane_no = $rows['in_out'] == 'CI' ? "入{$rows['in_lane']}" : "入{$rows['in_lane']} -> 出{$rows['out_lane']}"; - $lane_no = empty($rows['out_time']) ? "入{$rows['in_lane']}" : "入{$rows['in_lane']} -> 出{$rows['out_lane']}"; // 2016/08/22 有離場時間就顯示 - - $pic_name = str_replace('.jpg', '', empty($rows['out_pic_name']) ? $rows['in_pic_name'] : $rows['out_pic_name']); - $arr = explode('-', $pic_name); - $pic_path = APP_URL.'pics/'.substr($arr[7], 0, 8).'/'.$pic_name; - - $data[$idx] = array - ( - // 'io_name' => $io_name[$rows['in_out']], - 'io_name' => $lane_no, - 'lpr' => $rows['lpr'], - // 'etag' => $rows['etag'], - 'etag' => $rows['etag'], - 'owner' => $rows['owner'], - 'io_time' => empty($rows['out_time']) ? $rows['in_time'] : "{$rows['in_time']}(入)
{$rows['out_time']}(出)
{$rows['minutes']}分(停留時間)", - 'pic_name' => $pic_path - ); - ++$idx; - } - - return $data; - } - - // 模糊比對 - function getLevenshteinSQLStatement($word, $target) - { - $words = array(); - - if(strlen($word) >= 5) - { - for ($i = 0; $i < strlen($word); $i++) { - // insertions - $words[] = substr($word, 0, $i) . '_' . substr($word, $i); - // deletions - $words[] = substr($word, 0, $i) . substr($word, $i + 1); - // substitutions - //$words[] = substr($word, 0, $i) . '_' . substr($word, $i + 1); - } - } - else - { - for ($i = 0; $i < strlen($word); $i++) { - // insertions - $words[] = substr($word, 0, $i) . '_' . substr($word, $i); - } - } - - // last insertion - $words[] = $word . '_'; - //return $words; - - $fuzzy_statement = ' ('; - foreach ($words as $idx => $word) - { - $fuzzy_statement .= " {$target} LIKE '%{$word}%' OR "; - } - $last_or_pos = strrpos($fuzzy_statement, 'OR'); - if($last_or_pos !== false) - { - $fuzzy_statement = substr_replace($fuzzy_statement, ')', $last_or_pos, strlen('OR')); - } - - return $fuzzy_statement; - } - - // 車號入場查詢 - public function carin_lpr_query($word) - { - // updated 2016/09/09 fuzzy search - if(empty($word) || strlen($word) < 4 || strlen($word) > 10) - { - return array(); - } - $fuzzy_statement = $this->getLevenshteinSQLStatement($word, 'c.obj_id'); - trigger_error("模糊比對 {$word} where: {$fuzzy_statement}"); - - $sql = "SELECT c.cario_no, c.in_out, in_lane, out_lane, c.in_time, c.out_time, c.minutes, c.obj_id as lpr, c.etag, c.in_pic_name, c.out_pic_name, m.member_name as owner - FROM cario c - LEFT JOIN members m ON c.obj_id = m.lpr - WHERE {$fuzzy_statement} AND c.err = 0 AND c.obj_type = 1 - ORDER BY c.update_time DESC - LIMIT 50"; - $data_cario = $this->db->query($sql)->result_array(); - - /* - $data_cario = $this->db - ->select('c.cario_no, c.in_out, in_lane, out_lane, c.in_time, c.out_time, c.minutes, c.obj_id as lpr, c.etag, c.in_pic_name, c.out_pic_name, m.member_name as owner') - ->from('cario c') - ->join('members m', 'c.member_no = m.member_no', 'left') - ->where(array('c.obj_type' => 1, 'obj_id' => $lpr)) - ->order_by('c.update_time', 'desc') - ->get() - ->result_array(); - */ - - $data = array(); - $idx = 0; - foreach($data_cario as $rows) - { - ++$rows['in_lane']; - ++$rows['out_lane']; - - $lane_no = empty($rows['out_time']) ? "入{$rows['in_lane']}" : "入{$rows['in_lane']} -> 出{$rows['out_lane']}"; - $io_time = empty($rows['out_time']) ? $rows['in_time'] : "{$rows['in_time']}(入)
{$rows['out_time']}(出)
{$rows['minutes']}分(停留時間)"; - - $pic_name = str_replace('.jpg', '', empty($rows['out_pic_name']) ? $rows['in_pic_name'] : $rows['out_pic_name']); - $arr = explode('-', $pic_name); - $pic_path = APP_URL.'pics/'.substr($arr[7], 0, 8).'/'.$pic_name; - - $data[$idx++] = array - ( - 'io_name' => $lane_no, - 'lpr' => $rows['lpr'], - 'etag' => $rows['etag'], - 'owner' => empty($rows['owner']) ? '' : $rows['owner'], - 'io_time' => $io_time, - 'pic_name' => $pic_path - ); - } - - return $data; - } - - - - // 車號入場查詢 - public function carin_time_query($time_query, $minutes_range) - { - $curr_time = date('Y-m-d H:i:s'); - - $start_time = date('Y-m-d H:i:s', strtotime("{$time_query} - {$minutes_range} minutes")); - $end_time = date('Y-m-d H:i:s', strtotime("{$time_query} + {$minutes_range} minutes")); - - $data_cario = $this->db - ->select('c.cario_no, c.in_out, in_lane, out_lane, c.in_time, c.out_time, c.minutes, c.obj_id as lpr, c.etag, c.in_pic_name, c.out_pic_name, m.member_name as owner') - ->from('cario c') - ->join('members m', 'c.obj_id = m.lpr', 'left') - ->where(array('c.obj_type' => 1, 'c.in_time >=' => $start_time, 'c.in_time <=' => $end_time)) - ->order_by('c.update_time', 'desc') - ->get() - ->result_array(); - - $data = array(); - $idx = 0; - foreach($data_cario as $rows) - { - ++$rows['in_lane']; - ++$rows['out_lane']; - - $lane_no = empty($rows['out_time']) ? "入{$rows['in_lane']}" : "入{$rows['in_lane']} -> 出{$rows['out_lane']}"; - $io_time = empty($rows['out_time']) ? $rows['in_time'] : "{$rows['in_time']}(入)
{$rows['out_time']}(出)
{$rows['minutes']}分(停留時間)"; - - $pic_name = str_replace('.jpg', '', empty($rows['out_pic_name']) ? $rows['in_pic_name'] : $rows['out_pic_name']); - $arr = explode('-', $pic_name); - $pic_path = APP_URL.'pics/'.substr($arr[7], 0, 8).'/'.$pic_name; - - $data[$idx++] = array - ( - 'io_name' => $lane_no, - 'lpr' => $rows['lpr'], - 'etag' => $rows['etag'], - 'owner' => empty($rows['owner']) ? '' : $rows['owner'], - 'io_time' => $io_time, - 'pic_name' => $pic_path - ); - } - - return $data; - } - - // 時間長度轉成日時分秒 - public function time2str($d1, $d2) - { - $time = strtotime($d2) - strtotime($d1); - - $day_str = floor($time/3600/24); - $day_str = $day_str ? $day_str .= '天 ' : ''; - - $hour_str = floor($time%(24*3600)/3600); - $hour_str = $hour_str ? $hour_str .= '小時 ' : ''; - - $minute_str = floor($time%3600/60); - $minute_str = $minute_str ? $minute_str .= '分' : ''; - - /* - $second_str = $time%3600%60; - $second_str = $second_str ? $second_str .= ' seconds ' : ''; - - $n_time = floor($time/3600/24)."days".floor($time%(24*3600)/3600)."Hour".floor($time%3600/60)."Minute".($time%3600%60)."Second"; - */ - - $n_time = $day_str . $hour_str . $minute_str; - return $n_time; - } - - - // 在席車位檢查未有入場資料清單 - public function pks_check_list($max_rows) - { - $data = array(); - $data = $this->db - ->select('pksno, lpr, in_time, pic_name') - ->from('pks') - ->where(array('status' => 'LR', 'cario_no' => 0, 'confirms' => 0, 'station_no' => STATION_NO)) - ->order_by('in_time', 'desc') - ->limit($max_rows) - ->get() - ->result_array(); - return $data; - } - - - // 重設在席查核 - public function reset_pks_check() - { - // 讀出未查核過的資料 - $data_pks = $this->db - ->select('pksno, lpr, in_time') - ->from('pks') - ->where(array('status' => 'LR', 'cario_no' => 0, 'station_no' => STATION_NO)) - ->get() - ->result_array(); - - // $tot = $this->db->num_rows(); // 總筆數 - $tot = count($data_pks); // 總筆數 - - $num_cario = 0; // 入場資料筆數 - foreach($data_pks as $rows) - { - $lpr = $rows['lpr']; - if ($lpr == 'NONE') continue; // 車辨失敗者不處理 - - $pksno = $rows['pksno']; - $pks_in_time = $rows['in_time']; - // 讀取進場時間, 如讀不到資料, 以目前時間取代(add by TZUSS 2016-02-23) - $rows_cario = $this->db - ->select('cario_no, in_time') - ->from('cario') - ->where(array('in_out' => 'CI', 'obj_id' => $lpr, 'finished' => 0, 'err' => 0, 'station_no' => STATION_NO)) - ->order_by('cario_no', 'desc') - ->limit(1) - ->get() - ->row_array(); - // if ($this->db->num_rows() == 1) // 有入場資料 - if (!empty($rows_cario['cario_no'])) // 有入cario_no場資料 - { - $cario_no = $rows_cario['cario_no']; // 入場序號 - $in_time = $rows_cario['in_time']; - // 在席與入場資料相符, 分別在cario與pks記錄之 - $data = array - ( - 'pksno' => $pksno, - 'pks_time' => $pks_in_time - ); - $this->db->update('cario', $data, array('cario_no' => $cario_no, 'station_no' => STATION_NO)); - $data = array - ( - 'cario_no' => $cario_no, - 'in_time' => $in_time - ); - // 車號及照片檔名填入資料庫內 - $this->db->update('pks', $data, array('pksno' => $pksno, 'station_no' => STATION_NO)); - ++$num_cario; - } - - } - return array('tot' => $tot, 'tot_correct' => $num_cario); - } - - - // 更正在席車號 - public function correct_pks_lpr($pksno, $lpr) - { - - // 讀取進場時間, 如讀不到資料, 以目前時間取代(add by TZUSS 2016-02-23) - $rows_cario = $this->db - ->select('cario_no, in_time') - ->from('cario') - ->where(array('in_out' => 'CI', 'obj_id' => $lpr, 'finished' => 0, 'err' => 0, 'station_no' => STATION_NO)) - ->order_by('cario_no', 'desc') - ->limit(1) - ->get() - ->row_array(); - - if (!empty($rows_cario['cario_no'])) // 有cario_no入場資料 - { - $cario_no = $rows_cario['cario_no']; // 入場序號 - $in_time = $rows_cario['in_time']; - // 在席與入場資料相符, 分別在cario與pks記錄之 - $data = array - ( - 'pksno' => $pksno, - 'pks_time' => $in_time - ); - $this->db->update('cario', $data, array('cario_no' => $cario_no, 'station_no' => STATION_NO)); - $data = array - ( - 'confirms' => 1, - 'cario_no' => $cario_no, - 'lpr' => $lpr, - 'in_time' => $in_time - ); - // 車號及照片檔名填入資料庫內 - $this->db->update('pks', $data, array('pksno' => $pksno, 'station_no' => STATION_NO)); - $results = array - ( - 'err' => 0, - 'cario_no' => $cario_no - ); - } - else // 無入場資料 - { - $data = array - ( - 'confirms' => 1, - 'lpr' => $lpr - ); - - $this->db->update('pks', $data, array('pksno' => $pksno, 'station_no' => STATION_NO)); - $results = array - ( - 'err' => 0, - 'cario_no' => 0 // 車號查無入場資料 - ); - } - - return $results; - } - - - // 入場車號查核在席無資料清單 - public function carin_check_list($max_rows) - { - $data = array(); - $rows_cario = $this->db - ->select('cario_no, obj_id as lpr, in_time, member_no, in_pic_name') - ->from('cario') - ->where('in_out', 'CI') - ->where(array('pksno' => 0, 'finished' => 0, 'err' => 0, 'confirms' => 0, 'station_no' => STATION_NO, 'in_time <=' => 'date_sub(now(), interval 20 minute)'), null, false) - ->order_by('cario_no', 'desc') - ->limit($max_rows) - ->get() - ->result_array(); - - $idx = 0; - foreach($rows_cario as $rows) - { - $data[$idx++] = array - ( - 'cario_no' => $rows['cario_no'], - 'lpr' => $rows['lpr'], - 'in_time' => $rows['in_time'], - 'type' => $rows['member_no'] == 0 ? '' : '月租', - 'pic_name' => str_replace('-', '', substr($rows['in_time'], 0, 10)) . '/' . $rows['in_pic_name'] - ); - } - return $data; - } - - - - // 更正入場車號 - public function correct_carin_lpr($cario_no, $lpr, $in_time) - { - $rows = $this->db - ->select('pksno, cario_no, in_time') - ->from('pks') - ->where(array('status' => 'LR', 'lpr' => $lpr, 'confirms' => 0, 'station_no' => STATION_NO)) - ->limit(1) - ->get() - ->row_array(); - // 如果在席資料相符 - if (!empty($rows['pksno'])) - { - $pksno = $rows['pksno']; - - $data = array - ( - 'cario_no' => $cario_no, - 'in_time' => $in_time - ); - - $this->db->update('pks', $data, array('pksno' => $pksno, 'station_no' => STATION_NO)); - - $data_cario = array - ( - 'obj_id' => $lpr, - 'pksno' => $pksno, - 'pks_time' => $in_time, - 'confirms' => 1 - ); - } - else // 無在席資料 - { - $data_cario = array - ( - 'obj_id' => $lpr, - 'pksno' => 0, - 'confirms' => 1 - ); - } - - $this->db->update('cario', $data_cario, array('cario_no' => $cario_no, 'station_no' => STATION_NO)); - - return(array('pksno' => $data_cario['pksno'])); - } - - // 查詢行動支付記錄 - public function tx_bill_query() - { - $sql = "SELECT - tx_bill.order_no as order_no, tx_bill.lpr as lpr, tx_bill.invoice_no as invoice_no, tx_bill.in_time as in_time, tx_bill.balance_time as balance_time, tx_bill.company_no as company_no, tx_bill.email as email, tx_bill.mobile as mobile, tx_bill.amt as amt, tx_bill.tx_time as tx_time, - allpay_feedback_log.rtn_msg as rtn_msg, allpay_feedback_log.payment_type as payment_type, - cario.out_before_time as out_before_time - FROM tx_bill - LEFT JOIN cario ON tx_bill.cario_no = cario.cario_no - LEFT JOIN allpay_feedback_log ON tx_bill.order_no = allpay_feedback_log.merchant_trade_no - WHERE tx_bill.status = 111 - ORDER BY tx_bill.tx_time DESC"; - - return $this->db->query($sql)->result_array(); - } - - // 查詢月租繳款機記錄 - public function tx_bill_ats_query() - { - $sql = "SELECT - tx_bill_ats.order_no as order_no, tx_bill_ats.lpr as lpr, tx_bill_ats.invoice_no as invoice_no, - tx_bill_ats.end_time as end_time, tx_bill_ats.next_start_time as next_start_time, tx_bill_ats.next_end_time as next_end_time, - tx_bill_ats.company_no as company_no, tx_bill_ats.email as email, tx_bill_ats.mobile as mobile, tx_bill_ats.amt as amt, - tx_bill_ats.remarks as remarks, tx_bill_ats.member_name as member_name, tx_bill_ats.tx_time as tx_time, - allpay_feedback_log.rtn_msg as rtn_msg, allpay_feedback_log.payment_type as payment_type - FROM tx_bill_ats - LEFT JOIN allpay_feedback_log ON tx_bill_ats.order_no = allpay_feedback_log.merchant_trade_no - WHERE tx_bill_ats.status = 111 - ORDER BY tx_bill_ats.tx_time DESC"; - - return $this->db->query($sql)->result_array(); - } - - // 查詢樓層在席群組 - public function pks_group_query() - { - $sql = "SELECT - pks_groups.station_no, pks_groups.group_name as group_name, pks_groups.tot as tot, pks_groups.parked as parked, pks_groups.availables as availables, pks_groups.group_id as group_id, pks_groups.renum as renum - FROM pks_groups - ORDER BY pks_groups.group_id DESC"; - - return $this->db->query($sql)->result_array(); - } - - // 送出至message queue(目前用mqtt) - public function mq_send($topic, $msg) - { - $this->vars['mqtt']->publish($topic, $msg, 0); - trigger_error("mqtt:{$topic}|{$msg}"); - } - -} +load->database(); + } + + public function init($vars) + { + $this->vars = $vars; + } + + // 月租會員加入 + public function member_add($data) + { + // 會員車輛基本資料檔 + // $data['start_date'] = "{$data['start_date']} 00:00:00"; + // $data['end_date'] = "{$data['end_date']} 23:59:59"; + $old_lpr = $data['old_lpr']; + unset($data['old_lpr']); + $data_car = array + ( + 'lpr' => $data['lpr'], + 'lpr_correct' => $data['lpr'], + 'etag' => $data['etag'], + 'station_no' => $data['station_no'], + 'start_time' => $data['start_date'], + 'end_time' => $data['end_date'] + ); + + $check_member_no = $data['member_no']; + unset($data['member_no']); + trigger_error("members:".print_r($data, true)."car:".print_r($data_car, true)); + if ($check_member_no == 0) // 新增一筆會員資料 + { + $this->db->insert('members', $data); + $data_car['member_no'] = $this->db->insert_id(); + $this->db->insert('member_car', $data_car); + } + else // update會員資料 + { + $this->db->update('members', $data, array('member_no' => $check_member_no)); + if ($old_lpr == $data['lpr']) // 沒有異動到車牌, 使用update, 否則重建一筆 + { + unset($data_car['lpr']); + unset($data_car['lpr_correct']); + + $this->db->update('member_car', $data_car, array('member_no' => $check_member_no)); + } + else + { + $this->db->delete('member_car', array('member_no' => $check_member_no)); + $data_car['member_no'] = $check_member_no; + $this->db->insert('member_car', $data_car); + } + } + + return true; + } + + + // 查詢車牌是否重複 + public function check_lpr($lpr) + { + $rows = $this->db->select('count(*) as counts') + ->from('members') + ->where(array('lpr' => $lpr)) + ->get() + ->row_array(); + + return $rows['counts']; + } + + + // 月租會員查詢 + public function member_query() + { + $results = $this->db->select('member_no, lpr, etag, member_name, mobile_no, start_date, end_date, contract_no, amt, member_id, tel_h, tel_o, addr, valid_time, station_no') + ->from('members') + ->order_by('station_no, lpr', 'asc') + ->get() + ->result_array(); + + return $results; + } + + + // 刪除月租會員 + public function member_delete($member_no) + { + $this->db->delete('members', array('member_no' => $member_no)); + $this->db->delete('member_car', array('member_no' => $member_no)); + + return true; + } + + + // 進出場現況表 + public function cario_list() + { + /* + $data_cario = $this->db + ->select('c.cario_no, c.in_out, in_lane, out_lane, c.in_time, c.out_time, c.minutes, c.obj_id as lpr, c.etag, c.in_pic_name, c.out_pic_name, m.member_name as owner') + ->from('cario c') + ->join('members m', 'c.member_no = m.member_no', 'left') + ->where(array('c.err' => 0)) + ->order_by('c.update_time', 'desc') + ->limit(10) + ->get() + ->result_array(); + */ + $sql = '(select c.cario_no, c.in_out, in_lane, out_lane, c.in_time, c.out_time, c.minutes, c.obj_id as lpr, c.etag, c.in_pic_name, c.out_pic_name, m.member_name as owner, c.in_time as time_order + from cario c left join members m on c.obj_id = m.lpr + where c.err = 0 and c.out_time is null) + union + (select c.cario_no, c.in_out, in_lane, out_lane, c.in_time, c.out_time, c.minutes, c.obj_id as lpr, c.etag, c.in_pic_name, c.out_pic_name, m.member_name as owner, c.out_time as time_order + from cario c left join members m on c.obj_id = m.lpr + where c.err = 0 and c.out_time is not null) + order by time_order desc limit 10;'; + $data_cario = $this->db->query($sql)->result_array(); + + // $lane_arr = array(0 => '入1', 1 => '入2', 3 => '出3', 3 => '出4'); + $idx = 0; + foreach($data_cario as $rows) + { + ++$rows['in_lane']; + ++$rows['out_lane']; + + //$lane_no = $rows['in_out'] == 'CI' ? "入{$rows['in_lane']}" : "入{$rows['in_lane']} -> 出{$rows['out_lane']}"; + $lane_no = empty($rows['out_time']) ? "入{$rows['in_lane']}" : "入{$rows['in_lane']} -> 出{$rows['out_lane']}"; // 2016/08/22 有離場時間就顯示 + + $pic_name = str_replace('.jpg', '', empty($rows['out_pic_name']) ? $rows['in_pic_name'] : $rows['out_pic_name']); + $arr = explode('-', $pic_name); + $pic_path = APP_URL.'pics/'.substr($arr[7], 0, 8).'/'.$pic_name; + + $data[$idx] = array + ( + // 'io_name' => $io_name[$rows['in_out']], + 'io_name' => $lane_no, + 'lpr' => $rows['lpr'], + // 'etag' => $rows['etag'], + 'etag' => $rows['etag'], + 'owner' => $rows['owner'], + 'io_time' => empty($rows['out_time']) ? $rows['in_time'] : "{$rows['in_time']}(入)
{$rows['out_time']}(出)
{$rows['minutes']}分(停留時間)", + 'pic_name' => $pic_path + ); + ++$idx; + } + + return $data; + } + + // 模糊比對 + function getLevenshteinSQLStatement($word, $target) + { + $words = array(); + + if(strlen($word) >= 5) + { + for ($i = 0; $i < strlen($word); $i++) { + // insertions + $words[] = substr($word, 0, $i) . '_' . substr($word, $i); + // deletions + $words[] = substr($word, 0, $i) . substr($word, $i + 1); + // substitutions + //$words[] = substr($word, 0, $i) . '_' . substr($word, $i + 1); + } + } + else + { + for ($i = 0; $i < strlen($word); $i++) { + // insertions + $words[] = substr($word, 0, $i) . '_' . substr($word, $i); + } + } + + // last insertion + $words[] = $word . '_'; + //return $words; + + $fuzzy_statement = ' ('; + foreach ($words as $idx => $word) + { + $fuzzy_statement .= " {$target} LIKE '%{$word}%' OR "; + } + $last_or_pos = strrpos($fuzzy_statement, 'OR'); + if($last_or_pos !== false) + { + $fuzzy_statement = substr_replace($fuzzy_statement, ')', $last_or_pos, strlen('OR')); + } + + return $fuzzy_statement; + } + + // 車號入場查詢 + public function carin_lpr_query($word) + { + // updated 2016/09/09 fuzzy search + if(empty($word) || strlen($word) < 4 || strlen($word) > 10) + { + return array(); + } + $fuzzy_statement = $this->getLevenshteinSQLStatement($word, 'c.obj_id'); + trigger_error("模糊比對 {$word} where: {$fuzzy_statement}"); + + $sql = "SELECT c.cario_no, c.in_out, in_lane, out_lane, c.in_time, c.out_time, c.minutes, c.obj_id as lpr, c.etag, c.in_pic_name, c.out_pic_name, m.member_name as owner + FROM cario c + LEFT JOIN members m ON c.obj_id = m.lpr + WHERE {$fuzzy_statement} AND c.err = 0 AND c.obj_type = 1 + ORDER BY c.update_time DESC + LIMIT 50"; + $data_cario = $this->db->query($sql)->result_array(); + + /* + $data_cario = $this->db + ->select('c.cario_no, c.in_out, in_lane, out_lane, c.in_time, c.out_time, c.minutes, c.obj_id as lpr, c.etag, c.in_pic_name, c.out_pic_name, m.member_name as owner') + ->from('cario c') + ->join('members m', 'c.member_no = m.member_no', 'left') + ->where(array('c.obj_type' => 1, 'obj_id' => $lpr)) + ->order_by('c.update_time', 'desc') + ->get() + ->result_array(); + */ + + $data = array(); + $idx = 0; + foreach($data_cario as $rows) + { + ++$rows['in_lane']; + ++$rows['out_lane']; + + $lane_no = empty($rows['out_time']) ? "入{$rows['in_lane']}" : "入{$rows['in_lane']} -> 出{$rows['out_lane']}"; + $io_time = empty($rows['out_time']) ? $rows['in_time'] : "{$rows['in_time']}(入)
{$rows['out_time']}(出)
{$rows['minutes']}分(停留時間)"; + + $pic_name = str_replace('.jpg', '', empty($rows['out_pic_name']) ? $rows['in_pic_name'] : $rows['out_pic_name']); + $arr = explode('-', $pic_name); + $pic_path = APP_URL.'pics/'.substr($arr[7], 0, 8).'/'.$pic_name; + + $data[$idx++] = array + ( + 'io_name' => $lane_no, + 'lpr' => $rows['lpr'], + 'etag' => $rows['etag'], + 'owner' => empty($rows['owner']) ? '' : $rows['owner'], + 'io_time' => $io_time, + 'pic_name' => $pic_path + ); + } + + return $data; + } + + + + // 車號入場查詢 + public function carin_time_query($time_query, $minutes_range) + { + $curr_time = date('Y-m-d H:i:s'); + + $start_time = date('Y-m-d H:i:s', strtotime("{$time_query} - {$minutes_range} minutes")); + $end_time = date('Y-m-d H:i:s', strtotime("{$time_query} + {$minutes_range} minutes")); + + $data_cario = $this->db + ->select('c.cario_no, c.in_out, in_lane, out_lane, c.in_time, c.out_time, c.minutes, c.obj_id as lpr, c.etag, c.in_pic_name, c.out_pic_name, m.member_name as owner') + ->from('cario c') + ->join('members m', 'c.obj_id = m.lpr', 'left') + ->where(array('c.obj_type' => 1, 'c.in_time >=' => $start_time, 'c.in_time <=' => $end_time)) + ->order_by('c.update_time', 'desc') + ->get() + ->result_array(); + + $data = array(); + $idx = 0; + foreach($data_cario as $rows) + { + ++$rows['in_lane']; + ++$rows['out_lane']; + + $lane_no = empty($rows['out_time']) ? "入{$rows['in_lane']}" : "入{$rows['in_lane']} -> 出{$rows['out_lane']}"; + $io_time = empty($rows['out_time']) ? $rows['in_time'] : "{$rows['in_time']}(入)
{$rows['out_time']}(出)
{$rows['minutes']}分(停留時間)"; + + $pic_name = str_replace('.jpg', '', empty($rows['out_pic_name']) ? $rows['in_pic_name'] : $rows['out_pic_name']); + $arr = explode('-', $pic_name); + $pic_path = APP_URL.'pics/'.substr($arr[7], 0, 8).'/'.$pic_name; + + $data[$idx++] = array + ( + 'io_name' => $lane_no, + 'lpr' => $rows['lpr'], + 'etag' => $rows['etag'], + 'owner' => empty($rows['owner']) ? '' : $rows['owner'], + 'io_time' => $io_time, + 'pic_name' => $pic_path + ); + } + + return $data; + } + + // 時間長度轉成日時分秒 + public function time2str($d1, $d2) + { + $time = strtotime($d2) - strtotime($d1); + + $day_str = floor($time/3600/24); + $day_str = $day_str ? $day_str .= '天 ' : ''; + + $hour_str = floor($time%(24*3600)/3600); + $hour_str = $hour_str ? $hour_str .= '小時 ' : ''; + + $minute_str = floor($time%3600/60); + $minute_str = $minute_str ? $minute_str .= '分' : ''; + + /* + $second_str = $time%3600%60; + $second_str = $second_str ? $second_str .= ' seconds ' : ''; + + $n_time = floor($time/3600/24)."days".floor($time%(24*3600)/3600)."Hour".floor($time%3600/60)."Minute".($time%3600%60)."Second"; + */ + + $n_time = $day_str . $hour_str . $minute_str; + return $n_time; + } + + + // 在席車位檢查未有入場資料清單 + public function pks_check_list($max_rows) + { + $data = array(); + $data = $this->db + ->select('pksno, lpr, in_time, pic_name') + ->from('pks') + ->where(array('status' => 'LR', 'cario_no' => 0, 'confirms' => 0, 'station_no' => STATION_NO)) + ->order_by('in_time', 'desc') + ->limit($max_rows) + ->get() + ->result_array(); + return $data; + } + + + // 重設在席查核 + public function reset_pks_check() + { + // 讀出未查核過的資料 + $data_pks = $this->db + ->select('pksno, lpr, in_time') + ->from('pks') + ->where(array('status' => 'LR', 'cario_no' => 0, 'station_no' => STATION_NO)) + ->get() + ->result_array(); + + // $tot = $this->db->num_rows(); // 總筆數 + $tot = count($data_pks); // 總筆數 + + $num_cario = 0; // 入場資料筆數 + foreach($data_pks as $rows) + { + $lpr = $rows['lpr']; + if ($lpr == 'NONE') continue; // 車辨失敗者不處理 + + $pksno = $rows['pksno']; + $pks_in_time = $rows['in_time']; + // 讀取進場時間, 如讀不到資料, 以目前時間取代(add by TZUSS 2016-02-23) + $rows_cario = $this->db + ->select('cario_no, in_time') + ->from('cario') + ->where(array('in_out' => 'CI', 'obj_id' => $lpr, 'finished' => 0, 'err' => 0, 'station_no' => STATION_NO)) + ->order_by('cario_no', 'desc') + ->limit(1) + ->get() + ->row_array(); + // if ($this->db->num_rows() == 1) // 有入場資料 + if (!empty($rows_cario['cario_no'])) // 有入cario_no場資料 + { + $cario_no = $rows_cario['cario_no']; // 入場序號 + $in_time = $rows_cario['in_time']; + // 在席與入場資料相符, 分別在cario與pks記錄之 + $data = array + ( + 'pksno' => $pksno, + 'pks_time' => $pks_in_time + ); + $this->db->update('cario', $data, array('cario_no' => $cario_no, 'station_no' => STATION_NO)); + $data = array + ( + 'cario_no' => $cario_no, + 'in_time' => $in_time + ); + // 車號及照片檔名填入資料庫內 + $this->db->update('pks', $data, array('pksno' => $pksno, 'station_no' => STATION_NO)); + ++$num_cario; + } + + } + return array('tot' => $tot, 'tot_correct' => $num_cario); + } + + + // 更正在席車號 + public function correct_pks_lpr($pksno, $lpr) + { + + // 讀取進場時間, 如讀不到資料, 以目前時間取代(add by TZUSS 2016-02-23) + $rows_cario = $this->db + ->select('cario_no, in_time') + ->from('cario') + ->where(array('in_out' => 'CI', 'obj_id' => $lpr, 'finished' => 0, 'err' => 0, 'station_no' => STATION_NO)) + ->order_by('cario_no', 'desc') + ->limit(1) + ->get() + ->row_array(); + + if (!empty($rows_cario['cario_no'])) // 有cario_no入場資料 + { + $cario_no = $rows_cario['cario_no']; // 入場序號 + $in_time = $rows_cario['in_time']; + // 在席與入場資料相符, 分別在cario與pks記錄之 + $data = array + ( + 'pksno' => $pksno, + 'pks_time' => $in_time + ); + $this->db->update('cario', $data, array('cario_no' => $cario_no, 'station_no' => STATION_NO)); + $data = array + ( + 'confirms' => 1, + 'cario_no' => $cario_no, + 'lpr' => $lpr, + 'in_time' => $in_time + ); + // 車號及照片檔名填入資料庫內 + $this->db->update('pks', $data, array('pksno' => $pksno, 'station_no' => STATION_NO)); + $results = array + ( + 'err' => 0, + 'cario_no' => $cario_no + ); + } + else // 無入場資料 + { + $data = array + ( + 'confirms' => 1, + 'lpr' => $lpr + ); + + $this->db->update('pks', $data, array('pksno' => $pksno, 'station_no' => STATION_NO)); + $results = array + ( + 'err' => 0, + 'cario_no' => 0 // 車號查無入場資料 + ); + } + + return $results; + } + + + // 入場車號查核在席無資料清單 + public function carin_check_list($max_rows) + { + $data = array(); + $rows_cario = $this->db + ->select('cario_no, obj_id as lpr, in_time, member_no, in_pic_name') + ->from('cario') + ->where('in_out', 'CI') + ->where(array('pksno' => 0, 'finished' => 0, 'err' => 0, 'confirms' => 0, 'station_no' => STATION_NO, 'in_time <=' => 'date_sub(now(), interval 20 minute)'), null, false) + ->order_by('cario_no', 'desc') + ->limit($max_rows) + ->get() + ->result_array(); + + $idx = 0; + foreach($rows_cario as $rows) + { + $data[$idx++] = array + ( + 'cario_no' => $rows['cario_no'], + 'lpr' => $rows['lpr'], + 'in_time' => $rows['in_time'], + 'type' => $rows['member_no'] == 0 ? '' : '月租', + 'pic_name' => str_replace('-', '', substr($rows['in_time'], 0, 10)) . '/' . $rows['in_pic_name'] + ); + } + return $data; + } + + + + // 更正入場車號 + public function correct_carin_lpr($cario_no, $lpr, $in_time) + { + $rows = $this->db + ->select('pksno, cario_no, in_time') + ->from('pks') + ->where(array('status' => 'LR', 'lpr' => $lpr, 'confirms' => 0, 'station_no' => STATION_NO)) + ->limit(1) + ->get() + ->row_array(); + // 如果在席資料相符 + if (!empty($rows['pksno'])) + { + $pksno = $rows['pksno']; + + $data = array + ( + 'cario_no' => $cario_no, + 'in_time' => $in_time + ); + + $this->db->update('pks', $data, array('pksno' => $pksno, 'station_no' => STATION_NO)); + + $data_cario = array + ( + 'obj_id' => $lpr, + 'pksno' => $pksno, + 'pks_time' => $in_time, + 'confirms' => 1 + ); + } + else // 無在席資料 + { + $data_cario = array + ( + 'obj_id' => $lpr, + 'pksno' => 0, + 'confirms' => 1 + ); + } + + $this->db->update('cario', $data_cario, array('cario_no' => $cario_no, 'station_no' => STATION_NO)); + + return(array('pksno' => $data_cario['pksno'])); + } + + // 查詢行動支付記錄 + public function tx_bill_query() + { + $sql = "SELECT + tx_bill.order_no as order_no, tx_bill.lpr as lpr, tx_bill.invoice_no as invoice_no, tx_bill.in_time as in_time, tx_bill.balance_time as balance_time, tx_bill.company_no as company_no, tx_bill.email as email, tx_bill.mobile as mobile, tx_bill.amt as amt, tx_bill.tx_time as tx_time, + allpay_feedback_log.rtn_msg as rtn_msg, allpay_feedback_log.payment_type as payment_type, + cario.out_before_time as out_before_time + FROM tx_bill + LEFT JOIN cario ON tx_bill.cario_no = cario.cario_no + LEFT JOIN allpay_feedback_log ON tx_bill.order_no = allpay_feedback_log.merchant_trade_no + WHERE tx_bill.status = 111 + ORDER BY tx_bill.tx_time DESC"; + + return $this->db->query($sql)->result_array(); + } + + // 查詢月租繳款機記錄 + public function tx_bill_ats_query() + { + $sql = "SELECT + tx_bill_ats.order_no as order_no, tx_bill_ats.lpr as lpr, tx_bill_ats.invoice_no as invoice_no, + tx_bill_ats.end_time as end_time, tx_bill_ats.next_start_time as next_start_time, tx_bill_ats.next_end_time as next_end_time, + tx_bill_ats.company_no as company_no, tx_bill_ats.email as email, tx_bill_ats.mobile as mobile, tx_bill_ats.amt as amt, + tx_bill_ats.remarks as remarks, tx_bill_ats.member_name as member_name, tx_bill_ats.tx_time as tx_time, + allpay_feedback_log.rtn_msg as rtn_msg, allpay_feedback_log.payment_type as payment_type + FROM tx_bill_ats + LEFT JOIN allpay_feedback_log ON tx_bill_ats.order_no = allpay_feedback_log.merchant_trade_no + WHERE tx_bill_ats.status = 111 + ORDER BY tx_bill_ats.tx_time DESC"; + + return $this->db->query($sql)->result_array(); + } + + // 查詢樓層在席群組 + public function pks_group_query() + { + $sql = "SELECT + pks_groups.station_no, pks_groups.group_name as group_name, pks_groups.tot as tot, pks_groups.parked as parked, pks_groups.availables as availables, pks_groups.group_id as group_id, pks_groups.renum as renum + FROM pks_groups + ORDER BY pks_groups.group_name * 1 DESC"; + + return $this->db->query($sql)->result_array(); + } + + // 送出至message queue(目前用mqtt) + public function mq_send($topic, $msg) + { + $this->vars['mqtt']->publish($topic, $msg, 0); + trigger_error("mqtt:{$topic}|{$msg}"); + } + +} diff --git a/models/Pks_model.php b/models/Pks_model.php index 585897e..23937c4 100644 --- a/models/Pks_model.php +++ b/models/Pks_model.php @@ -1,356 +1,359 @@ -load->database(); - } - - public function init($vars) - { - $this->vars = $vars; - } - - - // 車輛進出傳入車牌號碼 - public function pksio($parms) - { - switch($parms['io']) - { - case 'KL': // 車輛入席車辨(lpr)及圖檔 - if ($parms['lpr'] == 'NONE') // 在席車辨失敗, 不處理 - { - trigger_error('在席車辨失敗' . print_r($parms, true)); - return false; - } - - // 讀取在席資料(pks) - $rows_pks = $this->db - ->select('cario_no, lpr, status, confirms') - ->from('pks') - ->where(array('pksno' => $parms['pksno'], 'station_no' => $this->vars['station_no'])) - ->limit(1) - ->get() - ->row_array(); - - trigger_error('KL read pks:'.print_r($rows_pks, true)); - - // 如果已經人工確認或之前已比對有入場資料者, 則重覆再送來的車辨不予理會 - if ($rows_pks['confirms'] == 1 || $rows_pks['lpr'] == $parms['lpr']) - { - trigger_error('KL ignored:'.$rows_pks['lpr']); - return false; - } - /* - if ($rows_pks['cario_no'] != 0 || $rows_pks['confirms'] == 1 || $rows_pks['lpr'] == $parms['lpr']) - { - trigger_error('人工已確認或車號相同不更新pks:'.$rows_pks['lpr']); - return false; - } - */ - - // 讀取進場時間, 如讀不到資料, 以目前時間取代(add by TZUSS 2016-02-23) - $rows_cario = $this->db - ->select('cario_no, in_time') - ->from('cario') - ->where(array('in_out' => 'CI', 'obj_id' => $parms['lpr'], 'finished' => 0, 'err' => 0, 'station_no' => $this->vars['station_no'])) - ->order_by('cario_no', 'desc') - ->limit(1) - ->get() - ->row_array(); - if (!empty($rows_cario['cario_no'])) // 有入場資料 - { - $cario_no = $rows_cario['cario_no']; // 入場序號 - $in_time = $rows_cario['in_time']; - // 在席與入場資料相符, 分別在cario與pks記錄之 - $data_cario = array - ( - 'pksno' => $parms['pksno'], - 'pks_time' => date('Y-m-d H:i:s') - ); - $this->db->update('cario', $data_cario, array('cario_no' => $cario_no, 'station_no' => $this->vars['station_no'])); - } - else // 查無入場資料, 即時通知 - { - $cario_no = 0; - $in_time = date('Y-m-d H:i:s'); - $jdata = json_encode(array - ( - 'pksno' => $parms['pksno'], - 'lpr' => $parms['lpr'], - 'in_time' => $in_time, - 'pic_name' => $parms['pic_name'] - ), JSON_UNESCAPED_UNICODE); - // $this->vars['mqtt']-lish('PKS_WITHOUT_IN', "{$jdata}", 0); // 待web完成 ??? - trigger_error('在席無進場資料:'. print_r($parms, true)); - } - - // 車入格後的車牌辨識(lpr), 傅送圖檔 - array_map('unlink', glob(PKS_PIC."pks-{$parms['pksno']}-*.jpg")); // 刪除舊照片 - $config['upload_path'] = PKS_PIC; - $config['allowed_types'] = 'gif|jpg|png'; - // ex. pks-2016-1625AB-1-2015080526.jpg -> pks-車位編號-車號-設備編號-時間.jpg - $config['file_name'] = "pks-{$parms['pksno']}-{$parms['lpr']}-{$parms['ivsno']}-{$this->vars['time_num']}.jpg"; - $this->load->library('upload', $config); - - $parms['pic_name'] = $config['file_name']; - if($this->upload->do_upload('cars')) - { - // 若無錯誤,則上傳檔案 - $file = $this->upload->data('cars'); - } - else - { - trigger_error('入席傳檔錯誤:'. print_r($parms, true)); - } - - $data = array - ( - 'cario_no' => $cario_no, - 'lpr' => $parms['lpr'], - 'status' => 'LR', // 車格佔用並有車號 - 'confirms' => 0, // 預設人工未確認 - 'pic_name' => $parms['pic_name'], - 'in_time' => $in_time - ); - // 車號及照片檔名填入資料庫內 - $this->db->update('pks', $data, array('pksno' => $parms['pksno'], 'station_no' => $this->vars['station_no'])); - break; - - case 'KI': // 車輛入席, 各區空車位與佔位各加減1 - $rows = $this->db->select('status') - ->from('pks') - ->where(array('pksno' => $parms['pksno'], 'station_no' => $this->vars['station_no'])) - ->get() - ->row_array(); - // if (!empty($rows['status']) && $rows['status'] == 'LR') break; // 仍有車在席, 不應再有KI, ignore - if (!empty($rows['status']) && $rows['status'] == 'LR') return true; // 仍有車在席, 不應再有KI, ignore - - $data = array - ( - 'cario_no' => 0, - 'lpr' => '', - 'status' => 'OC', // 車格佔用但尚無車號 - 'confirms' => 0, - 'pic_name' => '', - 'in_time' => null - ); - $this->db->update('pks', $data, array('pksno' => $parms['pksno'], 'station_no' => $this->vars['station_no'])); - break; - - case 'KO': // 車輛離席, 各區空車位與佔位各加減1 - $data = array - ( - 'cario_no' => 0, - 'lpr' => '', - 'status' => 'VA', // 車格佔用但尚無車號 - 'confirms' => 0, - 'pic_name' => '', - 'in_time' => null - ); - $this->db->update('pks', $data, array('pksno' => $parms['pksno'], 'station_no' => $this->vars['station_no'])); - break; - } - - /* - // 找出與與此車位相關的群組 - $sql = "select group_id, tot, renum - from pks_groups - where group_id in - (select group_id from pks_group_member where station_no = {$this->vars['station_no']} and pksno = {$parms['pksno']})"; - - $retults = $this->db->query($sql)->result_array(); - - foreach ($retults as $rows) - { - // 計算群組異動後的空車位數, 先讀出已停車位數 - $sql = "select count(*) as parked from pks where status != 'VA' and pksno in (select pksno from pks_group_member where group_id = '{$rows['group_id']}')"; - $row_group = $this->db->query($sql)->row_array(); - $group_va = $rows['tot'] + $rows['renum'] - $row_group['parked']; // 群組空車位數 - $this->db->update('pks_groups', array('parked' => $row_group['parked'], 'availables' => $group_va), array('group_id' => $rows['group_id'])); - - get_headers("http://192.168.51.15/set_num.php?group_id={$rows['group_id']}&num={$group_va}"); - // $this->vars['mqtt']->publish("VA-{$rows['group_id']}", "{$group_va}", 0); // 送出剩餘車位數給字幕機 - // 總車位數暫無需處理 - } - */ - // 找出與與此車位相關的群組 - $sql = "select group_id, tot, renum, availables - from pks_groups - where group_id in - (select group_id from pks_group_member where station_no = {$this->vars['station_no']} and pksno = {$parms['pksno']})"; - - $retults = $this->db->query($sql)->result_array(); - - foreach ($retults as $rows) - { - // 計算群組異動後的空車位數, 先讀出已停車位數 - $sql = "select count(*) as parked from pks where status != 'VA' and pksno in (select pksno from pks_group_member where group_id = '{$rows['group_id']}')"; - $row_group = $this->db->query($sql)->row_array(); - $group_va = $rows['tot'] + $rows['renum'] - $row_group['parked']; // 群組空車位數 - - // 有變動才處理更新 - if($rows['availables'] != $group_va) - { - // 防止負值 - if($group_va < 0){ - $group_va = 0; - } - - $group_va_pad = str_pad($group_va, 3, '0', STR_PAD_LEFT); // 補零 - $this->db->update('pks_groups', array('parked' => $row_group['parked'], 'availables' => $group_va), array('group_id' => $rows['group_id'])); - - $this->vars['mqtt']->publish(MQ_TOPIC_SUBLEVEL, "{$rows['group_id']},{$group_va_pad}", 0); // 送出剩餘車位數給字幕機 - // 總車位數暫無需處理 - - // 七樓無在席, 手動或用猜的 - /* - $f7_total = 74; - $sql = "select renum from pks_groups where group_id = 'F7'"; - $row_group = $this->db->query($sql)->row_array(); - $f7_renum = $row_group['renum']; - $total_parked_sql = "select count(cario_no) as parked - from cario where - cario.in_time > DATE_SUB(CURRENT_TIMESTAMP, INTERVAL 5 DAY) - and cario.finished = 0 - and cario.err = 0 - and cario.in_out = 'CI' - and cario.out_time is null"; - $total_parked_row_group = $this->db->query($total_parked_sql)->row_array(); - $sql = "select count(pksno) as parked from pks where status != 'VA'"; - $row_group = $this->db->query($sql)->row_array(); - $f7_mqtt = $total_parked_row_group['parked'] - $row_group['parked']; - if($f7_mqtt > $f7_total){ - $f7_mqtt = $f7_total; - }else if($f7_mqtt <= 0){ - $f7_mqtt = 0; - } - $this->db->update('pks_groups', array('parked' => $f7_mqtt, 'availables' => $f7_total - $f7_mqtt + $f7_renum), array('group_id' => 'F7')); - $f7_mqtt_pad = str_pad($f7_total - $f7_mqtt + $f7_renum, 3, '0', STR_PAD_LEFT); - - $this->vars['mqtt']->publish(MQ_TOPIC_SUBLEVEL, "F7,{$f7_mqtt_pad}", 0); // F7 MQTT - */ - } - } - } - - - // 重新計算 - public function reculc() - { - // 找出與與此車位相關的群組 - $sql = "select group_id, tot, renum - from pks_groups"; - - $retults = $this->db->query($sql)->result_array(); - - foreach ($retults as $rows) - { - // 計算群組異動後的空車位數, 先讀出已停車位數 - $sql = "select count(*) as parked from pks where status != 'VA' and pksno in (select pksno from pks_group_member where group_id = '{$rows['group_id']}')"; - $row_group = $this->db->query($sql)->row_array(); - $group_va = $rows['tot'] + $rows['renum'] - $row_group['parked']; // 群組空車位數 - $this->db->update('pks_groups', array('parked' => $row_group['parked'], 'availables' => $group_va), array('group_id' => $rows['group_id'])); - - // $this->vars['mqtt']->publish("VA-{$rows['group_id']}", "{$group_va}", 0); // 送出剩餘車位數給字幕機 - - get_headers("http://192.168.51.15/set_num.php?group_id={$rows['group_id']}&num={$group_va}"); - - echo "group_id:{$rows['group_id']}, tot:{$rows['tot']}, availables:{$group_va}, parked:{$row_group['parked']}, renum:{$rows['renum']}
"; - } - } - - // 取得所有車位使用狀態 - public function query_station_status($station_no) - { - /* 沒有group_id, pks不能直接用, 要多撈兩張表 - - $sql = "select pksno, posx, posy, in_time - FROM pks - WHERE station_no = '".$station_no."' and lpr != ''"; - */ - $sql = "SELECT pks.pksno AS pksno, pks.posx AS posx, pks.posy AS posy, pks.in_time AS in_time, - pks_groups.group_id AS group_id - FROM pks - LEFT JOIN pks_group_member ON pks.pksno = pks_group_member.pksno AND pks.station_no = pks_group_member.station_no - LEFT JOIN pks_groups ON pks_group_member.group_id = pks_groups.group_id - WHERE pks.lpr != '' AND pks.station_no = '".$station_no."' AND pks_groups.group_type = '1' "; - - $retults = $this->db->query($sql)->result_array(); - - $currentTime = new DateTime("now"); - foreach ($retults as $idx => $rows) - { - $startTime = new DateTime($rows['in_time']); // 進場時間 - $interval = $startTime->diff($currentTime); - $status = $this->gen_pks_s($interval); // 一般:0, 隔日:1, 超過3日:3, 隔週:7, 隔20日:20 - - $data['result'][$idx] = array - ( - 'g'=> $rows['group_id'], - 'id'=> $rows['pksno'], - 'x' => $rows['posx'], - 'y' => $rows['posy'], - 's' => $status - ); - } - return $data; - } - - // 取得車位狀態 - private function gen_pks_s($interval) - { - $status = 0; // 一般:0, 隔日:1, 超過3日:3, 隔週:7, 隔20日:20 - if($interval->y > 0 || $interval->m > 0 || $interval->d >= 20){ - $status = 20; - }else if($interval->d >= 7){ - $status = 7; - }else if($interval->d >= 3){ - $status = 3; - }else if($interval->d >= 1){ - $status = 1; - } - return $status; - } - - // 取得指定車位使用狀態 - public function query_station_pks($station_no, $pksno) - { - $sql = "SELECT pks.pksno AS pksno, pks.lpr AS lpr, pks.in_time AS in_time, pks.station_no AS station_no, - pks_groups.group_id AS group_id, pks_groups.group_name AS group_name, pks_groups.group_type AS type - FROM pks - LEFT JOIN pks_group_member ON pks.pksno = pks_group_member.pksno AND pks.station_no = pks_group_member.station_no - LEFT JOIN pks_groups ON pks_group_member.group_id = pks_groups.group_id - WHERE pks.pksno = '".$pksno."' AND pks.station_no = '".$station_no."' AND pks_groups.group_type = '1' "; - - $retults = $this->db->query($sql)->result_array(); - - $currentTime = new DateTime("now"); - foreach ($retults as $idx => $rows) - { - $startTime = new DateTime($rows['in_time']); // 進場時間 - $interval = $startTime->diff($currentTime); - $status = $this->gen_pks_s($interval); // 一般:0, 隔日:1, 超過3日:3, 隔週:7, 隔20日:20 - - $data['result'][$idx] = array - ( - 'pksno'=> $rows['pksno'], - 'lpr' => $rows['lpr'], - 'time' => $rows['in_time'], - 'station_no' => $rows['station_no'], - 'group_id' => $rows['group_id'], - 'group_name' => $rows['group_name'], - 'status' => $status - ); - } - return $data; - } - -} +load->database(); + } + + public function init($vars) + { + $this->vars = $vars; + } + + + // 車輛進出傳入車牌號碼 + public function pksio($parms) + { + switch($parms['io']) + { + case 'KL': // 車輛入席車辨(lpr)及圖檔 + if ($parms['lpr'] == 'NONE') // 在席車辨失敗, 不處理 + { + trigger_error('在席車辨失敗' . print_r($parms, true)); + return false; + } + + // 讀取在席資料(pks) + $rows_pks = $this->db + ->select('cario_no, lpr, status, confirms') + ->from('pks') + ->where(array('pksno' => $parms['pksno'], 'station_no' => $parms['sno'])) + ->limit(1) + ->get() + ->row_array(); + + trigger_error('KL read pks:'.print_r($rows_pks, true)); + + // 如果已經人工確認或之前已比對有入場資料者, 則重覆再送來的車辨不予理會 + if ($rows_pks['confirms'] == 1 || $rows_pks['lpr'] == $parms['lpr']) + { + trigger_error('KL ignored:'.$rows_pks['lpr']); + return false; + } + /* + if ($rows_pks['cario_no'] != 0 || $rows_pks['confirms'] == 1 || $rows_pks['lpr'] == $parms['lpr']) + { + trigger_error('人工已確認或車號相同不更新pks:'.$rows_pks['lpr']); + return false; + } + */ + + // 讀取進場時間, 如讀不到資料, 以目前時間取代(add by TZUSS 2016-02-23) + $rows_cario = $this->db + ->select('cario_no, in_time') + ->from('cario') + ->where(array('in_out' => 'CI', 'obj_id' => $parms['lpr'], 'finished' => 0, 'err' => 0, 'station_no' => $parms['sno'])) + ->order_by('cario_no', 'desc') + ->limit(1) + ->get() + ->row_array(); + if (!empty($rows_cario['cario_no'])) // 有入場資料 + { + $cario_no = $rows_cario['cario_no']; // 入場序號 + $in_time = $rows_cario['in_time']; + // 在席與入場資料相符, 分別在cario與pks記錄之 + $data_cario = array + ( + 'pksno' => $parms['pksno'], + 'pks_time' => date('Y-m-d H:i:s') + ); + $this->db->update('cario', $data_cario, array('cario_no' => $cario_no, 'station_no' => $parms['sno'])); + } + else // 查無入場資料, 即時通知 + { + $cario_no = 0; + $in_time = date('Y-m-d H:i:s'); + + /* + $jdata = json_encode(array + ( + 'pksno' => $parms['pksno'], + 'lpr' => $parms['lpr'], + 'in_time' => $in_time + ,'pic_name' => $parms['pic_name'] + ), JSON_UNESCAPED_UNICODE); + */ + // $this->vars['mqtt']-lish('PKS_WITHOUT_IN', "{$jdata}", 0); // 待web完成 ??? + trigger_error('在席無進場資料:'. print_r($parms, true)); + } + + // 車入格後的車牌辨識(lpr), 傅送圖檔 + array_map('unlink', glob(PKS_PIC."pks-{$parms['pksno']}-*.jpg")); // 刪除舊照片 + $config['upload_path'] = PKS_PIC; + $config['allowed_types'] = 'gif|jpg|png'; + // ex. pks-2016-1625AB-1-2015080526.jpg -> pks-車位編號-車號-設備編號-時間.jpg + $config['file_name'] = "pks-{$parms['pksno']}-{$parms['lpr']}-{$parms['ivsno']}-{$this->vars['time_num']}.jpg"; + $this->load->library('upload', $config); + + $parms['pic_name'] = $config['file_name']; + if($this->upload->do_upload('cars')) + { + // 若無錯誤,則上傳檔案 + $file = $this->upload->data('cars'); + } + else + { + trigger_error('入席傳檔錯誤:'. print_r($parms, true)); + } + + $data = array + ( + 'cario_no' => $cario_no, + 'lpr' => $parms['lpr'], + 'status' => 'LR', // 車格佔用並有車號 + 'confirms' => 0, // 預設人工未確認 + 'pic_name' => $parms['pic_name'], + 'in_time' => $in_time + ); + // 車號及照片檔名填入資料庫內 + $this->db->update('pks', $data, array('pksno' => $parms['pksno'], 'station_no' => $parms['sno'])); + break; + + case 'KI': // 車輛入席, 各區空車位與佔位各加減1 + $rows = $this->db->select('status') + ->from('pks') + ->where(array('pksno' => $parms['pksno'], 'station_no' => $parms['sno'])) + ->get() + ->row_array(); + // if (!empty($rows['status']) && $rows['status'] == 'LR') break; // 仍有車在席, 不應再有KI, ignore + if (!empty($rows['status']) && $rows['status'] == 'LR') return true; // 仍有車在席, 不應再有KI, ignore + + $data = array + ( + 'cario_no' => 0, + 'lpr' => '', + 'status' => 'OC', // 車格佔用但尚無車號 + 'confirms' => 0, + 'pic_name' => '', + 'in_time' => null + ); + $this->db->update('pks', $data, array('pksno' => $parms['pksno'], 'station_no' => $parms['sno'])); + break; + + case 'KO': // 車輛離席, 各區空車位與佔位各加減1 + $data = array + ( + 'cario_no' => 0, + 'lpr' => '', + 'status' => 'VA', // 車格佔用但尚無車號 + 'confirms' => 0, + 'pic_name' => '', + 'in_time' => null + ); + $this->db->update('pks', $data, array('pksno' => $parms['pksno'], 'station_no' => $parms['sno'])); + break; + } + + /* + // 找出與與此車位相關的群組 + $sql = "select group_id, tot, renum + from pks_groups + where group_id in + (select group_id from pks_group_member where station_no = {$this->vars['station_no']} and pksno = {$parms['pksno']})"; + + $retults = $this->db->query($sql)->result_array(); + + foreach ($retults as $rows) + { + // 計算群組異動後的空車位數, 先讀出已停車位數 + $sql = "select count(*) as parked from pks where status != 'VA' and pksno in (select pksno from pks_group_member where group_id = '{$rows['group_id']}')"; + $row_group = $this->db->query($sql)->row_array(); + $group_va = $rows['tot'] + $rows['renum'] - $row_group['parked']; // 群組空車位數 + $this->db->update('pks_groups', array('parked' => $row_group['parked'], 'availables' => $group_va), array('group_id' => $rows['group_id'])); + + get_headers("http://192.168.51.15/set_num.php?group_id={$rows['group_id']}&num={$group_va}"); + // $this->vars['mqtt']->publish("VA-{$rows['group_id']}", "{$group_va}", 0); // 送出剩餘車位數給字幕機 + // 總車位數暫無需處理 + } + */ + // 找出與與此車位相關的群組 + $sql = "select group_id, tot, renum, availables + from pks_groups + where group_id in + (select group_id from pks_group_member where station_no = {$parms['sno']} and pksno = {$parms['pksno']})"; + + $retults = $this->db->query($sql)->result_array(); + + foreach ($retults as $rows) + { + // 計算群組異動後的空車位數, 先讀出已停車位數 + $sql = "select count(*) as parked from pks where status != 'VA' and pksno in (select pksno from pks_group_member where group_id = '{$rows['group_id']}')"; + $row_group = $this->db->query($sql)->row_array(); + $group_va = $rows['tot'] + $rows['renum'] - $row_group['parked']; // 群組空車位數 + + // 有變動才處理更新 + if($rows['availables'] != $group_va) + { + // 防止負值 + if($group_va < 0){ + $group_va = 0; + } + + $group_va_pad = str_pad($group_va, 3, '0', STR_PAD_LEFT); // 補零 + $this->db->update('pks_groups', array('parked' => $row_group['parked'], 'availables' => $group_va), array('group_id' => $rows['group_id'])); + + $this->vars['mqtt']->publish(MQ_TOPIC_SUBLEVEL, "{$rows['group_id']},{$group_va_pad}", 0); // 送出剩餘車位數給字幕機 + // 總車位數暫無需處理 + + // 七樓無在席, 手動或用猜的 + /* + $f7_total = 74; + $sql = "select renum from pks_groups where group_id = 'F7'"; + $row_group = $this->db->query($sql)->row_array(); + $f7_renum = $row_group['renum']; + $total_parked_sql = "select count(cario_no) as parked + from cario where + cario.in_time > DATE_SUB(CURRENT_TIMESTAMP, INTERVAL 5 DAY) + and cario.finished = 0 + and cario.err = 0 + and cario.in_out = 'CI' + and cario.out_time is null"; + $total_parked_row_group = $this->db->query($total_parked_sql)->row_array(); + $sql = "select count(pksno) as parked from pks where status != 'VA'"; + $row_group = $this->db->query($sql)->row_array(); + $f7_mqtt = $total_parked_row_group['parked'] - $row_group['parked']; + if($f7_mqtt > $f7_total){ + $f7_mqtt = $f7_total; + }else if($f7_mqtt <= 0){ + $f7_mqtt = 0; + } + $this->db->update('pks_groups', array('parked' => $f7_mqtt, 'availables' => $f7_total - $f7_mqtt + $f7_renum), array('group_id' => 'F7')); + $f7_mqtt_pad = str_pad($f7_total - $f7_mqtt + $f7_renum, 3, '0', STR_PAD_LEFT); + + $this->vars['mqtt']->publish(MQ_TOPIC_SUBLEVEL, "F7,{$f7_mqtt_pad}", 0); // F7 MQTT + */ + } + } + } + + + // 重新計算 + public function reculc() + { + // 找出與與此車位相關的群組 + $sql = "select group_id, tot, renum + from pks_groups"; + + $retults = $this->db->query($sql)->result_array(); + + foreach ($retults as $rows) + { + // 計算群組異動後的空車位數, 先讀出已停車位數 + $sql = "select count(*) as parked from pks where status != 'VA' and pksno in (select pksno from pks_group_member where group_id = '{$rows['group_id']}')"; + $row_group = $this->db->query($sql)->row_array(); + $group_va = $rows['tot'] + $rows['renum'] - $row_group['parked']; // 群組空車位數 + $this->db->update('pks_groups', array('parked' => $row_group['parked'], 'availables' => $group_va), array('group_id' => $rows['group_id'])); + + // $this->vars['mqtt']->publish("VA-{$rows['group_id']}", "{$group_va}", 0); // 送出剩餘車位數給字幕機 + + get_headers("http://192.168.51.15/set_num.php?group_id={$rows['group_id']}&num={$group_va}"); + + echo "group_id:{$rows['group_id']}, tot:{$rows['tot']}, availables:{$group_va}, parked:{$row_group['parked']}, renum:{$rows['renum']}
"; + } + } + + // 取得所有車位使用狀態 + public function query_station_status($station_no) + { + /* 沒有group_id, pks不能直接用, 要多撈兩張表 + + $sql = "select pksno, posx, posy, in_time + FROM pks + WHERE station_no = '".$station_no."' and lpr != ''"; + */ + $sql = "SELECT pks.pksno AS pksno, pks.posx AS posx, pks.posy AS posy, pks.in_time AS in_time, + pks_groups.group_id AS group_id + FROM pks + LEFT JOIN pks_group_member ON pks.pksno = pks_group_member.pksno AND pks.station_no = pks_group_member.station_no + LEFT JOIN pks_groups ON pks_group_member.group_id = pks_groups.group_id + WHERE pks.lpr != '' AND pks.station_no = '".$station_no."' AND pks_groups.group_type = '1' "; + + $retults = $this->db->query($sql)->result_array(); + + $currentTime = new DateTime("now"); + foreach ($retults as $idx => $rows) + { + $startTime = new DateTime($rows['in_time']); // 進場時間 + $interval = $startTime->diff($currentTime); + $status = $this->gen_pks_s($interval); // 一般:0, 隔日:1, 超過3日:3, 隔週:7, 隔20日:20 + + $data['result'][$idx] = array + ( + 'g'=> $rows['group_id'], + 'id'=> $rows['pksno'], + 'x' => $rows['posx'], + 'y' => $rows['posy'], + 's' => $status + ); + } + return $data; + } + + // 取得車位狀態 + private function gen_pks_s($interval) + { + $status = 0; // 一般:0, 隔日:1, 超過3日:3, 隔週:7, 隔20日:20 + if($interval->y > 0 || $interval->m > 0 || $interval->d >= 20){ + $status = 20; + }else if($interval->d >= 7){ + $status = 7; + }else if($interval->d >= 3){ + $status = 3; + }else if($interval->d >= 1){ + $status = 1; + } + return $status; + } + + // 取得指定車位使用狀態 + public function query_station_pks($station_no, $pksno) + { + $sql = "SELECT pks.pksno AS pksno, pks.lpr AS lpr, pks.in_time AS in_time, pks.station_no AS station_no, + pks_groups.group_id AS group_id, pks_groups.group_name AS group_name, pks_groups.group_type AS type + FROM pks + LEFT JOIN pks_group_member ON pks.pksno = pks_group_member.pksno AND pks.station_no = pks_group_member.station_no + LEFT JOIN pks_groups ON pks_group_member.group_id = pks_groups.group_id + WHERE pks.pksno = '".$pksno."' AND pks.station_no = '".$station_no."' AND pks_groups.group_type = '1' "; + + $retults = $this->db->query($sql)->result_array(); + + $currentTime = new DateTime("now"); + foreach ($retults as $idx => $rows) + { + $startTime = new DateTime($rows['in_time']); // 進場時間 + $interval = $startTime->diff($currentTime); + $status = $this->gen_pks_s($interval); // 一般:0, 隔日:1, 超過3日:3, 隔週:7, 隔20日:20 + + $data['result'][$idx] = array + ( + 'pksno'=> $rows['pksno'], + 'lpr' => $rows['lpr'], + 'time' => $rows['in_time'], + 'station_no' => $rows['station_no'], + 'group_id' => $rows['group_id'], + 'group_name' => $rows['group_name'], + 'status' => $status + ); + } + return $data; + } + +} diff --git a/models/Qcar2_model.php b/models/Qcar2_model.php new file mode 100644 index 0000000..c5462a2 --- /dev/null +++ b/models/Qcar2_model.php @@ -0,0 +1,144 @@ +load->database(); + } + + // 查車 + public function q_pks($lpr) + { + $sql = "select p.pksno, p.pic_name, p.update_time, p.in_time, p.posx, p.posy, m.group_id, g.group_name, g.floors + from pks p, pks_group_member m, pks_groups g + where p.pksno = m.pksno + and m.group_id = g.group_id + and g.group_type = 1 + and p.lpr = '{$lpr}' + limit 1"; + $rows = $this->db->query($sql)->row_array(); + + //if (!empty($rows['pic_name'])) $rows['pic_name'] = str_replace('.jpg', '', $rows['pic_name']); + //else $rows['pksno'] = 0; // 如無該筆資料, 車位號碼設為0 + + return $rows; + } + + // 模糊比對 + function getLevenshteinSQLStatement($word, $target) + { + $words = array(); + + if(strlen($word) >= 5) + { + for ($i = 0; $i < strlen($word); $i++) { + // insertions + $words[] = substr($word, 0, $i) . '_' . substr($word, $i); + // deletions + $words[] = substr($word, 0, $i) . substr($word, $i + 1); + // substitutions + //$words[] = substr($word, 0, $i) . '_' . substr($word, $i + 1); + } + } + else + { + for ($i = 0; $i < strlen($word); $i++) { + // insertions + $words[] = substr($word, 0, $i) . '_' . substr($word, $i); + } + } + + // last insertion + $words[] = $word . '_'; + //return $words; + + $fuzzy_statement = ' ('; + foreach ($words as $idx => $word) + { + $fuzzy_statement .= " {$target} LIKE '%{$word}%' OR "; + } + $last_or_pos = strrpos($fuzzy_statement, 'OR'); + if($last_or_pos !== false) + { + $fuzzy_statement = substr_replace($fuzzy_statement, ')', $last_or_pos, strlen('OR')); + } + + return $fuzzy_statement; + } + + // 取得進場資訊 (模糊比對) + public function q_fuzzy_pks($word) + { + if(empty($word) || strlen($word) <= 0 || strlen($word) > 10) + { + return null; + } + + $sql = "SELECT station_no, lpr, in_time, pic_name as pks_pic_name + FROM pks + WHERE {$this->getLevenshteinSQLStatement($word, 'lpr')} + ORDER BY lpr ASC"; + $retults = $this->db->query($sql)->result_array(); + + if(count($retults) > 0) + { + foreach ($retults as $idx => $rows) + { + $pks_pic_path = ''; + if(!empty($rows['pks_pic_name'])) + { + //$pks_pic_path = APP_URL.'pks_pics/'.str_replace('.jpg', '', $rows['pks_pic_name']); + $pks_pic_path = SERVER_URL.'pkspic/'.$rows['pks_pic_name']; + } + + $data['result'][$idx] = array + ( + 'lpr'=> $rows['lpr'], + 'pks_pic_path' => $pks_pic_path, + 'station_no' => $rows['station_no'], + 'in_time' => $rows['in_time'] + ); + } + } + else + { + // 讀取入場資料 + $sql = "SELECT cario.station_no as station_no, cario.obj_id as lpr, cario.in_time as in_time, cario.in_pic_name as pks_pic_name + FROM cario + WHERE {$this->getLevenshteinSQLStatement($word, 'obj_id')} + AND in_out = 'CI' AND finished = 0 AND err = 0 AND out_time IS NULL + ORDER BY lpr ASC"; + // AND in_time > DATE_SUB(CURRENT_TIMESTAMP, INTERVAL 5 DAY) + $retults = $this->db->query($sql)->result_array(); + + if(count($retults) > 0) + { + foreach ($retults as $idx => $rows) + { + $pks_pic_path = ''; + if(!empty($rows['pks_pic_name'])) + { + $pic_name = str_replace('.jpg', '', $rows['pks_pic_name']); + $arr = explode('-', $pic_name); + $pks_pic_path = SERVER_URL.'carspic/'.substr($arr[7], 0, 8).'/'.$pic_name.'.jpg'; + } + + $data['result'][$idx] = array + ( + 'lpr'=> $rows['lpr'], + 'pks_pic_path' => $pks_pic_path, + 'station_no' => $rows['station_no'], + 'in_time' => $rows['in_time'] + ); + } + } + } + return $data; + } + +} diff --git a/views/carpark/main_page.php b/views/carpark/main_page.php index 5372781..43f73bc 100644 --- a/views/carpark/main_page.php +++ b/views/carpark/main_page.php @@ -1,1787 +1,1787 @@ - - - - - - - - - 歐特儀管理系統 - - - - - - - - - - - - - - - - -
- - -
-
-
-

管理作業

-
- -
- - -
-
-
-
- 所有場站更新 -
-
-
-
-
-
- - -
-
- - -
- - - -
-
- -
- -
-
-
-
- -
- -
- -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- -
- - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/views/qcar2/main_page.php b/views/qcar2/main_page.php new file mode 100644 index 0000000..d0a7f78 --- /dev/null +++ b/views/qcar2/main_page.php @@ -0,0 +1,454 @@ + + + + + + + + + 歐特儀自動化服務機 + + + + + + + + + + + + + + + +
+ +
+
+
+

歐特儀自動化服務機

+
+ +
+ + + +
+
+
+
+ 車位查詢 +
+
+
+
+
+
+ +
+ +    + +    + +    + +
+
+
+ +
+ + +
+ +
+ +
+ +
+ + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +