689 lines
23 KiB
Go
689 lines
23 KiB
Go
package model
|
||
|
||
import (
|
||
"testing"
|
||
)
|
||
|
||
func TestIdentifyTxType(t *testing.T) {
|
||
// 测试数据:24 36 F3 02 96 37 06 01 00 04 00 00 00 04 9C D3 9A 01 88 F5 7E 00 2A 9C F6
|
||
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)
|
||
binary, hex, dec := protocol.IdentifyTxType()
|
||
|
||
// 预期结果
|
||
expectedBinary := "00100100" // 24 的二进制表示
|
||
expectedHex := "24" // 第一个字节的十六进制表示
|
||
expectedDec := uint8(36) // 24 的十进制表示
|
||
|
||
// 验证二进制结果
|
||
if binary != expectedBinary {
|
||
t.Errorf("Binary representation incorrect. Got %s, want %s", binary, expectedBinary)
|
||
}
|
||
|
||
// 验证十六进制结果
|
||
if hex != expectedHex {
|
||
t.Errorf("Hex representation incorrect. Got %s, want %s", hex, expectedHex)
|
||
}
|
||
|
||
// 验证十进制结果
|
||
if dec != expectedDec {
|
||
t.Errorf("Decimal representation incorrect. Got %d, want %d", dec, expectedDec)
|
||
}
|
||
}
|
||
|
||
func TestGetCompleteID(t *testing.T) {
|
||
// 测试数据:24 36 F3 02 96 37 06 01 00 04 00 00 00 04 9C D3 9A 01 88 F5 7E 00 2A 9C F6
|
||
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)
|
||
result, err := protocol.GetCompleteID()
|
||
|
||
if err != nil {
|
||
t.Fatalf("Unexpected error: %v", err)
|
||
}
|
||
|
||
// 测试 HSB (应该是 00)
|
||
expectedHSBBinary := "00000000" // 00 的二进制
|
||
expectedHSBHex := "00" // 00 的十六进制
|
||
expectedHSBDec := uint8(0) // 00 的十进制
|
||
|
||
if result.HSB.Binary != expectedHSBBinary {
|
||
t.Errorf("HSB Binary incorrect. Got %s, want %s", result.HSB.Binary, expectedHSBBinary)
|
||
}
|
||
if result.HSB.Hex != expectedHSBHex {
|
||
t.Errorf("HSB Hex incorrect. Got %s, want %s", result.HSB.Hex, expectedHSBHex)
|
||
}
|
||
if result.HSB.Dec != expectedHSBDec {
|
||
t.Errorf("HSB Dec incorrect. Got %d, want %d", result.HSB.Dec, expectedHSBDec)
|
||
}
|
||
|
||
// 测试 MSB (应该是 2A)
|
||
expectedMSBBinary := "00101010" // 2A 的二进制
|
||
expectedMSBHex := "2A" // 2A 的十六进制
|
||
expectedMSBDec := uint8(42) // 2A 的十进制
|
||
|
||
if result.MSB.Binary != expectedMSBBinary {
|
||
t.Errorf("MSB Binary incorrect. Got %s, want %s", result.MSB.Binary, expectedMSBBinary)
|
||
}
|
||
if result.MSB.Hex != expectedMSBHex {
|
||
t.Errorf("MSB Hex incorrect. Got %s, want %s", result.MSB.Hex, expectedMSBHex)
|
||
}
|
||
if result.MSB.Dec != expectedMSBDec {
|
||
t.Errorf("MSB Dec incorrect. Got %d, want %d", result.MSB.Dec, expectedMSBDec)
|
||
}
|
||
|
||
// 测试 LSB (应该是 36)
|
||
expectedLSBBinary := "00110110" // 36 的二进制
|
||
expectedLSBHex := "36" // 36 的十六进制
|
||
expectedLSBDec := uint8(54) // 36 的十进制
|
||
|
||
if result.LSB.Binary != expectedLSBBinary {
|
||
t.Errorf("LSB Binary incorrect. Got %s, want %s", result.LSB.Binary, expectedLSBBinary)
|
||
}
|
||
if result.LSB.Hex != expectedLSBHex {
|
||
t.Errorf("LSB Hex incorrect. Got %s, want %s", result.LSB.Hex, expectedLSBHex)
|
||
}
|
||
if result.LSB.Dec != expectedLSBDec {
|
||
t.Errorf("LSB Dec incorrect. Got %d, want %d", result.LSB.Dec, expectedLSBDec)
|
||
}
|
||
|
||
// 测试完整的24位ID (应该是 00 2A 36)
|
||
expectedCompleteBinary := "000000000010101000110110" // 完整24位二进制
|
||
expectedCompleteHex := "002A36" // 完整24位十六进制
|
||
expectedCompleteDec := uint32(0x002A36) // 完整24位十进制
|
||
|
||
if result.Complete.Binary != expectedCompleteBinary {
|
||
t.Errorf("Complete Binary incorrect. Got %s, want %s", result.Complete.Binary, expectedCompleteBinary)
|
||
}
|
||
if result.Complete.Hex != expectedCompleteHex {
|
||
t.Errorf("Complete Hex incorrect. Got %s, want %s", result.Complete.Hex, expectedCompleteHex)
|
||
}
|
||
if result.Complete.Dec != expectedCompleteDec {
|
||
t.Errorf("Complete Dec incorrect. Got %d, want %d", result.Complete.Dec, expectedCompleteDec)
|
||
}
|
||
}
|
||
|
||
func TestGetWindDirection(t *testing.T) {
|
||
tests := []struct {
|
||
name string
|
||
data []byte
|
||
expectedValue uint16
|
||
expectedValid bool
|
||
expectedDegree float64
|
||
}{
|
||
{
|
||
name: "Valid Direction",
|
||
data: []byte{0x24, 0x36, 0xF3, 0x02, 0x96}, // 原始数据,方向值为243
|
||
expectedValue: 0xF3,
|
||
expectedValid: true,
|
||
expectedDegree: 243,
|
||
},
|
||
{
|
||
name: "Invalid Direction",
|
||
data: []byte{0x24, 0x36, 0xFF, 0x03, 0x96}, // 设置一个大于359的值
|
||
expectedValue: 0x1FF,
|
||
expectedValid: false,
|
||
expectedDegree: 0,
|
||
},
|
||
}
|
||
|
||
for _, tt := range tests {
|
||
t.Run(tt.name, func(t *testing.T) {
|
||
protocol := NewProtocol(tt.data)
|
||
result, err := protocol.GetWindDirection()
|
||
|
||
if err != nil {
|
||
t.Fatalf("Unexpected error: %v", err)
|
||
}
|
||
|
||
if result.Complete.Value != tt.expectedValue {
|
||
t.Errorf("Value incorrect. Got %X, want %X", result.Complete.Value, tt.expectedValue)
|
||
}
|
||
|
||
if result.Complete.IsValid != tt.expectedValid {
|
||
t.Errorf("IsValid incorrect. Got %v, want %v", result.Complete.IsValid, tt.expectedValid)
|
||
}
|
||
|
||
if result.Complete.Degree != tt.expectedDegree {
|
||
t.Errorf("Degree incorrect. Got %v, want %v", result.Complete.Degree, tt.expectedDegree)
|
||
}
|
||
})
|
||
}
|
||
}
|
||
|
||
func TestGetTemperature(t *testing.T) {
|
||
tests := []struct {
|
||
name string
|
||
data []byte
|
||
expectedRaw uint16
|
||
expectedTemp float64
|
||
expectedValid bool
|
||
}{
|
||
{
|
||
name: "Temperature 10.5°C",
|
||
// 0x1F9 = 505 -> (505-400)/10 = 10.5
|
||
// TMP_H = 1 (001)
|
||
// TMP_M = F (1111)
|
||
// TMP_L = 9 (1001)
|
||
data: []byte{0x24, 0x36, 0xF3, 0x20, 0xF9}, // 设置byte3的bit29-31为001,byte4为0xF9
|
||
expectedRaw: 0x1F9,
|
||
expectedTemp: 10.5,
|
||
expectedValid: true,
|
||
},
|
||
{
|
||
name: "Temperature -10.5°C",
|
||
// 0x127 = 295 -> (295-400)/10 = -10.5
|
||
// TMP_H = 1 (001)
|
||
// TMP_M = 2 (0010)
|
||
// TMP_L = 7 (0111)
|
||
data: []byte{0x24, 0x36, 0xF3, 0x20, 0x27}, // 设置byte3的bit29-31为001,byte4为0x27
|
||
expectedRaw: 0x127,
|
||
expectedTemp: -10.5,
|
||
expectedValid: true,
|
||
},
|
||
{
|
||
name: "Invalid Temperature",
|
||
// 0x7FF = 2047 (超出范围)
|
||
// TMP_H = 7 (111)
|
||
// TMP_M = F (1111)
|
||
// TMP_L = F (1111)
|
||
data: []byte{0x24, 0x36, 0xF3, 0xE0, 0xFF}, // 设置byte3的bit29-31为111,byte4为0xFF
|
||
expectedRaw: 0x7FF,
|
||
expectedTemp: 0,
|
||
expectedValid: false,
|
||
},
|
||
}
|
||
|
||
for _, tt := range tests {
|
||
t.Run(tt.name, func(t *testing.T) {
|
||
protocol := NewProtocol(tt.data)
|
||
result, err := protocol.GetTemperature()
|
||
|
||
if err != nil {
|
||
t.Fatalf("Unexpected error: %v", err)
|
||
}
|
||
|
||
if result.Complete.RawValue != tt.expectedRaw {
|
||
t.Errorf("Raw value incorrect. Got %X, want %X", result.Complete.RawValue, tt.expectedRaw)
|
||
}
|
||
|
||
if result.Complete.Value != tt.expectedTemp {
|
||
t.Errorf("Temperature value incorrect. Got %.1f, want %.1f", result.Complete.Value, tt.expectedTemp)
|
||
}
|
||
|
||
if result.Complete.IsValid != tt.expectedValid {
|
||
t.Errorf("Validity incorrect. Got %v, want %v", result.Complete.IsValid, tt.expectedValid)
|
||
}
|
||
})
|
||
}
|
||
}
|
||
|
||
func TestGetHumidity(t *testing.T) {
|
||
tests := []struct {
|
||
name string
|
||
data []byte
|
||
expectedRaw uint8
|
||
expectedValue uint8
|
||
expectedValid bool
|
||
}{
|
||
{
|
||
name: "Valid Humidity 55%",
|
||
// 0x37 = 3*16 + 7 = 55%
|
||
data: []byte{0x24, 0x36, 0xF3, 0x02, 0x96, 0x37},
|
||
expectedRaw: 0x37,
|
||
expectedValue: 55,
|
||
expectedValid: true,
|
||
},
|
||
{
|
||
name: "Valid Humidity 1%",
|
||
// 0x01 = 0*16 + 1 = 1%
|
||
data: []byte{0x24, 0x36, 0xF3, 0x02, 0x96, 0x01},
|
||
expectedRaw: 0x01,
|
||
expectedValue: 1,
|
||
expectedValid: true,
|
||
},
|
||
{
|
||
name: "Valid Humidity 99%",
|
||
// 0x63 = 6*16 + 3 = 99%
|
||
data: []byte{0x24, 0x36, 0xF3, 0x02, 0x96, 0x63},
|
||
expectedRaw: 0x63,
|
||
expectedValue: 99,
|
||
expectedValid: true,
|
||
},
|
||
{
|
||
name: "Invalid Humidity (Too High)",
|
||
// 0x64 = 6*16 + 4 = 100% (无效)
|
||
data: []byte{0x24, 0x36, 0xF3, 0x02, 0x96, 0x64},
|
||
expectedRaw: 0xFF,
|
||
expectedValue: 0,
|
||
expectedValid: false,
|
||
},
|
||
{
|
||
name: "Invalid Humidity (Zero)",
|
||
// 0x00 = 0% (无效)
|
||
data: []byte{0x24, 0x36, 0xF3, 0x02, 0x96, 0x00},
|
||
expectedRaw: 0xFF,
|
||
expectedValue: 0,
|
||
expectedValid: false,
|
||
},
|
||
}
|
||
|
||
for _, tt := range tests {
|
||
t.Run(tt.name, func(t *testing.T) {
|
||
protocol := NewProtocol(tt.data)
|
||
result, err := protocol.GetHumidity()
|
||
|
||
if err != nil {
|
||
t.Fatalf("Unexpected error: %v", err)
|
||
}
|
||
|
||
if result.Complete.RawValue != tt.expectedRaw {
|
||
t.Errorf("Raw value incorrect. Got %X, want %X", result.Complete.RawValue, tt.expectedRaw)
|
||
}
|
||
|
||
if result.Complete.Value != tt.expectedValue {
|
||
t.Errorf("Humidity value incorrect. Got %d%%, want %d%%", result.Complete.Value, tt.expectedValue)
|
||
}
|
||
|
||
if result.Complete.IsValid != tt.expectedValid {
|
||
t.Errorf("Validity incorrect. Got %v, want %v", result.Complete.IsValid, tt.expectedValid)
|
||
}
|
||
})
|
||
}
|
||
}
|
||
|
||
func TestGetWindSpeed(t *testing.T) {
|
||
// 测试数据:24 36 F3 02 96 37 06 01 00 04 00 00 00 04 9C D3 9A 01 88 F5 7E 00 2A 9C F6
|
||
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)
|
||
result, err := protocol.GetWindSpeed()
|
||
|
||
if err != nil {
|
||
t.Fatalf("Unexpected error: %v", err)
|
||
}
|
||
|
||
// 验证 WSP_FLAG (bit 25)
|
||
if !result.WspFlag.Value {
|
||
t.Error("WSP_FLAG should be 1")
|
||
}
|
||
|
||
// 验证 WIND_H 和 WIND_L
|
||
if result.WspH.Value != 0x0 {
|
||
t.Errorf("WIND_H incorrect. Got %X, want 0", result.WspH.Value)
|
||
}
|
||
if result.WspL.Value != 0x6 {
|
||
t.Errorf("WIND_L incorrect. Got %X, want 6", result.WspL.Value)
|
||
}
|
||
|
||
// 验证完整值(9bit模式:000 + bit27 + WIND_H + WIND_L)
|
||
expectedRaw := uint16(0x006)
|
||
if result.Complete.RawValue != expectedRaw {
|
||
t.Errorf("Raw value incorrect. Got %X, want %X", result.Complete.RawValue, expectedRaw)
|
||
}
|
||
|
||
// 验证实际风速值:6/8*0.51 = 0.38250 m/s
|
||
expectedValue := float64(0x006) / 8.0 * 0.51
|
||
if result.Complete.Value != expectedValue {
|
||
t.Errorf("Wind speed value incorrect. Got %.5f m/s, want %.5f m/s",
|
||
result.Complete.Value, expectedValue)
|
||
}
|
||
}
|
||
|
||
func TestGetGustSpeed(t *testing.T) {
|
||
// 测试数据:24 36 F3 02 96 37 06 01 00 04 00 00 00 04 9C D3 9A 01 88 F5 7E 00 2A 9C F6
|
||
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)
|
||
result, err := protocol.GetGustSpeed()
|
||
|
||
if err != nil {
|
||
t.Fatalf("Unexpected error: %v", err)
|
||
}
|
||
|
||
// 验证 GUST_H (0x0)
|
||
expectedGustHBinary := "0000"
|
||
if result.GustH.Binary != expectedGustHBinary {
|
||
t.Errorf("GUST_H Binary incorrect. Got %s, want %s", result.GustH.Binary, expectedGustHBinary)
|
||
}
|
||
if result.GustH.Value != 0x0 {
|
||
t.Errorf("GUST_H Value incorrect. Got %X, want 0", result.GustH.Value)
|
||
}
|
||
|
||
// 验证 GUST_L (0x1)
|
||
expectedGustLBinary := "0001"
|
||
if result.GustL.Binary != expectedGustLBinary {
|
||
t.Errorf("GUST_L Binary incorrect. Got %s, want %s", result.GustL.Binary, expectedGustLBinary)
|
||
}
|
||
if result.GustL.Value != 0x1 {
|
||
t.Errorf("GUST_L Value incorrect. Got %X, want 1", result.GustL.Value)
|
||
}
|
||
|
||
// 验证完整值
|
||
expectedBinary := "00000001" // 0x01
|
||
if result.Complete.Binary != expectedBinary {
|
||
t.Errorf("Complete Binary incorrect. Got %s, want %s", result.Complete.Binary, expectedBinary)
|
||
}
|
||
if result.Complete.RawValue != 0x01 {
|
||
t.Errorf("Raw value incorrect. Got %X, want 01", result.Complete.RawValue)
|
||
}
|
||
|
||
// 验证实际阵风速度值:0x01 = 1, 1*0.51 = 0.51 m/s
|
||
expectedValue := float64(0x01) * 0.51
|
||
if result.Complete.Value != expectedValue {
|
||
t.Errorf("Gust speed value incorrect. Got %.5f m/s, want %.5f m/s", result.Complete.Value, expectedValue)
|
||
}
|
||
}
|
||
|
||
func TestGetRainfall(t *testing.T) {
|
||
// 测试数据:24 36 F3 02 96 37 06 01 00 04 00 00 00 04 9C D3 9A 01 88 F5 7E 00 2A 9C F6
|
||
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)
|
||
result, err := protocol.GetRainfall()
|
||
|
||
if err != nil {
|
||
t.Fatalf("Unexpected error: %v", err)
|
||
}
|
||
|
||
// 验证 RAIN_HH (0x0)
|
||
expectedRainHHBinary := "0000"
|
||
if result.RainHH.Binary != expectedRainHHBinary {
|
||
t.Errorf("RAIN_HH Binary incorrect. Got %s, want %s", result.RainHH.Binary, expectedRainHHBinary)
|
||
}
|
||
if result.RainHH.Value != 0x0 {
|
||
t.Errorf("RAIN_HH Value incorrect. Got %X, want 0", result.RainHH.Value)
|
||
}
|
||
|
||
// 验证 RAIN_HL (0x0)
|
||
expectedRainHLBinary := "0000"
|
||
if result.RainHL.Binary != expectedRainHLBinary {
|
||
t.Errorf("RAIN_HL Binary incorrect. Got %s, want %s", result.RainHL.Binary, expectedRainHLBinary)
|
||
}
|
||
if result.RainHL.Value != 0x0 {
|
||
t.Errorf("RAIN_HL Value incorrect. Got %X, want 0", result.RainHL.Value)
|
||
}
|
||
|
||
// 验证 RAIN_LH (0x0)
|
||
expectedRainLHBinary := "0000"
|
||
if result.RainLH.Binary != expectedRainLHBinary {
|
||
t.Errorf("RAIN_LH Binary incorrect. Got %s, want %s", result.RainLH.Binary, expectedRainLHBinary)
|
||
}
|
||
if result.RainLH.Value != 0x0 {
|
||
t.Errorf("RAIN_LH Value incorrect. Got %X, want 0", result.RainLH.Value)
|
||
}
|
||
|
||
// 验证 RAIN_LL (0x4)
|
||
expectedRainLLBinary := "0100"
|
||
if result.RainLL.Binary != expectedRainLLBinary {
|
||
t.Errorf("RAIN_LL Binary incorrect. Got %s, want %s", result.RainLL.Binary, expectedRainLLBinary)
|
||
}
|
||
if result.RainLL.Value != 0x4 {
|
||
t.Errorf("RAIN_LL Value incorrect. Got %X, want 4", result.RainLL.Value)
|
||
}
|
||
|
||
// 验证完整值
|
||
expectedBinary := "0000000000000100" // 0x0004
|
||
if result.Complete.Binary != expectedBinary {
|
||
t.Errorf("Complete Binary incorrect. Got %s, want %s", result.Complete.Binary, expectedBinary)
|
||
}
|
||
if result.Complete.RawValue != 0x0004 {
|
||
t.Errorf("Raw value incorrect. Got %X, want 0004", result.Complete.RawValue)
|
||
}
|
||
|
||
// 验证实际降雨量值:0x0004 = 4, 4*0.254 = 1.016 mm
|
||
expectedValue := float64(0x0004) * 0.254
|
||
if result.Complete.Value != expectedValue {
|
||
t.Errorf("Rainfall value incorrect. Got %.3f mm, want %.3f mm", result.Complete.Value, expectedValue)
|
||
}
|
||
}
|
||
|
||
func TestGetUVIndex(t *testing.T) {
|
||
tests := []struct {
|
||
name string
|
||
data []byte
|
||
expectedRaw uint16
|
||
expectedValue float64
|
||
expectedValid bool
|
||
}{
|
||
{
|
||
name: "Valid UV Index",
|
||
// 测试数据:24 36 F3 02 96 37 06 01 00 04 00 00 00 04 9C D3 9A 01 88 F5 7E 00 2A 9C F6
|
||
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},
|
||
expectedRaw: 0x0000, // byte10=0x00, byte11=0x00
|
||
expectedValue: 0, // 0 uW/c㎡
|
||
expectedValid: true,
|
||
},
|
||
{
|
||
name: "Invalid UV Index (Too High)",
|
||
data: []byte{0x24, 0x36, 0xF3, 0x02, 0x96, 0x37, 0x06, 0x01, 0x00, 0x04, 0xFF, 0xFF}, // 设置一个超出范围的值
|
||
expectedRaw: 0xFFFF, // 无效值
|
||
expectedValue: 0, // 无效时返回0
|
||
expectedValid: false,
|
||
},
|
||
}
|
||
|
||
for _, tt := range tests {
|
||
t.Run(tt.name, func(t *testing.T) {
|
||
protocol := NewProtocol(tt.data)
|
||
result, err := protocol.GetUVIndex()
|
||
|
||
if err != nil {
|
||
t.Fatalf("Unexpected error: %v", err)
|
||
}
|
||
|
||
if result.Complete.RawValue != tt.expectedRaw {
|
||
t.Errorf("Raw value incorrect. Got %X, want %X", result.Complete.RawValue, tt.expectedRaw)
|
||
}
|
||
|
||
if result.Complete.Value != tt.expectedValue {
|
||
t.Errorf("UV Index value incorrect. Got %.1f uW/c㎡, want %.1f uW/c㎡",
|
||
result.Complete.Value, tt.expectedValue)
|
||
}
|
||
|
||
if result.Complete.IsValid != tt.expectedValid {
|
||
t.Errorf("Validity incorrect. Got %v, want %v", result.Complete.IsValid, tt.expectedValid)
|
||
}
|
||
})
|
||
}
|
||
}
|
||
|
||
func TestGetLight(t *testing.T) {
|
||
// 测试数据:24 36 F3 02 96 37 06 01 00 04 00 00 00 04 9C D3 9A 01 88 F5 7E 00 2A 9C F6
|
||
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)
|
||
result, err := protocol.GetLight()
|
||
|
||
if err != nil {
|
||
t.Fatalf("Unexpected error: %v", err)
|
||
}
|
||
|
||
// 验证原始值 (byte13-15: 00 04 9C)
|
||
expectedRaw := uint32(0x00049C)
|
||
if result.Complete.RawValue != expectedRaw {
|
||
t.Errorf("Raw value incorrect. Got %X, want %X", result.Complete.RawValue, expectedRaw)
|
||
}
|
||
|
||
// 验证实际光照值:0x00049C = 1180, 1180/10 = 118.0 lux
|
||
expectedValue := float64(0x00049C) / 10.0
|
||
if result.Complete.Value != expectedValue {
|
||
t.Errorf("Light value incorrect. Got %.1f lux, want %.1f lux",
|
||
result.Complete.Value, expectedValue)
|
||
}
|
||
|
||
if !result.Complete.IsValid {
|
||
t.Error("Light value should be valid")
|
||
}
|
||
}
|
||
|
||
func TestGetPressure(t *testing.T) {
|
||
// 测试数据:24 36 F3 02 96 37 06 01 00 04 00 00 00 04 9C D3 9A 01 88 F5 7E 00 2A 9C F6
|
||
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)
|
||
result, err := protocol.GetPressure()
|
||
|
||
if err != nil {
|
||
t.Fatalf("Unexpected error: %v", err)
|
||
}
|
||
|
||
// 验证原始值 (01 88 F5)
|
||
expectedRaw := uint32(0x0188F5)
|
||
if result.Complete.RawValue != expectedRaw {
|
||
t.Errorf("Raw value incorrect. Got %X, want %X", result.Complete.RawValue, expectedRaw)
|
||
}
|
||
|
||
// 验证实际气压值:0x0188F5 = 100597, 100597/100 = 1005.97 hPa
|
||
expectedValue := float64(0x0188F5) / 100.0
|
||
if result.Complete.Value != expectedValue {
|
||
t.Errorf("Pressure value incorrect. Got %.2f hPa, want %.2f hPa",
|
||
result.Complete.Value, expectedValue)
|
||
}
|
||
|
||
if !result.Complete.IsValid {
|
||
t.Error("Pressure value should be valid")
|
||
}
|
||
}
|
||
|
||
func TestParseNewData(t *testing.T) {
|
||
// 新的测试数据:24 F2 30 02 AF 51 03 01 00 08 00 00 00 00 00 6E C2 01 82 D8 5B 00 29 87 EA
|
||
data := []byte{0x24, 0xF2, 0x30, 0x02, 0xAF, 0x51, 0x03, 0x01, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6E, 0xC2, 0x01, 0x82, 0xD8, 0x5B, 0x00, 0x29, 0x87, 0xEA}
|
||
|
||
protocol := NewProtocol(data)
|
||
|
||
// 1. 解析风速
|
||
windSpeed, err := protocol.GetWindSpeed()
|
||
if err != nil {
|
||
t.Fatalf("Failed to parse wind speed: %v", err)
|
||
}
|
||
t.Logf("Wind Speed: %.5f m/s (raw: 0x%X)", windSpeed.Complete.Value, windSpeed.Complete.RawValue)
|
||
|
||
// 2. 解析阵风速度
|
||
gustSpeed, err := protocol.GetGustSpeed()
|
||
if err != nil {
|
||
t.Fatalf("Failed to parse gust speed: %v", err)
|
||
}
|
||
t.Logf("Gust Speed: %.5f m/s (raw: 0x%X)", gustSpeed.Complete.Value, gustSpeed.Complete.RawValue)
|
||
|
||
// 3. 解析温度
|
||
temp, err := protocol.GetTemperature()
|
||
if err != nil {
|
||
t.Fatalf("Failed to parse temperature: %v", err)
|
||
}
|
||
t.Logf("Temperature: %.2f °C (raw: 0x%X)", temp.Complete.Value, temp.Complete.RawValue)
|
||
|
||
// 4. 解析湿度
|
||
humidity, err := protocol.GetHumidity()
|
||
if err != nil {
|
||
t.Fatalf("Failed to parse humidity: %v", err)
|
||
}
|
||
t.Logf("Humidity: %d%% (raw: 0x%X)", humidity.Complete.Value, humidity.Complete.RawValue)
|
||
|
||
// 5. 解析光照
|
||
light, err := protocol.GetLight()
|
||
if err != nil {
|
||
t.Fatalf("Failed to parse light: %v", err)
|
||
}
|
||
t.Logf("Light: %.1f lux (raw: 0x%X)", light.Complete.Value, light.Complete.RawValue)
|
||
|
||
// 6. 解析大气压
|
||
pressure, err := protocol.GetPressure()
|
||
if err != nil {
|
||
t.Fatalf("Failed to parse pressure: %v", err)
|
||
}
|
||
t.Logf("Pressure: %.2f hPa (raw: 0x%X)", pressure.Complete.Value, pressure.Complete.RawValue)
|
||
|
||
// 7. 解析UV指数
|
||
uv, err := protocol.GetUVIndex()
|
||
if err != nil {
|
||
t.Fatalf("Failed to parse UV index: %v", err)
|
||
}
|
||
t.Logf("UV Index: %.1f uW/c㎡ (raw: 0x%X)", uv.Complete.Value, uv.Complete.RawValue)
|
||
|
||
// 8. 解析降雨量
|
||
rainfall, err := protocol.GetRainfall()
|
||
if err != nil {
|
||
t.Fatalf("Failed to parse rainfall: %v", err)
|
||
}
|
||
t.Logf("Rainfall: %.3f mm (raw: 0x%X)", rainfall.Complete.Value, rainfall.Complete.RawValue)
|
||
}
|
||
|
||
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
|
||
//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)
|
||
|
||
// 1. 风速解析
|
||
t.Log("\n=== 风速解析 ===")
|
||
windSpeed, _ := protocol.GetWindSpeed()
|
||
t.Logf("风速: %.5f m/s (raw: 0x%X)", windSpeed.Complete.Value, windSpeed.Complete.RawValue)
|
||
t.Logf("WSP_H: %s (0x%X), WSP_L: %s (0x%X)",
|
||
windSpeed.WspH.Binary, windSpeed.WspH.Value,
|
||
windSpeed.WspL.Binary, windSpeed.WspL.Value)
|
||
if windSpeed.Extend.Value != 0 {
|
||
t.Logf("WIND_Extend: %s (0x%X)", windSpeed.Extend.Binary, windSpeed.Extend.Value)
|
||
}
|
||
|
||
// 2. 温度解析
|
||
t.Log("\n=== 温度解析 ===")
|
||
temp, _ := protocol.GetTemperature()
|
||
t.Logf("温度: %.2f °C (raw: 0x%X)", temp.Complete.Value, temp.Complete.RawValue)
|
||
t.Logf("TMP_H: %s (0x%X), TMP_M: %s (0x%X), TMP_L: %s (0x%X)",
|
||
temp.TmpH.Binary, temp.TmpH.Value,
|
||
temp.TmpM.Binary, temp.TmpM.Value,
|
||
temp.TmpL.Binary, temp.TmpL.Value)
|
||
|
||
// 3. 湿度解析
|
||
t.Log("\n=== 湿度解析 ===")
|
||
humidity, _ := protocol.GetHumidity()
|
||
t.Logf("湿度: %d%% (raw: 0x%X)", humidity.Complete.Value, humidity.Complete.RawValue)
|
||
t.Logf("HM_H: %s (0x%X), HM_L: %s (0x%X)",
|
||
humidity.HmH.Binary, humidity.HmH.Value,
|
||
humidity.HmL.Binary, humidity.HmL.Value)
|
||
|
||
// 4. 光照解析
|
||
t.Log("\n=== 光照解析 ===")
|
||
light, _ := protocol.GetLight()
|
||
t.Logf("光照: %.1f lux (raw: 0x%X)", light.Complete.Value, light.Complete.RawValue)
|
||
t.Logf("原始字节: %02X %02X %02X", data[12], data[13], data[14])
|
||
|
||
// 5. 大气压解析
|
||
t.Log("\n=== 大气压解析 ===")
|
||
pressure, _ := protocol.GetPressure()
|
||
t.Logf("大气压: %.2f hPa (raw: 0x%X)", pressure.Complete.Value, pressure.Complete.RawValue)
|
||
t.Logf("原始字节: %02X %02X %02X", data[17], data[18], data[19])
|
||
|
||
// 6. UV指数解析
|
||
t.Log("\n=== UV指数解析 ===")
|
||
uv, _ := protocol.GetUVIndex()
|
||
t.Logf("UV指数: %.1f uW/c㎡ (raw: 0x%X)", uv.Complete.Value, uv.Complete.RawValue)
|
||
t.Logf("原始字节: %02X %02X", data[10], data[11])
|
||
|
||
// 7. 降雨量解析
|
||
t.Log("\n=== 降雨量解析 ===")
|
||
rainfall, _ := protocol.GetRainfall()
|
||
t.Logf("降雨量: %.3f mm (raw: 0x%X)", rainfall.Complete.Value, rainfall.Complete.RawValue)
|
||
t.Logf("原始字节: %02X %02X", data[8], data[9])
|
||
|
||
// 8. 阵风速度解析
|
||
t.Log("\n=== 阵风速度解析 ===")
|
||
gust, _ := protocol.GetGustSpeed()
|
||
t.Logf("阵风速度: %.2f m/s (raw: 0x%X)", gust.Complete.Value, gust.Complete.RawValue)
|
||
t.Logf("原始字节: %02X", data[7])
|
||
|
||
// 9. 风向解析
|
||
t.Log("\n=== 风向解析 ===")
|
||
windDir, _ := protocol.GetWindDirection()
|
||
t.Logf("风向: %.1f° (raw: 0x%X)", windDir.Complete.Degree, windDir.Complete.Value)
|
||
t.Logf("原始字节: %02X %02X", data[2], data[3])
|
||
|
||
// 10. 设备ID解析
|
||
t.Log("\n=== 设备ID解析 ===")
|
||
id, _ := protocol.GetCompleteID()
|
||
t.Logf("设备ID: %02X %02X %02X", id.HSB.Dec, id.MSB.Dec, id.LSB.Dec)
|
||
t.Logf("原始字节: HSB=%02X, MSB=%02X, LSB=%02X", data[21], data[22], data[1])
|
||
}
|