HeartBeatServer.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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\Maps\BikeMap;
  11. use App\Models\BatteryTraitModel;
  12. use App\Models\WarningLogTraitModel;
  13. class HeartBeatServer extends BaseServer
  14. {
  15. use BatteryTraitModel, WarningLogTraitModel;
  16. public function main($body)
  17. {
  18. $data = $this->decode($body);
  19. self::log($data, 'HeartBeatServer', self::$LOG_MAJOR);
  20. $bike_no = $_SESSION['bike_no'];
  21. if (!$bike_no) return [];
  22. $box_no = $data['box_no'];
  23. if ($data['box_no'] === '003454406') return $this->response();
  24. if (!$this->is_ep_min($data['box_no'], 'heart', 30)) {
  25. $cols['is_link'] = BikeMap::LINK_ONLINE;
  26. $cols['battery_power'] = $this->byVoltageGetElectric($data['battery_voltage']);
  27. $cols['is_low_battery_power'] = BikeMap::BATTERY_POWER_OK;
  28. if ($cols['battery_power'] <= self::$max_ride_v) {
  29. // 低电量
  30. $cols['is_low_battery_power'] = BikeMap::BATTERY_POWER_LOW;
  31. //电量低报警信息
  32. $this->warningLogBatteryLow($bike_no, $data['box_no'], ['battery_power' => $cols['battery_power']], $body, 'heart');
  33. }
  34. $this->db->update('bikes')->where('box_no = ' . $data['box_no'])->cols($cols)->query();
  35. } else {
  36. //换电池及时更新电量
  37. $openBatteryKey = "cache:open_battery:{$box_no}";
  38. if ($this->redis->exists($openBatteryKey)) {
  39. $cols['is_link'] = BikeMap::LINK_ONLINE;
  40. $cols['battery_power'] = $this->byVoltageGetElectric($data['battery_voltage']);
  41. $cols['is_low_battery_power'] = BikeMap::BATTERY_POWER_OK;
  42. if ($cols['battery_power'] <= self::$max_ride_v) {
  43. // 低电量
  44. $cols['is_low_battery_power'] = BikeMap::BATTERY_POWER_LOW;
  45. }
  46. $this->db->update('bikes')->where('box_no = ' . $data['box_no'])->cols($cols)->query();
  47. }
  48. }
  49. if ((!$data['bike_status']['is_huanche'])) {
  50. // 获取用户信息
  51. $order = (new BikeStatusInfoSyncHandler($this->redis))->getRideBikeOrderInfo($bike_no);
  52. if (!$order) {
  53. //非法骑行
  54. $num = $this->redis->incr('bike:illegal:open:' . $bike_no, 1);
  55. $this->redis->expire('bike:illegal:open:' . $bike_no, 120);
  56. BikeControl::closeLock($data['box_no']);
  57. if ($num > 5) {
  58. if (!$this->is_ep_min($box_no, 'bike:illegal:open:location', 20)) {
  59. $this->warningFF($bike_no, $data['box_no'], $data, $body);
  60. }
  61. }
  62. }
  63. }
  64. return $this->response();
  65. }
  66. /**
  67. * 解析装载的登录数据
  68. * @param $body
  69. * @return array
  70. * User: Mead
  71. */
  72. private function decode($body)
  73. {
  74. $i = 0;
  75. $box_no = $this->decodeBoxNo(self::stitching($body, $i, 5));
  76. $i += 5;
  77. // 信号强度
  78. // $signal_status = self::stitching($body, $i, 2);
  79. $i += 2;
  80. // 车辆状态
  81. $bike_status = self::stitching($body, $i, 2);
  82. $i += 2;
  83. // 电池电压
  84. $battery_voltage = self::stitching($body, $i, 2);
  85. $i += 2;
  86. // 相对SOC
  87. // $soc = self::stitching($body, $i, 1);
  88. $i += 1;
  89. // 电池余量
  90. // $battery_mb_free = self::stitching($body, $i, 2);
  91. return [
  92. 'box_no' => $box_no,
  93. 'bike_status' => $this->decodeBikeStatus($bike_status),
  94. 'battery_voltage' => $this->decodeBatteryVoltage($battery_voltage),
  95. ];
  96. }
  97. }