package mqtt import ( "encoding/json" "fmt" "kpt-pasture/model" "testing" ) func TestFirstFilterUpdate(t *testing.T) { newNeckActiveHabitList := []*model.NeckActiveHabit{ { Id: 1, NeckRingNumber: "123", High: 731, Rumina: 20, Intake: 0, Inactive: 65, Gasp: 1, Other: 0, Active: 27, FilterHigh: 0, FilterRumina: 0, FilterChew: 0, }, { Id: 2, NeckRingNumber: "123", High: 318, Rumina: 55, Intake: 2, Inactive: 99, Gasp: 1, Other: 0, Active: 18, FilterHigh: 0, FilterRumina: 0, FilterChew: 0, }, { Id: 2, NeckRingNumber: "123", High: 254, Rumina: 68, Intake: 0, Inactive: 114, Gasp: 22, Other: 0, Active: 6, FilterHigh: 0, FilterRumina: 0, FilterChew: 0, }, } test(newNeckActiveHabitList) } func test(newNeckActiveHabitList []*model.NeckActiveHabit) { var ( neckRingNumber string filterHigh float64 filterRumina float64 filterChew float64 ) // 活动量滤波 for _, v := range newNeckActiveHabitList { if v.FilterHigh > 0 { filterHigh = float64(v.FilterHigh) } else { if v.NeckRingNumber == neckRingNumber { filterHigh = computeIfPositiveElse(float64(v.High), filterHigh, 0.23, 0.77) } else { filterHigh = float64(v.High) } } if v.FilterRumina > 0 { filterRumina = float64(v.FilterRumina) } else { if v.NeckRingNumber == neckRingNumber { filterRumina = computeIfPositiveElse(float64(v.Rumina), filterRumina, 0.33, 0.67) } else { filterRumina = float64(v.Rumina) } } if v.FilterChew > 0 { filterChew = float64(v.FilterChew) } else { if v.NeckRingNumber == neckRingNumber { filterChew = computeIfPositiveElse(float64(v.Rumina+v.Intake), filterChew, 0.33, 0.67) } else { filterChew = float64(v.Rumina + v.Intake) } } // Select("filter_high", "filter_rumina", "filter_chew") b, _ := json.Marshal(v) fmt.Println("======v=====", string(b)) } }