liquid.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package main
  2. import (
  3. "math"
  4. "time"
  5. "lc/common/util"
  6. )
  7. type LiquidData struct {
  8. Time time.Time //采集时间
  9. SensorVal float64 //传感器采集值
  10. CalcVal float64 //深度
  11. }
  12. type LiguidDataMgr struct {
  13. LastHourData []LiquidData //最近一小时数据
  14. }
  15. func (o *LiguidDataMgr) GetAverageLast() (float64, float64) {
  16. n := 0
  17. for i, v := range o.LastHourData {
  18. if util.MlNow().Sub(v.Time).Minutes() > 1 {
  19. n = i
  20. } else {
  21. break
  22. }
  23. }
  24. if n > 0 {
  25. o.LastHourData = o.LastHourData[n+1:]
  26. }
  27. if len(o.LastHourData) > 0 {
  28. //取过去一小时的平均值
  29. depthSum := 0.000
  30. for _, v := range o.LastHourData {
  31. depthSum += v.CalcVal
  32. }
  33. depthSum = depthSum / float64(len(o.LastHourData))
  34. return Precision(depthSum, 3, false), o.LastHourData[len(o.LastHourData)-1].CalcVal
  35. }
  36. return 0.000, 0.000
  37. }
  38. func (o *LiguidDataMgr) Adjust(h0, h float64) float64 {
  39. depth := 0.000
  40. if h0 > h {
  41. depth = Precision(h0-h, 3, false)
  42. }
  43. data := LiquidData{Time: util.MlNow(), SensorVal: h, CalcVal: 0.000}
  44. if len(o.LastHourData) > 0 {
  45. agv, last := o.GetAverageLast()
  46. diff := math.Abs(last - depth)
  47. //本次深度值和上次相差100mm,则忽略本次值
  48. if diff > 100 {
  49. data.CalcVal = agv
  50. } else {
  51. data.CalcVal = depth
  52. }
  53. } else {
  54. if depth < 200 {
  55. data.CalcVal = depth
  56. }
  57. }
  58. o.LastHourData = append(o.LastHourData, data)
  59. return data.CalcVal
  60. }