VM暫存
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
3.0KB

  1. <?php
  2. // file: mqtt_service.php 接收 MQTT 轉發
  3. require_once('/home/bigbang/libs/phplibs/phpMQTT.php');
  4. require_once '/home/bigbang/apps/coworker/station.config.php';
  5. define('APP_NAME', 'mqtt_service'); // application name
  6. // 發生錯誤時集中在此處理
  7. function error_handler($errno, $errstr, $errfile, $errline, $errcontext)
  8. {
  9. //$str = date('H:i:s')."|{$errstr}|{$errfile}|{$errline}|{$errno}\n";
  10. $str = date('H:i:s')."|{$errstr}\n";
  11. echo $str;
  12. error_log($str, 3, LOG_PATH.APP_NAME . '.' . date('Ymd').'.log.txt'); // 3代表參考後面的檔名
  13. }
  14. set_error_handler('error_handler', E_ALL);
  15. trigger_error('..start..');
  16. // 共用記憶體
  17. $mcache = new Memcache;
  18. $mcache->pconnect('localhost', 11211) or die ('Could not connect memcache');
  19. // 取得 memcache settings
  20. $retry_count = 0;
  21. while(!$mcache->get('altob_station_settings'))
  22. {
  23. trigger_error("altob_station_settings..not_found..{$retry_count}");
  24. if($retry_count > 5)
  25. die ('Could not init altob_station_settings');
  26. $retry_count++;
  27. // call & retry
  28. $ch = curl_init();
  29. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  30. curl_setopt($ch, CURLOPT_POST, true); // 啟用POST
  31. curl_setopt($ch, CURLOPT_URL, 'http://localhost/carpark.html/station_setting_query/');
  32. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('reload' => 1)));
  33. $result = curl_exec($ch);
  34. trigger_error("..retry..curl:{$result}..");
  35. sleep(5);
  36. }
  37. $settings = $mcache->get('altob_station_settings');
  38. trigger_error("memcache['altob_station_settings'] = " . print_r($settings, true));
  39. // 取得第一個場站編號
  40. $station_no_str = $mcache->get('station_no_str');
  41. $station_no_arr = explode(',', $station_no_str);
  42. $first_station_no = $station_no_arr[0];
  43. trigger_error("station_no: {$first_station_no}");
  44. // 取得 mqtt 設定
  45. $mqtt_ip = isset($settings[$first_station_no]['mqtt_ip']) ? $settings[$first_station_no]['mqtt_ip']:'localhost';
  46. $mqtt_port = isset($settings[$first_station_no]['mqtt_port']) ? $settings[$first_station_no]['mqtt_port']:1883;
  47. trigger_error("mqtt: {$mqtt_ip}:{$mqtt_port}");
  48. // mqtt subscribe
  49. $mqtt = new phpMQTT($mqtt_ip, $mqtt_port, uniqid());
  50. if(!$mqtt->connect()){ die ('Could not connect mqtt'); }
  51. // 場站資料庫資訊
  52. $topics['#'] = array('qos'=> 0, 'function'=>'procmsg');
  53. $mqtt->subscribe($topics, 0);
  54. trigger_error("..mqtt subscribe..".print_r($topics, true));
  55. while($mqtt->proc()){ }
  56. $mqtt->close();
  57. function procmsg($topic, $msg)
  58. {
  59. trigger_error("..{$topic}|{$msg}..");
  60. $data = array('topic' => $topic, 'msg' => $msg, 'ck' => md5($topic.'altob'.$msg));
  61. if(in_array($topic, array('altob.888.mqtt')))
  62. {
  63. // dispatch
  64. $ch = curl_init();
  65. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  66. curl_setopt($ch, CURLOPT_POST, true); // 啟用POST
  67. curl_setopt($ch, CURLOPT_URL, 'http://localhost/carpark.html/mqtt_service/');
  68. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
  69. $result = curl_exec($ch);
  70. trigger_error("..curl|{$result}..");
  71. }
  72. }