et-go 20240919重建
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.9 KiB

1 month ago
package et_analyze
import (
"encoding/json"
"fmt"
"gitea.anxinyun.cn/container/common_models"
"strings"
"testing"
"time"
)
// 时变化速率 应变 (-,-2);(2,+); (-2,-1);(1,2);
var aggDataJson_alarm = `
{"date":"2024-04-19T01:10:59.999+0000","sensorId":106,"structId":1,"factorId":11,"aggTypeId":2006,"aggMethodId":3004,"agg":{"strain":-19.399999618530273},"changed":{"strain":-3}}`
var aggDataJson_normal = `
{"date":"2024-04-19T01:20:59.999+0000","sensorId":106,"structId":1,"factorId":11,"aggTypeId":2006,"aggMethodId":3004,"agg":{"strain":-16.399999618530273},"changed":{"strain":0}}`
func Test_aggThreshold_processData(t *testing.T) {
// aggData 中的时间为UTC格式 2024-04-19T01:10:59.999+0000,
// 在进行 json.Unmarshal() 时报错
// 解决方案:先将 +0000 -> +00:00,然后再将 UTC 时间转换为 +08:00 时区时间
// 将 "+0000" 替换为 "+00:00"
replacedStr := strings.Replace(aggDataJson_alarm, "+0000", "+00:00", 1)
aggData := &common_models.AggData{}
err := json.Unmarshal([]byte(replacedStr), aggData)
if err != nil {
fmt.Printf("json parse error: %v", err)
return
}
// 将 UTC 时间加上8小时得到中国的本地时间
aggData.Date = aggData.Date.Add(8 * time.Hour)
handler := NewAggThresholdHandler()
handler.ProcessData(aggData)
time.Sleep(2 * time.Second) // 不同协程,需要等待处理
// 重复发送一条,是否生成恢复告警?
// 将 "+0000" 替换为 "+00:00"
replacedStr = strings.Replace(aggDataJson_normal, "+0000", "+00:00", 1)
aggData_noAlarm := &common_models.AggData{}
json.Unmarshal([]byte(replacedStr), aggData_noAlarm)
// 将 UTC 时间加上8小时得到中国的本地时间
aggData_noAlarm.Date = aggData_noAlarm.Date.Add(8 * time.Hour)
handler.ProcessData(aggData_noAlarm)
time.Sleep(2 * time.Second) // 不同协程,需要等待处理
println("=====")
// aggThreshold 直接在 recv 处理
}