123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- <?php
- namespace App\Console\Commands;
- use App\Jobs\SendDataJob;
- use App\Model\BigKingCbj;
- use App\Service\DwyService;
- use App\Service\LabelDealService;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\Cache;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\Redis;
- class ReadCommand extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'command:tcp';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = '读取文件';
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct()
- {
- parent::__construct();
- }
- const inventory_port = 7880; //盘点设备端口
- /**
- * Execute the console command.
- *
- * @return mixed
- */
- public function handle()
- {
- $this->tcpServer();
- $this->info('Your command executed!');
- }
- public function tcpServer(){
- $host = "121.41.102.225";
- $port = self::inventory_port;
- // 创建一个TCP socket
- $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
- // 绑定到指定的主机和端口 第二个参数不写 默认局域网ipv4地址
- socket_bind($socket, '', $port);
- // 监听连接
- socket_listen($socket);
- // 初始内存使用量
- $initialMemoryUsage = memory_get_usage();
- echo date("Y-m-d H:i:s") . " 服务器" . $host . ":" . $port . "已启动监听\n";
- // file_put_contents('C:\Users\Administrator\Desktop\record.txt',date("Y-m-d H:i:s") . "服务器已启动".PHP_EOL,8);
- while (true) {
- // 接受连接请求并创建新的套接字
- $clientSocket = socket_accept($socket);
- if ($clientSocket === false) {
- // 错误处理
- $error = socket_strerror(socket_last_error($socket));
- echo "接受连接请求失败:{$error}\n";
- continue; // 继续下一次循环
- }
- // 读取客户端发送的数据
- $data = socket_read($clientSocket, 1024);
- // file_put_contents('C:\Users\Administrator\Desktop\record2.txt',$data .PHP_EOL,8);
- $this->byteParsingInventory($data);
- // 发送响应给客户端
- // $response = "服务器已接收到您的消息:" . $data;
- // socket_write($clientSocket, $response, strlen($response));
- $currentMemoryUsage = memory_get_usage();
- $memoryDiff = $currentMemoryUsage - $initialMemoryUsage;
- echo "内存使用量变化:" . $memoryDiff . " 字节\n";
- $initialMemoryUsage = $currentMemoryUsage;
- // 关闭当前连接的套接字
- socket_close($clientSocket);
- gc_collect_cycles();
- sleep(5);
- }
- // 关闭服务器套接字
- socket_close($socket);
- }
- //盘点设备 数据解析发送
- // 5a55167fa90d010cbbdd8204000116010a000000000000476a69
- // 5a55166dcd0d010cbbdd8204000116010a000000000000596a69
- // 5a551679b50d010cbbdd8204000116010a0000000000004d6a69
- //
- public function byteParsingInventory($data){
- Log::channel('request')->info('request', ['paramlog'=> $data]);
- DB::table('test')->insert(['data' => $data]);
- echo $data . PHP_EOL;
- $hexData = bin2hex($data);
- $hexData = str_replace(' ', '', $hexData);
- // $toReplace = array("5a55167fa90d010cbbdd8204000116010a000000000000476a69", "5a55166dcd0d010cbbdd8204000116010a000000000000596a69","5a551679b50d010cbbdd8204000116010a0000000000004d6a69","5a551673c10d010cbbdd8204000116010a000000000000536a69","5a55167faa0d010cbbdd8204000116010a000000000000486a69","5a551679b60d010cbbdd8204000116010a0000000000004e6a69");
- $toReplace = [];
- $result = str_replace($toReplace, "", $hexData);
- }
- //获取本机ipv4地址
- public function getMyIpV4(){
- $ip = exec("ipconfig | findstr /i \"IPv4\"");
- $parts = explode(": ", $ip);
- $ipAddress = $parts[1];
- 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);
- }
- public function cbjSettle(){
- $id = 0;
- BigKingCbj::where('id',0);
- $data = $this->data;
- $dv = $data['key'];
- $return = $box_list = [];
- //处理数据
- LabelDealService::getInstance()->clearData($data,$return,$box_list);
- //调用外部方法
- list($lead_bind,$lead_out) = DwyService::getInstance()->setBoxData($this->header,$dv,$return,$box_list,$data);
- //调用保存接口
- LabelDealService::getInstance()->boxOut($lead_bind,$lead_out,$this->header,$this->id);
- }
- }
|