uploaddata.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package main
  2. import (
  3. "fmt"
  4. "lc/common/models"
  5. "math"
  6. "sort"
  7. "sync"
  8. "time"
  9. "github.com/thinkgos/timing/v3"
  10. "lc/common/protocol"
  11. "lc/common/util"
  12. )
  13. func truncateNaive(f float64, unit float64) float64 {
  14. return math.Trunc(f/unit) * unit
  15. }
  16. func Precision(f float64, prec int, round bool) float64 {
  17. pow10N := math.Pow10(prec)
  18. if round {
  19. return math.Trunc((f+0.5/pow10N)*pow10N) / pow10N
  20. }
  21. return math.Trunc((f)*pow10N) / pow10N
  22. }
  23. var mapRtuUploadManager sync.Map
  24. type RtuUploadManager struct {
  25. DataLock sync.Mutex
  26. Rtuinfo *protocol.DevInfo
  27. Datatime time.Time
  28. Data map[uint16]float64
  29. Timer *timing.Timing
  30. LiguidDataMgr *LiguidDataMgr //液位计专用
  31. }
  32. func NewRtuUploadManager(rtuinfo *protocol.DevInfo) *RtuUploadManager {
  33. o := RtuUploadManager{
  34. Rtuinfo: rtuinfo,
  35. Data: make(map[uint16]float64),
  36. Timer: timing.New().Run(),
  37. }
  38. if rtuinfo.DevType == 4 && (rtuinfo.TID == 3 || rtuinfo.TID == 5) {
  39. o.LiguidDataMgr = &LiguidDataMgr{}
  40. }
  41. util.GetTagLog().Infof("sys", "NewRtuUploadManager rtuinfo = %v \n", rtuinfo)
  42. o.Timer.AddJobFunc(o.SendData, time.Duration(o.Rtuinfo.SendCloud)*time.Millisecond)
  43. return &o
  44. }
  45. func (o *RtuUploadManager) Stop() {
  46. o.Timer.Close()
  47. o.DataLock.Lock()
  48. defer o.DataLock.Unlock()
  49. o.Rtuinfo = nil
  50. o.Data = nil
  51. }
  52. func (o *RtuUploadManager) AddData(data map[uint16]float64) {
  53. o.DataLock.Lock()
  54. defer o.DataLock.Unlock()
  55. //液位计需要追加深度数据
  56. if o.Rtuinfo.DevType == 4 && (o.Rtuinfo.TID == 3 || o.Rtuinfo.TID == 5) {
  57. for k, v := range data {
  58. o.Data[k] = v
  59. if k == 1 {
  60. fval := o.LiguidDataMgr.Adjust(o.Rtuinfo.Height, v)
  61. o.Data[2] = fval
  62. }
  63. }
  64. } else {
  65. for k, v := range data {
  66. o.Data[k] = v
  67. }
  68. }
  69. // PrintMap(o.Data)
  70. o.Datatime = util.MlNow()
  71. }
  72. func (o *RtuUploadManager) SendData() {
  73. o.DataLock.Lock()
  74. defer func() {
  75. o.Data = make(map[uint16]float64)
  76. o.DataLock.Unlock()
  77. o.Timer.AddJobFunc(o.SendData, time.Duration(o.Rtuinfo.SendCloud)*time.Millisecond)
  78. }()
  79. if len(o.Data) == 0 {
  80. return
  81. }
  82. var obj protocol.Pack_UploadData
  83. if str, err := obj.EnCode(o.Rtuinfo.DevCode, appConfig.GID, GetNextUint64(), nil, o.Rtuinfo.TID, o.Data); err == nil {
  84. topic := GetTopic(o.GetDevType(), o.Rtuinfo.DevCode, protocol.TP_MODBUS_DATA)
  85. GetMQTTMgr().Publish(topic, str, 0, ToAll) //上传消息 modbus rtu
  86. }
  87. }
  88. func (o *RtuUploadManager) GetDevType() string {
  89. if o.Rtuinfo.DevType == 1 {
  90. return protocol.DT_CONCENTRATOR
  91. } else if o.Rtuinfo.DevType == 2 {
  92. return protocol.DT_ENVIRONMENT
  93. } else if o.Rtuinfo.DevType == 4 {
  94. return protocol.DT_LIQUID
  95. } else if o.Rtuinfo.DevType == 5 {
  96. return protocol.DT_ROAD_COND
  97. } else if o.Rtuinfo.DevType == uint8(models.CableGuardian) {
  98. return protocol.DT_CableGuardian
  99. }
  100. return "unknown"
  101. }
  102. type kv struct {
  103. Key uint16
  104. Value float64
  105. }
  106. func PrintMap(m map[uint16]float64) {
  107. var ss []kv
  108. for k, v := range m {
  109. ss = append(ss, kv{k, v})
  110. }
  111. sort.Slice(ss, func(i, j int) bool {
  112. //return ss[i].Key > ss[j].Key // 降序
  113. return ss[i].Key < ss[j].Key // 升序
  114. })
  115. for _, kv := range ss {
  116. fmt.Printf("%d, %0.3f\n", kv.Key, kv.Value)
  117. }
  118. }