bcb95d7772
- 在 NewCurrentCollectorParser 中增加正则校验,提高数据包匹配准确性 - 修复 Packet 类中 length 属性的格式问题,确保正确访问 - 调整 PacketParserFactory,修复可能的空数据问题,增强健壮性 - TcpServer 中新增 logMessage 方法拆分日志记录逻辑 - 根据包匹配结果分别记录详细或简化日志,增强调试信息 - 修复 TcpServer 中不匹配数据包时的响应,避免无效处理
31 lines
812 B
PHP
31 lines
812 B
PHP
<?php
|
|
|
|
namespace app\net\parsers;
|
|
|
|
|
|
/**
|
|
* 新版电流采集器报文解析器(长度38)
|
|
*/
|
|
class NewCurrentCollectorParser implements PacketParserInterface
|
|
{
|
|
public function supports(string $hexString): bool
|
|
{
|
|
return strlen($hexString) === 38;
|
|
}
|
|
|
|
public function parse(string $hexString): array
|
|
{
|
|
$pattern = '/^BBBB([0-9A-F]{2})([0-9A-F]{2})([0-9A-F]{2})([0-9A-F]{2})([0-9A-F]{2})([0-9A-F]{2})([0-9A-F]{8})$/';
|
|
|
|
if (preg_match($pattern, $hexString, $matches)) {
|
|
return [
|
|
'hexType' => 4,
|
|
'isMatched' => true,
|
|
'detectionDevice' => substr($hexString, 16, 2),
|
|
'channel1' => substr($hexString, 24, 2),
|
|
];
|
|
}
|
|
|
|
return ['isMatched' => false];
|
|
}
|
|
} |