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