BoxSyncServer.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 BoxSyncServer extends BaseServer
  11. {
  12. use BatteryTraitModel;
  13. public function main($body)
  14. {
  15. $data = $this->decode($body);
  16. self::log($data, 'BoxSyncServer', self::$LOG_MAJOR);
  17. return $this->response();
  18. }
  19. protected function response()
  20. {
  21. $time = time();
  22. $hex_time = dechex($time);
  23. $body = [
  24. $hex_time
  25. ];
  26. $body = str_split(implode('', $body), 2);
  27. return $body;
  28. }
  29. /**
  30. * 解析装载的数据
  31. * @param $body
  32. * @return array
  33. * User: Mead
  34. */
  35. private function decode($body)
  36. {
  37. $i = 0;
  38. $box_no = self::stitching($body, $i, 5);
  39. $i += 5;
  40. // 设备时间
  41. $time = self::stitching($body, $i, 4);
  42. // 处理值
  43. $box_no = $this->decodeBoxNo($box_no);
  44. $time = $this->decodeTime($time);
  45. return [
  46. 'box_no' => $box_no,
  47. 'time' => $time,
  48. ];
  49. }
  50. }