瀏覽代碼

增加数据管理增加排序功能
增加数据管理可修改温湿度上下限
增加温湿度显示
增加根据设备SN查询公司名称
补漏功能修改

huangyan 3 周之前
父節點
當前提交
a507d9c9a7

+ 6 - 7
conf/app.conf

@@ -5,10 +5,10 @@ EnableDocs = true
 copyrequestbody = true
 
 # Nats
-NatsServer_Url = "127.0.0.1:43422"
+NatsServer_Url = "182.44.113.60:4222"
 
 # Mysql
-MysqlServer_UrlPort = "127.0.0.1:40306"
+MysqlServer_UrlPort = "182.44.114.135:3306"
 MysqlServer_Database = "cold"
 MysqlServer_Username = "cold"
 MysqlServer_Password = "yjwyEckZS7rE5H!"
@@ -16,13 +16,12 @@ MysqlServer_MaxIdleConnections = 100
 MysqlServer_MaxOpenConnections = 200
 
 # Redis
-# Redis
-Redis_address = "127.0.0.1:43379"
-Redis_password = ""
+Redis_address = "182.44.113.60:6379"
+Redis_password = "redis_wsxaMH"
 Redis_dbNum = "1"
 
 #MQTT
-Mqtt_suffix = ".coldbaozhida.com"
+Mqtt_suffix = "coldbaozhida.com"
 Mqtt_port = 1883
-Mqtt_username = test
+Mqtt_username = coldp
 Mqtt_password = EHM5PpXDD579gmp

+ 1 - 1
controllers/DataController.go

@@ -379,7 +379,7 @@ func (c *DataController) GetCompanyBySn() {
 		c.ServeJSON()
 		return
 	}
