DeviceReal.go 3.1 KB

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