Files
jianfeng-server/app/im/controller/Car.php
2025-10-02 10:33:06 +08:00

178 lines
3.5 KiB
PHP
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
//include 'crc.php';
//include 'net.php';
//打印十六进制(可以不用)
function println($bytes) {
for($i=0;$i<count($bytes);$i++)
{
printf("%02X ", $bytes[$i]);
}
printf(".");
}
//这里开始
//$net = new net();
$socket = listen('0.0.0.0', 50000);
while(true) {
$cs = accept($socket);
printf($cs);
while($cs) {
try{
$ret = recv_bytes($cs, 1024);
abc($ret);
//println($ret);
}
catch (Exception $e)
{
close();
printf('cs close');
break;
}
}
}
close();
function listen($ip, $port)
{
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind($socket, $ip, $port);
$ex = socket_listen($socket, 4);
//socket_bind($this->socket, '127.0.0.1', $port) or die("socket_bind() fail:" . socket_strerror(socket_last_error()) . "/n");
//$ex = socket_listen($this->socket, 4) or die("socket_listen() fail:" . socket_strerror(socket_last_error()) . "/n");
// return $ex;
return $socket;
}
//abc处理读取到的卡4字节
function abc($ret)
{
$len = $ret[4];
if($len < 14)
{
//printf('无效数据');
return ;
}
//
$count = $ret[9];
$count = ($count - 3) / 4;
//
for($i=0; $i<$count; $i++)
{
$idx = 10 + ($i * 4);
$card = sprintf("%02X%02X%02X%02X", $ret[$idx+0], $ret[$idx+1], $ret[$idx+2], $ret[$idx+3]);//这是获取到的卡数据(按文档意思可能会读到多张卡,处理这个$card就可以了
$card = sprintf("%02X%02X%02X", $ret[$idx+0], $ret[$idx+1], $ret[$idx+2]);//这是获取到的卡数据(按文档意思可能会读到多张卡,处理这个$card就可以了
printf('card['.$i.']>>'.$card.' | ');
// $post_data['data'] = 'card['.$i.']>>'.$card.' | ';
$card_id = $card;
$card = 'card['.$i.']>>'.$card.' | ';
$url = 'https://car.cncnconnect.com/massage/admin/Tcp/getInfo?card_id='.$card_id.'&time='.msectime();
$a = file_get_contents($url);
// printf(json_encode($a));
}
}
function msectime() {
list($msec,$sec) = explode(' ', microtime());
$msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
return $msectime;
}
function crulGetData($url, $post, $method, $header=1){
// $this->_errno = NULL;
// $this->_error = NULL;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
if($post != NULL)
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);
curl_setopt($ch, CURLOPT_AUTOREFERER,true);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
if($header)
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-type: application/json',
)
);
$result = curl_exec($ch);
return $result;
}
function accept($socket)
{
return socket_accept($socket);
}
function close()
{
if($this->socket == 0) return;
socket_close($this->socket);
$this->socket = 0;
}
function recv_bytes($cs, $len = 1024)
{
$ret = recv($cs, $len);
$bytes = string_to_bytes($ret);
return $bytes;
}
function recv($cs, $len = 1024)
{
$ret = socket_read($cs, $len) or die("/n");
return $ret;
}
function string_to_bytes($str)
{
$bytes = array();
for($i=0; $i<strlen($str); $i++)
{
$bytes[] = ord($str[$i]);
}
return $bytes;
}
?>