Преглед изворни кода

2023-08-07 优化实时数据

zoie пре 1 година
родитељ
комит
57ddf80a47

+ 1 - 1
Nats/NatsServer/NatsMqtt.go

@@ -91,7 +91,7 @@ func Del_DeviceSensor(v Device.DeviceSensor_Del, T_mqttid string) {
 }
 
 func Get_Device_Realtime(v string, T_mqttid string) {
-	//logs.Println("Nats =>", lib.FuncName(), v)
+	logs.Println("Nats =>", lib.FuncName(),T_mqttid, v)
 	err := lib.Nats.Publish("Get_Device_Realtime"+T_mqttid, []byte(v))
 	if err != nil {
 		logs.Println("Nats =>", lib.FuncName(), err)

+ 45 - 18
controllers/DeviceReal.go

@@ -2,6 +2,7 @@ package controllers
 
 import (
 	"Cold_Api/Nats/NatsServer"
+	"Cold_Api/conf"
 	"Cold_Api/controllers/lib"
 	"Cold_Api/logs"
 	"Cold_Api/models/Device"
@@ -59,36 +60,62 @@ func DeviceRealTime() {
 	time.Sleep(time.Second * 3)
 	for true {
 		// 如果能拿到key,10s等待时间还没结束,则不继续往下执行
-		if Device.Redis_DeviceRealWait_IsExist("DeviceRealWait10Second") {
+		if Device.Redis_DeviceRealWait_IsExist("DeviceRealWait10Second"+conf.MqttServer_id) {
+			fmt.Println("-----------------------",time.Now())
 			time.Sleep(time.Second * 1)
 			continue
 		}
+
 		lib.DeviceRealSnMap.Range(func(k, v interface{}) bool {
+
+			logs.Println("=================", k)
 			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)
+			//num, is := Device.Redis_DeviceReal_Get(sn)
+			//if num >= 3 {
+			//	lib.DeviceRealSnMap.Delete(k)
+			//	Device.Redis_DeviceReal_DelK(sn)
+			//	return true
+			//}
+			//if !is {
+			//	Device.Redis_DeviceReal_Set(sn, 1)
+			//} else {
+			//	Device.Redis_DeviceReal_Set(sn, num+1)
+			//}
 
+			NatsServer.Get_Device_Realtime(sn, mqttid)
+			lib.DeviceRealSnMap.Delete(k)
 			time.Sleep(time.Millisecond * 100)
 			return true
 		})
 
-		Device.Redis_DeviceRealWait_Set("DeviceRealWait10Second")
+		//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"+conf.MqttServer_id)
 		time.Sleep(time.Second * 10)
 	}
 }

+ 6 - 5
controllers/Product.go

@@ -91,12 +91,13 @@ func (c *ProductController) ProductUpgrade_Add() {
 		c.ServeJSON()
 		return
 	}
+	if T_carryout == 1 {
+		deviceList := Device.Read_Device_List_ByT_model(T_model)
 
-	deviceList := Device.Read_Device_List_ByT_model(T_model)
-
-	for _, v := range deviceList {
-		device, _ := Device.Read_Device_ByT_sn(v.T_sn)
-		NatsServer.Up_ProductUpgrade(v.T_sn, T_version, T_file, device.T_mqttid)
+		for _, v := range deviceList {
+			device, _ := Device.Read_Device_ByT_sn(v.T_sn)
+			NatsServer.Up_ProductUpgrade(v.T_sn, T_version, T_file, device.T_mqttid)
+		}
 	}
 
 	System.Add_UserLogs_T(c.Admin_r.T_uuid, "设备版本升级管理", "添加", var_)

+ 1 - 1
controllers/lib/Qiniu.go

@@ -83,7 +83,7 @@ func UploadToken(T_suffix string) string {
 		ForceSaveKey: true,
 		SaveKey:      "UpImage/" + Tokey + "." + T_suffix,
 		FsizeLimit:   1024 * 1024 * 100,
-		MimeLimit:    "image/*;application/pdf;application/octet-stream;application/zip",
+		MimeLimit:    "image/*;application/pdf;application/octet-stream;application/zip;application/x-tar",
 	}
 
 	upToken := putPolicy.UploadToken(Qiniu)

+ 1 - 1
logs/LogPrintln.go

@@ -9,7 +9,7 @@ var logx *logs.BeeLogger
 
 func init() {
 	logx = logs.NewLogger()
-	logx.SetLogger(logs.AdapterFile, `{"filename":"logs/logx/logx.log"}`)
+	logx.SetLogger(logs.AdapterFile, `{"filename":"logs/logx/logx.log","color":true,"Perm":"777"}`)
 	logx.EnableFuncCallDepth(true)
 	logx.SetLogFuncCallDepth(3)
 	logx.SetLevel(logs.LevelInformational)

+ 1 - 1
logs/access.log

@@ -13,7 +13,7 @@ type DataSource struct {
 	Id         int       `orm:"column(ID);size(11);auto;pk"`
 	T_id       string    `orm:"size(256);null"`                                        // 数据源id
 	T_name     string    `orm:"size(256);null"`                                        // 数据源名称
-	T_sql      string    `orm:"size(256);null"`                                        // sql语句
+	T_sql      string    `orm:"text;null"`                                             // sql语句
 	CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
 	UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"`     //auto_now 每次 model 保存时都会对时间自动更新
 }

+ 1 - 0
models/Device/DeviceData.go

@@ -479,6 +479,7 @@ func Read_DeviceData(T_sn string, T_id int) (t DeviceData_) {
 	}
 
 	if len(maps) == 0 {
+		RedisDeviceData_Set(key, t)
 		return t
 	}
 

+ 1 - 1
models/Device/DeviceSensorParameter.go

@@ -137,7 +137,7 @@ func DeviceSensorParameter_T_text(r DeviceSensorParameter) string {
 	s += fmt.Sprintf(" 温度预警下限:%.2f", r.T_tprel)
 	s += fmt.Sprintf(" 温度预警上限:%.2f", r.T_tpreu)
 	s += fmt.Sprintf(" 湿度预警下限:%.2f", r.T_hprel)
-	s += fmt.Sprintf(" 度预警上限:%.2f", r.T_hpreu)
+	s += fmt.Sprintf(" 湿度预警上限:%.2f", r.T_hpreu)
 
 	s += fmt.Sprintf(" 传感器采样率:%d", r.T_speed)
 	s += fmt.Sprintf(" 传感器灵敏度:%d", r.T_sense)