forewarn.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package service
  2. import (
  3. "fmt"
  4. "log"
  5. "math"
  6. "strconv"
  7. "time"
  8. "ydMonitoring/databases"
  9. )
  10. type Devices struct {
  11. Tsn string // 设备编号
  12. }
  13. type DeviceSensors struct {
  14. Tsn string // 设备编号
  15. TId string // 设备ID
  16. TPid string // PID
  17. TName string // 设备名称
  18. }
  19. type DeviceDatas struct {
  20. T_T float64 // 温度
  21. T_rh float64 // 湿度
  22. T_Time string // 数据上传时间
  23. }
  24. // Forewarn 查询所有数据间隔时间
  25. func Forewarn(devices []Device) {
  26. message := make([]string, 0)
  27. // 查询设备信息
  28. sqllist := make([]string, 0)
  29. for _, v := range devices {
  30. sql := fmt.Sprintf("select t_sn,t_id,t_pid,t_name from device_sensor where t_sn = %s", v.Tsn)
  31. sqllist = append(sqllist, sql)
  32. }
  33. deviceSensor := make([]DeviceSensors, 0)
  34. for _, sql := range sqllist {
  35. rows, err := databases.Db.Query(sql)
  36. if err != nil {
  37. log.Println("查询设备信息失败:", err)
  38. continue
  39. }
  40. defer rows.Close()
  41. for rows.Next() {
  42. var sn, t_id, t_pid, t_name string
  43. err := rows.Scan(&sn, &t_id, &t_pid, &t_name)
  44. if err != nil {
  45. log.Println("扫描设备信息失败:", err)
  46. continue
  47. }
  48. deviceSensor = append(deviceSensor, DeviceSensors{sn, t_id, t_pid, t_name})
  49. }
  50. }
  51. // 获取设备参数
  52. for _, sensor := range deviceSensor {
  53. var t_save_t string // 保存间隔时间
  54. var t_warn string // 报警间隔时间
  55. var t_tlower float64 // 低温下限
  56. var t_tupper float64 // 高温上限
  57. var t_r_hlower float64 // 湿度下限
  58. var t_r_hupper float64 // 湿度上限
  59. var companyname string
  60. var id string
  61. b, _ := Ignore(sensor.Tsn)
  62. if b {
  63. log.Println("该设备已忽略:", sensor.Tsn)
  64. continue
  65. }
  66. sqls := fmt.Sprintf("SELECT t_save_t,t_warn FROM device_parameter WHERE t_sn='%s' ORDER BY update_time DESC LIMIT 1", sensor.Tsn)
  67. company := fmt.Sprintf("SELECT t_name,id FROM company WHERE id='%s'", sensor.TPid)
  68. device_sensor_parameter := 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", sensor.Tsn, sensor.TId)
  69. databases.Db.QueryRow(sqls).Scan(&t_save_t, &t_warn)
  70. databases.Db.QueryRow(company).Scan(&companyname, &id)
  71. databases.Db.QueryRow(device_sensor_parameter).Scan(&t_tlower, &t_tupper, &t_r_hlower, &t_r_hupper)
  72. // 查询历史数据
  73. sql := fmt.Sprintf("SELECT t_time,t_t,t_rh FROM z_device_data_%s WHERE t_id = '%s' ORDER BY t_time DESC LIMIT 10", sensor.Tsn, sensor.TId)
  74. rows, err := databases.Db.Query(sql)
  75. if err != nil {
  76. log.Println("查询数据失败:", err)
  77. continue
  78. }
  79. defer rows.Close()
  80. deviceDatas := make([]DeviceDatas, 0)
  81. for rows.Next() {
  82. var t string
  83. var t_t, t_rh float64
  84. rows.Scan(&t, &t_t, &t_rh)
  85. deviceDatas = append(deviceDatas, DeviceDatas{t_t, t_rh, t})
  86. }
  87. if len(deviceDatas) == 10 {
  88. timeFormat := "2006-01-02 15:04:05"
  89. start := deviceDatas[0]
  90. end := deviceDatas[len(deviceDatas)-1]
  91. startTime, err := time.Parse(timeFormat, start.T_Time)
  92. if err != nil {
  93. log.Println("解析时间失败:", err)
  94. continue
  95. }
  96. endTime, err := time.Parse(timeFormat, end.T_Time)
  97. if err != nil {
  98. log.Println("解析时间失败:", err)
  99. continue
  100. }
  101. timeDiff := startTime.Sub(endTime).Seconds()
  102. if IsWarn(deviceDatas, t_tlower, t_tupper, t_r_hlower, t_r_hupper) {
  103. floatWarn, _ := strconv.ParseFloat(t_warn, 64)
  104. expectedCount := timeDiff / floatWarn
  105. actualCount := float64(len(deviceDatas))
  106. if actualCount > expectedCount {
  107. //fmt.Println("时间间隔(报警):", timeDiff)
  108. //fmt.Println("数据预警(报警)", deviceDatas[0], sensor.Tsn)
  109. }
  110. } else {
  111. floatSave, _ := strconv.ParseFloat(t_save_t, 64)
  112. expectedCount := timeDiff / floatSave
  113. actualCount := float64(len(deviceDatas))
  114. //fmt.Println("开始时间与结束时间", timeDiff, startTime, endTime)
  115. todayStart := time.Now().Truncate(24 * time.Hour)
  116. if startTime.After(todayStart) || startTime.Equal(todayStart) {
  117. if actualCount > expectedCount+1 {
  118. fmt.Println("向上取整:", math.Ceil(expectedCount), "非向上取整:", expectedCount+1, actualCount)
  119. fmt.Println("时间间隔(保存):", timeDiff, floatSave)
  120. fmt.Println("数据预警(保存)", deviceDatas[0], sensor.Tsn)
  121. }
  122. }
  123. }
  124. }
  125. }
  126. fmt.Println(message, len(message))
  127. }
  128. // IsWarn 判断是否产生报警数据
  129. func IsWarn(deviceDatas []DeviceDatas, t_tlower, t_tupper, t_r_hlower, t_r_hupper float64) bool {
  130. for _, data := range deviceDatas {
  131. if data.T_T < t_tlower || data.T_T > t_tupper || data.T_rh < t_r_hlower || data.T_rh > t_r_hupper {
  132. return true
  133. }
  134. }
  135. return false
  136. }