bcb95d7772
- 在 NewCurrentCollectorParser 中增加正则校验,提高数据包匹配准确性 - 修复 Packet 类中 length 属性的格式问题,确保正确访问 - 调整 PacketParserFactory,修复可能的空数据问题,增强健壮性 - TcpServer 中新增 logMessage 方法拆分日志记录逻辑 - 根据包匹配结果分别记录详细或简化日志,增强调试信息 - 修复 TcpServer 中不匹配数据包时的响应,避免无效处理
241 lines
6.3 KiB
PHP
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
|
|
];
|
|
}
|
|
} |