Jytgps.php 5.0 KB

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