123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- <?php
- namespace App\Servers;
- use App\Common\ClientTrait;
- use App\Common\HelperTrait;
- use \GatewayWorker\Lib\Gateway;
- use Illuminate\Support\Facades\Redis;
- class ClientServer
- {
- use ClientTrait, HelperTrait;
- const RUN_CMD = '5a';
- //运行命令
- const STATUS_CMD = 'a5';
- //设备状态
- const LOCATION_CMD = 'a6';
- //设备位置
- const CACHE_KEY = 'qingyu_CMD:RESPONSE:';
- private static $registerAddress = '127.0.0.1:1238';
- /**
- * 开始命令
- * @param $box_no
- * @param $mode
- * @param $time
- * @param int $reset
- * @param bool $is_response
- * Author: Mead
- */
- public static function startBox($box_no, $mode, $time, $reset = 1, $is_response = true)
- {
- $time = dechex($time);
- $body = [
- "0{$reset}",
- "0{$mode}",
- $time
- ];
- return self::send(self::RUN_CMD, $box_no, $body, $is_response);
- }
- /**
- * @param $cmd
- * @param $box_no
- * @param array $body
- * @param bool $is_response
- * @return int -1离线 0发送失败(终端执行) 1正常 3超时
- * Author: Mead
- */
- private static function send($cmd, $box_no, $body = [], $is_response = true)
- {
- //是否在线
- if (!Gateway::isUidOnline($box_no)) return -1;
- $response = [
- 'aa',
- $cmd,
- count($body)
- ];
- $response = array_merge($response, $body);
- $response[] = self::sumDexArr($response);
- $responseBody = strtolower(implode('', $response)) . '0d0a';
- Gateway::sendToUid($box_no, $responseBody);
- if (!$is_response) {
- //不进行状态监测
- return 1;
- }
- $redis = Redis::connection();
- $result = 3;
- for ($i = 1; $i <= 30; $i++) {
- usleep(500000);
- $data = $redis->get(self::CACHE_KEY . $box_no . ':' . $cmd);
- if ($data != null) {
- $result = $data;
- break;
- }
- //补发
- if (in_array($i, [10, 20])) {
- Gateway::sendToUid($box_no, $responseBody);
- }
- }
- return $result;
- }
- /**
- * 获取设备状态
- * @param $box_no
- * @return int
- * Author: Mead
- */
- public static function getBoxStatus($box_no)
- {
- $body = [
- '5a'
- ];
- return self::send(self::STATUS_CMD, $box_no, $body, true);
- }
- /**
- * 获取定位
- * @param $box_no
- * @return int
- * Author: Mead
- */
- public static function getLocation($box_no)
- {
- $body = [
- 'a6'
- ];
- return self::send(self::LOCATION_CMD, $box_no, $body, true);
- }
- }
|