Jytgps.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. namespace Workerman\Protocols;
  3. class Jytgps {
  4. const LOCATION_FORMAT_FLOAT = 0;
  5. const LOCATION_FORMAT_RAW = 1;
  6. private static $locationFormat ;
  7. public static function str2bin( $text ){
  8. if (!is_string($text))
  9. return null;
  10. $arr = explode(' ',$text);
  11. $bin = '';
  12. foreach($arr as $hex){
  13. if(strlen($hex) == 2){
  14. $bin .= chr( hexdec($hex) );
  15. }
  16. }
  17. return $bin;
  18. }
  19. public static function input( $buffer, $connection ){
  20. $end = substr($buffer,-1);
  21. if($end != '#'){
  22. return 0;
  23. }
  24. return strlen($buffer);
  25. }
  26. public static function encode( $value ){
  27. //echo 'encode:'.PHP_EOL;
  28. //var_dump($value);
  29. return $value['head_char'].$value['identify'].$value['version'].'Y'.$value['function_code'].$value['function_keyword'].$value['end_char'];
  30. }
  31. public static function decode( $buffer ){
  32. /*
  33. function_code(终端--功能类指令): A-上传状态类信息 B-上传定位类信息
  34. function_keyword(功能项):
  35. */
  36. $length = strlen($buffer);
  37. $str = rtrim($buffer,"#");
  38. $arr = explode('#',$str);
  39. $decode = array();
  40. foreach($arr as $key=>$row){
  41. $buffer = $row.'#';
  42. $data = array();
  43. $data['head_char'] = substr($buffer,0,1);
  44. $data['identify'] = substr($buffer,1,2);
  45. $data['version'] = substr($buffer,3,2);
  46. $data['reply'] = substr($buffer,5,1);
  47. $index = strpos($buffer,',')+1;
  48. $data['device_id'] = substr($buffer,6,$index-1-6);
  49. $re = substr($buffer,$index,1);
  50. if($re == 'Y'){ //Y回应的数据包,功能码和关键词的起始位置和非回应的不一样
  51. $data['function_code'] = substr($buffer,$index+1,1);
  52. $data['function_keyword'] = substr($buffer,$index+2,1);
  53. $data['end_char'] = substr($buffer,$index+3,1);
  54. }else{
  55. $data['function_code'] = substr($buffer,$index,1);
  56. $data['function_keyword'] = substr($buffer,$index+1,1);
  57. $data['order_data'] = substr($buffer,$index+2,$length-1);
  58. $data['end_char'] = substr($buffer,-1);
  59. //解析指令数据
  60. $data['method'] = 'method000'.$data['function_code'];
  61. if($data['function_code'] == 'A'){ //上报状态
  62. array_push($decode, $data);
  63. }
  64. elseif($data['function_code'] == 'B'){ //上报定位
  65. if($data['function_keyword'] == 'A'){
  66. $data = self::decodeLocation($data);
  67. if($data['lat'] !== false)
  68. array_push($decode, $data);
  69. else
  70. echo 'lat empty ' . PHP_EOL;
  71. }
  72. else{
  73. //....跳过
  74. }
  75. }
  76. else{
  77. //....跳过
  78. }
  79. }
  80. }
  81. return $decode;
  82. }
  83. public static function bin2str( $hex, $space ){
  84. $data = unpack("C*chars",$hex);
  85. $bin = '';
  86. foreach($data as $key=>$value){
  87. $bin .= sprintf('%02X',$value);
  88. if($space)
  89. $bin .= ' ';
  90. }
  91. return trim($bin);
  92. }
  93. private function decodeLocation( $data ){
  94. $orders = explode('&',$data['order_data']);
  95. $data['lat'] = false;
  96. $data['lng'] = false;
  97. foreach ($orders as $order) {
  98. $code = substr($order,0,1);
  99. //解析定位数据
  100. if($code=='A'){
  101. //A0732142233550011405829060520190600
  102. //时分秒
  103. $times = substr($order,1,2).':'.substr($order,3,2).':'.substr($order,5,2);
  104. //定位纠偏 纠偏公式 abcde.fghi abc+de/60+fghi/600000
  105. $lat1 = substr($order,7,4);
  106. $lat2 = substr($order,11,4);
  107. $lng1 = substr($order,15,5);
  108. $lng2 = substr($order,20,4);
  109. if(self::locationFormat == self::LACATION_FORMAT_FLOAT){
  110. $lat_a = floor($lat1/100);
  111. $lat_b = (($lat1 - $lat_a*100).'.'.$lat2 )/60;
  112. $data['lat'] = $lat_a + $lat_b;
  113. $lng_a = floor($lng1/100);
  114. $lng_b = (($lng1 - $lng_a*100).'.'.$lng2 )/60;
  115. $data['lng'] = $lng_a + $lng_b;
  116. }else{
  117. $data['lat'] = $lat1 .'.'. $lat2;
  118. $data['lng'] = $lng1 .'.'. $lng2;
  119. }
  120. /*
  121. $lat_0 = substr($order,7,4).'.'.substr($order,11,4);
  122. $lng_0 = substr($order,15,5).'.'.substr($order,20,4);
  123. $data['lat_0'] = $lat_0;
  124. $data['lng_0'] = $lng_0;
  125. */
  126. $f = ord(substr($order,24,1));
  127. $data['f'] = $f;
  128. $data['type'] = 1;//1-表示取GPS坐标数据,2-表示取基站坐标
  129. switch ($f) {
  130. case 48://0x0011 0000 西经、南纬、定位
  131. $data['ew']='W';
  132. $data['ns']='S';
  133. $data['location']=true;
  134. break;
  135. case 49://0x0011 0001 西经、南纬、非定位
  136. $data['ew']='W';
  137. $data['ns']='S';
  138. $data['location']=false;
  139. break;
  140. case 50://0x0011 0010 西经、北纬、定位
  141. $data['ew']='W';
  142. $data['ns']='N';
  143. $data['location']=true;
  144. break;
  145. case 51://0x0011 0011 西经、北纬、非定位
  146. $data['ew']='W';
  147. $data['ns']='N';
  148. $data['location']=false;
  149. break;
  150. case 52://0x0011 0100 东经、南纬、定位
  151. $data['ew']='E';
  152. $data['ns']='S';
  153. $data['location']=true;
  154. break;
  155. case 53://0x0011 0101东经、南纬、非定位
  156. $data['ew']='E';
  157. $data['ns']='S';
  158. $data['location']=false;
  159. break;
  160. case 54://0x0011 0110东经、北纬、定位
  161. $data['ew']='E';
  162. $data['ns']='N';
  163. $data['location']=true;
  164. break;
  165. case 55://0x0011 0111 东经、北纬、非定位
  166. $data['ew']='E';
  167. $data['ns']='N';
  168. $data['location']=false;
  169. break;
  170. case 63://0x0011 1111 表示设备直接调用第三方的基站位置解析接口成经纬度信息上传,该标志位用来区分正常的GPS 定位经纬度信息
  171. $data['ew']=null;
  172. $data['ns']=null;
  173. $data['type']=2;
  174. break;
  175. default:
  176. break;
  177. }
  178. $data['speed'] = substr($order,25,2);
  179. $data['direction'] = substr($order,27,2);
  180. $dates = '20'.substr($order,33,2).'-'.substr($order,31,2).'-'.substr($order,29,2); //年月日
  181. $data['device_time'] = $dates.' '.$times;
  182. }
  183. }
  184. return $data;
  185. }
  186. public function setLocationFormat( $format ){
  187. self::locationFormat = $format;
  188. }
  189. }