angle_dtu/parser.go
2025-09-08 11:44:56 +08:00

107 lines
3.2 KiB
Go
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.

package main
import (
"fmt"
"regexp"
"strconv"
"strings"
)
// parseData 使用正则表达式解析传感器数据,支持新格式 #{1602301014-01,1,1,28.4,-6.884,1.540}!
func parseData(data string) (int, float64, float64, float64, float64, error) {
// 尝试解析新格式: #{1602301014-01,1,1,28.4,-6.884,1.540}!
newPattern := regexp.MustCompile(`#\{[^,]+-(\d+),\d+,(\d+),([-]?\d+\.\d+),([-]?\d+\.\d+),([-]?\d+\.\d+)\}!`)
matches := newPattern.FindStringSubmatch(data)
if len(matches) == 6 {
// 新格式解析
sensorID, err := strconv.Atoi(matches[2]) // 使用传感器地址编号
if err != nil {
return 0, 0, 0, 0, 0, fmt.Errorf("解析传感器ID失败: %v", err)
}
temperature, err := strconv.ParseFloat(strings.TrimSpace(matches[3]), 64)
if err != nil {
return 0, 0, 0, 0, 0, fmt.Errorf("解析温度值失败: %v", err)
}
x, err := strconv.ParseFloat(strings.TrimSpace(matches[4]), 64)
if err != nil {
return 0, 0, 0, 0, 0, fmt.Errorf("解析X值失败: %v", err)
}
y, err := strconv.ParseFloat(strings.TrimSpace(matches[5]), 64)
if err != nil {
return 0, 0, 0, 0, 0, fmt.Errorf("解析Y值失败: %v", err)
}
z := 0.0 // 新格式没有Z值设为0
return sensorID, x, y, z, temperature, nil
}
// 尝试解析无包装符号的格式: 1602301014-01,1,1,31.1,-6.781,1.542
plainPattern := regexp.MustCompile(`([^,]+)-(\d+),\d+,(\d+),([-]?\d+\.\d+),([-]?\d+\.\d+),([-]?\d+\.\d+)`)
matches = plainPattern.FindStringSubmatch(data)
if len(matches) == 7 {
// 无包装符号格式解析
sensorID, err := strconv.Atoi(matches[3]) // 使用传感器地址编号
if err != nil {
return 0, 0, 0, 0, 0, fmt.Errorf("解析传感器ID失败: %v", err)
}
temperature, err := strconv.ParseFloat(strings.TrimSpace(matches[4]), 64)
if err != nil {
return 0, 0, 0, 0, 0, fmt.Errorf("解析温度值失败: %v", err)
}
x, err := strconv.ParseFloat(strings.TrimSpace(matches[5]), 64)
if err != nil {
return 0, 0, 0, 0, 0, fmt.Errorf("解析X值失败: %v", err)
}
y, err := strconv.ParseFloat(strings.TrimSpace(matches[6]), 64)
if err != nil {
return 0, 0, 0, 0, 0, fmt.Errorf("解析Y值失败: %v", err)
}
z := 0.0 // 这种格式没有Z值设为0
return sensorID, x, y, z, temperature, nil
}
// 尝试解析旧格式: 1:1.000, 2.000, 3.000
oldPattern := regexp.MustCompile(`(\d+):([-]?\d+\.\d+),\s*([-]?\d+\.\d+),\s*([-]?\d+\.\d+)`)
matches = oldPattern.FindStringSubmatch(data)
if len(matches) == 5 {
// 旧格式解析
sensorID, err := strconv.Atoi(matches[1])
if err != nil {
return 0, 0, 0, 0, 0, fmt.Errorf("解析传感器ID失败: %v", err)
}
x, err := strconv.ParseFloat(strings.TrimSpace(matches[2]), 64)
if err != nil {
return 0, 0, 0, 0, 0, fmt.Errorf("解析X值失败: %v", err)
}
y, err := strconv.ParseFloat(strings.TrimSpace(matches[3]), 64)
if err != nil {
return 0, 0, 0, 0, 0, fmt.Errorf("解析Y值失败: %v", err)
}
z, err := strconv.ParseFloat(strings.TrimSpace(matches[4]), 64)
if err != nil {
return 0, 0, 0, 0, 0, fmt.Errorf("解析Z值失败: %v", err)
}
temperature := 0.0 // 旧格式没有温度值设为0
return sensorID, x, y, z, temperature, nil
}
return 0, 0, 0, 0, 0, fmt.Errorf("数据格式不正确: %s", data)
}