feat: 新增 485 的解析

This commit is contained in:
yarnom 2025-08-03 13:13:12 +08:00
parent cb1728ef00
commit bc3290c501
3 changed files with 33 additions and 12 deletions

29
main.go
View File

@ -138,6 +138,22 @@ func startUDP() {
log.Printf("设备类型: %s", getDeviceTypeString(deviceType)) log.Printf("设备类型: %s", getDeviceTypeString(deviceType))
log.Println(data) log.Println(data)
// 如果是RS485数据打印更详细的信息
if deviceType == model.DeviceTypeRS485 {
if rs485Data, ok := data.(*model.RS485WeatherData); ok {
log.Println("RS485数据详情:")
log.Printf("设备ID: %s", rs485Data.DeviceID)
log.Printf("温度: %.2f°C", rs485Data.Temperature)
log.Printf("湿度: %.2f%%", rs485Data.Humidity)
log.Printf("风向: %.2f°", rs485Data.WindDirection)
log.Printf("风速: %.2f m/s", rs485Data.WindSpeed)
log.Printf("降雨量: %.2f mm", rs485Data.Rainfall)
log.Printf("光照: %.2f lux", rs485Data.Light)
log.Printf("紫外线: %.2f", rs485Data.UV)
log.Printf("气压: %.2f hPa", rs485Data.Pressure)
}
}
var stationID string var stationID string
switch v := data.(type) { switch v := data.(type) {
case *model.WeatherData: case *model.WeatherData:
@ -153,12 +169,13 @@ func startUDP() {
log.Printf("警告: 收到的数据没有站点ID") log.Printf("警告: 收到的数据没有站点ID")
} }
err = model.SaveWeatherData(data, string(rawData)) // 暂时不保存到数据库
if err != nil { // err = model.SaveWeatherData(data, string(rawData))
log.Printf("保存数据到数据库失败: %v", err) // if err != nil {
} else { // log.Printf("保存数据到数据库失败: %v", err)
log.Printf("数据已成功保存到数据库") // } else {
} // log.Printf("数据已成功保存到数据库")
// }
} }
} }

View File

@ -612,8 +612,8 @@ func TestParseNewData(t *testing.T) {
func TestParseNewDataWithDetails(t *testing.T) { func TestParseNewDataWithDetails(t *testing.T) {
// 新的测试数据24 F2 09 02 BA 4F 13 03 00 6A 02 33 04 13 5C AC FE 01 83 93 17 00 29 35 88 // 新的测试数据24 F2 09 02 BA 4F 13 03 00 6A 02 33 04 13 5C AC FE 01 83 93 17 00 29 35 88
data := []byte{0x24, 0xF2, 0x09, 0x02, 0xBA, 0x4F, 0x13, 0x03, 0x00, 0x6A, 0x02, 0x33, 0x04, 0x13, 0x5C, 0xAC, 0xFE, 0x01, 0x83, 0x93, 0x17, 0x00, 0x29, 0x35, 0x88} //data := []byte{0x24, 0xF2, 0x09, 0x02, 0xBA, 0x4F, 0x13, 0x03, 0x00, 0x6A, 0x02, 0x33, 0x04, 0x13, 0x5C, 0xAC, 0xFE, 0x01, 0x83, 0x93, 0x17, 0x00, 0x29, 0x35, 0x88}
data := []byte{0x24, 0x36, 0xF3, 0x02, 0x96, 0x37, 0x06, 0x01, 0x00, 0x04, 0x00, 0x00, 0x00, 0x04, 0x9C, 0xD3, 0x9A, 0x01, 0x88, 0xF5, 0x7E, 0x00, 0x2A, 0x9C, 0xF6}
protocol := NewProtocol(data) protocol := NewProtocol(data)
// 1. 风速解析 // 1. 风速解析

View File

@ -53,12 +53,9 @@ var urlRegex = regexp.MustCompile(`/weatherstation/updateweatherstation\.php\?([
func ParseData(data []byte) (interface{}, DeviceType, error) { func ParseData(data []byte) (interface{}, DeviceType, error) {
// 检查是否为RS485数据 // 检查是否为RS485数据
if len(data) == 25 && data[0] == 0x24 { if len(data) == 25 && data[0] == 0x24 {
// 创建协议解析器
protocol := NewProtocol(data) protocol := NewProtocol(data)
rs485Protocol := NewRS485Protocol(data) rs485Protocol := NewRS485Protocol(data)
rs485Data, err := rs485Protocol.ParseRS485Data()
if err != nil {
return nil, DeviceTypeRS485, err
}
// 获取设备ID // 获取设备ID
idParts, err := protocol.GetCompleteID() idParts, err := protocol.GetCompleteID()
@ -66,6 +63,13 @@ func ParseData(data []byte) (interface{}, DeviceType, error) {
return nil, DeviceTypeRS485, fmt.Errorf("获取设备ID失败: %v", err) return nil, DeviceTypeRS485, fmt.Errorf("获取设备ID失败: %v", err)
} }
// 解析RS485数据
rs485Data, err := rs485Protocol.ParseRS485Data()
if err != nil {
return nil, DeviceTypeRS485, err
}
// 添加设备ID和时间戳
rs485Data.DeviceID = idParts.Complete.Hex rs485Data.DeviceID = idParts.Complete.Hex
rs485Data.ReceivedAt = time.Now() rs485Data.ReceivedAt = time.Now()
rs485Data.RawDataHex = fmt.Sprintf("%X", data) rs485Data.RawDataHex = fmt.Sprintf("%X", data)