Files
tcpserver-flow/app/net/Packet.php
T
2026-03-08 22:58:56 +08:00

241 lines
6.3 KiB
PHP

<?php
namespace app\net;
use app\Exceptions\UnsupportedPacketException;
class Packet
{
/**
* @var string 原始报文字节数据
*/
public string $rawBytes {
get {
return $this->rawBytes;
}
}
/**
* @var string 原始报文的十六进制字符串
*/
public string $hexString {
get {
return $this->hexString;
}
}
/**
* @var int 十六进制字符串长度
*/
public int $hexLength {
get {
return $this->hexLength;
}
}
/**
* @var PacketType 报文类型 0:未知 1:有线读卡器 2:无线读卡器 3:电流采集器 4:新版电流采集器
*/
public PacketType $hexType = PacketType::UNKNOWN {
get {
return $this->hexType;
}
}
/**
* @var bool 是否匹配成功
*/
public bool $isMatched = false {
get {
return $this->isMatched;
}
}
/**
* @throws UnsupportedPacketException 当前报文未解析成功时抛出异常
* @var string 卡号
*/
public string $card = '' {
get {
if (!$this->isMatched) throw new UnsupportedPacketException();
return $this->card;
}
}
/**
* @throws UnsupportedPacketException 当前报文未解析成功时抛出异常
* @var string 读卡器编号
*/
public string $reader = '' {
get {
if (!$this->isMatched) throw new UnsupportedPacketException();
return $this->reader;
}
}
/**
* @throws UnsupportedPacketException 当前报文未解析成功时抛出异常
* @var string 检测设备/站号
*/
public string $detectionDevice = '' {
get {
if (!$this->isMatched) throw new UnsupportedPacketException();
return $this->detectionDevice;
}
}
/**
* @throws UnsupportedPacketException 当前报文未解析成功时抛出异常
* @var string 网关编号
*/
public string $gateway = '' {
get {
if (!$this->isMatched) throw new UnsupportedPacketException();
return $this->gateway;
}
}
/**
* @throws UnsupportedPacketException 当前报文未解析成功时抛出异常
* @var string 通道1数据
*/
public string $channel1 = '' {
get {
if (!$this->isMatched) throw new UnsupportedPacketException();
return $this->channel1;
}
}
/**
* @throws UnsupportedPacketException 当前报文未解析成功时抛出异常
* @var string 通道2数据
*/
public string $channel2 = '' {
get {
if (!$this->isMatched) throw new UnsupportedPacketException();
return $this->channel2;
}
}
/**
* @throws UnsupportedPacketException 当前报文未解析成功时抛出异常
* @var string 通道3数据
*/
public string $channel3 = '' {
get {
if (!$this->isMatched) throw new UnsupportedPacketException();
return $this->channel3;
}
}
/**
* @throws UnsupportedPacketException 当前报文未解析成功时抛出异常
* @var string 通道4数据
*/
public string $channel4 = '' {
get {
if (!$this->isMatched) throw new UnsupportedPacketException();
return $this->channel4;
}
}
/**
* @throws UnsupportedPacketException 当前报文未解析成功时抛出异常
* @var string 通道5数据
*/
public string $channel5 = '' {
get {
if (!$this->isMatched) throw new UnsupportedPacketException();
return $this->channel5;
}
}
/**
* @throws UnsupportedPacketException 当前报文未解析成功时抛出异常
* @var string 通道6数据
*/
public string $channel6 = '' {
get {
if (!$this->isMatched) throw new UnsupportedPacketException();
return $this->channel6;
}
}
/**
* @throws UnsupportedPacketException 当前报文未解析成功时抛出异常
* @var float 电池电量
*/
public float $batteryLevel = 0.0 {
get {
if (!$this->isMatched) throw new UnsupportedPacketException();
return $this->batteryLevel;
}
}
public string $rawText {
get => $this->rawText;
}
public int $length{
get => strlen($this->rawBytes);
}
/**
* 构造函数
* @param string $rawBytes 原始报文字节数据
* @param array $parsedData 解析后的属性数据(可选)
*/
public function __construct(string $rawBytes, array $parsedData = [])
{
// 初始化基础属性
$this->rawBytes = $rawBytes;
$this->hexString = strtoupper(bin2hex($rawBytes));
$this->hexLength = strlen($this->hexString);
// 合并解析后的属性
if (!empty($parsedData)) {
foreach ($parsedData as $key => $value) {
if (property_exists($this, $key)) {
// 处理报文类型枚举
if ($key === 'hexType' && is_int($value)) {
$this->hexType = PacketType::fromValue($value);
continue;
}
$this->$key = $value;
}
}
}
}
// ========== 对外暴露的读取方法 ==========
public function getRawText(): string
{
return $this->hexString;
}
/**
* 获取所有解析结果(数组形式)
*/
public function toArray(): array
{
return [
'hex' => $this->hexString,
'len' => $this->hexLength,
'hextype' => $this->hexType,
'match' => $this->isMatched ? 1 : 0,
'card' => $this->card,
'reader' => $this->reader,
'detection_device' => $this->detectionDevice,
'gateway' => $this->gateway,
'channel_1' => $this->channel1,
'channel_2' => $this->channel2,
'channel_3' => $this->channel3,
'channel_4' => $this->channel4,
'channel_5' => $this->channel5,
'channel_6' => $this->channel6,
'battery_level' => $this->batteryLevel
];
}
}