| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- package main
- import (
- "math"
- "time"
- "lc/common/util"
- )
- type LiquidData struct {
- Time time.Time //采集时间
- SensorVal float64 //传感器采集值
- CalcVal float64 //深度
- }
- type LiguidDataMgr struct {
- LastHourData []LiquidData //最近一小时数据
- }
- func (o *LiguidDataMgr) GetAverageLast() (float64, float64) {
- n := 0
- for i, v := range o.LastHourData {
- if util.MlNow().Sub(v.Time).Minutes() > 1 {
- n = i
- } else {
- break
- }
- }
- if n > 0 {
- o.LastHourData = o.LastHourData[n+1:]
- }
- if len(o.LastHourData) > 0 {
- //取过去一小时的平均值
- depthSum := 0.000
- for _, v := range o.LastHourData {
- depthSum += v.CalcVal
- }
- depthSum = depthSum / float64(len(o.LastHourData))
- return Precision(depthSum, 3, false), o.LastHourData[len(o.LastHourData)-1].CalcVal
- }
- return 0.000, 0.000
- }
- func (o *LiguidDataMgr) Adjust(h0, h float64) float64 {
- depth := 0.000
- if h0 > h {
- depth = Precision(h0-h, 3, false)
- }
- data := LiquidData{Time: util.MlNow(), SensorVal: h, CalcVal: 0.000}
- if len(o.LastHourData) > 0 {
- agv, last := o.GetAverageLast()
- diff := math.Abs(last - depth)
- //本次深度值和上次相差100mm,则忽略本次值
- if diff > 100 {
- data.CalcVal = agv
- } else {
- data.CalcVal = depth
- }
- } else {
- if depth < 200 {
- data.CalcVal = depth
- }
- }
- o.LastHourData = append(o.LastHourData, data)
- return data.CalcVal
- }
|