LocationTransform.php 16 KB

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