ClientServer.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace App\Handlers;
  3. use GatewayClient\Gateway;
  4. use Illuminate\Support\Facades\Log;
  5. use Illuminate\Support\Facades\Redis;
  6. class ClientServer
  7. {
  8. use HelperTrait, OrderTrait;
  9. const RUN_CMD = '5a';
  10. //运行命令
  11. const STATUS_CMD = 'a5';
  12. //设备状态
  13. const LOCATION_CMD = 'a6';
  14. //设备位置
  15. const CACHE_KEY = 'CMD:RESPONSE:';
  16. public static $registerAddress = '127.0.0.1:1238';
  17. /**
  18. * 开始命令
  19. * @param $box_no
  20. * @param $mode
  21. * @param $time
  22. * @param int $reset
  23. * @param bool $is_response
  24. * Author: Mead
  25. */
  26. public static function startBox($box_no, $mode, $time_a, $time_b, $reset = 0, $is_response = true)
  27. {
  28. $time_a = str_pad(dechex($time_a), 2, 0, STR_PAD_LEFT);
  29. $time_b = str_pad(dechex($time_b), 2, 0, STR_PAD_LEFT);
  30. $body = [
  31. "0{$reset}",
  32. "0{$mode}",
  33. $time_a,
  34. $time_b
  35. ];
  36. return self::send(self::RUN_CMD, $box_no, $body, $is_response);
  37. }
  38. /**
  39. * @param $cmd
  40. * @param $box_no
  41. * @param array $body
  42. * @param bool $is_response
  43. * @return int -1离线 0发送失败(终端执行) 1正常 3超时
  44. * Author: Mead
  45. */
  46. private static function send($cmd, $box_no, $body = [], $is_response = true)
  47. {
  48. Gateway::$registerAddress = self::$registerAddress;
  49. //是否在线
  50. if (!Gateway::isUidOnline($box_no)) {
  51. Log::error('设备不在线' . $box_no);
  52. return -1;
  53. }
  54. Log::error("sendMSG||" . $box_no . '||' . arr2str($body, '||'));
  55. $response = [
  56. 'aa',
  57. $cmd,
  58. str_pad(dechex(count($body)), 2, 0, STR_PAD_LEFT)
  59. ];
  60. $response = array_merge($response, $body);
  61. $response[] = self::sumDexArr($response);
  62. $responseBody = strtolower(implode('', $response)) . '0d0a';
  63. Gateway::sendToUid($box_no, hex2bin($responseBody));
  64. if (!$is_response) {
  65. //不进行状态监测
  66. return $responseBody;
  67. }
  68. $redis = Redis::connection();
  69. $result = 3;
  70. for ($i = 1; $i <= 30; $i++) {
  71. usleep(500000);
  72. $data = $redis->get(self::CACHE_KEY . $box_no . ':' . $cmd);
  73. if ($data != null) {
  74. $result = self::getCache($data);
  75. $redis->del(self::CACHE_KEY . $box_no . ':' . $cmd);
  76. break;
  77. }
  78. //补发
  79. if (in_array($i, [10, 20])) {
  80. Gateway::sendToUid($box_no, hex2bin($responseBody));
  81. }
  82. }
  83. return $result;
  84. }
  85. /**
  86. * 获取设备状态
  87. * @param $box_no
  88. * @return int
  89. * Author: Mead
  90. */
  91. public static function getBoxStatus($box_no, $is_response = true)
  92. {
  93. $body = [
  94. '5a'
  95. ];
  96. return self::send(self::STATUS_CMD, $box_no, $body, $is_response);
  97. }
  98. /**
  99. * 获取定位
  100. * @param $box_no
  101. * @return int
  102. * Author: Mead
  103. */
  104. public static function getLocation($box_no, $is_response = true)
  105. {
  106. $body = [
  107. 'a5'
  108. ];
  109. return self::send(self::LOCATION_CMD, $box_no, $body, $is_response);
  110. }
  111. public static function getCache($val)
  112. {
  113. $array = explode('||', $val);
  114. if (count($array) !== 2) return false;
  115. list($type, $data) = $array;
  116. switch ($type) {
  117. case 'string':
  118. return $data;
  119. break;
  120. case 'array':
  121. return json_decode($data, true);
  122. }
  123. return false;
  124. }
  125. }