cqpCow 1 éve
szülő
commit
34d7bb1511
1 módosított fájl, 102 hozzáadás és 11 törlés
  1. 102 11
      app/Console/Commands/ReadCommand.php

+ 102 - 11
app/Console/Commands/ReadCommand.php

@@ -5,6 +5,7 @@ namespace App\Console\Commands;
 use App\Jobs\SendDataJob;
 use Illuminate\Console\Command;
 use Illuminate\Support\Facades\Cache;
+use Illuminate\Support\Facades\DB;
 use Illuminate\Support\Facades\Redis;
 
 class ReadCommand extends Command
@@ -54,7 +55,7 @@ class ReadCommand extends Command
         $host = env('HOST_TCP');  // 主机局域网地址
         $port = env('HOST_PORT'); // 端口号
         if(empty($host)) $host = $ipv4;
-        if(empty($port)) $port = self::desktop_port;
+        if(empty($port)) $port = self::inventory_port;
 
         // 创建一个TCP socket
         $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
@@ -84,7 +85,7 @@ class ReadCommand extends Command
             $data = socket_read($clientSocket, 1024);
 //            file_put_contents('C:\Users\Administrator\Desktop\record2.txt',$data .PHP_EOL,8);
 
-            $this->byteParsing3($data);
+            $this->byteParsingInventory($data);
 
             // 发送响应给客户端
 //            $response = "服务器已接收到您的消息:" . $data;
@@ -140,19 +141,62 @@ class ReadCommand extends Command
     // 5a551679b50d010cbbdd8204000116010a0000000000004d6a69
     //
     public function byteParsingInventory($data){
-        $hexData = bin2hex($data);
-        $hexData = str_replace(' ', '', $hexData);
-        $result = $hexData;
+        $frame = $this->test($data);
+        // 获取传递的数据
+        $payload = $frame['payload'];
+        // 输出传递的数据
+//        echo bin2hex($payload);die;
+
+//        echo "替换后:" . $data . PHP_EOL;
+        DB::table('test')->insert(['data' => bin2hex($payload)]);
+//        $hexData = bin2hex($data);
+//        $hexData = str_replace(' ', '', $hexData);
+////        $toReplace = array("5a55167fa90d010cbbdd8204000116010a000000000000476a69", "5a55166dcd0d010cbbdd8204000116010a000000000000596a69","5a551679b50d010cbbdd8204000116010a0000000000004d6a69","5a551673c10d010cbbdd8204000116010a000000000000536a69","5a55167faa0d010cbbdd8204000116010a000000000000486a69","5a551679b60d010cbbdd8204000116010a0000000000004e6a69");
+//        $toReplace = [];
+//        $result = str_replace($toReplace, "", $hexData);
+//
+//        if($result){
+//            echo "替换后:" . $result . PHP_EOL;
+//
+//            file_put_contents('C:\Users\Administrator\Desktop\record.txt',$result.PHP_EOL,8);
+//            if(! empty($last38Chars)) {
+//                $this->sendData([$last38Chars]);
+//                echo '发送成功---!' . "\n";
+//            }
+//        }
+    }
 
-        if($result){
-            echo "替换后:" . $result . PHP_EOL;
+    public function test($data){
+        $frame = array();
 
-            file_put_contents('C:\Users\Administrator\Desktop\record.txt',$result.PHP_EOL,8);
-            if(! empty($last38Chars)) {
-                $this->sendData([$last38Chars]);
-                echo '发送成功---!' . "\n";
+        // 查找帧头
+        $start = strpos($data, "\x5A\x55");
+
+        if ($start !== false) {
+            // 查找帧尾
+            $end = strpos($data, "\x6A\x69", $start);
+
+            if ($end !== false) {
+                // 获取帧头到帧尾之间的数据
+                $frameData = substr($data, $start + 2, $end - $start - 2);
+
+                // 解析帧头子域、帧长度子域等字段
+
+                // 获取帧长度子域值
+                $frameLength = ord($frameData[0]);
+
+                // 获取端口子域值
+                $port = ord($frameData[3]);
+
+                // 获取数据载荷子域
+                $payload = substr($frameData, 6, $frameLength - 7);
+
+                // 存储解析结果
+                $frame['payload'] = $payload;
             }
         }
+
+        return $frame;
     }
 
     //获取本机ipv4地址
@@ -163,8 +207,49 @@ class ReadCommand extends Command
         return $ipAddress;
     }
 
+    public function sendData($data){
+        $url = env('CLOUD_ADDRESS',null);
+
+        if(empty($url)) return;
+
+        $curl = curl_init();
+        curl_setopt_array($curl, array(
+            CURLOPT_URL => $url,
+            CURLOPT_RETURNTRANSFER => true,
+            CURLOPT_ENCODING => '',
+            CURLOPT_MAXREDIRS => 10,
+            CURLOPT_TIMEOUT => 0,
+            CURLOPT_FOLLOWLOCATION => true,
+            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
+            CURLOPT_CUSTOMREQUEST => 'POST',
+            CURLOPT_POSTFIELDS => $data,
+        ));
+        $response = curl_exec($curl);
+        curl_close($curl);
+    }
+
     //----------------------暂时没用的代码--------------------------------------------------------
 
+    function convertToHex($string) {
+        $encoding = mb_detect_encoding($string);
+        switch ($encoding) {
+            case 'ASCII':
+                return bin2hex($string);
+            case 'GBK':
+                return mb_convert_encoding($string, 'GBK', $encoding);
+            case 'GB2312':
+                return mb_convert_encoding($string, 'GBK', $encoding);
+            case 'UTF-8':
+                return bin2hex($string);
+            case 'Unicode':
+                // 对于Unicode编码,需要使用适当的函数或库来进行转换
+                // 这里使用PHP的mb_convert_encoding函数将Unicode编码转换为UTF-8编码,然后再转换为十六进制
+                return bin2hex(mb_convert_encoding($string, 'UTF-8', $encoding));
+            default:
+                return 'Unsupported encoding';
+        }
+    }
+
     public function byteParsing2($data){
         $frame = $data;
         // 提取字段值
@@ -355,4 +440,10 @@ class ReadCommand extends Command
             }
         }
     }
+
+    public function cs(){
+        $filePath = public_path('rfidreader-1.1.0.jar');
+        $command = "java -cp $filePath com.example.MyJavaClass methodName argument1 argument2";
+        $output = shell_exec($command);
+    }
 }