DeviceReal.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package controllers
  2. import (
  3. "Cold_Api/Nats/NatsServer"
  4. "Cold_Api/controllers/lib"
  5. "Cold_Api/logs"
  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") {
  58. time.Sleep(time.Second * 1)
  59. continue
  60. }
  61. lib.DeviceRealSnMap.Range(func(k, v interface{}) bool {
  62. sn := strings.Split(k.(string), "|")[0]
  63. mqttid := strings.Split(k.(string), "|")[1]
  64. num, is := Device.Redis_DeviceReal_Get(sn)
  65. if !is {
  66. Device.Redis_DeviceReal_Set(sn, 1)
  67. } else {
  68. Device.Redis_DeviceReal_Set(sn, num+1)
  69. }
  70. // 每次拿到Map的key后,删除该key,否则会造成死循环
  71. lib.DeviceRealSnMap.Delete(k)
  72. // 实时数据请求次数达到180次(30分钟*6次/分钟=180)
  73. // 更新实时请求为2分钟每次(2分钟*6次/分钟=12)
  74. if num > 180 && (num-180)%12 != 0 {
  75. fmt.Println("num:", num)
  76. return true
  77. }
  78. logs.Debug("DeviceRealSnMap:", sn, " num:", num)
  79. NatsServer.Get_Device_Realtime(sn, mqttid)
  80. time.Sleep(time.Millisecond * 100)
  81. return true
  82. })
  83. Device.Redis_DeviceRealWait_Set("DeviceRealWait10Second")
  84. time.Sleep(time.Second * 10)
  85. }
  86. }