Преглед на файлове

补漏逻辑修改为按时间逻辑倒叙补漏

huangyan преди 1 месец
родител
ревизия
0b44795357
променени са 1 файла, в които са добавени 25 реда и са изтрити 49 реда
  1. 25 49
      service/test.go

+ 25 - 49
service/test.go

@@ -33,7 +33,7 @@ func TestService() {
 	sn := "2024332675927850"
 	t_id := "1"
 	startTime := "2024-09-30 00:00:00"
-	endTime := "2024-10-09 10:04:00"
+	endTime := "2024-10-09 10:11:00"
 	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)
 	fmt.Println(sql)
 	devices := make([]DeviceData, 0)
@@ -60,7 +60,7 @@ func TestService() {
 	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)
 	databases.Db.QueryRow(sqls).Scan(&t_save_t, &t_warn)
 	databases.Db.QueryRow(deviceSensorParameter).Scan(&t_tlower, &t_tupper, &t_r_hlower, &t_r_hupper)
-	fmt.Println("保存间隔:", t_save_t, "报警间隔:", t_warn, "温湿度上下限:", t_tlower, t_tupper, t_r_hlower, t_r_hupper)
+	//fmt.Println("保存间隔:", t_save_t, "报警间隔:", t_warn, "温湿度上下限:", t_tlower, t_tupper, t_r_hlower, t_r_hupper)
 	adjustData(devices, t_save_t, t_warn, sn, t_tlower, t_tupper, t_r_hlower, t_r_hupper)
 }
 
@@ -80,61 +80,37 @@ func adjustData(devices []DeviceData, t_save_t, t_warn, sn string, t_tlower, t_t
 			continue
 		}
 		timeDiff := t2.Sub(t1).Seconds()
-		// 检查是否已经添加数据
-		//isInstrt := false
 		// 检查是否为报警数据
 		isAlarm := devices[i].T_T < t_tlower || devices[i].T_T > t_tupper || devices[i].T_Rh < t_r_hlower || devices[i].T_Rh > t_r_hupper
 		isNextAlarm := devices[i+1].T_T < t_tlower || devices[i+1].T_T > t_tupper || devices[i+1].T_Rh < t_r_hlower || devices[i+1].T_Rh > t_r_hupper
 		// 处理报警数据与正常数据之间的间隔
-		//if isAlarm && !isNextAlarm {
-		//	fmt.Printf("报警数据与正常数据:%s 和 %s\n", devices[i].T_Time, devices[i+1].T_Time)
-		//	newTime := t1.Add(time.Duration(int(floatWarn-rand.Float64()*rand.Float64())) * time.Second)
-		//	//newTime.Sub()
-		//	fmt.Println(newTime.Format(timeFormat))
-		//	T_T, T_Rh := IsNotWarn(devices, t_tlower, t_tupper, t_r_hlower, t_r_hupper)
-		//	devi := DeviceData{
-		//		T_Id:        devices[i].T_Id,
-		//		T_Sp:        devices[i].T_Sp,
-		//		T_Time:      newTime.Format(timeFormat),
-		//		T_T:         T_T,
-		//		T_Rh:        T_Rh,
-		//		T_Site:      devices[i].T_Site,
-		//		Create_time: devices[i].Create_time,
-		//	}
-		//	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)
-		//	_, err := databases.Db.Exec(insertSql, devi.T_Id, devi.T_Sp, devi.T_Time, devi.T_T, devi.T_Rh, devi.T_Site.String, devi.Create_time)
-		//	if err != nil {
-		//		fmt.Println("插入新时间点失败:", err)
-		//	}
-		//	log.Println("sn:", sn, "连续报警补漏数据:", devi, " 插入时间点:", newTime.Format(timeFormat))
-		//}
-		// 处理报警与正常数据之间的间隔
 		if isAlarm && !isNextAlarm {
 			fmt.Println("处理报警数据与正常数据之间的间隔", devices[i].T_Time, devices[i+1].T_Time)
-			interval := floatWarn - 20
-			newTime := t1.Add(time.Duration(int(interval)) * time.Second)
+			//interval := floatWarn - 20
+			//newTime := t1.Add(time.Duration(int(interval)) * time.Second)
+			//fmt.Println("newTime:", newTime)
+			newTime := t2.Add(time.Duration(floatSave * float64(time.Second) * -1))
 			for timeDiff > floatSave {
-				if timeDiff > floatSave {
-					fmt.Printf("间隔时间大于系统设置时间:%s 和 %s\n", devices[i].T_Time, devices[i+1].T_Time)
-					T_T, T_Rh := IsNotWarn(devices, t_tlower, t_tupper, t_r_hlower, t_r_hupper)
-					devi := DeviceData{
-						T_Id:        devices[i].T_Id,
-						T_Sp:        devices[i].T_Sp,
-						T_Time:      newTime.Format(timeFormat),
-						T_T:         T_T,
-						T_Rh:        T_Rh,
-						T_Site:      devices[i].T_Site,
-						Create_time: newTime.Format(timeFormat),
-					}
-					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)
-					_, err := databases.Db.Exec(insertSql, devi.T_Id, devi.T_Sp, devi.T_Time, devi.T_T, devi.T_Rh, devi.T_Site.String, devi.Create_time)
-					if err != nil {
-						fmt.Println("插入新时间点失败:", err)
-					}
-					log.Println("sn:", sn, "连续报警补漏数据:", devi, " 插入时间点:", newTime.Format(timeFormat))
-					timeDiff = t2.Sub(newTime).Seconds()
-					newTime = newTime.Add(time.Duration(int(floatSave)) * time.Second)
+				fmt.Printf("间隔时间大于系统设置时间:%s 和 %s\n", devices[i].T_Time, devices[i+1].T_Time)
+				T_T, T_Rh := IsNotWarn(devices, t_tlower, t_tupper, t_r_hlower, t_r_hupper)
+				devi := DeviceData{
+					T_Id:        devices[i].T_Id,
+					T_Sp:        devices[i].T_Sp,
+					T_Time:      newTime.Format(timeFormat),
+					T_T:         T_T,
+					T_Rh:        T_Rh,
+					T_Site:      devices[i].T_Site,
+					Create_time: newTime.Format(timeFormat),
 				}
+				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)
+				_, err := databases.Db.Exec(insertSql, devi.T_Id, devi.T_Sp, devi.T_Time, devi.T_T, devi.T_Rh, devi.T_Site.String, devi.Create_time)
+				if err != nil {
+					fmt.Println("插入新时间点失败:", err)
+				}
+				log.Println("sn:", sn, "连续报警补漏数据:", devi, " 插入时间点:", newTime.Format(timeFormat))
+				//t, _ := time.Parse("2006-01-02 15:04:05", endTime)
+				timeDiff = newTime.Sub(t1).Seconds()
+				newTime = newTime.Add(time.Duration(floatSave * float64(time.Second) * -1))
 			}
 		}
 		// 处理时间差大于系统设置时间的情况