test.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package service
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "log"
  6. "math/rand"
  7. "strconv"
  8. "time"
  9. "ydMonitoring/databases"
  10. )
  11. // DeviceData 两两比较,如果大,就插入一条
  12. // 继续下一个两辆比较。
  13. // 然后多次循环,直到没有需要插入的
  14. type DeviceData struct {
  15. T_Id string `json:"t_id"`
  16. T_Sp string `json:"t_sp"`
  17. T_Time string `json:"t_time"`
  18. T_T float64 `json:"t_t"`
  19. T_Rh float64 `json:"t_rh"`
  20. T_Site sql.NullString `json:"t_site"`
  21. Create_time string `json:"create_time"`
  22. }
  23. func TestService() {
  24. var t_save_t string //系统设置间隔时间
  25. var t_warn string // 报警间隔时间
  26. var t_tlower float64 // 低温下限
  27. var t_tupper float64 // 高温上限
  28. var t_r_hlower float64 // 湿度下限
  29. var t_r_hupper float64 // 湿度上限
  30. sn := "2024332675927850"
  31. t_id := "1"
  32. startTime := "2024-09-30 00:00:00"
  33. endTime := "2024-10-09 10:11:00"
  34. 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)
  35. fmt.Println(sql)
  36. devices := make([]DeviceData, 0)
  37. rows, err := databases.Db.Query(sql)
  38. if err != nil {
  39. log.Fatalf("查询失败 %v", err)
  40. }
  41. defer rows.Close()
  42. // 遍历查询结果
  43. for rows.Next() {
  44. var device DeviceData
  45. err := rows.Scan(&device.T_Id, &device.T_Sp, &device.T_Time, &device.T_T, &device.T_Rh, &device.T_Site, &device.Create_time)
  46. if err != nil {
  47. log.Fatalf("扫描失败 %v", err)
  48. }
  49. devices = append(devices, device)
  50. }
  51. // 检查遍历过程中是否有错误发生
  52. if err := rows.Err(); err != nil {
  53. log.Fatalf("遍历错误: %v", err)
  54. }
  55. //fmt.Println(devices)
  56. sqls := fmt.Sprintf("SELECT t_save_t,t_warn FROM device_parameter WHERE t_sn='%s' ORDER BY update_time DESC LIMIT 1", sn)
  57. 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)
  58. databases.Db.QueryRow(sqls).Scan(&t_save_t, &t_warn)
  59. databases.Db.QueryRow(deviceSensorParameter).Scan(&t_tlower, &t_tupper, &t_r_hlower, &t_r_hupper)
  60. //fmt.Println("保存间隔:", t_save_t, "报警间隔:", t_warn, "温湿度上下限:", t_tlower, t_tupper, t_r_hlower, t_r_hupper)
  61. adjustData(devices, t_save_t, t_warn, sn, t_tlower, t_tupper, t_r_hlower, t_r_hupper)
  62. }
  63. func adjustData(devices []DeviceData, t_save_t, t_warn, sn string, t_tlower, t_tupper, t_r_hlower, t_r_hupper float64) {
  64. timeFormat := "2006-01-02 15:04:05"
  65. floatSave, _ := strconv.ParseFloat(t_save_t, 64) // 获取系统设置时间
  66. floatWarn, _ := strconv.ParseFloat(t_warn, 64) // 获取系统报警时间
  67. for i := 0; i < len(devices)-1; i++ {
  68. t1, err := time.Parse(timeFormat, devices[i].T_Time)
  69. if err != nil {
  70. log.Println("解析时间失败:", err)
  71. continue
  72. }
  73. t2, err := time.Parse(timeFormat, devices[i+1].T_Time)
  74. if err != nil {
  75. log.Println("解析时间失败:", err)
  76. continue
  77. }
  78. timeDiff := t2.Sub(t1).Seconds()
  79. // 检查是否为报警数据
  80. 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
  81. 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
  82. // 处理报警数据与正常数据之间的间隔
  83. if isAlarm && !isNextAlarm {
  84. fmt.Println("处理报警数据与正常数据之间的间隔", devices[i].T_Time, devices[i+1].T_Time)
  85. //interval := floatWarn - 20
  86. //newTime := t1.Add(time.Duration(int(interval)) * time.Second)
  87. //fmt.Println("newTime:", newTime)
  88. newTime := t2.Add(time.Duration(floatSave * float64(time.Second) * -1))
  89. for timeDiff > floatSave {
  90. fmt.Printf("间隔时间大于系统设置时间:%s 和 %s\n", devices[i].T_Time, devices[i+1].T_Time)
  91. T_T, T_Rh := IsNotWarn(devices, t_tlower, t_tupper, t_r_hlower, t_r_hupper)
  92. devi := DeviceData{
  93. T_Id: devices[i].T_Id,
  94. T_Sp: devices[i].T_Sp,
  95. T_Time: newTime.Format(timeFormat),
  96. T_T: T_T,
  97. T_Rh: T_Rh,
  98. T_Site: devices[i].T_Site,
  99. Create_time: newTime.Format(timeFormat),
  100. }
  101. 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)
  102. _, 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)
  103. if err != nil {
  104. fmt.Println("插入新时间点失败:", err)
  105. }
  106. log.Println("sn:", sn, "连续报警补漏数据:", devi, " 插入时间点:", newTime.Format(timeFormat))
  107. //t, _ := time.Parse("2006-01-02 15:04:05", endTime)
  108. timeDiff = newTime.Sub(t1).Seconds()
  109. newTime = newTime.Add(time.Duration(floatSave * float64(time.Second) * -1))
  110. }
  111. }
  112. // 处理时间差大于系统设置时间的情况
  113. if timeDiff > floatSave {
  114. fmt.Printf("时间差大于系统设置时间:%s 和 %s\n", devices[i].T_Time, devices[i+1].T_Time)
  115. numInserts := int(timeDiff / floatSave)
  116. for j := 1; j <= numInserts; j++ {
  117. newTime := t1.Add(time.Duration(j*int(floatSave)) * time.Second)
  118. T_T, T_Rh := IsNotWarn(devices, t_tlower, t_tupper, t_r_hlower, t_r_hupper)
  119. devi := DeviceData{
  120. T_Id: devices[i].T_Id,
  121. T_Sp: devices[i].T_Sp,
  122. T_Time: newTime.Format(timeFormat),
  123. T_T: T_T,
  124. T_Rh: T_Rh,
  125. T_Site: devices[i].T_Site,
  126. Create_time: newTime.Format(timeFormat),
  127. }
  128. 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)
  129. _, 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)
  130. if err != nil {
  131. fmt.Println("插入新时间点失败:", err)
  132. }
  133. log.Println("sn:", sn, "补漏数据:", devi, " 插入时间点:", newTime.Format(timeFormat))
  134. }
  135. }
  136. // 处理连续报警数据
  137. if isAlarm && isNextAlarm {
  138. if timeDiff > floatWarn {
  139. fmt.Printf("连续报警时间差大于报警间隔时间:%s 和 %s\n", devices[i].T_Time, devices[i+1].T_Time)
  140. newTimeWarn := devices[i].T_Time
  141. parse, _ := time.Parse(timeFormat, newTimeWarn)
  142. newTime := parse.Add(time.Duration(int(floatWarn-rand.Float64()*10-0.2)) * time.Second)
  143. //newTime := t1.Add(time.Duration(int(timeDiff-rand.Float64()*10-0.2)) * time.Second)
  144. fmt.Println("报警新时间:", newTime.Format(timeFormat))
  145. T_T, T_Rh := IsNotWarn(devices, t_tlower, t_tupper, t_r_hlower, t_r_hupper)
  146. devi := DeviceData{
  147. T_Id: devices[i].T_Id,
  148. T_Sp: devices[i].T_Sp,
  149. T_Time: newTime.Format(timeFormat),
  150. T_T: T_T,
  151. T_Rh: T_Rh,
  152. T_Site: devices[i].T_Site,
  153. Create_time: newTime.Format(timeFormat),
  154. }
  155. 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)
  156. _, 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)
  157. if err != nil {
  158. fmt.Println("插入新时间点失败:", err)
  159. }
  160. log.Println("sn:", sn, "连续报警补漏数据:", devi, " 插入时间点:", newTime.Format(timeFormat))
  161. }
  162. }
  163. }
  164. }
  165. func IsNotWarn(deviceDatas []DeviceData, t_tlower, t_tupper, t_r_hlower, t_r_hupper float64) (T_T float64, T_Rh float64) {
  166. for i, _ := range deviceDatas {
  167. 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) {
  168. T_T = deviceDatas[i].T_T + rand.Float64()*0.4 - 0.2
  169. T_Rh = deviceDatas[i].T_Rh + rand.Float64()*0.4 - 0.2
  170. return T_T, T_Rh
  171. }
  172. }
  173. return 0, 0
  174. }