eqiupmentMonitoring.go.temp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. package service
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "math"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "ydMonitoring/config"
  10. "ydMonitoring/databases"
  11. "ydMonitoring/util"
  12. )
  13. var (
  14. thresholdTemperature,
  15. thresholdHumidity float64
  16. // DateFormat 格式化时间字符串
  17. DateFormat = "2006-01-02 15:04:05"
  18. //EquipmentMonitoringTime uint64
  19. errRangeTime int
  20. )
  21. func init() {
  22. thresholdTemperature = config.Config.GetFloat64("monitoring.temperature")
  23. thresholdHumidity = config.Config.GetFloat64("monitoring.humidity")
  24. errRangeTime = config.Config.GetInt("monitoring.errRangeTime")
  25. //EquipmentMonitoringTime = config.Config.GetUint64("monitoring.equipmentMonitoringTime")
  26. }
  27. // EquipmentMonitoring
  28. // 监控设备总函数
  29. func EquipmentMonitoring() {
  30. db := databases.Db
  31. var (
  32. //sqlStatement string
  33. err error
  34. rows *sql.Rows
  35. )
  36. //设备YD监控
  37. sqlStatement := "SELECT t_sn,t_dev_name FROM device WHERE (t__site = '' OR t__site IS NULL) AND t__state = 1 AND t_monitor = 1"
  38. fmt.Printf("YD设备查询SQL:%s\n", sqlStatement)
  39. rows, err = db.Query(sqlStatement)
  40. if err != nil {
  41. fmt.Println("数据查询失败:", err.Error())
  42. }
  43. handlerData(rows)
  44. }
  45. // handlerData
  46. // 处理数据库查询响应数据
  47. func handlerData(rows *sql.Rows) {
  48. db := databases.Db
  49. //设备配置表,获取备份时间
  50. sql := "SELECT t_sn,t_save_time FROM device_parameter where ( t__state = 1 ) OR ( t__state = 2 and t__send_state = 1 ) order by ID asc"
  51. tSnRows, err := db.Query(sql)
  52. fmt.Printf("查询设备表设备备份时间SQL:%s\n", sql)
  53. if err != nil {
  54. fmt.Println("数据查询失败:", err.Error())
  55. }
  56. var tSnThresholdTimeMap = make(map[string]int)
  57. for tSnRows.Next() {
  58. var tSn string
  59. var saveTime int
  60. tSnRows.Scan(&tSn, &saveTime)
  61. tSnThresholdTimeMap[tSn] = saveTime
  62. }
  63. fmt.Printf("查询到%d条记录\n", len(tSnThresholdTimeMap))
  64. //获取所有tsn,并将其处理判断是否异常
  65. for rows.Next() {
  66. var tSn, tDevName string
  67. rows.Scan(&tSn, &tDevName)
  68. // z_devicedata_kf861551053476933
  69. tableName := fmt.Sprintf("z_device_data_%s", tSn)
  70. if tableName == "z_device_data_202335objgv2pz1" {
  71. fmt.Println()
  72. }
  73. //需要查出所有设备的t_id,对t_id对应的记录进行对比
  74. selectTId := fmt.Sprintf("SELECT t_id FROM %s GROUP BY t_id", tableName)
  75. fmt.Printf("查询%s表中t_id进行分类执行SQL:\n", tSn)
  76. tIdRows, err := db.Query(selectTId)
  77. if err != nil {
  78. fmt.Printf(err.Error())
  79. if strings.Contains(err.Error(), "Error 1146 (42S02): Table") {
  80. continue
  81. }
  82. }
  83. var tIds = make([]string, 0)
  84. for tIdRows.Next() {
  85. var tId string
  86. tIdRows.Scan(&tId)
  87. fmt.Println(tId)
  88. tIds = append(tIds, tId)
  89. }
  90. fmt.Printf("执行SQL后查询到%d条数据\n", len(tIds))
  91. //判断是否有时间间隔,对于表tsn来说
  92. thresholdTime, ok := tSnThresholdTimeMap[tSn]
  93. if !ok {
  94. util.SendWarning(tSn, -1, tDevName, "", fmt.Sprintf("[设备名称:%s|t_sn:%s]\t不存在配置时间", tDevName, tSn))
  95. continue
  96. }
  97. for _, tId := range tIds {
  98. //如果t_id为空则跳出当前循环
  99. if tId == "" {
  100. continue
  101. }
  102. t, _ := strconv.ParseInt(tId, 10, 64)
  103. id := int(t)
  104. sql := fmt.Sprintf("SELECT t_t,t_rh,create_time FROM %s where create_time >= DATE_SUB(NOW(),INTERVAL 1 DAY) AND t_id = %s ORDER BY create_time DESC LIMIT 10", tableName, tId)
  105. fmt.Printf("查询设备最近一天得前十条记录,执行SQL:%s\n", sql)
  106. tSnRows, err = db.Query(sql)
  107. if err != nil {
  108. util.SendWarning(tSn, -1, tDevName, "", fmt.Sprintf("[设备名称:%s|t_sn:%s]\t不存在配置时间", tDevName, tSn))
  109. }
  110. tTs := make([]float64, 0)
  111. tRhs := make([]float64, 0)
  112. createTimes := make([]string, 0)
  113. //获取sn上传信息
  114. for tSnRows.Next() {
  115. var (
  116. tT,
  117. tRh float64
  118. createTime string
  119. )
  120. tSnRows.Scan(&tT, &tRh, &createTime)
  121. tTs = append(tTs, tT)
  122. tRhs = append(tRhs, tRh)
  123. createTimes = append(createTimes, createTime)
  124. }
  125. fmt.Printf("查询到%d条记录\n", len(tTs))
  126. //如果不存在上传记录则跳出本次循环
  127. if len(createTimes) == 0 || len(tTs) == 0 || len(tRhs) == 0 {
  128. fmt.Printf("%s设备执行SQL后不存在记录,设备已停用\n", tableName)
  129. util.SendWarning(tSn, -1, tDevName, "", fmt.Sprintf("【设备名称:%s|t_sn:%s|t_id:%s】\t缺少上传记录,该设备下没有数据记录,时间间隔%d", tDevName, tSn, tId, thresholdTime))
  130. continue
  131. }
  132. //处理时间
  133. //情况1:查询出来的最近时间节点对于现在的时间,是否在其中还存在一个记录时间段
  134. currentTime := time.Now()
  135. firstTime, _ := time.Parse(DateFormat, createTimes[0])
  136. if int((currentTime.Unix()-firstTime.Unix())/1000) > thresholdTime {
  137. fmt.Printf("当前时间:%s 最近记录时间:%s,时间间隔:%d", currentTime.Format(DateFormat), createTimes[0], thresholdTime)
  138. util.SendWarning(tSn, -1, tDevName, "", fmt.Sprintf("【设备名称:%s|t_sn:%s|t_id:%s】\t缺少上传记录,对于当前时间%s,最近记录时间%s,时间间隔%d", tDevName, tSn, tId, currentTime.Format(DateFormat), createTimes[0], thresholdTime))
  139. continue
  140. }
  141. // 处理数据
  142. for i := 0; i < len(tTs); i++ {
  143. //情况2:设备记录时间段不在阈值内,存在上传时间误差
  144. if i == len(createTimes)-1 {
  145. break
  146. }
  147. previous, _ := time.Parse(DateFormat, createTimes[i])
  148. next, _ := time.Parse(DateFormat, createTimes[i+1])
  149. previous = previous.Add(time.Minute * time.Duration(-(thresholdTime / 60)))
  150. //判断时分是否一致,若不一致则存在误差
  151. if previous.Day() != next.Day() || previous.Hour() != next.Hour() || previous.Minute() != next.Minute() {
  152. //误差再1分钟内可接受
  153. if int(math.Abs((float64(previous.Unix()-next.Unix()))/1000)) > errRangeTime {
  154. fmt.Printf("当前%s,前条%s\n", next.Format(DateFormat), previous.Format(DateFormat))
  155. util.SendWarning(tSn, id, tDevName, "", fmt.Sprintf("【设备名称:%s|t_sn:%s|t_id:%s】\t数据上传与配置时间不符合,时间为:[%s],[%s],时间阀值为:%d秒", tDevName, tSn, tId, createTimes[i+1], createTimes[i], thresholdTime))
  156. continue
  157. }
  158. }
  159. //处理温度
  160. previousTemperature := tTs[i]
  161. nextTemperature := tTs[i+1]
  162. if math.Abs(previousTemperature-nextTemperature) > thresholdTemperature {
  163. util.SendWarning(tSn, id, tDevName, "", fmt.Sprintf("【设备名称:%s|t_sn:%s|t_id:%s】\t温度变化超出阈值警告,当前温度:%.2f 上一次温度:%.2f 阈值温度:%.2f,温差为:%.2f\t出现时间为:%s", tDevName, tSn, tId, nextTemperature, previousTemperature, thresholdTemperature, math.Abs(previousTemperature-nextTemperature), createTimes[i]))
  164. continue
  165. }
  166. //处理湿度
  167. previousHumidity := tRhs[i]
  168. nextHumidity := tRhs[i+1]
  169. if math.Abs(previousHumidity-nextHumidity) > thresholdHumidity {
  170. util.SendWarning(tSn, id, tDevName, "", fmt.Sprintf("【设备名称:%s|t_sn:%s|t_id:%s】\t湿度变化超出阈值警告,当前湿度:%.2f,前一次湿度:%.2f,温度阀值为:%.2f,湿度差:%.2f\t出现时间:%s", tDevName, tSn, tId, nextHumidity, previousHumidity, thresholdHumidity, math.Abs(previousTemperature-nextHumidity), createTimes[i]))
  171. continue
  172. }
  173. }
  174. }
  175. }
  176. }