From 922c01fe2ef63916c9ade59328f9c822fa89f716 Mon Sep 17 00:00:00 2001 From: "altob.rd" Date: Tue, 24 Oct 2017 16:36:57 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20mqtt=20=E8=BD=89=E7=99=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controllers/Carpark.php | 23 ++++++++++++- coworker/mqtt_service.php | 84 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 coworker/mqtt_service.php diff --git a/controllers/Carpark.php b/controllers/Carpark.php index d27a1e7..9f8ded6 100644 --- a/controllers/Carpark.php +++ b/controllers/Carpark.php @@ -139,7 +139,28 @@ class Carpark extends CI_Controller // // 接收端 (START) // - // ------------------------------------------------ + // ------------------------------------------------ + + // [mqtt] 接收端 + public function mqtt_service() + { + $topic = $this->input->post('topic', true); + $msg = $this->input->post('msg', true); + $ck = $this->input->post('ck', true); + + if(md5($topic.'altob'.$msg) != $ck) + { + echo 'ck_error'; + exit; + } + + trigger_error(__FUNCTION__ . "|{$topic}|{$msg}"); + + + + echo 'ok'; + exit; + } // [設定檔] 取得設定 public function station_setting_query() diff --git a/coworker/mqtt_service.php b/coworker/mqtt_service.php new file mode 100644 index 0000000..106fdfa --- /dev/null +++ b/coworker/mqtt_service.php @@ -0,0 +1,84 @@ +pconnect('localhost', 11211) or die ('Could not connect memcache'); + +// 取得 memcache settings +$retry_count = 0; +while(!$mcache->get('altob_station_settings')) +{ + trigger_error("altob_station_settings..not_found..{$retry_count}"); + + if($retry_count > 5) + die ('Could not init altob_station_settings'); + $retry_count++; + + // call & retry + $ch = curl_init(); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_POST, true); // 啟用POST + curl_setopt($ch, CURLOPT_URL, 'http://localhost/carpark.html/station_setting_query/'); + curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('reload' => 1))); + $result = curl_exec($ch); + trigger_error("..retry..curl:{$result}.."); + + sleep(5); +} +$settings = $mcache->get('altob_station_settings'); +trigger_error("memcache['altob_station_settings'] = " . print_r($settings, true)); + +// 取得第一個場站編號 +$station_no_str = $mcache->get('station_no_str'); +$station_no_arr = explode(',', $station_no_str); +$first_station_no = $station_no_arr[0]; +trigger_error("station_no: {$first_station_no}"); + +// 取得 mqtt 設定 +$mqtt_ip = isset($settings[$first_station_no]['mqtt_ip']) ? $settings[$first_station_no]['mqtt_ip']:'localhost'; +$mqtt_port = isset($settings[$first_station_no]['mqtt_port']) ? $settings[$first_station_no]['mqtt_port']:1883; +trigger_error("mqtt: {$mqtt_ip}:{$mqtt_port}"); + +// mqtt subscribe +$mqtt = new phpMQTT($mqtt_ip, $mqtt_port, uniqid()); +if(!$mqtt->connect()){ die ('Could not connect mqtt'); } + +// 場站資料庫資訊 +$topics['#'] = array('qos'=>2, 'function'=>'procmsg'); +$mqtt->subscribe($topics, 2); + +while($mqtt->proc()){ } +$mqtt->close(); + +function procmsg($topic, $msg) +{ + $data = array('topic' => $topic, 'msg' => $msg, 'ck' => md5($topic.'altob'.$msg)); + + // dispatch + $ch = curl_init(); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_POST, true); // 啟用POST + curl_setopt($ch, CURLOPT_URL, 'http://localhost/carpark.html/mqtt_service/'); + curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); + $result = curl_exec($ch); + trigger_error( __FUNCTION__ . "..{$topic}|{$msg}..curl:{$result}.."); +}