-	if admin.T_name == "魏公梅" {
+	if admin.T_name == "宝智达-魏公梅" {
 		sn := c.GetString("sn")
 		company, err := Device.GetCompanyBySn(sn)
 		if err != nil {

+ 140 - 214
controllers/DataGeneratorController.go

@@ -11,6 +11,8 @@ import (
 	beego "github.com/beego/beego/v2/server/web"
 	"github.com/xuri/excelize/v2"
 	"io"
+	"log"
+	"math/rand"
 	"strconv"
 	"strings"
 	"time"
@@ -227,7 +229,7 @@ func (c *DataGeneratorController) CopyFromPosition() {
 
 // RepairSensorData 数据补漏
 func (c *DataGeneratorController) RepairSensorData() {
-	b_, admin := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
+	b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
 	if !b_ {
 		c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
 		c.ServeJSON()
@@ -242,226 +244,150 @@ func (c *DataGeneratorController) RepairSensorData() {
 	end := time.UnixMilli(timeRange[1]).Format("2006-01-02 15:04:05")
 	num := 0
 	for _, v := range sns {
-		sn := v[0]
-		tId := v[1]
-		saveTime := Device.ReadDeviceParameterByTsn(sn).T_saveT
-		list := Device.SelectDeviceSensorDataListByTimeRange(sn, tId, start, end)
+		listdevices, lists := Listdevices(v[0], v[1], start, end)
+		num = normal(listdevices, lists.T_save_t, lists.T_warn, lists.Sn, lists.T_tlower, lists.T_tupper, lists.T_r_hlower, lists.T_r_hupper)
+	}
+	//for _, v := range sns {
+	//	sn := v[0]
+	//	tId := v[1]
+	//	saveTime := Device.ReadDeviceParameterByTsn(sn).T_saveT
+	//	list := Device.SelectDeviceSensorDataListByTimeRange(sn, tId, start, end)
+	//
+	//	for i := 0; i < len(list)-1; i++ {
+	//		current := list[i].T_time
+	//		next := list[i+1].T_time
+	//		ct, _ := time.Parse("2006-01-02 15:04:05 +0800 CST", current)
+	//		//format := cts.Format("2006-01-02 15:04:05")
+	//		//ct, _ := time.Parse("2006-01-02 15:04:05", format)
+	//
+	//		n, _ := time.Parse("2006-01-02 15:04:05 +0800 CST", next)
+	//		//nsfmt := ns.Format("2006-01-02 15:04:05")
+	//		//n, _ := time.Parse("2006-01-02 15:04:05", nsfmt)
+	//		interval := n.Unix() - ct.Unix()
+	//		logs.Debug("时间间隔:", interval)
+	//		fmt.Println("当前:", current, "下一个:", next)
+	//		if int(interval) > saveTime {
+	//			ttInterval := list[i+1].T_t - list[i].T_t
+	//			ttt := list[i].T_t // 温度临时变量
+	//			trhInterval := list[i+1].T_rh - list[i].T_rh
+	//			trht := list[i].T_rh //湿度临时变量
+	//			count := int(interval) / saveTime
+	//			num += count
+	//			for k := 0; k < count; k++ {
+	//				t := ct.Format("2006-01-02 15:04:05") //时间临时变量
+	//				ttt += ttInterval / float64(count)
+	//				trht += trhInterval / float64(count)
+	//				Device.InsertDeviceSensorData(sn, Device.DeviceSensorData{
+	//					list[i].T_id,
+	//					list[i].T_sp,
+	//					t,
+	//					lib.Decimal(ttt),
+	//					lib.Decimal(trht),
+	//					list[i].T_site,
+	//					list[i].CreateTime,
+	//				}, admin)
+	//				ct = ct.Add(time.Second * time.Duration(saveTime))
+	//			}
+	//		}
+	//	}
+	//
+	//}
+
+	c.Data["json"] = lib.JSONS{200, fmt.Sprintf("补漏完成!共补漏%d条数据", num), nil}
+	c.ServeJSON()
+	return
+}
+func Listdevices(sn, t_id, startTime, endTime string) ([]Device.DeviceData, Device.DeviceLists) {
+	var deviceslist Device.DeviceLists
+	sql := fmt.Sprintf("SELECT * FROM z_device_data_%s WHERE t_id=%s and  t_time  BETWEEN '%s' AND '%s';", sn, t_id, startTime, endTime)
+	deviceslist.Sn = sn
+	var devices []Device.DeviceData
+	o := orm.NewOrm()
+	queryRows, err := o.Raw(sql).QueryRows(&devices)
+	if err != nil {
+		log.Fatal(queryRows, err)
+		return devices, deviceslist
+	}
+	sqls := fmt.Sprintf("SELECT t_save_t,t_warn FROM device_parameter WHERE t_sn='%s' ORDER BY update_time DESC LIMIT 1", sn)
+	deviceSensorParameter := fmt.Sprintf("SELECT t__tlower,t__tupper,t__r_hlower,t__r_hupper FROM device_sensor_parameter WHERE  t_sn='%s' and t__state=1 and t_id = '%s' ORDER BY update_time DESC LIMIT 1", sn, t_id)
+	var save_t string
+	var t_warn string
+	err = o.Raw(sqls).QueryRow(&save_t, &t_warn)
+	deviceslist.T_save_t = save_t
+	deviceslist.T_warn = t_warn
+	if err != nil {
+		log.Fatal(err)
+		return devices, deviceslist
+	}
+	err = o.Raw(deviceSensorParameter).QueryRow(&deviceslist.T_tlower, &deviceslist.T_tupper, &deviceslist.T_r_hlower, &deviceslist.T_r_hupper)
+	if err != nil {
+		log.Fatal(err)
+		return devices, deviceslist
+	}
+	return devices, deviceslist
+}
 
-		for i := 0; i < len(list)-1; i++ {
-			current := list[i].T_time
-			next := list[i+1].T_time
-			ct, _ := time.Parse("2006-01-02 15:04:05 +0800 CST", current)
-			//format := cts.Format("2006-01-02 15:04:05")
-			//ct, _ := time.Parse("2006-01-02 15:04:05", format)
-
-			n, _ := time.Parse("2006-01-02 15:04:05 +0800 CST", next)
-			//nsfmt := ns.Format("2006-01-02 15:04:05")
-			//n, _ := time.Parse("2006-01-02 15:04:05", nsfmt)
-			interval := n.Unix() - ct.Unix()
-			logs.Debug("时间间隔:", interval)
-			fmt.Println("当前:", current, "下一个:", next)
-			if int(interval) > saveTime {
-				ttInterval := list[i+1].T_t - list[i].T_t
-				ttt := list[i].T_t // 温度临时变量
-				trhInterval := list[i+1].T_rh - list[i].T_rh
-				trht := list[i].T_rh //湿度临时变量
-				count := int(interval) / saveTime
-				num += count
-				for k := 0; k < count; k++ {
-					t := ct.Format("2006-01-02 15:04:05") //时间临时变量
-					ttt += ttInterval / float64(count)
-					trht += trhInterval / float64(count)
-					Device.InsertDeviceSensorData(sn, Device.DeviceSensorData{
-						list[i].T_id,
-						list[i].T_sp,
-						t,
-						lib.Decimal(ttt),
-						lib.Decimal(trht),
-						list[i].T_site,
-						list[i].CreateTime,
-					}, admin)
-					ct = ct.Add(time.Second * time.Duration(saveTime))
+// 正常情况下补漏数据
+func normal(devices []Device.DeviceData, t_save_t, t_warn, sn string, t_tlower, t_tupper, t_r_hlower, t_r_hupper float64) int {
+	num := 0
+	timeFormat := "2006-01-02 15:04:05 -0700 MST"
+	Format := "2006-01-02 15:04:05"
+	floatSave, _ := strconv.ParseFloat(t_save_t, 64)
+	newOrm := orm.NewOrm()
+	for i := 0; i < len(devices)-1; i++ {
+		t1, err := time.Parse(timeFormat, devices[i].T_time)
+		if err != nil {
+			log.Println("解析时间失败:", err)
+			continue
+		}
+		t2, err := time.Parse(timeFormat, devices[i+1].T_time)
+		if err != nil {
+			log.Println("解析时间失败:", err)
+			continue
+		}
+		timeDiff := t2.Sub(t1).Seconds()
+		if timeDiff > floatSave {
+			fmt.Printf("时间差大于系统设置时间:%s 和 %s\n", devices[i].T_time, devices[i+1].T_time)
+			numInserts := int(timeDiff / floatSave)
+			for j := 1; j <= numInserts-1; j++ {
+				t := t1.Truncate(time.Minute)
+				newTime := t.Add(time.Duration(j*int(floatSave)) * time.Second)
+				T_T, T_Rh := IsNotWarn(devices, t_tlower, t_tupper, t_r_hlower, t_r_hupper)
+				devi := Device.DeviceData{
+					T_id:        devices[i].T_id,
+					T_sp:        devices[i].T_sp,
+					T_time:      newTime.Format(Format),
+					T_t:         T_T,
+					T_rh:        T_Rh,
+					T_site:      devices[i].T_site,
+					Create_time: newTime.Format(Format),
+				}
+				insertSql := fmt.Sprintf("INSERT INTO z_device_data_%s (t_id, t_sp, t_time, t_t, t_rh, t_site, create_time) VALUES (?, ?, ?, ?, ?, ?, ?)", sn)
+				log.Println(insertSql)
+				_, err := newOrm.Raw(insertSql, devi.T_id, devi.T_sp, devi.T_time, devi.T_t, devi.T_rh, devi.T_site.String, devi.Create_time).Exec()
+				if err != nil {
+					log.Println("插入新时间点失败:", err)
+					return 0
 				}
+				num++
+				log.Println("sn:", sn, "补漏数据:", devi, " 插入时间点:", newTime.Format(timeFormat))
 			}
 		}
-
 	}
-
-	c.Data["json"] = lib.JSONS{200, fmt.Sprintf("补漏完成!共补漏%d条数据", num), nil}
-	c.ServeJSON()
-	return
+	return num
 }
 
-// RepairAllSensorData 补漏所有缺失数据
-//
-//	func (c *DataGeneratorController) RepairAllSensorData() {
-//		b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
-//		if !b_ {
-//			//c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
-//			//c.ServeJSON()
-//			//return
-//		}
-//		o := orm.NewOrm()
-//		// 查询设备  BX200GSE  MD100  MD200G  BX100  KF100  MD300G
-//		deviceList := []Device.Device{}
-//
-//		var err error
-//		_, err = o.QueryTable(new(Device.Device)).
-//			Filter("T_model__in", []string{"BX200GSE", "MD100", "MD200G", "BX100", "KF100", "MD300G"}).
-//			Filter("T_state", 1).
-//			//Filter("T_sn", "2023388677995151").
-//			All(&deviceList)
-//		if err != nil {
-//			logs.Error("获取设备列表失败", err)
-//			return
-//		}
-//		start := "2024-07-01 00:00:00"
-//		end := time.Now().Format("2006-01-02 15:04:05")
-//		num := 0
-//		for _, device := range deviceList {
-//			// 获取传感器列表
-//			deviceSensorList := []Device.DeviceSensor{}
-//			_, err = o.QueryTable(new(Device.DeviceSensor)).Filter("T_sn", device.T_sn).All(&deviceSensorList)
-//			if err != nil {
-//				logs.Error(lib.FuncName(), err)
-//			}
-//			counts := 0
-//			for _, v := range deviceSensorList {
-//				sn := v.T_sn
-//				//tId := v.T_id
-//				itoa := strconv.Itoa(v.T_id)
-//				saveTime := Device.ReadDeviceParameterByTsn(sn).T_saveT
-//				list := Device.SelectDeviceSensorDataListByTimeRange(sn, itoa, start, end)
-//
-//				for i := 0; i < len(list)-1; i++ {
-//					current := list[i].T_time
-//					next := list[i+1].T_time
-//					ct, _ := time.Parse("2006-01-02 15:04:05 +0800 CST", current)
-//					n, _ := time.Parse("2006-01-02 15:04:05 +0800 CST", next)
-//					interval := n.Unix() - ct.Unix()
-//					if int(interval) > saveTime {
-//						ttInterval := list[i+1].T_t - list[i].T_t
-//						ttt := list[i].T_t // 温度临时变量
-//						trhInterval := list[i+1].T_rh - list[i].T_rh
-//						trht := list[i].T_rh //湿度临时变量
-//						count := int(interval) / saveTime
-//						num += count
-//						counts += count
-//						for k := 0; k < count; k++ {
-//							t := ct.Format("2006-01-02 15:04:05") //时间临时变量
-//							ttt += ttInterval / float64(count)
-//							trht += trhInterval / float64(count)
-//							//Device.InsertDeviceSensorData(sn, Device.DeviceSensorData{
-//							//	list[i].T_id,
-//							//	list[i].T_sp,
-//							//	t,
-//							//	lib.Decimal(ttt),
-//							//	lib.Decimal(trht),
-//							//	list[i].T_site,
-//							//	list[i].CreateTime,
-//							//}, admin)
-//							ct = ct.Add(time.Second * time.Duration(saveTime))
-//							log.Print(t)
-//						}
-//					}
-//				}
-//			}
-//			// 每个设备补漏完成后睡眠一秒
-//			time.Sleep(time.Second * 1)
-//			logs.Info(fmt.Sprintf("设备 %s 补漏完成!共补漏 %d 条数据", device.T_sn, counts))
-//		}
-//		c.Data["json"] = lib.JSONS{200, fmt.Sprintf("补漏完成!共补漏%d条数据", num), nil}
-//		c.ServeJSON()
-//		return
-//	}
-//func (c *DataGeneratorController) RepairAllSensorData() {
-//	b_, admin := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
-//	if !b_ {
-//		// 用户验证逻辑省略
-//	}
-//	o := orm.NewOrm()
-//	deviceList := []Device.Device{}
-//
-//	var err error
-//	_, err = o.QueryTable(new(Device.Device)).
-//		Filter("T_model__in", []string{"BX200GSE", "MD100", "MD200G", "BX100", "KF100", "MD300G"}).
-//		Filter("T_state", 1).
-//		//Filter("T_sn", "2023388677995151").
-//		All(&deviceList)
-//	if err != nil {
-//		logs.Error("获取设备列表失败", err)
-//		return
-//	}
-//
-//	start := "2024-07-01 00:00:00"
-//	end := time.Now().Format("2006-01-02 15:04:05")
-//
-//	// 创建或打开日志文件
-//	logFile, err := os.OpenFile("repair_log.txt", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
-//	if err != nil {
-//		logs.Error("打开日志文件失败", err)
-//		return
-//	}
-//	defer logFile.Close()
-//
-//	// 使用 log 包的 Writer 将日志输出到文件
-//	fileLogger := log.New(logFile, "", log.LstdFlags)
-//	for _, device := range deviceList {
-//		deviceSensorList := []Device.DeviceSensor{}
-//		_, err = o.QueryTable(new(Device.DeviceSensor)).Filter("T_sn", device.T_sn).All(&deviceSensorList)
-//		if err != nil {
-//			logs.Error(lib.FuncName(), err)
-//			continue
-//		}
-//
-//		num := 0 // 统计当前设备补漏的数据条数
-//		for _, v := range deviceSensorList {
-//			sn := v.T_sn
-//			itoa := strconv.Itoa(v.T_id)
-//			saveTime := Device.ReadDeviceParameterByTsn(sn).T_saveT
-//			list := Device.SelectDeviceSensorDataListByTimeRange(sn, itoa, start, end)
-//
-//			for i := 0; i < len(list)-1; i++ {
-//				current := list[i].T_time
-//				next := list[i+1].T_time
-//				ct, _ := time.Parse("2006-01-02 15:04:05 +0800 CST", current)
-//				n, _ := time.Parse("2006-01-02 15:04:05 +0800 CST", next)
-//				interval := n.Unix() - ct.Unix()
-//				if int(interval) > saveTime {
-//					ttInterval := list[i+1].T_t - list[i].T_t
-//					ttt := list[i].T_t // 温度临时变量
-//					trhInterval := list[i+1].T_rh - list[i].T_rh
-//					trht := list[i].T_rh //湿度临时变量
-//					count := int(interval) / saveTime
-//					num += count
-//					for k := 0; k < count; k++ {
-//						t := ct.Format("2006-01-02 15:04:05") //时间临时变量
-//						ttt += ttInterval / float64(count)
-//						trht += trhInterval / float64(count)
-//						Device.InsertDeviceSensorData(sn, Device.DeviceSensorData{
-//							list[i].T_id,
-//							list[i].T_sp,
-//							t,
-//							lib.Decimal(ttt),
-//							lib.Decimal(trht),
-//							list[i].T_site,
-//							list[i].CreateTime,
-//						}, admin)
-//						ct = ct.Add(time.Second * time.Duration(saveTime))
-//						log.Print(t)
-//					}
-//				}
-//			}
-//		}
-//		// 每个设备补漏完成后记录到文件
-//		fileLogger.Printf("设备 %s 补漏完成!共补漏 %d 条数据\n", device.T_sn, num)
-//		time.Sleep(time.Second * 1)
-//	}
-//
-//	c.Data["json"] = lib.JSONS{200, fmt.Sprintf("所有设备补漏完成!"), nil}
-//	c.ServeJSON()
-//	return
-//}
+// 判断是否在预警范围内,并且返回在预警内的值
+func IsNotWarn(deviceDatas []Device.DeviceData, t_tlower, t_tupper, t_r_hlower, t_r_hupper float64) (T_T, T_Rh float64) {
+	for i, _ := range deviceDatas {
+		if !(deviceDatas[i].T_t < t_tlower || deviceDatas[i].T_t > t_tupper || deviceDatas[i].T_rh < t_r_hlower || deviceDatas[i].T_rh > t_r_hupper) {
+			T_T = deviceDatas[i].T_t + rand.Float64()*0.4 - 0.2
+			T_Rh = deviceDatas[i].T_rh + rand.Float64()*0.4 - 0.2
+			return T_T, T_Rh
+		}
+	}
+	return 0, 0
+}
 
 func (c *DataGeneratorController) RepairAllSensorData() {
 	b_, admin := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))

+ 213 - 65
controllers/DeviceController.go

@@ -7,11 +7,11 @@ import (
 	"ColdP_server/models/Company"
 	"ColdP_server/models/Device"
 	"ColdP_server/models/Warning"
+	"context"
 	"encoding/json"
 	"fmt"
 	beego "github.com/beego/beego/v2/server/web"
 	"io"
-	"io/ioutil"
 	"log"
 	"math"
 	"strconv"
@@ -80,7 +80,6 @@ func (c *DeviceController) CompanyClass() {
 
 // DataRepeat 数据重传
 func (c *DeviceController) DataRepeat() {
-
 	b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
 	if !b_ {
 		c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
@@ -89,34 +88,45 @@ func (c *DeviceController) DataRepeat() {
 	}
 
 	t := MqttServer.DataRepeat_C{}
-	bytes, _ := ioutil.ReadAll(c.Ctx.Request.Body)
+	bytes, _ := io.ReadAll(c.Ctx.Request.Body)
 	json.Unmarshal(bytes, &t)
 	fmt.Println("浏览器接收数据:", t)
 	s, _ := time.Parse("2006-01-02 15:04:05", t.StartTime)
 	e, _ := time.Parse("2006-01-02 15:04:05", t.EndTime)
 
-	//发送MQTT
+	// 发送MQTT
 	for k, v := range t.Sns {
 		topic := fmt.Sprintf("/pub/%s", k)
 		repeatPub := MqttServer.DataRepeat_Pub{Sn: k, Type: 9, Mid: time.Now().Unix(), Data: MqttServer.DataRepeat_Pub_Data{Start: s.Unix(), End: e.Unix(), Id: v}}
 		msg, _ := json.Marshal(repeatPub)
 		mqttId := strings.Split(Device.ReadDeviceMqttId(k), "\"")[0]
-		client := MqttServer.GetMqttClient(mqttId)
+
+		client, err := MqttServer.GetMqttClient(mqttId)
+		if err != nil {
+			log.Printf("Error getting MQTT client for SN: %s, %v", k, err)
+			continue
+		}
+
 		for i := 0; i < 3; i++ {
 			time.Sleep(time.Second * time.Duration(i+1))
-			MqttServer.PubMqttMessage(client, topic, msg)
+			if err := MqttServer.PubMqttMessage(client, topic, msg); err != nil {
+				log.Printf("Error publishing MQTT message for SN: %s, %v", k, err)
+				c.Data["json"] = lib.JSONS{Code: 0, Msg: "设置失败", Data: k}
+				return
+			}
 		}
+
 		client.Disconnect()
 		client.Terminate()
 	}
-	c.Data["json"] = lib.JSONS{200, "数据重传成功", nil}
+
+	c.Data["json"] = lib.JSONS{Code: 200, Msg: "数据重传成功", Data: nil}
 	c.ServeJSON()
 	return
 }
 
 // ReadDeviation 读取偏差值
 func (c *DeviceController) ReadDeviation() {
-
 	b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
 	if !b_ {
 		c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
@@ -125,44 +135,74 @@ func (c *DeviceController) ReadDeviation() {
 	}
 
 	t := make(map[string][]int)
-	bytes, _ := ioutil.ReadAll(c.Ctx.Request.Body)
-	json.Unmarshal(bytes, &t)
+	bytes, _ := io.ReadAll(c.Ctx.Request.Body)
+	if err := json.Unmarshal(bytes, &t); err != nil {
+		log.Printf("Error unmarshalling JSON: %v", err)
+		c.Data["json"] = lib.JSONS{Code: 400, Msg: "Bad Request"}
+		c.ServeJSON()
+		return
+	}
 	fmt.Println("浏览器接收数据:", t)
 
-	//MQTT发送
+	// MQTT发送
 	fmt.Println("发送MQTT t:", t)
 	deviation := make(chan string, 10)
 	var count = 0
+
 	for k, v := range t {
 		topicSub := fmt.Sprintf("/sub/%s", k)
-		fmt.Println(v)
 		topicPub := fmt.Sprintf("/pub/%s", k)
 		mqttId := Device.ReadDeviceMqttId(k)
-		client := MqttServer.GetMqttClient(mqttId)
-		MqttServer.Subscript(client, topicSub, deviation, "\"type\":7,")
+
+		client, err := MqttServer.GetMqttClient(mqttId)
+		if err != nil {
+			log.Printf("Error getting MQTT client for SN: %s, %v", k, err)
+			continue
+		}
+
+		// 订阅主题
+		if err := MqttServer.Subscript(client, topicSub, deviation, "\"type\":7,"); err != nil {
+			log.Printf("Error subscribing to MQTT topic for SN: %s, %v", k, err)
+			c.Data["json"] = lib.JSONS{Code: 1, Msg: "mqtt订阅连接失败", Data: nil}
+			return
+			continue
+		}
+
 		pubData, _ := json.Marshal(MqttServer.Deviation_Pub{
 			Sn:   k,
 			Type: 7,
 			Mid:  time.Now().Unix(),
 			Data: v,
 		})
-		MqttServer.PubMqttMessage(client, topicPub, pubData)
+
+		if err := MqttServer.PubMqttMessage(client, topicPub, pubData); err != nil {
+			log.Printf("Error publishing MQTT message for SN: %s, %v", k, err)
+			c.Data["json"] = lib.JSONS{Code: 1, Msg: "mqtt发送失败", Data: k}
+			continue
+		}
 		count++
 	}
+
 	deviceRepeatData := make([]string, 0)
-	select {
-	case v := <-deviation:
-		fmt.Println("channel收到数据:", v)
-		fmt.Println("count:", count)
-		deviceRepeatData = append(deviceRepeatData, v)
-		count--
-		if count <= 0 {
-			close(deviation)
-			break
+	timeout := time.After(10 * time.Second) // 设置超时时间
+	for count > 0 {
+		select {
+		case v := <-deviation:
+			fmt.Println("channel收到数据:", v)
+			fmt.Println("count:", count)
+			deviceRepeatData = append(deviceRepeatData, v)
+			count--
+		case <-timeout:
+			log.Println("Timeout waiting for MQTT responses")
+			c.Data["json"] = lib.JSONS{Code: 500, Msg: "Timeout waiting for MQTT responses"}
+			c.ServeJSON()
+			return
 		}
 	}
+
+	close(deviation)
 	fmt.Println("响应数据:", deviceRepeatData)
-	c.Data["json"] = lib.JSONS{200, "偏差值上传成功", deviceRepeatData}
+	c.Data["json"] = lib.JSONS{Code: 200, Msg: "偏差值上传成功", Data: deviceRepeatData}
 	c.ServeJSON()
 	return
 }
@@ -175,33 +215,60 @@ func (c *DeviceController) WriteDeviation() {
 		c.ServeJSON()
 		return
 	}
+
 	bytes, _ := io.ReadAll(c.Ctx.Request.Body)
-	data := make([]MqttServer.Deviation_Sub, 0)
 	fmt.Println("请求json:", string(bytes))
-	json.Unmarshal(bytes, &data)
+
+	data := make([]MqttServer.Deviation_Sub, 0)
+	if err := json.Unmarshal(bytes, &data); err != nil {
+		log.Printf("Error unmarshalling JSON: %v", err)
+		c.Data["json"] = lib.JSONS{Code: 400, Msg: "Bad Request"}
+		c.ServeJSON()
+		return
+	}
+
+	// 处理每个设备的数据
 	for _, v := range data {
 		go func(v MqttServer.Deviation_Sub) {
 			v.Type = 8
 			v.Mid = int(time.Now().Unix())
-			mqttid := Device.ReadDeviceMqttId(v.Sn)
-			cli := MqttServer.GetMqttClient(mqttid)
-			bytes, _ := json.Marshal(v)
+			mqttId := Device.ReadDeviceMqttId(v.Sn)
+
+			client, err := MqttServer.GetMqttClient(mqttId)
+			if err != nil {
+				log.Printf("Error getting MQTT client for SN: %s, %v", v.Sn, err)
+				return
+			}
+
+			msgBytes, err := json.Marshal(v)
+			if err != nil {
+				log.Printf("Error marshalling JSON for SN: %s, %v", v.Sn, err)
+				client.Disconnect()
+				client.Terminate()
+				return
+			}
+
 			for i := 0; i < 3; i++ {
 				time.Sleep(time.Second * time.Duration(i+1))
-				MqttServer.PubMqttMessage(cli, fmt.Sprintf("/pub/%s", v.Sn), bytes)
+				if err := MqttServer.PubMqttMessage(client, fmt.Sprintf("/pub/%s", v.Sn), msgBytes); err != nil {
+					log.Printf("Error publishing MQTT message for SN: %s, %v", v.Sn, err)
+					c.Data["json"] = lib.JSONS{Code: 0, Msg: "设置失败", Data: v.Sn}
+					return
+				}
 			}
-			cli.Disconnect()
-			cli.Terminate()
+
+			client.Disconnect()
+			client.Terminate()
 		}(v)
 	}
-	c.Data["json"] = lib.JSONS{200, "设置偏差值成功!", nil}
+
+	c.Data["json"] = lib.JSONS{Code: 200, Msg: "设置偏差值成功!", Data: nil}
 	c.ServeJSON()
 	return
 }
 
 // ReadSensor 读取偏差值
 func (c *DeviceController) ReadSensor() {
-
 	b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
 	if !b_ {
 		c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
@@ -210,51 +277,78 @@ func (c *DeviceController) ReadSensor() {
 	}
 
 	t := make(map[string][]int)
-	bytes, _ := ioutil.ReadAll(c.Ctx.Request.Body)
-	json.Unmarshal(bytes, &t)
+	bytes, _ := io.ReadAll(c.Ctx.Request.Body)
+	if err := json.Unmarshal(bytes, &t); err != nil {
+		log.Printf("Error unmarshalling JSON: %v", err)
+		c.Data["json"] = lib.JSONS{Code: 400, Msg: "Bad Request"}
+		c.ServeJSON()
+		return
+	}
 	fmt.Println("浏览器接收数据:", t)
 
-	//MQTT发送
+	// MQTT发送
 	fmt.Println("发送MQTT t:", t)
 	deviation := make(chan string, 10)
 	var count = 0
+
 	for k, v := range t {
 		topicSub := fmt.Sprintf("/sub/%s", k)
-		fmt.Println(v)
 		topicPub := fmt.Sprintf("/pub/%s", k)
 		mqttId := Device.ReadDeviceMqttId(k)
-		client := MqttServer.GetMqttClient(mqttId)
-		MqttServer.Subscript(client, topicSub, deviation, "\"type\":5,")
+
+		client, err := MqttServer.GetMqttClient(mqttId)
+		if err != nil {
+			log.Printf("Error getting MQTT client for SN: %s, %v", k, err)
+			continue
+		}
+
+		// 订阅主题
+		if err := MqttServer.Subscript(client, topicSub, deviation, "\"type\":5,"); err != nil {
+			log.Printf("Error subscribing to MQTT topic for SN: %s, %v", k, err)
+			continue
+		}
+
 		pubData, _ := json.Marshal(MqttServer.Deviation_Pub{
 			Sn:   k,
 			Type: 5,
 			Mid:  time.Now().Unix(),
 			Data: v,
 		})
-		MqttServer.PubMqttMessage(client, topicPub, pubData)
+
+		if err := MqttServer.PubMqttMessage(client, topicPub, pubData); err != nil {
+			log.Printf("Error publishing MQTT message for SN: %s, %v", k, err)
+			c.Data["json"] = lib.JSONS{Code: 1, Msg: "mqtt发送失败", Data: k}
+			continue
+		}
+
 		count++
 	}
+
 	deviceRepeatData := make([]string, 0)
-	select {
-	case v := <-deviation:
-		fmt.Println("channel收到数据:", v)
-		fmt.Println("count:", count)
-
-		deviceRepeatData = append(deviceRepeatData, v)
-		count--
-		if count <= 0 {
-			close(deviation)
-			break
+	timeout := time.After(10 * time.Second) // 设置超时时间
+	for count > 0 {
+		select {
+		case v := <-deviation:
+			fmt.Println("channel收到数据:", v)
+			fmt.Println("count:", count)
+			deviceRepeatData = append(deviceRepeatData, v)
+			count--
+		case <-timeout:
+			log.Println("Timeout waiting for MQTT responses")
+			c.Data["json"] = lib.JSONS{Code: 500, Msg: "Timeout waiting for MQTT responses"}
+			c.ServeJSON()
+			return
 		}
-
 	}
+
+	close(deviation)
 	fmt.Println("响应数据:", deviceRepeatData)
-	c.Data["json"] = lib.JSONS{200, "偏差值上传成功", deviceRepeatData}
+	c.Data["json"] = lib.JSONS{Code: 200, Msg: "偏差值上传成功", Data: deviceRepeatData}
 	c.ServeJSON()
 	return
 }
 
-// WriteSensor WriteDeviation 设置偏差值
+// WriteSensor 设置偏差值
 func (c *DeviceController) WriteSensor() {
 	b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
 	if !b_ {
@@ -262,26 +356,80 @@ func (c *DeviceController) WriteSensor() {
 		c.ServeJSON()
 		return
 	}
-	bytes, _ := io.ReadAll(c.Ctx.Request.Body)
-	data := make([]MqttServer.Sensor_Sub, 0)
+
+	bytes, err := io.ReadAll(c.Ctx.Request.Body)
+	if err != nil {
+		log.Printf("Error reading request body: %v", err)
+		c.Data["json"] = lib.JSONS{Code: 500, Msg: "Internal Server Error"}
+		c.ServeJSON()
+		return
+	}
 	fmt.Println("请求json:", string(bytes))
-	json.Unmarshal(bytes, &data)
+
+	data := make([]MqttServer.Sensor_Sub, 0)
+	if err := json.Unmarshal(bytes, &data); err != nil {
+		log.Printf("Error unmarshalling JSON: %v", err)
+		c.Data["json"] = lib.JSONS{Code: 400, Msg: "Bad Request"}
+		c.ServeJSON()
+		return
+	}
+
+	// 设置上下文超时
+	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
+	defer cancel()
+
+	// 处理每个设备的数据
 	for _, v := range data {
 		go func(v MqttServer.Sensor_Sub) {
+			select {
+			case <-ctx.Done():
+				log.Printf("Context canceled or timeout for SN: %s", v.Sn)
+				return
+			default:
+			}
+
 			v.Type = 6
 			v.Mid = int(time.Now().Unix())
-			mqttid := Device.ReadDeviceMqttId(v.Sn)
-			cli := MqttServer.GetMqttClient(mqttid)
-			bytes, _ := json.Marshal(v)
+			mqttId := Device.ReadDeviceMqttId(v.Sn)
+
+			client, err := MqttServer.GetMqttClient(mqttId)
+			if err != nil {
+				log.Printf("Error getting MQTT client for SN: %s, %v", v.Sn, err)
+				return
+			}
+
+			msgBytes, err := json.Marshal(v)
+			if err != nil {
+				log.Printf("Error marshalling JSON for SN: %s, %v", v.Sn, err)
+				client.Disconnect()
+				client.Terminate()
+				return
+			}
+
 			for i := 0; i < 3; i++ {
 				time.Sleep(time.Second * time.Duration(i+1))
-				MqttServer.PubMqttMessage(cli, fmt.Sprintf("/pub/%s", v.Sn), bytes)
+				select {
+				case <-ctx.Done():
+					log.Printf("Context canceled or timeout for SN: %s", v.Sn)
+					client.Disconnect()
+					client.Terminate()
+					return
+				default:
+				}
+
+				if err := MqttServer.PubMqttMessage(client, fmt.Sprintf("/pub/%s", v.Sn), msgBytes); err != nil {
+					log.Printf("Error publishing MQTT message for SN: %s, %v", v.Sn, err)
+					c.Data["json"] = lib.JSONS{Code: 0, Msg: "设置失败", Data: v.Sn}
+					return
+				}
 			}
-			cli.Disconnect()
-			cli.Terminate()
+
+			client.Disconnect()
+			client.Terminate()
 		}(v)
 	}
-	c.Data["json"] = lib.JSONS{200, "设置偏差值成功!", nil}
+
+	c.Data["json"] = lib.JSONS{Code: 200, Msg: "设置偏差值成功!", Data: nil}
 	c.ServeJSON()
 	return
 }

+ 63 - 35
controllers/MqttServer/MqttServev2.go

@@ -12,79 +12,107 @@ import (
 )
 
 var (
-	mqtt_suffix,
-	mqtt_port,
-	mqtt_username,
-	mqtt_password string
-	err error
+	mqttSuffix   string
+	mqttPort     string
+	mqttUsername string
+	mqttPassword string
 )
 
 func init() {
-	mqtt_suffix, err = beego.AppConfig.String("Mqtt_suffix")
-	mqtt_port, err = beego.AppConfig.String("Mqtt_port")
-	mqtt_username, err = beego.AppConfig.String("Mqtt_username")
-	mqtt_password, err = beego.AppConfig.String("Mqtt_password")
-}
+	var err error
+	mqttSuffix, err = beego.AppConfig.String("Mqtt_suffix")
+	if err != nil {
+		log.Fatalf("Failed to load Mqtt_suffix: %v", err)
+	}
+
+	mqttPort, err = beego.AppConfig.String("Mqtt_port")
+	if err != nil {
+		log.Fatalf("Failed to load Mqtt_port: %v", err)
+	}
+
+	mqttUsername, err = beego.AppConfig.String("Mqtt_username")
+	if err != nil {
+		log.Fatalf("Failed to load Mqtt_username: %v", err)
+	}
 
-// GetConnectionOptions 获取链接参数
+	mqttPassword, err = beego.AppConfig.String("Mqtt_password")
+	if err != nil {
+		log.Fatalf("Failed to load Mqtt_password: %v", err)
+	}
+}
 
-func GetMqttClient(mqttAddrPrefix string) *client.Client {
+// GetMqttClient 获取MQTT客户端连接
+func GetMqttClient(mqttAddrPrefix string) (*client.Client, error) {
 	r := rand.New(rand.NewSource(time.Now().Unix()))
 	options := client.ConnectOptions{
 		Network:  "tcp",
-		Address:  fmt.Sprintf("%s.coldbaozhida.com:1883", mqttAddrPrefix),
-		UserName: []byte(mqtt_username),
-		Password: []byte(mqtt_password),
+		Address:  fmt.Sprintf("%s.%s:%s", mqttAddrPrefix, mqttSuffix, mqttPort),
+		UserName: []byte(mqttUsername),
+		Password: []byte(mqttPassword),
 		ClientID: []byte(fmt.Sprintf("ColdP_server_%d", r.Intn(9))),
 	}
-	cli := client.New(&client.Options{ErrorHandler: func(e error) {
-		fmt.Println("PubMqtt Error 发布信息错误:", e.Error())
-	}})
-	err = cli.Connect(&options)
-	if err != nil {
-		fmt.Println("MQTT 连接失败", err.Error())
+
+	cli := client.New(&client.Options{
+		ErrorHandler: func(e error) {
+			log.Printf("MQTT Error: %v", e)
+		},
+	})
+
+	if err := cli.Connect(&options); err != nil {
+		log.Printf("MQTT 连接失败: %v", err)
+		return nil, err
 	}
-	return cli
+
+	return cli, nil
 }
 
 // PubMqttMessage 发送信息
-func PubMqttMessage(cli *client.Client, topic string, msg []byte) {
+func PubMqttMessage(cli *client.Client, topic string, msg []byte) error {
 	fmt.Printf("发布主题:%s -> 发布内容:%s\n", topic, string(msg))
-	time.Sleep(time.Second)
-	err = cli.Publish(&client.PublishOptions{
+
+	opts := &client.PublishOptions{
 		QoS:       mqtt.QoS0,
 		TopicName: []byte(topic),
 		Message:   msg,
-	})
-	if err != nil {
-		log.Panicln("Mqtt发布失败", err.Error())
-		return
 	}
+
+	if err := cli.Publish(opts); err != nil {
+		log.Printf("MQTT 发布失败: %v", err)
+		return err
+	}
+
 	fmt.Println("MQTT发送成功!")
+	return nil
 }
 
 // Subscript 订阅信息
-func Subscript(cli *client.Client, topic string, msg chan string, is_str string) {
+func Subscript(cli *client.Client, topic string, msg chan string, isStr string) error {
 	fmt.Println("连接成功", time.Now().Format("2006-01-02 15:04:05"))
 	fmt.Printf("订阅主题:%s\n", topic)
-	cli.Subscribe(&client.SubscribeOptions{
+
+	subOpts := &client.SubscribeOptions{
 		SubReqs: []*client.SubReq{
 			&client.SubReq{
 				TopicFilter: []byte(topic),
 				QoS:         mqtt.QoS0,
 				Handler: func(topicName, message []byte) {
-					//如果管道已经关闭,表示已经超时
 					fmt.Println("MQTT接收到信息:", string(message))
-					if strings.Contains(strings.Replace(string(message), " ", "", -1), is_str) {
+					if strings.Contains(strings.ReplaceAll(string(message), " ", ""), isStr) {
 						msg <- topic + "||" + string(message)
 						if _, ok := <-msg; !ok {
 							cli.Disconnect()
 							cli.Terminate()
 						}
 					}
-
 				},
 			},
 		},
-	})
+	}
+
+	if err := cli.Subscribe(subOpts); err != nil {
+		log.Printf("MQTT 订阅失败: %v", err)
+		return err
+	}
+
+	return nil
 }

+ 1 - 1
lastupdate.tmp

@@ -1 +1 @@
-{"E:\\WebstormProjects\\ColdP_server\\controllers":1727162853853977700}
+{"E:\\WebstormProjects\\ColdP_server\\controllers":1730689691305635800}

+ 210 - 0
logs/logx/logx.log

@@ -105278,3 +105278,213 @@
 2024/09/24 16:02:25.734 [I] [WarningType.go:193]  =========== 初始化报警类型 =========
 2024/09/24 16:04:04.775 [I] [WarningType.go:193]  =========== 初始化报警类型 =========
 2024/09/24 16:04:59.582 [I] [WarningType.go:193]  =========== 初始化报警类型 =========
+2024/09/24 16:34:43.981 [I] [WarningType.go:193]  =========== 初始化报警类型 =========
+2024/09/24 16:35:54.106 [I] [WarningType.go:193]  =========== 初始化报警类型 =========
+2024/09/24 16:36:47.090 [I] [WarningType.go:193]  =========== 初始化报警类型 =========
+2024/09/24 16:47:43.669 [I] [WarningType.go:193]  =========== 初始化报警类型 =========
+2024/09/24 16:54:35.089 [I] [WarningType.go:193]  =========== 初始化报警类型 =========
+2024/10/18 10:18:39.552 [I] [WarningType.go:193]  =========== 初始化报警类型 =========
+2024/10/18 10:36:31.592 [I] [WarningType.go:193]  =========== 初始化报警类型 =========
+2024/10/18 10:40:40.096 [I] [WarningType.go:193]  =========== 初始化报警类型 =========
+2024/10/18 11:00:33.567 [I] [WarningType.go:193]  =========== 初始化报警类型 =========
+2024/10/28 13:55:30.735 [I] [WarningType.go:193]  =========== 初始化报警类型 =========
+2024/10/28 13:57:06.381 [I] [WarningType.go:193]  =========== 初始化报警类型 =========
+2024/10/28 14:34:22.380 [I] [WarningType.go:193]  =========== 初始化报警类型 =========
+2024/10/29 15:04:30.032 [I] [WarningType.go:193]  =========== 初始化报警类型 =========
+2024/10/29 15:09:42.561 [I] [WarningType.go:193]  =========== 初始化报警类型 =========
+2024/10/29 15:12:34.716 [I] [WarningType.go:193]  =========== 初始化报警类型 =========
+2024/10/29 15:24:12.609 [I] [WarningType.go:193]  =========== 初始化报警类型 =========
+2024/10/29 15:25:57.864 [I] [WarningType.go:193]  =========== 初始化报警类型 =========
+2024/10/29 17:31:42.185 [I] [WarningType.go:193]  =========== 初始化报警类型 =========
+2024/10/29 17:45:52.155 [I] [WarningType.go:193]  =========== 初始化报警类型 =========
+2024/11/04 10:02:52.600 [I] [WarningType.go:193]  =========== 初始化报警类型 =========
+2024/11/04 10:05:07.100 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.100 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.100 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.100 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.100 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.100 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.100 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.101 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.101 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.101 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.101 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.101 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.101 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.101 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.101 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.101 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.101 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.101 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.101 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.101 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.101 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.101 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.101 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.101 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.101 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.101 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.101 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.102 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.102 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.102 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.102 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.102 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.102 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.102 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.102 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.102 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.102 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.102 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.102 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.102 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.102 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.102 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.102 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.102 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.102 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.102 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.102 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.102 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.102 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.102 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.102 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.102 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.102 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.102 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.103 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.103 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.103 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.103 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.103 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.103 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.103 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.103 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.103 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.103 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.103 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.103 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.103 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.103 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.103 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:05:07.103 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.305 [D] [DataGeneratorController.go:263]  时间间隔: 1800
+2024/11/04 10:06:30.305 [D] [DataGeneratorController.go:263]  时间间隔: 1800
+2024/11/04 10:06:30.305 [D] [DataGeneratorController.go:263]  时间间隔: 1800
+2024/11/04 10:06:30.305 [D] [DataGeneratorController.go:263]  时间间隔: 1800
+2024/11/04 10:06:30.305 [D] [DataGeneratorController.go:263]  时间间隔: 1800
+2024/11/04 10:06:30.305 [D] [DataGeneratorController.go:263]  时间间隔: 1800
+2024/11/04 10:06:30.305 [D] [DataGeneratorController.go:263]  时间间隔: 1800
+2024/11/04 10:06:30.305 [D] [DataGeneratorController.go:263]  时间间隔: 1800
+2024/11/04 10:06:30.305 [D] [DataGeneratorController.go:263]  时间间隔: 1800
+2024/11/04 10:06:30.305 [D] [DataGeneratorController.go:263]  时间间隔: 1800
+2024/11/04 10:06:30.305 [D] [DataGeneratorController.go:263]  时间间隔: 1800
+2024/11/04 10:06:30.306 [D] [DataGeneratorController.go:263]  时间间隔: 1800
+2024/11/04 10:06:30.306 [D] [DataGeneratorController.go:263]  时间间隔: 1800
+2024/11/04 10:06:30.327 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.328 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.328 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.328 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.328 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.328 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.328 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.328 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.328 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.328 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.328 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.328 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.328 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.328 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.328 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.328 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.328 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.328 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.328 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.328 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.328 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.328 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.328 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.328 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.328 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.328 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.328 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.328 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.328 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.328 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.328 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.328 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.328 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.328 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.329 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.329 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.329 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.329 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.329 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.329 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.329 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.329 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.329 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.329 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.329 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.329 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.329 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.329 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.329 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.329 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.329 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.329 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.329 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.329 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.329 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.330 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.330 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.330 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.330 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.330 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.330 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.330 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.330 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.330 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.330 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.330 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.330 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.330 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.330 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.330 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.330 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.330 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.330 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.330 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.330 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.330 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.330 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.330 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.330 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.330 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.330 [D] [DataGeneratorController.go:263]  时间间隔: 300
+2024/11/04 10:06:30.349 [D] [DataGeneratorController.go:263]  时间间隔: 1800
+2024/11/04 10:06:30.349 [D] [DataGeneratorController.go:263]  时间间隔: 1800
+2024/11/04 10:06:30.349 [D] [DataGeneratorController.go:263]  时间间隔: 1800
+2024/11/04 10:06:30.349 [D] [DataGeneratorController.go:263]  时间间隔: 1800
+2024/11/04 10:06:30.349 [D] [DataGeneratorController.go:263]  时间间隔: 1800
+2024/11/04 10:06:30.349 [D] [DataGeneratorController.go:263]  时间间隔: 1800
+2024/11/04 10:06:30.349 [D] [DataGeneratorController.go:263]  时间间隔: 1800
+2024/11/04 10:06:30.349 [D] [DataGeneratorController.go:263]  时间间隔: 1800
+2024/11/04 10:06:30.349 [D] [DataGeneratorController.go:263]  时间间隔: 1800
+2024/11/04 10:06:30.350 [D] [DataGeneratorController.go:263]  时间间隔: 1800
+2024/11/04 10:06:30.350 [D] [DataGeneratorController.go:263]  时间间隔: 1800
+2024/11/04 10:06:30.350 [D] [DataGeneratorController.go:263]  时间间隔: 1800
+2024/11/04 10:06:30.350 [D] [DataGeneratorController.go:263]  时间间隔: 1800
+2024/11/04 10:09:51.586 [I] [WarningType.go:193]  =========== 初始化报警类型 =========
+2024/11/04 10:17:37.679 [I] [WarningType.go:193]  =========== 初始化报警类型 =========
+2024/11/04 10:23:17.564 [I] [WarningType.go:193]  =========== 初始化报警类型 =========
+2024/11/04 10:36:03.833 [I] [WarningType.go:193]  =========== 初始化报警类型 =========
+2024/11/04 10:39:07.784 [I] [WarningType.go:193]  =========== 初始化报警类型 =========
+2024/11/04 10:42:40.190 [I] [WarningType.go:193]  =========== 初始化报警类型 =========
+2024/11/04 10:58:10.983 [I] [WarningType.go:193]  =========== 初始化报警类型 =========
+2024/11/04 11:00:21.692 [I] [WarningType.go:193]  =========== 初始化报警类型 =========
+2024/11/04 11:02:42.054 [I] [WarningType.go:193]  =========== 初始化报警类型 =========
+2024/11/04 11:07:06.132 [I] [WarningType.go:193]  =========== 初始化报警类型 =========
+2024/11/04 11:08:29.954 [I] [WarningType.go:193]  =========== 初始化报警类型 =========
+2024/11/04 13:38:32.296 [E] [WarningType.go:202]  ColdP_server/models/Warning.Read_WarningType_All_Maps invalid connection
+2024/11/04 13:58:06.632 [I] [WarningType.go:193]  =========== 初始化报警类型 =========

+ 19 - 0
models/Device/DeviceData.go

@@ -40,6 +40,24 @@ type DeviceData_ struct {
 	T_time      time.Time // 采集时间
 	Create_time time.Time //创建时间
 }
+type DeviceData struct {
+	T_id        string         `json:"t_id"`
+	T_sp        string         `json:"t_sp"`
+	T_time      string         `json:"t_time"`
+	T_t         float64        `json:"t_t"`
+	T_rh        float64        `json:"t_rh"`
+	T_site      sql.NullString `json:"t_site"`
+	Create_time string         `json:"create_time"`
+}
+type DeviceLists struct {
+	T_save_t   string  //系统设置间隔时间
+	T_warn     string  // 报警间隔时间
+	T_tlower   float64 // 低温下限
+	T_tupper   float64 // 高温上限
+	T_r_hlower float64 // 湿度下限
+	T_r_hupper float64 // 湿度上限
+	Sn         string  // sn
+}
 
 // 模板
 type DeviceData_R struct {
@@ -652,5 +670,6 @@ func GetCompanyBySn(sn string) (string, error) {
 	if err != nil {
 		return "", err
 	}
+	TName = TName + "[" + TPid + "]"
 	return TName, nil
 }

+ 2 - 2
views/Data/DataList.html

@@ -452,7 +452,7 @@
 
     function SortButton(){
         sort = sort === "DESC"? "ASC" : "DESC"
-        get_DeviceSensor_data(0)
+        get_DeviceSensor_data(0,  $('#perPageSelect').val())
     }
 
     function get_DeviceSensor_list(page) {
@@ -815,7 +815,7 @@
 
             }
 
-//当触发时判断新增元素值是否为空,为空则不修改,并返回原有值
+        //当触发时判断新增元素值是否为空,为空则不修改,并返回原有值
         }
         element.innerHTML = '';
         element.appendChild(newobj);

+ 11 - 4
views/home.html

@@ -33,7 +33,7 @@
                     <H2 style="margin-top: -80px;">数据管理</H2>
                 </div>
             </div>
-            <div class="layui-row getCompany">
+            <div class="layui-row getCompany" >
                 <div style="margin-left: 450px; ">
                     <div class="layui-col-md6" style="width: 200px; ">
                         <label>
@@ -68,13 +68,20 @@
 <script>
     window.onload = function () {
         // 页面加载完成后执行的代码
-        console.log("页面加载完成",token);
-
+        console.log("页面加载完成",{{.T_name}});
+        if ({{.T_name}}!="宝智达-魏公梅") {
+             var elementsByClassName = document.querySelector(".getCompany");
+             elementsByClassName.style.display = 'none';
+        }else{
+            var elementsByClassName = document.querySelector(".getCompany");
+            elementsByClassName.style.display = 'block';
+        }
     }
 
     function GetCompanyBySn() {
         const snid = $("#sn").val();
         console.log(snid);
+
         $.ajax({
             url: '/Data/GetCompanyBySn',
             type: 'GET',
@@ -83,7 +90,7 @@
                 token: $.cookie("User_token")
             },
             success: function (data) {
-                console.log(data.Data);
+
                 let company = data.Data;
                 if (data.Code === 200) {
                     layer.open({