package controllers import ( "Cold_Api/Nats/NatsServer" "Cold_Api/controllers/lib" "Cold_Api/logs" "Cold_Api/models/Device" "fmt" "strings" "time" ) // 循环刷新 //func DeviceRealTime() { // fmt.Println("=====================DeviceRealTime GO===============") // time.Sleep(time.Second * 3) // for true { // for k, v := range lib.DeviceRealSnMap { // fmt.Println("DeviceRealSnMap:", k, " num:", v) // NatsServer.Get_Device_Realtime(k) // // v-- // lib.DeviceRealSnMap[k] = v // if v == 0 { // delete(lib.DeviceRealSnMap, k) // } // time.Sleep(time.Millisecond * 100) // } // time.Sleep(time.Second * 10) // // } //} //func DeviceRealTime() { // fmt.Println("=====================DeviceRealTime GO===============") // time.Sleep(time.Second * 3) // for true { // lib.DeviceRealSnMap.Range(func(k, v interface{}) bool { // fmt.Println("DeviceRealSnMap:", k, " num:", v) // NatsServer.Get_Device_Realtime(k.(string)) // // temp := v.(int) // temp-- // lib.DeviceRealSnMap.Store(k, temp) // if temp == 0 { // lib.DeviceRealSnMap.Delete(k) // } // time.Sleep(time.Millisecond * 100) // return true // }) // time.Sleep(time.Second * 10) // } //} // DeviceRealSnMap[sn] = 3 func DeviceRealTime() { fmt.Println("=====================DeviceRealTime GO===============") time.Sleep(time.Second * 3) for true { // 如果能拿到key,10s等待时间还没结束,则不继续往下执行 if Device.Redis_DeviceRealWait_IsExist("DeviceRealWait10Second") { time.Sleep(time.Second * 1) continue } lib.DeviceRealSnMap.Range(func(k, v interface{}) bool { sn := strings.Split(k.(string), "|")[0] mqttid := strings.Split(k.(string), "|")[1] num, is := Device.Redis_DeviceReal_Get(sn) if !is { Device.Redis_DeviceReal_Set(sn, 1) } else { Device.Redis_DeviceReal_Set(sn, num+1) } // 每次拿到Map的key后,删除该key,否则会造成死循环 lib.DeviceRealSnMap.Delete(k) // 实时数据请求次数达到180次(30分钟*6次/分钟=180) // 更新实时请求为2分钟每次(2分钟*6次/分钟=12) if num > 180 && (num-180)%12 != 0 { fmt.Println("num:", num) return true } logs.Debug("DeviceRealSnMap:", sn, " num:", num) NatsServer.Get_Device_Realtime(sn, mqttid) time.Sleep(time.Millisecond * 100) return true }) Device.Redis_DeviceRealWait_Set("DeviceRealWait10Second") time.Sleep(time.Second * 10) } }