123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Mead
- * Date: 2019/9/3
- * Time: 8:05 PM
- */
- namespace App\Servers;
- use App\Handlers\BikeStatusInfoSyncHandler;
- use App\Maps\BikeMap;
- use App\Models\BatteryTraitModel;
- use App\Models\WarningLogTraitModel;
- class HeartBeatServer extends BaseServer
- {
- use BatteryTraitModel, WarningLogTraitModel;
- public function main($body)
- {
- $data = $this->decode($body);
- self::log($data, 'HeartBeatServer', self::$LOG_MAJOR);
- $bike_no = $_SESSION['bike_no'];
- if (!$bike_no) return [];
- $box_no = $data['box_no'];
- if ($data['box_no'] === '003454406') return $this->response();
- if (!$this->is_ep_min($data['box_no'], 'heart', 30)) {
- $cols['is_link'] = BikeMap::LINK_ONLINE;
- $cols['battery_power'] = $this->byVoltageGetElectric($data['battery_voltage']);
- $cols['is_low_battery_power'] = BikeMap::BATTERY_POWER_OK;
- if ($cols['battery_power'] <= self::$max_ride_v) {
- // 低电量
- $cols['is_low_battery_power'] = BikeMap::BATTERY_POWER_LOW;
- //电量低报警信息
- $this->warningLogBatteryLow($bike_no, $data['box_no'], ['battery_power' => $cols['battery_power']], $body, 'heart');
- }
- $this->db->update('bikes')->where('box_no = ' . $data['box_no'])->cols($cols)->query();
- } else {
- //换电池及时更新电量
- $openBatteryKey = "cache:open_battery:{$box_no}";
- if ($this->redis->exists($openBatteryKey)) {
- $cols['is_link'] = BikeMap::LINK_ONLINE;
- $cols['battery_power'] = $this->byVoltageGetElectric($data['battery_voltage']);
- $cols['is_low_battery_power'] = BikeMap::BATTERY_POWER_OK;
- if ($cols['battery_power'] <= self::$max_ride_v) {
- // 低电量
- $cols['is_low_battery_power'] = BikeMap::BATTERY_POWER_LOW;
- }
- $this->db->update('bikes')->where('box_no = ' . $data['box_no'])->cols($cols)->query();
- }
- }
- if ((!$data['bike_status']['is_huanche'])) {
- // 获取用户信息
- $order = (new BikeStatusInfoSyncHandler($this->redis))->getRideBikeOrderInfo($bike_no);
- if (!$order) {
- //非法骑行
- $num = $this->redis->incr('bike:illegal:open:' . $bike_no, 1);
- $this->redis->expire('bike:illegal:open:' . $bike_no, 120);
- BikeControl::closeLock($data['box_no']);
- if ($num > 5) {
- if (!$this->is_ep_min($box_no, 'bike:illegal:open:location', 20)) {
- $this->warningFF($bike_no, $data['box_no'], $data, $body);
- }
- }
- }
- }
- return $this->response();
- }
- /**
- * 解析装载的登录数据
- * @param $body
- * @return array
- * User: Mead
- */
- private function decode($body)
- {
- $i = 0;
- $box_no = $this->decodeBoxNo(self::stitching($body, $i, 5));
- $i += 5;
- // 信号强度
- // $signal_status = self::stitching($body, $i, 2);
- $i += 2;
- // 车辆状态
- $bike_status = self::stitching($body, $i, 2);
- $i += 2;
- // 电池电压
- $battery_voltage = self::stitching($body, $i, 2);
- $i += 2;
- // 相对SOC
- // $soc = self::stitching($body, $i, 1);
- $i += 1;
- // 电池余量
- // $battery_mb_free = self::stitching($body, $i, 2);
- return [
- 'box_no' => $box_no,
- 'bike_status' => $this->decodeBikeStatus($bike_status),
- 'battery_voltage' => $this->decodeBatteryVoltage($battery_voltage),
- ];
- }
- }
|