LocationTransform.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Mead
  5. * Date: 2019/9/3
  6. * Time: 8:05 PM
  7. */
  8. namespace App\Servers\Weikemu\Transforms;
  9. use App\Handlers\BikeStatusInfoSyncHandler;
  10. use App\Handlers\ConvertHandler;
  11. use App\Handlers\MapHandler;
  12. use App\Maps\BikeMap;
  13. use App\Models\AreaTraitModel;
  14. use App\Models\BoxSettingTraitModel;
  15. use App\Models\LocationLosTraitModel;
  16. use App\Models\OrderTraitModel;
  17. use App\Models\WarningLogTraitModel;
  18. use App\Servers\BaseServer;
  19. use App\Servers\Weikemu\BikeControl;
  20. use App\Servers\Weikemu\Maps\VideoMap;
  21. use App\Servers\Weikemu\Models\CacheLogTraitModel;
  22. /**
  23. * 实时追踪和查询位置消息报文
  24. * Class LoginServer
  25. * @package App\Servers
  26. */
  27. class LocationTransform extends BaseServer
  28. {
  29. use BoxSettingTraitModel, WarningLogTraitModel, LocationLosTraitModel, OrderTraitModel, AreaTraitModel, CacheLogTraitModel;
  30. /**
  31. * 处理设备序列号
  32. * @param $no
  33. * @return bool|string
  34. * User: Mead
  35. */
  36. private static function box_no($no)
  37. {
  38. return substr($no, 0, 9);
  39. }
  40. public function main($body)
  41. {
  42. $data = $this->decode($body);
  43. // 位置信息
  44. // $bike_no = $_SESSION['bike_no'];
  45. // $box_no = $data['box_no'];
  46. // if (!$bike_no) return [];
  47. //过滤同一时候多包
  48. if ($_SESSION['last_box_location_time'] == $data['time']) {
  49. return $this->response();
  50. }
  51. $_SESSION['last_box_location_time'] = $data['time'];
  52. $bike_no = $_SESSION['bike_no'];
  53. $box_no = $_SESSION['box_no'];
  54. //车位位置更新redis
  55. $this->cacheJsonLog("NOW_LOCATION:{$bike_no}", [
  56. 'lat' => $data['location']['lat'],
  57. 'lng' => $data['location']['lng'],
  58. 'mileage' => $data['bike']['bike_single_mileage'],
  59. 'time' => time()
  60. ]);
  61. $location = [
  62. 'order_id' => 0,
  63. 'bike_id' => 0,
  64. 'area_id' => 0,
  65. 'type' => 'no',
  66. 'is_rent' => 0
  67. ];
  68. $battery_power = $data['battery_voltage'];
  69. self::log($data, 'Location', self::$LOG_COMMON);
  70. if (!$data['location_type']) {
  71. //基站
  72. return $this->response();
  73. }
  74. $last_location = [
  75. 'lat' => $data['yjlx']['lngLat'][1],
  76. 'lng' => $data['yjlx']['lngLat'][0],
  77. ];
  78. $cols = [];
  79. // 验证位置是否正常
  80. if ($last_location['lat'] >= 3) {
  81. $cols['last_location'] = json_encode($last_location, true);
  82. $cols['last_location_time'] = date('Y-m-d H:i:s');
  83. } else {
  84. //位置数据为0,说明车可能在房内
  85. if ($this->is_throw_num_time($data['box_no'], 'location_error', 10, 60)) {
  86. // 发出警告
  87. self::warningLocationError($bike_no, $box_no, $data, $body, "location");
  88. }
  89. return $this->response();
  90. }
  91. if ($data['status']['order_status']) {
  92. // 电车打开
  93. // 获取用户信息
  94. $order = (new BikeStatusInfoSyncHandler($this->redis))->getRideBikeOrderInfo($bike_no);
  95. if ($order) {
  96. //是否有电量信息
  97. if (array_key_exists('is_rent', $order)) {
  98. $location['is_rent'] = $order['is_rent'];
  99. }
  100. // 订单信息
  101. if (array_key_exists('id', $order)) {
  102. $location['order_id'] = $order['id'];
  103. $location['area_id'] = $order['area_id'];
  104. }
  105. if (array_key_exists('bike_id', $order)) {
  106. $location['bike_id'] = $order['bike_id'];
  107. }
  108. $location['type'] = BikeStatusInfoSyncHandler::ROLE_USER;
  109. if ($order['role'] === BikeStatusInfoSyncHandler::ROLE_USER) {
  110. if ($location['is_rent']) {
  111. //租车
  112. if ($order['is_close_bike'] && $data['status']['bike_lock'] == 1) {
  113. //关车之后没有关上,强制锁车
  114. self::log($box_no, 'LOCATION_FOUCE_CLOSE_BIKE', self::$LOG_COMMON);
  115. BikeControl::closeLock($data['box_no']);
  116. }
  117. } else {
  118. // 临时停车没有关锁
  119. if ($order['is_temporary_close'] && $data['status']['bike_lock'] == 1) {
  120. self::log($box_no, 'LOCATION_FOUCE_TEMPORARY_CLOSE_BIKE', self::$LOG_COMMON);
  121. BikeControl::temporaryCloseLock($data['box_no']);
  122. }
  123. }
  124. //用户正常骑行订单
  125. if ($last_location['lat'] > 0) {//判断是否在骑行区域内
  126. // 判断后台是否设置超速区域播报语音
  127. if ($order['is_out_area_lost_electric']) {
  128. $is_out_status = $this->isOutArea($last_location['lat'], $last_location['lng'], $order['area_id'], $data['box_no']);
  129. if (!$is_out_status['is_out_area']) {//播报语言
  130. self::log('超区域');
  131. if ($this->is_throw_num_time($data['box_no'], 'is_out_area_nearby', 3, 0.5)) {
  132. self::log('播报语音');
  133. if (!$is_out_status['is_out_area_nearby']) {
  134. //断电播放
  135. BikeControl::playVoice($data['box_no'], VideoMap::VIDEO_BATTERY_EDGE);
  136. } else {
  137. //超出运营区
  138. BikeControl::playVoice($data['box_no'], VideoMap::VIDEO_GO_BEYOND);
  139. }
  140. }
  141. if (!$is_out_status['is_out_area_nearby']) {
  142. //失能
  143. echo '要失能';
  144. if ($this->is_throw_num_time($data['box_no'], 'is_out_area_nearby', 4, 1)) {
  145. if ((!$this->redis->exists('bike_out_area_open_electric_' . $bike_no)) || $data['status']['bike_lock']) {
  146. echo '失能';
  147. self::log($box_no, 'LOCATION_BIKE_OUT_AREA_LOSE', self::$LOG_COMMON);
  148. BikeControl::outAreaLoseElectric($data['box_no'], VideoMap::VIDEO_POWER_FAILURE);
  149. $this->redis->set('bike_out_area_' . $bike_no, 1, 60);
  150. }
  151. }
  152. } else {
  153. //供能
  154. if ($this->redis->exists('bike_out_area_' . $bike_no)) {
  155. self::log($box_no, 'LOCATION_BIKE_OUT_AREA_ADD', self::$LOG_COMMON);
  156. BikeControl::outAreaGetElectric($data['box_no']);
  157. $this->redis->del('bike_out_area' . $bike_no);
  158. $this->redis->set('bike_out_area_open_electric_' . $bike_no, 1, 120);
  159. }
  160. }
  161. } else {
  162. //供能
  163. if ($this->redis->exists('bike_out_area_' . $bike_no)) {
  164. self::log($box_no, 'LOCATION_BIKE_OUT_AREA_ADD', self::$LOG_COMMON);
  165. BikeControl::outAreaGetElectric($data['box_no']);
  166. $this->redis->del('bike_out_area' . $bike_no);
  167. $this->redis->set('bike_out_area_open_electric_' . $bike_no, 1, 120);
  168. }
  169. }
  170. }
  171. }
  172. //电量过低自动关车
  173. //是否低电关电车
  174. if ($order['is_low_electric_close_bike']) {
  175. if ($battery_power <= 10) {
  176. if ($this->is_throw_num_time($data['box_no'], 'is_low_battery', 3, 0.5)) {
  177. self::log('低电语音');
  178. BikeControl::playVoice($data['box_no'], VideoMap::VIDEO_LOW_POWER);
  179. }
  180. if ($this->is_throw_num_time($data['box_no'], 'battery_low', 40, 5)) {
  181. $status = json_decode(file_get_contents(Config['close_order_api_url'] . "&box_no={$box_no}&bike_no={$bike_no}&type=电量低&position=2"), true);
  182. self::log($box_no, 'LOCATION_BATTERY_LOW_AUTO_ORDER', self::$LOG_COMMON);
  183. }
  184. }
  185. }
  186. } else if ($order['role'] === BikeStatusInfoSyncHandler::ROLE_WORKER) {
  187. //运维骑行订单
  188. // 订单信息
  189. $location['type'] = BikeStatusInfoSyncHandler::ROLE_WORKER;
  190. } else if ($order['role'] === BikeStatusInfoSyncHandler::ROLE_BIND) {
  191. $location['type'] = BikeStatusInfoSyncHandler::ROLE_BIND;
  192. } else if ($order['role'] === BikeStatusInfoSyncHandler::ROLE_SERVER) {
  193. //系统本身操作
  194. $location['type'] = BikeStatusInfoSyncHandler::ROLE_SERVER;
  195. } else {
  196. self::log($box_no, 'LOCATION_CLOSE_BIKE_NO_USER_TYPE', self::$LOG_COMMON);
  197. BikeControl::closeLock($data['box_no']);
  198. }
  199. } else {
  200. //处理晚到的数据
  201. // $next_log = $this->byTimeGetOrder($data['box_time'], $box_no);
  202. // if ($next_log) {
  203. // $re = $this->byIdAndIsRentAndTime($next_log['order_id'], $next_log['is_rent'], $data['box_time']);
  204. // if ($re) {
  205. // $location['is_rent'] = $next_log['is_rent'];
  206. // $location['order_id'] = $next_log['order_id'];
  207. // $location['area_id'] = $next_log['area_id'];
  208. // $location['bike_id'] = $next_log['bike_id'];
  209. // $location['type'] = BikeStatusInfoSyncHandler::ROLE_USER;
  210. // }
  211. // } else {
  212. //// //非法骑行
  213. // $num = $this->redis->incr('bike:illegal:open:' . $bike_no, 1);
  214. // $this->redis->expire('bike:illegal:open:' . $bike_no, 120);
  215. // self::log($box_no, 'LOCATION_CLOSE_BIKE_NO_ORDER', self::$LOG_COMMON);
  216. // BikeControl::closeLock($data['box_no']);
  217. //
  218. // if ($num > 5) {
  219. // if (!$this->is_ep_min($box_no, 'illegal_location', 20)) {
  220. // $this->warningFF($bike_no, $data['box_no'], $data, $body);
  221. // }
  222. // }
  223. // }
  224. }
  225. if (!$this->is_ep_min($box_no, 'update_battery', 5)) {
  226. $cols['battery_power'] = $battery_power;
  227. $cols['is_low_battery_power'] = BikeMap::BATTERY_POWER_OK;
  228. if ($cols['battery_power'] <= self::$max_ride_v) {
  229. $cols['is_low_battery_power'] = BikeMap::BATTERY_POWER_LOW;
  230. //30分钟插一次报警信息
  231. if (!$this->is_ep_min($box_no, 'warning_log_battery', 30)) {
  232. $this->warningLogBatteryLow($bike_no, $data['box_no'], ['battery_power' => $cols['battery_power']], $body, 'location');
  233. }
  234. }
  235. }
  236. // 更新车的位置信息(非骑行状态)
  237. if (count($cols) && !$this->is_ep_min($box_no, 'update_ride_bike_location', 1)) {
  238. // $this->db->update('bikes')->where('box_no = ' . (string)$data['box_no'])->cols($cols)->query();
  239. $this->db->update('bikes')->where("box_no = '{$box_no}'")->cols($cols)->query();
  240. }
  241. } else {
  242. // 车静止状态
  243. if ($data['status']['bike_lock']) {
  244. BikeControl::closeLock($data['box_no']);
  245. }
  246. if (!$this->is_ep_min($box_no, 'update_stop_bike_battery', 15)) {
  247. $cols['battery_power'] = $battery_power;
  248. $cols['is_low_battery_power'] = BikeMap::BATTERY_POWER_OK;
  249. if ($cols['battery_power'] <= self::$max_ride_v) {
  250. $cols['is_low_battery_power'] = BikeMap::BATTERY_POWER_LOW;
  251. //30分钟插一次报警信息
  252. // if (!$this->is_ep_min($box_no, 'warning_log_battery', 30)) {
  253. $this->warningLogBatteryLow($bike_no, $data['box_no'], ['battery_power' => $cols['battery_power']], $body, 'location');
  254. // }
  255. }
  256. } else {
  257. // $openBatteryKey = "cache:open_battery:{$box_no}";
  258. // if ($this->redis->exists($openBatteryKey)) {
  259. // $cols['battery_power'] = $battery_power;
  260. // $cols['is_low_battery_power'] = BikeMap::BATTERY_POWER_OK;
  261. // if ($cols['battery_power'] <= self::$max_ride_v) {
  262. // $cols['is_low_battery_power'] = BikeMap::BATTERY_POWER_LOW;
  263. // }
  264. // }
  265. }
  266. // 更新车的位置信息(非骑行状态)
  267. // if (count($cols) && !$this->is_ep_min($box_no, 'update_bike_location', 10)) {
  268. // $this->db->update('bikes')->where('box_no = ' . $data['box_no'])->cols($cols)->query();
  269. // } else {
  270. // //换电池及时更新电量
  271. // $openBatteryKey = "cache:open_battery:{$box_no}";
  272. // if ($this->redis->exists($openBatteryKey)) {
  273. // $cols['battery_power'] = $battery_power;
  274. // $cols['is_low_battery_power'] = BikeMap::BATTERY_POWER_OK;
  275. // if ($cols['battery_power'] <= self::$max_ride_v) {
  276. // $cols['is_low_battery_power'] = BikeMap::BATTERY_POWER_LOW;
  277. // }
  278. // $this->db->update('bikes')->where('box_no = ' . $data['box_no'])->cols($cols)->query();
  279. // }
  280. // }
  281. // $this->db->update('bikes')->where('box_no = ' . $data['box_no'])->cols($cols)->query();
  282. $this->db->update('bikes')->where("box_no = '{$box_no}'")->cols($cols)->query();
  283. if (($last_location['lat'] > 0)) {
  284. // 修改车的位置
  285. $is_location_ex = $this->redis->geopos(BikeStatusInfoSyncHandler::REDIS_BIKE_LOCATION_TAG, $bike_no)[0];
  286. if ((count($is_location_ex) !== 0)) {
  287. $this->redis->geoadd(BikeStatusInfoSyncHandler::REDIS_BIKE_LOCATION_TAG, $last_location['lng'], $last_location['lat'], $bike_no);
  288. }
  289. }
  290. }
  291. $this->mongo->location_logs->insertOne([
  292. 'bike_no' => $bike_no,
  293. 'box_no' => $data['box_no'],
  294. 'order_id' => $location['order_id'],
  295. 'bike_id' => $location['bike_id'],
  296. 'area_id' => $location['area_id'],
  297. 'latitude' => $last_location['lat'],
  298. 'longitude' => $last_location['lng'],
  299. 'speed' => $data['yjlx']['speed'],
  300. 'battery_power' => $battery_power,
  301. 'mileage' => $data['bike']['bike_single_mileage'] ?? 0,
  302. 'is_riding' => $data['status']['order_status'] ?? 0,
  303. 'is_yundong' => $data['status']['rear_wheel_motion'] ?? 0,
  304. 'type' => $location['type'],
  305. 'is_rent' => $location['is_rent'],
  306. 'created_at' => date('Y-m-d H:i:s'),
  307. 'box_time' => $data['box_time'],
  308. 'status' => 1,
  309. 'source' => 'location',
  310. 'day' => date("Ymd")
  311. ]);
  312. return $this->response();
  313. }
  314. /**
  315. * 解析装载的状态消息
  316. * @param $body
  317. * @return array
  318. * User: Mead
  319. */
  320. private function decode($body)
  321. {
  322. $top34Data = $this->decodeWeiKeMuTop34($body);
  323. $i = 34;
  324. $location_type = self::stitching($body, $i, 1); // i=34
  325. $i += 1;
  326. $acc_status = self::stitching($body, $i, 1); // i=35
  327. $i += 1;
  328. // 动态计算
  329. $yjlx = self::stitching($body, $i, 14); // i=36
  330. $yjlxData = [];
  331. if ($yjlx) {
  332. $yjlxData = self::handleYjlx($yjlx);
  333. }
  334. return array_merge($top34Data, [
  335. 'location_type' => self::handleLocationType($location_type),//0为基站定位 1为卫星定位
  336. 'acc_status' => self::handleACCStatus($acc_status),
  337. 'yjlx' => $yjlxData,
  338. ]);
  339. }
  340. private static function handleYjlx($yjlx)
  341. {
  342. // echo '动态计算' . PHP_EOL;
  343. $i = 0;
  344. $time = substr($yjlx, $i, 8); // 时间
  345. $i += 8;
  346. $latitude = hexdec(substr($yjlx, $i, 8)); // 维度
  347. $i += 8;
  348. $longitude = hexdec(substr($yjlx, $i, 8)); // 经度
  349. $i += 8;
  350. $speed = substr($yjlx, $i, 2);
  351. $i += 2;
  352. $acc = self::handleACCStatus(substr($yjlx, $i, 2));
  353. $lnglat = (new MapHandler())->wgs84togcj02(self::formatCoordinate($longitude), self::formatCoordinate($latitude));
  354. return [
  355. 'time' => date('Y-m-d H:i:s', hexdec($time)),
  356. 'lngLat' => $lnglat,
  357. 'speed' => $speed,
  358. 'acc' => $acc,
  359. ];
  360. }
  361. private static function handleACCStatus($acc_status)
  362. {
  363. $data = self::handleU1($acc_status);
  364. return [
  365. 'activity' => $data[0], //1:动 0:静
  366. 'acc_status' => $data[1], // 1:ACC接通,0:ACC断开
  367. ];
  368. }
  369. private static function handleLocationType($location_type)
  370. {
  371. $data = self::handleU1($location_type);
  372. return $data[0];
  373. }
  374. /**
  375. * 状态响应
  376. * @param $login_type
  377. * @return array
  378. * User: Mead
  379. */
  380. public function response($login_type = '00')
  381. {
  382. $body = [
  383. '5b',
  384. '0000'
  385. ];
  386. return $body;
  387. }
  388. /**
  389. * 判断是否在骑行区
  390. * @param $lat
  391. * @param $lng
  392. * @param $box_no
  393. * User: Mead
  394. */
  395. private function isOutArea($lat, $lng, $area_id = false, $box_no = false)
  396. {
  397. $location = [
  398. 'latitude' => $lat,
  399. 'longitude' => $lng,
  400. ];
  401. if (!$area_id) $area_id = $this->byBoxNoGetAreaId($box_no);
  402. $area = $this->byAreaIdGetArea($area_id);
  403. $fences = $area['area_fence'];
  404. $centre = $area['area_centre'];
  405. $radius = $area['area_radius'];
  406. $area_fushe_fence = $area['area_fushe_fence'];
  407. // 判断是否在骑行区域
  408. $ConvertHandler = (new ConvertHandler());
  409. $is_out_area = $ConvertHandler->is_point_in_polygon($location, $fences);
  410. // 判断是否骑出辐射范围
  411. $is_out_area_nearby = true;
  412. if (!$is_out_area) {
  413. $is_out_area_nearby = $ConvertHandler->is_point_in_polygon($location, $area_fushe_fence);
  414. }
  415. return [
  416. 'is_out_area' => $is_out_area,
  417. 'is_out_area_nearby' => $is_out_area_nearby
  418. ];
  419. }
  420. }