123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- <?php
- namespace App\Handlers;
- use GatewayClient\Gateway;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\Redis;
- class ClientServer
- {
- use HelperTrait, OrderTrait;
- const RUN_CMD = '5a';
- //运行命令
- const STATUS_CMD = 'a5';
- //设备状态
- const LOCATION_CMD = 'a6';
- //设备位置
- const CACHE_KEY = 'CMD:RESPONSE:';
- public 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_a, $time_b, $reset = 0, $is_response = true)
- {
- $time_a = str_pad(dechex($time_a), 2, 0, STR_PAD_LEFT);
- $time_b = str_pad(dechex($time_b), 2, 0, STR_PAD_LEFT);
- $body = [
- "0{$reset}",
- "0{$mode}",
- $time_a,
- $time_b
- ];
- 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)
- {
- Gateway::$registerAddress = self::$registerAddress;
- //是否在线
- if (!Gateway::isUidOnline($box_no)) {
- Log::error('设备不在线' . $box_no);
- return -1;
- }
- Log::error("sendMSG||" . $box_no . '||' . arr2str($body, '||'));
- $response = [
- 'aa',
- $cmd,
- str_pad(dechex(count($body)), 2, 0, STR_PAD_LEFT)
- ];
- $response = array_merge($response, $body);
- $response[] = self::sumDexArr($response);
- $responseBody = strtolower(implode('', $response)) . '0d0a';
- Gateway::sendToUid($box_no, hex2bin($responseBody));
- if (!$is_response) {
- //不进行状态监测
- return $responseBody;
- }
- $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 = self::getCache($data);
- $redis->del(self::CACHE_KEY . $box_no . ':' . $cmd);
- break;
- }
- //补发
- if (in_array($i, [10, 20])) {
- Gateway::sendToUid($box_no, hex2bin($responseBody));
- }
- }
- return $result;
- }
- /**
- * 获取设备状态
- * @param $box_no
- * @return int
- * Author: Mead
- */
- public static function getBoxStatus($box_no, $is_response = true)
- {
- $body = [
- '5a'
- ];
- return self::send(self::STATUS_CMD, $box_no, $body, $is_response);
- }
- /**
- * 获取定位
- * @param $box_no
- * @return int
- * Author: Mead
- */
- public static function getLocation($box_no, $is_response = true)
- {
- $body = [
- 'a5'
- ];
- return self::send(self::LOCATION_CMD, $box_no, $body, $is_response);
- }
- public static function getCache($val)
- {
- $array = explode('||', $val);
- if (count($array) !== 2) return false;
- list($type, $data) = $array;
- switch ($type) {
- case 'string':
- return $data;
- break;
- case 'array':
- return json_decode($data, true);
- }
- return false;
- }
- }
|