ReadCommand.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Jobs\SendDataJob;
  4. use App\Model\BigKingCbj;
  5. use App\Service\DwyService;
  6. use App\Service\LabelDealService;
  7. use Illuminate\Console\Command;
  8. use Illuminate\Support\Facades\Cache;
  9. use Illuminate\Support\Facades\DB;
  10. use Illuminate\Support\Facades\Log;
  11. use Illuminate\Support\Facades\Redis;
  12. class ReadCommand extends Command
  13. {
  14. /**
  15. * The name and signature of the console command.
  16. *
  17. * @var string
  18. */
  19. protected $signature = 'command:tcp';
  20. /**
  21. * The console command description.
  22. *
  23. * @var string
  24. */
  25. protected $description = '读取文件';
  26. /**
  27. * Create a new command instance.
  28. *
  29. * @return void
  30. */
  31. public function __construct()
  32. {
  33. parent::__construct();
  34. }
  35. const inventory_port = 7880; //盘点设备端口
  36. /**
  37. * Execute the console command.
  38. *
  39. * @return mixed
  40. */
  41. public function handle()
  42. {
  43. $this->tcpServer();
  44. $this->info('Your command executed!');
  45. }
  46. public function tcpServer(){
  47. $host = "121.41.102.225";
  48. $port = self::inventory_port;
  49. // 创建一个TCP socket
  50. $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  51. // 绑定到指定的主机和端口 第二个参数不写 默认局域网ipv4地址
  52. socket_bind($socket, '', $port);
  53. // 监听连接
  54. socket_listen($socket);
  55. // 初始内存使用量
  56. $initialMemoryUsage = memory_get_usage();
  57. echo date("Y-m-d H:i:s") . " 服务器" . $host . ":" . $port . "已启动监听\n";
  58. // file_put_contents('C:\Users\Administrator\Desktop\record.txt',date("Y-m-d H:i:s") . "服务器已启动".PHP_EOL,8);
  59. while (true) {
  60. // 接受连接请求并创建新的套接字
  61. $clientSocket = socket_accept($socket);
  62. if ($clientSocket === false) {
  63. // 错误处理
  64. $error = socket_strerror(socket_last_error($socket));
  65. echo "接受连接请求失败:{$error}\n";
  66. continue; // 继续下一次循环
  67. }
  68. // 读取客户端发送的数据
  69. $data = socket_read($clientSocket, 1024);
  70. // file_put_contents('C:\Users\Administrator\Desktop\record2.txt',$data .PHP_EOL,8);
  71. $this->byteParsingInventory($data);
  72. // 发送响应给客户端
  73. // $response = "服务器已接收到您的消息:" . $data;
  74. // socket_write($clientSocket, $response, strlen($response));
  75. $currentMemoryUsage = memory_get_usage();
  76. $memoryDiff = $currentMemoryUsage - $initialMemoryUsage;
  77. echo "内存使用量变化:" . $memoryDiff . " 字节\n";
  78. $initialMemoryUsage = $currentMemoryUsage;
  79. // 关闭当前连接的套接字
  80. socket_close($clientSocket);
  81. gc_collect_cycles();
  82. sleep(5);
  83. }
  84. // 关闭服务器套接字
  85. socket_close($socket);
  86. }
  87. //盘点设备 数据解析发送
  88. // 5a55167fa90d010cbbdd8204000116010a000000000000476a69
  89. // 5a55166dcd0d010cbbdd8204000116010a000000000000596a69
  90. // 5a551679b50d010cbbdd8204000116010a0000000000004d6a69
  91. //
  92. public function byteParsingInventory($data){
  93. Log::channel('request')->info('request', ['paramlog'=> $data]);
  94. DB::table('test')->insert(['data' => $data]);
  95. echo $data . PHP_EOL;
  96. $hexData = bin2hex($data);
  97. $hexData = str_replace(' ', '', $hexData);
  98. // $toReplace = array("5a55167fa90d010cbbdd8204000116010a000000000000476a69", "5a55166dcd0d010cbbdd8204000116010a000000000000596a69","5a551679b50d010cbbdd8204000116010a0000000000004d6a69","5a551673c10d010cbbdd8204000116010a000000000000536a69","5a55167faa0d010cbbdd8204000116010a000000000000486a69","5a551679b60d010cbbdd8204000116010a0000000000004e6a69");
  99. $toReplace = [];
  100. $result = str_replace($toReplace, "", $hexData);
  101. }
  102. //获取本机ipv4地址
  103. public function getMyIpV4(){
  104. $ip = exec("ipconfig | findstr /i \"IPv4\"");
  105. $parts = explode(": ", $ip);
  106. $ipAddress = $parts[1];
  107. return $ipAddress;
  108. }
  109. public function sendData($data){
  110. $url = env('CLOUD_ADDRESS',null);
  111. if(empty($url)) return;
  112. $curl = curl_init();
  113. curl_setopt_array($curl, array(
  114. CURLOPT_URL => $url,
  115. CURLOPT_RETURNTRANSFER => true,
  116. CURLOPT_ENCODING => '',
  117. CURLOPT_MAXREDIRS => 10,
  118. CURLOPT_TIMEOUT => 0,
  119. CURLOPT_FOLLOWLOCATION => true,
  120. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  121. CURLOPT_CUSTOMREQUEST => 'POST',
  122. CURLOPT_POSTFIELDS => $data,
  123. ));
  124. $response = curl_exec($curl);
  125. curl_close($curl);
  126. }
  127. public function cbjSettle(){
  128. $id = 0;
  129. BigKingCbj::where('id',0);
  130. $data = $this->data;
  131. $dv = $data['key'];
  132. $return = $box_list = [];
  133. //处理数据
  134. LabelDealService::getInstance()->clearData($data,$return,$box_list);
  135. //调用外部方法
  136. list($lead_bind,$lead_out) = DwyService::getInstance()->setBoxData($this->header,$dv,$return,$box_list,$data);
  137. //调用保存接口
  138. LabelDealService::getInstance()->boxOut($lead_bind,$lead_out,$this->header,$this->id);
  139. }
  140. }