DeviceReal.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package controllers
  2. import (
  3. "Cold_Api/Nats/NatsServer"
  4. "Cold_Api/conf"
  5. "Cold_Api/controllers/lib"
  6. "Cold_Api/models/Device"
  7. "fmt"
  8. "strings"
  9. "time"
  10. )
  11. // 循环刷新
  12. //func DeviceRealTime() {
  13. // fmt.Println("=====================DeviceRealTime GO===============")
  14. // time.Sleep(time.Second * 3)
  15. // for true {
  16. // for k, v := range lib.DeviceRealSnMap {
  17. // fmt.Println("DeviceRealSnMap:", k, " num:", v)
  18. // NatsServer.Get_Device_Realtime(k)
  19. //
  20. // v--
  21. // lib.DeviceRealSnMap[k] = v
  22. // if v == 0 {
  23. // delete(lib.DeviceRealSnMap, k)
  24. // }
  25. // time.Sleep(time.Millisecond * 100)
  26. // }
  27. // time.Sleep(time.Second * 10)
  28. //
  29. // }
  30. //}
  31. //func DeviceRealTime() {
  32. // fmt.Println("=====================DeviceRealTime GO===============")
  33. // time.Sleep(time.Second * 3)
  34. // for true {
  35. // lib.DeviceRealSnMap.Range(func(k, v interface{}) bool {
  36. // fmt.Println("DeviceRealSnMap:", k, " num:", v)
  37. // NatsServer.Get_Device_Realtime(k.(string))
  38. //
  39. // temp := v.(int)
  40. // temp--
  41. // lib.DeviceRealSnMap.Store(k, temp)
  42. // if temp == 0 {
  43. // lib.DeviceRealSnMap.Delete(k)
  44. // }
  45. // time.Sleep(time.Millisecond * 100)
  46. // return true
  47. // })
  48. // time.Sleep(time.Second * 10)
  49. // }
  50. //}
  51. // DeviceRealSnMap[sn] = 3
  52. func DeviceRealTime() {
  53. fmt.Println("=====================DeviceRealTime GO===============")
  54. time.Sleep(time.Second * 3)
  55. for true {
  56. // 如果能拿到key,10s等待时间还没结束,则不继续往下执行
  57. if Device.Redis_DeviceRealWait_IsExist("DeviceRealWait10Second" + conf.MqttServer_id) {
  58. time.Sleep(time.Second * 1)
  59. continue
  60. }
  61. lib.DeviceRealSnMap.Range(func(k, v interface{}) bool {
  62. //logs.Info("=================", k)
  63. sn := strings.Split(k.(string), "|")[0]
  64. mqttid := strings.Split(k.(string), "|")[1]
  65. //num, is := Device.Redis_DeviceReal_Get(sn)
  66. //if num >= 3 {
  67. // lib.DeviceRealSnMap.Delete(k)
  68. // Device.Redis_DeviceReal_DelK(sn)
  69. // return true
  70. //}
  71. //if !is {
  72. // Device.Redis_DeviceReal_Set(sn, 1)
  73. //} else {
  74. // Device.Redis_DeviceReal_Set(sn, num+1)
  75. //}
  76. NatsServer.Get_Device_Realtime(sn, mqttid)
  77. lib.DeviceRealSnMap.Delete(k)
  78. time.Sleep(time.Millisecond * 100)
  79. return true
  80. })
  81. //lib.DeviceRealSnMap.Range(func(k, v interface{}) bool {
  82. // sn := strings.Split(k.(string), "|")[0]
  83. // mqttid := strings.Split(k.(string), "|")[1]
  84. // num, is := Device.Redis_DeviceReal_Get(sn)
  85. // if !is {
  86. // Device.Redis_DeviceReal_Set(sn, 1)
  87. // } else {
  88. // Device.Redis_DeviceReal_Set(sn, num+1)
  89. // }
  90. // // 每次拿到Map的key后,删除该key,否则会造成死循环
  91. // lib.DeviceRealSnMap.Delete(k)
  92. // // 实时数据请求次数达到180次(30分钟*6次/分钟=180)
  93. // // 更新实时请求为2分钟每次(2分钟*6次/分钟=12)
  94. // if num > 180 && (num-180)%12 != 0 {
  95. // return true
  96. // }
  97. //
  98. // logs.Debug("DeviceRealSnMap:", sn, " num:", num)
  99. // NatsServer.Get_Device_Realtime(sn, mqttid)
  100. //
  101. // time.Sleep(time.Millisecond * 100)
  102. // return true
  103. //})
  104. Device.Redis_DeviceRealWait_Set("DeviceRealWait10Second" + conf.MqttServer_id)
  105. time.Sleep(time.Second * 10)
  106. }
  107. }