cqpCow 2 years ago
parent
commit
5e62ed842a
1 changed files with 136 additions and 129 deletions
  1. 136 129
      app/Console/Commands/ReadCommand.php

+ 136 - 129
app/Console/Commands/ReadCommand.php

@@ -33,6 +33,9 @@ class ReadCommand extends Command
         parent::__construct();
     }
 
+    const desktop_port = 9501; //桌面设备端口
+    const inventory_port = 7880; //盘点设备端口
+
     /**
      * Execute the console command.
      *
@@ -44,108 +47,6 @@ class ReadCommand extends Command
         $this->info('Your command executed!');
     }
 
-    //读取数据
-    public function readTxtForData()
-    {
-        $directory = env('PROCESS_DIR',null);
-        if(! $directory) {
-            echo '未找到日志文件位置,请确认工具运行位置,再进行配置!';die;
-        }
-
-        $newLogFile = storage_path().'/app/public/record_device.txt';
-//        $positionFile = storage_path().'/app/public/position_device.txt';
-
-        date_default_timezone_set("PRC");
-        $key = date("Y-m-d",time());
-        $position = Cache::get($key);
-
-        // 读取上次记录的位置
-        $lastPosition = 0;
-        if ($position) $lastPosition = $position;
-//        if (file_exists($positionFile)) {
-//            $lastPosition = intval(file_get_contents($positionFile));
-//        }
-
-        // 持续监视日志文件的变化
-        while (true) {
-            $key = date("Y-m-d",time());
-
-            //日志源 文件位置
-            $logFile = $directory . "/TesttoolLog" . $key . '.log';
-
-            // 检查文件是否有更新
-            clearstatcache(true, $logFile);
-            $currentFileSize = filesize($logFile);
-
-            echo "当前指针位置:$currentFileSize 上一次指针位置:$lastPosition \n";
-
-            if ($currentFileSize > $lastPosition) {
-                // 打开原始日志文件和新的日志文件
-                $fileHandle = fopen($logFile, 'r');
-                $newFileHandle = fopen($newLogFile, 'a'); // 使用追加模式打开新的日志文件
-
-                // 移动文件指针到上次记录的位置
-                fseek($fileHandle, $lastPosition);
-
-                // 逐行读取并处理新增内容
-                $read_data = [];
-                while (($line = fgets($fileHandle)) !== false) {
-                    // 提取Tag数字
-                    $pattern = '/Tag:(\d+)/';
-                    preg_match($pattern, $line, $matches);
-
-                    if (isset($matches[1])) {
-                        $tagNumber = $matches[1];
-                        echo "接收到的数据:" . $tagNumber . "\n";
-
-                        $read_data[] = $tagNumber;
-                        // 将Tag数字写入新的日志文件
-                        fwrite($newFileHandle, $tagNumber . PHP_EOL);
-                    }
-                }
-
-                if(! empty($read_data)) {
-                    $this->sendData($read_data);
-                    echo '发送成功---!' . "\n";
-                }
-
-                // 获取新的位置并更新位置文件
-                $lastPosition = ftell($fileHandle);
-                Cache::put($key,$lastPosition,86400);
-//                file_put_contents($positionFile, $lastPosition);
-
-                // 关闭文件句柄
-                fclose($fileHandle);
-                fclose($newFileHandle);
-            } else {
-                echo '暂无新数据...' . "\n";
-                // 在下一次检查之前休眠一段时间
-                sleep(2);
-            }
-        }
-    }
-
-    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);
-    }
-
     public function tcpServer(){
         //本地ipv4地址
         $ipv4 = $this->getMyIpV4();
@@ -153,7 +54,7 @@ class ReadCommand extends Command
         $host = env('HOST_TCP');  // 主机局域网地址
         $port = env('HOST_PORT'); // 端口号
         if(empty($host)) $host = $ipv4;
-        if(empty($port)) $port = 9501;
+        if(empty($port)) $port = self::desktop_port;
 
         // 创建一个TCP socket
         $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
@@ -208,6 +109,62 @@ class ReadCommand extends Command
         socket_close($socket);
     }
 
+    //桌面设备 数据解析发送
+    public function byteParsing3($data){
+        $hexData = bin2hex($data);
+        $hexData = str_replace(' ', '', $hexData);
+        $toReplace = array("a6a8001300000a00010101001708120012000103b5a5a7", "a6a8000c00000a0003120001037da5a7");
+        $result = str_replace($toReplace, "", $hexData);
+//        echo "原数据:" . $hexData . PHP_EOL;
+//        $memoryUsage = memory_get_usage();
+//        echo "当前脚本占用的内存量:".$memoryUsage." 字节" . "\n";
+//
+//        $memoryUsage = memory_get_peak_usage();
+//        echo "PHP程序总内存使用量:" . $memoryUsage . " 字节" . "\n";
+
+        if($result){
+            $last38Chars = substr($result, -38, 32);
+            echo "替换后:" . $last38Chars . PHP_EOL;
+
+            file_put_contents('C:\Users\Administrator\Desktop\record.txt',$last38Chars.PHP_EOL,8);
+            if(! empty($last38Chars)) {
+                $this->sendData([$last38Chars]);
+                echo '发送成功---!' . "\n";
+            }
+        }
+    }
+
+    //盘点设备 数据解析发送
+    // 5a55167fa90d010cbbdd8204000116010a000000000000476a69
+    // 5a55166dcd0d010cbbdd8204000116010a000000000000596a69
+    // 5a551679b50d010cbbdd8204000116010a0000000000004d6a69
+    //
+    public function byteParsingInventory($data){
+        $hexData = bin2hex($data);
+        $hexData = str_replace(' ', '', $hexData);
+        $result = $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";
+            }
+        }
+    }
+
+    //获取本机ipv4地址
+    public function getMyIpV4(){
+        $ip = exec("ipconfig | findstr /i \"IPv4\"");
+        $parts = explode(": ", $ip);
+        $ipAddress = $parts[1];
+        return $ipAddress;
+    }
+
+    //----------------------暂时没用的代码--------------------------------------------------------
+
     public function byteParsing2($data){
         $frame = $data;
         // 提取字段值
@@ -224,7 +181,7 @@ class ReadCommand extends Command
 
 // 将4个字节转换为无符号整数(根据您的数据编码方式确定是否需要使用符号整数)
         $payloadValue = unpack('N', $payloadBytes)[1];
-            echo "zhen" . $payloadValue . "\n";
+        echo "zhen" . $payloadValue . "\n";
         $payloadHex = sprintf('%08X', $payloadValue);
 
         echo "帧载荷值: $payloadHex\n";
@@ -318,34 +275,84 @@ class ReadCommand extends Command
         }
     }
 
-    public function byteParsing3($data){
-        $hexData = bin2hex($data);
-        $hexData = str_replace(' ', '', $hexData);
-        $toReplace = array("a6a8001300000a00010101001708120012000103b5a5a7", "a6a8000c00000a0003120001037da5a7");
-        $result = str_replace($toReplace, "", $hexData);
-//        echo "原数据:" . $hexData . PHP_EOL;
-//        $memoryUsage = memory_get_usage();
-//        echo "当前脚本占用的内存量:".$memoryUsage." 字节" . "\n";
-//
-//        $memoryUsage = memory_get_peak_usage();
-//        echo "PHP程序总内存使用量:" . $memoryUsage . " 字节" . "\n";
+    //读取数据
+    public function readTxtForData()
+    {
+        $directory = env('PROCESS_DIR',null);
+        if(! $directory) {
+            echo '未找到日志文件位置,请确认工具运行位置,再进行配置!';die;
+        }
 
-        if($result){
-            $last38Chars = substr($result, -38, 32);
-            echo "替换后:" . $last38Chars . PHP_EOL;
+        $newLogFile = storage_path().'/app/public/record_device.txt';
+//        $positionFile = storage_path().'/app/public/position_device.txt';
 
-            file_put_contents('C:\Users\Administrator\Desktop\record.txt',$last38Chars.PHP_EOL,8);
-            if(! empty($last38Chars)) {
-                $this->sendData([$last38Chars]);
-                echo '发送成功---!' . "\n";
+        date_default_timezone_set("PRC");
+        $key = date("Y-m-d",time());
+        $position = Cache::get($key);
+
+        // 读取上次记录的位置
+        $lastPosition = 0;
+        if ($position) $lastPosition = $position;
+//        if (file_exists($positionFile)) {
+//            $lastPosition = intval(file_get_contents($positionFile));
+//        }
+
+        // 持续监视日志文件的变化
+        while (true) {
+            $key = date("Y-m-d",time());
+
+            //日志源 文件位置
+            $logFile = $directory . "/TesttoolLog" . $key . '.log';
+
+            // 检查文件是否有更新
+            clearstatcache(true, $logFile);
+            $currentFileSize = filesize($logFile);
+
+            echo "当前指针位置:$currentFileSize 上一次指针位置:$lastPosition \n";
+
+            if ($currentFileSize > $lastPosition) {
+                // 打开原始日志文件和新的日志文件
+                $fileHandle = fopen($logFile, 'r');
+                $newFileHandle = fopen($newLogFile, 'a'); // 使用追加模式打开新的日志文件
+
+                // 移动文件指针到上次记录的位置
+                fseek($fileHandle, $lastPosition);
+
+                // 逐行读取并处理新增内容
+                $read_data = [];
+                while (($line = fgets($fileHandle)) !== false) {
+                    // 提取Tag数字
+                    $pattern = '/Tag:(\d+)/';
+                    preg_match($pattern, $line, $matches);
+
+                    if (isset($matches[1])) {
+                        $tagNumber = $matches[1];
+                        echo "接收到的数据:" . $tagNumber . "\n";
+
+                        $read_data[] = $tagNumber;
+                        // 将Tag数字写入新的日志文件
+                        fwrite($newFileHandle, $tagNumber . PHP_EOL);
+                    }
+                }
+
+                if(! empty($read_data)) {
+                    $this->sendData($read_data);
+                    echo '发送成功---!' . "\n";
+                }
+
+                // 获取新的位置并更新位置文件
+                $lastPosition = ftell($fileHandle);
+                Cache::put($key,$lastPosition,86400);
+//                file_put_contents($positionFile, $lastPosition);
+
+                // 关闭文件句柄
+                fclose($fileHandle);
+                fclose($newFileHandle);
+            } else {
+                echo '暂无新数据...' . "\n";
+                // 在下一次检查之前休眠一段时间
+                sleep(2);
             }
         }
     }
-
-    public function getMyIpV4(){
-        $ip = exec("ipconfig | findstr /i \"IPv4\"");
-        $parts = explode(": ", $ip);
-        $ipAddress = $parts[1];
-        return $ipAddress;
-    }
 }