BatteryServer.php 2.8 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\Models\BatteryTraitModel;
  10. class BatteryServer extends BaseServer
  11. {
  12. use BatteryTraitModel;
  13. /**
  14. *
  15. * @param $body
  16. * @return array
  17. * User: Mead
  18. */
  19. public function main($body)
  20. {
  21. $data = $this->decode($body);
  22. self::log($data, 'BatteryServer', self::$LOG_MAJOR);
  23. if ($this->is_ep_min($data['box_no'], 'update_battery_box_no', 3)) {
  24. // 更新电车的电量
  25. $cols['battery_power'] = $this->byVoltageGetElectric($data['battery_voltage']);
  26. $cols['is_low_battery_power'] = 1;
  27. if ($cols['battery_power'] <= self::$max_ride_v) {
  28. $cols['is_low_battery_power'] = 0;
  29. }
  30. $this->db->update('bikes')->where('box_no = ' . $data['box_no'])->cols($cols)->query();
  31. }
  32. return $this->response();
  33. }
  34. /**
  35. * 解析装载的数据
  36. * @param $body
  37. * @return array
  38. * User: Mead
  39. */
  40. private function decode($body)
  41. {
  42. $i = 0;
  43. $box_no = self::stitching($body, $i, 5);
  44. $i += 5;
  45. // 设备时间
  46. $time = self::stitching($body, $i, 4);
  47. $i += 4;
  48. // 相对soc
  49. $battery_xd_soc = self::stitching($body, $i, 1);
  50. $i += 1;
  51. // 可用剩余容量
  52. $battery_mb_free = self::stitching($body, $i, 2);
  53. $i += 2;
  54. // 绝对soc
  55. $battery_jd_soc = self::stitching($body, $i, 1);
  56. $i += 1;
  57. // 绝对满电容量
  58. $i += 2;
  59. // SOH
  60. // $battery_soh = self::stitching($body, $i, 1);
  61. $i += 1;
  62. // 内部温度
  63. $i += 2;
  64. // 实时电流
  65. $i += 2;
  66. // 电压
  67. $battery_voltage = self::stitching($body, $i, 2);
  68. $i += 2;
  69. // 循环次数
  70. $i += 2;
  71. // 1~7节电池电压
  72. $i += 14;
  73. // 8~14节电池电压
  74. $i += 14;
  75. // 当前充电间隔时间
  76. $i += 2;
  77. // 最大充电间隔时间
  78. $i += 2;
  79. // 读写成本条形码
  80. $i += 16;
  81. //读版本号
  82. $i += 2;
  83. //电池组制造厂名称
  84. $battery_name = self::stitching($body, $i, 16);
  85. $i += 16;
  86. // 处理值
  87. $box_no = $this->decodeBoxNo($box_no);
  88. $time = $this->decodeTime($time);
  89. $battery_voltage = $this->decodeBatteryVoltage($battery_voltage);
  90. return [
  91. 'box_no' => $box_no,
  92. 'time' => $time,
  93. 'battery_voltage' => $battery_voltage,
  94. 'battery_name' => $battery_name,
  95. 'battery_xd_soc' => $battery_xd_soc,
  96. 'battery_mb_free' => $battery_mb_free,
  97. 'battery_jd_soc' => $battery_jd_soc,
  98. ];
  99. }
  100. }