LocationServer.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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;
  9. use App\Handlers\BikeStatusInfoSyncHandler;
  10. use App\Handlers\ConvertHandler;
  11. use App\Maps\BikeMap;
  12. use App\Maps\VideoMap;
  13. use App\Models\AreaTraitModel;
  14. use App\Models\BatteryTraitModel;
  15. use App\Models\BikeTraitModel;
  16. use App\Models\LocationLosTraitModel;
  17. use App\Models\OrderTraitModel;
  18. use App\Models\WarningLogTraitModel;
  19. /**
  20. * 定位解析包
  21. * Class LocationServer
  22. * @package App\Servers
  23. */
  24. class LocationServer extends BaseServer
  25. {
  26. use BatteryTraitModel, BikeTraitModel, AreaTraitModel, WarningLogTraitModel, LocationLosTraitModel, OrderTraitModel;
  27. public function main($body)
  28. {
  29. $data = $this->decode($body);
  30. self::log($data, 'LocationServer', self::$LOG_MAJOR);
  31. if ($this->is_ep_sec($data['box_no'], 'reload_location', 10)) {
  32. return $this->response();
  33. }
  34. $bike_no = $_SESSION['bike_no'];
  35. $box_no = $data['box_no'];
  36. if (!$bike_no) return [];
  37. $cols = [];
  38. // 验证位置是否正常
  39. if ($data['location']['lat'] > 0) {
  40. $cols['last_location'] = json_encode($data['location'], true);
  41. $cols['last_location_time'] = date('Y-m-d H:i:s');
  42. } else {
  43. //位置数据为0,说明车可能在房内
  44. if ($this->is_throw_num_time($data['box_no'], 'location_error', 10, 60)) {
  45. // 发出警告
  46. self::warningLocationError($bike_no, $box_no, $data, $body, "location");
  47. }
  48. return $this->response();
  49. }
  50. // 30 分钟更新一次
  51. $battery_power = $this->byVoltageGetElectric($data['bike']['battery_voltage']);
  52. $location = [
  53. 'order_id' => 0,
  54. 'bike_id' => 0,
  55. 'area_id' => 0,
  56. 'type' => 'no',
  57. 'is_rent' => 0
  58. ];
  59. // 判断是否接车状态
  60. if ((!$data['bike']['status']['is_huanche'])) {
  61. //车运动状态
  62. // 获取用户信息
  63. $order = (new BikeStatusInfoSyncHandler($this->redis))->getRideBikeOrderInfo($bike_no);
  64. if ($order) {
  65. //是否有电量信息
  66. if (array_key_exists('is_rent', $order)) {
  67. $location['is_rent'] = $order['is_rent'];
  68. }
  69. // 订单信息
  70. if (array_key_exists('id', $order)) {
  71. $location['order_id'] = $order['id'];
  72. $location['area_id'] = $order['area_id'];
  73. }
  74. if (array_key_exists('bike_id', $order)) {
  75. $location['bike_id'] = $order['bike_id'];
  76. }
  77. $location['type'] = BikeStatusInfoSyncHandler::ROLE_USER;
  78. if ($order['role'] === BikeStatusInfoSyncHandler::ROLE_USER) {
  79. if ($location['is_rent']) {
  80. //租车
  81. if ($order['is_close_bike']) {
  82. //关车之后没有关上,强制锁车
  83. self::log($box_no, 'LOCATION_FOUCE_CLOSE_BIKE', self::$LOG_COMMON);
  84. BikeControl::closeLock($data['box_no']);
  85. }
  86. } else {
  87. //临时用车
  88. // 临时停车没有关锁
  89. if ($order['is_temporary_close']) {
  90. self::log($box_no, 'LOCATION_FOUCE_TEMPORARY_CLOSE_BIKE', self::$LOG_COMMON);
  91. BikeControl::temporaryCloseLock($data['box_no']);
  92. }
  93. }
  94. //用户正常骑行订单
  95. if ($data['location']['lat'] > 0) {//判断是否在骑行区域内
  96. // 判断后台是否设置超速区域播报语音
  97. if ($order['is_out_area_lost_electric']) {
  98. $is_out_status = $this->isOutArea($data['location']['lat'], $data['location']['lng'], $order['area_id'], $data['box_no']);
  99. if (!$is_out_status['is_out_area']) {//播报语言
  100. if (!$is_out_status['is_out_area_nearby']) {
  101. //断电播放
  102. BikeControl::playVideo($data['box_no'], VideoMap::VIDEO_OUT_AREA_CLOSE_ELECTRIC);
  103. } else {
  104. //超出运营区
  105. BikeControl::outAreaPlayVideo($data['box_no']);
  106. }
  107. if (!$is_out_status['is_out_area_nearby']) {
  108. //失能
  109. echo '要失能';
  110. if (!$this->redis->exists('bike_out_area_open_electric_' . $bike_no)) {
  111. echo '失能';
  112. self::log($box_no, 'LOCATION_BIKE_OUT_AREA_LOSE', self::$LOG_COMMON);
  113. BikeControl::outAreaLoseElectric($data['box_no']);
  114. $this->redis->set('bike_out_area_' . $bike_no, 1, 60);
  115. }
  116. } else {
  117. //供能
  118. if ($this->redis->exists('bike_out_area_' . $bike_no)) {
  119. self::log($box_no, 'LOCATION_BIKE_OUT_AREA_ADD', self::$LOG_COMMON);
  120. BikeControl::outAreaGetElectric($data['box_no']);
  121. $this->redis->del('bike_out_area' . $bike_no);
  122. $this->redis->set('bike_out_area_open_electric_' . $bike_no, 1, 120);
  123. }
  124. }
  125. } else {
  126. //供能
  127. if ($this->redis->exists('bike_out_area_' . $bike_no)) {
  128. self::log($box_no, 'LOCATION_BIKE_OUT_AREA_ADD', self::$LOG_COMMON);
  129. BikeControl::outAreaGetElectric($data['box_no']);
  130. $this->redis->del('bike_out_area' . $bike_no);
  131. $this->redis->set('bike_out_area_open_electric_' . $bike_no, 1, 120);
  132. }
  133. }
  134. }
  135. }
  136. //电量过低自动关车
  137. //是否低电关电车
  138. if ($order['is_low_electric_close_bike']) {
  139. if ($battery_power <= 10) {
  140. BikeControl::playVideo($data['box_no'], VideoMap::VIDEO_NO_ELECTRIC);
  141. if ($this->is_throw_num_time($data['box_no'], 'battery_low', 50, 10)) {
  142. $status = json_decode(file_get_contents(Config['close_order_api_url'] . "&box_no={$box_no}&bike_no={$bike_no}&type=电量低"), true);
  143. self::log($box_no, 'LOCATION_BATTERY_LOW_AUTO_ORDER', self::$LOG_COMMON);
  144. }
  145. }
  146. }
  147. } else if ($order['role'] === BikeStatusInfoSyncHandler::ROLE_WORKER) {
  148. //运维骑行订单
  149. // 订单信息
  150. $location['type'] = BikeStatusInfoSyncHandler::ROLE_WORKER;
  151. } else if ($order['role'] === BikeStatusInfoSyncHandler::ROLE_BIND) {
  152. $location['type'] = BikeStatusInfoSyncHandler::ROLE_BIND;
  153. } elseif ($location['role'] === BikeStatusInfoSyncHandler::ROLE_SERVER) {
  154. //系统本身操作
  155. $location['type'] = BikeStatusInfoSyncHandler::ROLE_SERVER;
  156. } else {
  157. self::log($box_no, 'LOCATION_CLOSE_BIKE_NO_USER_TYPE', self::$LOG_COMMON);
  158. BikeControl::closeLock($data['box_no']);
  159. }
  160. } else {
  161. //处理晚到的数据
  162. $next_log = $this->byTimeGetOrder($data['time'], $box_no);
  163. if ($next_log) {
  164. $re = $this->byIdAndIsRentAndTime($next_log['order_id'], $next_log['is_rent'], $data['time']);
  165. if ($re) {
  166. $location['is_rent'] = $next_log['is_rent'];
  167. $location['order_id'] = $next_log['order_id'];
  168. $location['area_id'] = $next_log['area_id'];
  169. $location['bike_id'] = $next_log['bike_id'];
  170. $location['type'] = BikeStatusInfoSyncHandler::ROLE_USER;
  171. }
  172. } else {
  173. //非法骑行
  174. $num = $this->redis->incr('bike:illegal:open:' . $bike_no, 1);
  175. $this->redis->expire('bike:illegal:open:' . $bike_no, 120);
  176. self::log($box_no, 'LOCATION_CLOSE_BIKE_NO_ORDER', self::$LOG_COMMON);
  177. BikeControl::closeLock($data['box_no']);
  178. if ($num > 5) {
  179. if (!$this->is_ep_min($box_no, 'illegal_location', 20)) {
  180. $this->warningFF($bike_no, $data['box_no'], $data, $body);
  181. }
  182. }
  183. }
  184. }
  185. if (!$this->is_ep_min($box_no, 'update_battery', 5)) {
  186. $cols['battery_power'] = $battery_power;
  187. $cols['is_low_battery_power'] = BikeMap::BATTERY_POWER_OK;
  188. if ($cols['battery_power'] <= self::$max_ride_v) {
  189. $cols['is_low_battery_power'] = BikeMap::BATTERY_POWER_LOW;
  190. //30分钟插一次报警信息
  191. if (!$this->is_ep_min($box_no, 'warning_log_battery', 30)) {
  192. $this->warningLogBatteryLow($bike_no, $data['box_no'], ['battery_power' => $cols['battery_power']], $body, 'location');
  193. }
  194. }
  195. }
  196. // 更新车的位置信息(非骑行状态)
  197. if (count($cols) && !$this->is_ep_min($box_no, 'update_ride_bike_location', 1)) {
  198. $this->db->update('bikes')->where('box_no = ' . $data['box_no'])->cols($cols)->query();
  199. }
  200. } else {
  201. // 车静止状态
  202. if (!$this->is_ep_min($box_no, 'update_stop_bike_battery', 30)) {
  203. $cols['battery_power'] = $battery_power;
  204. $cols['is_low_battery_power'] = BikeMap::BATTERY_POWER_OK;
  205. if ($cols['battery_power'] <= self::$max_ride_v) {
  206. $cols['is_low_battery_power'] = BikeMap::BATTERY_POWER_LOW;
  207. //30分钟插一次报警信息
  208. // if (!$this->is_ep_min($box_no, 'warning_log_battery', 30)) {
  209. $this->warningLogBatteryLow($bike_no, $data['box_no'], ['battery_power' => $cols['battery_power']], $body, 'location');
  210. // }
  211. }
  212. }
  213. // 更新车的位置信息(非骑行状态)
  214. if (count($cols) && !$this->is_ep_min($box_no, 'update_bike_location', 20)) {
  215. $this->db->update('bikes')->where('box_no = ' . $data['box_no'])->cols($cols)->query();
  216. } else {
  217. //换电池及时更新电量
  218. $openBatteryKey = "cache:open_battery:{$box_no}";
  219. if ($this->redis->exists($openBatteryKey)) {
  220. $this->db->update('bikes')->where('box_no = ' . $data['box_no'])->cols($cols)->query();
  221. }
  222. }
  223. if (($data['location']['lat'] > 0)) {
  224. // 修改车的位置
  225. $is_location_ex = $this->redis->geopos(BikeStatusInfoSyncHandler::REDIS_BIKE_LOCATION_TAG, $bike_no)[0];
  226. if ((count($is_location_ex) !== 0)) {
  227. $this->redis->geoadd(BikeStatusInfoSyncHandler::REDIS_BIKE_LOCATION_TAG, $data['location']['lng'], $data['location']['lat'], $bike_no);
  228. }
  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' => $data['location']['lat'],
  239. 'longitude' => $data['location']['lng'],
  240. 'speed' => $data['bike']['speed'],
  241. 'battery_power' => $battery_power,
  242. 'mileage' => $data['bike']['bike_single_mileage'],
  243. 'is_riding' => $data['bike']['status']['is_ride'],
  244. 'is_yundong' => $data['bike']['status']['is_yundong'],
  245. 'type' => $location['type'],
  246. 'is_rent' => $location['is_rent'],
  247. 'created_at' => date('Y-m-d H:i:s'),
  248. 'box_time' => $data['time'],
  249. 'status' => 1
  250. ]);
  251. return $this->response();
  252. }
  253. /**
  254. * 判断是否在骑行区
  255. * @param $lat
  256. * @param $lng
  257. * @param $box_no
  258. * User: Mead
  259. */
  260. private function isOutArea($lat, $lng, $area_id = false, $box_no = false)
  261. {
  262. $location = [
  263. 'latitude' => $lat,
  264. 'longitude' => $lng,
  265. ];
  266. if (!$area_id) $area_id = $this->byBoxNoGetAreaId($box_no);
  267. $area = $this->byAreaIdGetArea($area_id);
  268. $fences = $area['area_fence'];
  269. $centre = $area['area_centre'];
  270. $radius = $area['area_radius'];
  271. $area_fushe_fence = $area['area_fushe_fence'];
  272. // 判断是否在骑行区域
  273. $ConvertHandler = (new ConvertHandler());
  274. $is_out_area = $ConvertHandler->is_point_in_polygon($location, $fences);
  275. // 判断是否骑出辐射范围
  276. $is_out_area_nearby = true;
  277. if (!$is_out_area) {
  278. $is_out_area_nearby = $ConvertHandler->is_point_in_polygon($location, $area_fushe_fence);
  279. }
  280. return [
  281. 'is_out_area' => $is_out_area,
  282. 'is_out_area_nearby' => $is_out_area_nearby
  283. ];
  284. }
  285. /**
  286. * 解析装载的登录数据
  287. * @param $body
  288. * @return array
  289. * User: Mead
  290. */
  291. private function decode($body)
  292. {
  293. $i = 0;
  294. $box_no = $this->decodeBoxNo(self::stitching($body, $i, 5));
  295. $i += 5;
  296. // 设备时间
  297. $box_time = $this->decodeTime(self::stitching($body, $i, 4));
  298. $i += 4;
  299. // 维度
  300. $location_lat = self::stitching($body, $i, 4);
  301. $i += 4;
  302. // 经度
  303. $location_lng = self::stitching($body, $i, 4);
  304. $i += 4;
  305. // 海报高度
  306. // $location_height = self::stitching($body, $i, 2);
  307. $i += 2;
  308. //方位角
  309. // $location_azimuth = self::stitching($body, $i, 1);
  310. $i += 1;
  311. // GPS速度
  312. $location_speed = self::stitching($body, $i, 1);
  313. $i += 1;
  314. //信号状态
  315. // $location_signal_status = self::stitching($body, $i, 2);
  316. $i += 2;
  317. // 基站 MCC
  318. // $station_mcc = self::stitching($body, $i, 2);
  319. $i += 2;
  320. // 基站 MNC
  321. // $station_mnc = self::stitching($body, $i, 2);
  322. $i += 2;
  323. // 基站 LAC
  324. // $station_lac = self::stitching($body, $i, 2);
  325. $i += 2;
  326. // 基站 CELLID
  327. // $station_cellid = self::stitching($body, $i, 2);
  328. $i += 2;
  329. // 车辆 相对SOC
  330. // $bike_soc = self::stitching($body, $i, 1);
  331. $i += 1;
  332. // 车辆 可用剩余容量
  333. // $bike_battery_mb_free = self::stitching($body, $i, 2);
  334. $i += 2;
  335. // 车辆 SOH
  336. // $bike_soh = self::stitching($body, $i, 1);
  337. $i += 1;
  338. // 车辆 重放电次数
  339. // $bike_charge_discharge_num = self::stitching($body, $i, 2);
  340. $i += 2;
  341. // 车辆 骑行电流
  342. // $bike_ride_electric_current = self::stitching($body, $i, 1);
  343. $i += 1;
  344. // 车辆 控制器温度
  345. // $bike_box_temperature = self::stitching($body, $i, 1);
  346. $i += 1;
  347. // 车辆 电瓶电压
  348. $bike_battery_voltage = self::stitching($body, $i, 2);
  349. $i += 2;
  350. // 车辆 总里程
  351. $bike_total_mileage = self::stitching($body, $i, 4);
  352. $i += 4;
  353. // 车辆 单次里程数
  354. $bike_single_mileage = self::stitching($body, $i, 4);
  355. $i += 4;
  356. // 车辆 霍尔速度
  357. $bike_speed = self::stitching($body, $i, 2);
  358. $i += 2;
  359. // 车辆 车辆状态
  360. $bike_status = self::stitching($body, $i, 2);
  361. // 处理经纬度
  362. list($lng, $lat) = $this->decodeLatAndLng($location_lat, $location_lng);
  363. // 处理车的状态
  364. $bike_status = $this->decodeBikeStatus($bike_status);
  365. // 处理电压
  366. $bike_battery_voltage = $this->decodeBatteryVoltage($bike_battery_voltage);
  367. return [
  368. 'box_no' => $box_no,
  369. 'time' => $box_time,
  370. 'location' => [
  371. 'lat' => $lat,
  372. 'lng' => $lng,
  373. 'speed' => $location_speed
  374. ],
  375. 'bike' => [
  376. 'status' => $bike_status,
  377. 'speed' => hexdec($bike_speed),
  378. 'battery_voltage' => $bike_battery_voltage,
  379. 'bike_total_mileage' => hexdec($bike_total_mileage),
  380. 'bike_single_mileage' => hexdec($bike_single_mileage),
  381. ]
  382. ];
  383. }
  384. }