eqiupmentMonitoringV2.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. package service
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "strconv"
  6. "time"
  7. "ydMonitoring/config"
  8. "ydMonitoring/databases"
  9. "ydMonitoring/util"
  10. )
  11. var (
  12. thresholdTemperature,
  13. thresholdHumidity float64
  14. // DateFormat 格式化时间字符串
  15. DateFormat = "2006-01-02 15:04:05"
  16. //EquipmentMonitoringTime uint64
  17. errRangeTime int
  18. )
  19. func init() {
  20. thresholdTemperature = config.Config.GetFloat64("monitoring.temperature")
  21. thresholdHumidity = config.Config.GetFloat64("monitoring.humidity")
  22. errRangeTime = config.Config.GetInt("monitoring.errRangeTime")
  23. //EquipmentMonitoringTime = config.Config.GetUint64("monitoring.equipmentMonitoringTime")
  24. }
  25. type Device struct {
  26. Tsn string //sn
  27. TDevName string //设备名称
  28. TSaveTime int64 //设备保存时间间隔
  29. TSensors [][2]string //设备探头
  30. TTasks [][2]string //任务列表
  31. }
  32. type DeviceSensor struct {
  33. Tsn string //sn
  34. TId string //探头
  35. TPid string //设备公司id
  36. TName string //设备名称
  37. }
  38. type Data struct {
  39. Tt float64
  40. Trh float64
  41. TTime time.Time
  42. }
  43. var (
  44. thresholdTt,
  45. thresholdTrh float64
  46. recordTime int
  47. )
  48. func init() {
  49. recordTime = config.Config.GetInt("monitoring.equipmentHour")
  50. }
  51. // []string{"BX200GSE", "MD100", "MD200G", "BX100", "KF100", "MD300G"}
  52. // EquipmentMonitoring2 监控设备v2
  53. func EquipmentMonitoring2() {
  54. //1.获取所有设备表
  55. sqlStatement := "select t_sn,t_dev_name from device where t__state = 1 and t_pid !=2 and (t_model = 'BX200GSE' OR t_model = 'MD100' OR t_model LIKE 'MD200G' OR t_model LIKE 'BX100' OR t_model LIKE 'KF100' OR t_model LIKE 'MD300G') "
  56. rows, _ := databases.Db.Query(sqlStatement)
  57. devices := make([]Device, 0)
  58. var sn, devName string
  59. for rows.Next() {
  60. rows.Scan(&sn, &devName)
  61. devices = append(devices, Device{Tsn: sn, TDevName: devName, TSensors: [][2]string{}, TTasks: [][2]string{}})
  62. }
  63. //fmt.Println("查询得到设备:", devices)
  64. //fmt.Println("查询得到设备:", len(devices))
  65. //HandleTsn(devices)
  66. //DataCaps(devices)
  67. Forewarn(devices)
  68. }
  69. // 处理sn
  70. func HandleTsn(devices []Device) {
  71. //查询所有设备探头
  72. var sensorId, sensorName string
  73. for i, v := range devices {
  74. sqlStatement := fmt.Sprintf("select t_id,t_name from device_sensor where t__state = 1 AND t_sn = '%s'", v.Tsn)
  75. rows, err := databases.Db.Query(sqlStatement)
  76. if err != nil {
  77. fmt.Println("查询", v.Tsn, "设备探头出错:", err.Error())
  78. }
  79. for rows.Next() {
  80. rows.Scan(&sensorId, &sensorName)
  81. // [设备名称,探头id,探头名称]
  82. v.TSensors = append(v.TSensors, [2]string{sensorId, sensorName})
  83. }
  84. devices[i] = v
  85. }
  86. fmt.Println("查询得到设备探头:", devices)
  87. //查询所有保存时间
  88. var tSaveT int64
  89. var devName any
  90. for i := 0; i < len(devices); i++ {
  91. sqlStatement := fmt.Sprintf("select t_name,t_save_t from device_parameter where t_sn = '%s' order by ID desc,update_time desc limit 1", devices[i].Tsn)
  92. row := databases.Db.QueryRow(sqlStatement)
  93. row.Scan(&devName, &tSaveT)
  94. devices[i].TSaveTime = tSaveT
  95. devices[i].TDevName = fmt.Sprintf("%v", devName)
  96. if tSaveT == 0 {
  97. util.SendWarning(devices[i].Tsn, -1, devices[i].TDevName, "", "设备保存时间未设置!")
  98. continue
  99. }
  100. fmt.Println("设备:", devices[i].Tsn, "保存时间:", devices[i].TSaveTime)
  101. }
  102. //查询是否存在执行计划,判断sn 是否为移动设备;
  103. var now = time.Now()
  104. var lastTowHours = now.Add(time.Hour * -time.Duration(recordTime))
  105. var start, end string
  106. for i, v := range devices {
  107. sqlStatement := fmt.Sprintf("select t__ut_start,t__ut_end from device_task where t_sn = '%s' and t__ut_end >= '%s'", v.Tsn, lastTowHours.Format(DateFormat))
  108. rows, err := databases.Db.Query(sqlStatement)
  109. if err != nil {
  110. fmt.Println(err)
  111. }
  112. //如果存在数据则表示是移动端
  113. for rows.Next() {
  114. rows.Scan(&start, &end)
  115. v.TTasks = append(v.TTasks, [2]string{start, end})
  116. }
  117. devices[i] = v
  118. }
  119. fmt.Println("查询得到设备任务:", devices)
  120. //公共的sql模板
  121. commonsStatement := "SELECT t_t,t_rh,DATE_FORMAT(t_time,'%%Y-%%m-%%d %%H:%%i:%%s') as t_time FROM z_device_data_%s WHERE t_id = '%s' AND t_time BETWEEN '%s' AND '%s' ORDER BY t_time DESC"
  122. var interval int64
  123. for _, v := range devices {
  124. //存在任务
  125. if len(v.TTasks) != 0 {
  126. for _, task := range v.TTasks {
  127. //计算时间差
  128. s, _ := time.Parse(DateFormat, task[0])
  129. e, _ := time.Parse(DateFormat, task[1])
  130. interval = e.Unix() - s.Unix()
  131. //循环查询各探头
  132. for _, sensor := range v.TSensors {
  133. sqlT := fmt.Sprintf(commonsStatement, v.Tsn, sensor[0], task[0], task[1])
  134. fmt.Println(sqlT)
  135. rows, err := databases.Db.Query(sqlT)
  136. if err != nil {
  137. fmt.Println("SOL查询错误!\t", err.Error())
  138. }
  139. handleDeviceData(v, sensor, interval, task[0], task[1], rows)
  140. }
  141. }
  142. continue
  143. }
  144. //不存在任务
  145. interval = now.Unix() - lastTowHours.Unix()
  146. for _, sensor := range v.TSensors {
  147. rows, err := databases.Db.Query(fmt.Sprintf(commonsStatement, v.Tsn, sensor[0], lastTowHours.Format(DateFormat), now.Format(DateFormat)))
  148. if err != nil {
  149. fmt.Println("SOL查询错误!\t", err.Error())
  150. }
  151. handleDeviceData(v, sensor, interval, lastTowHours.Format(DateFormat), now.Format(DateFormat), rows)
  152. }
  153. }
  154. }
  155. // 处理数据
  156. func handleDeviceData(d Device, sensor [2]string, interval int64, start, end string, rows *sql.Rows) {
  157. var sensorId, _ = strconv.ParseInt(sensor[0], 10, 64)
  158. fmt.Println("设备sn:", d.Tsn, "-", sensorId, " 任务时间:", start, end)
  159. //读取数据
  160. datas := make([]Data, 0)
  161. var t, rh float64
  162. var tTime string
  163. for rows.Next() {
  164. rows.Scan(&t, &rh, &tTime)
  165. ttime, _ := time.Parse(DateFormat, tTime)
  166. datas = append(datas, Data{t, rh, ttime})
  167. }
  168. fmt.Println("查询得到设备", d.Tsn, "数据:", len(datas), "最少条数:", d.TSaveTime, interval/d.TSaveTime, "条\t设备保存时间:")
  169. //
  170. ////时间内记录数小于应该保存的记录数,发出预警
  171. //if d.TSaveTime != 0 {
  172. // if int64(len(datas)) < (interval / d.TSaveTime) {
  173. // util.SendWarning(d.Tsn, int(sensorId), d.TDevName, sensor[1], fmt.Sprintf("设备[%s]探头[%d] [%s~%s]超出保存时间范围,缺少数据[%d]条", d.Tsn, sensorId, start, end, interval/d.TSaveTime-int64(len(datas))))
  174. // }
  175. //}
  176. //处理数据
  177. var current, next Data
  178. for i := 0; i < len(datas)-1; i++ {
  179. current = datas[i]
  180. next = datas[i+1]
  181. //不在保存时间范围内
  182. if d.TSaveTime != 0 {
  183. interval := current.TTime.Unix() - next.TTime.Unix() - d.TSaveTime
  184. if interval >= d.TSaveTime {
  185. util.SendWarning(d.Tsn, int(sensorId), d.TDevName, sensor[1], fmt.Sprintf("设备[%s]探头[%d] [%s~%s]超出保存时间范围 [%d] 上传时间间隔[%d],缺少数据[%d]条", d.Tsn, sensorId, next.TTime.Format(DateFormat), current.TTime.Format(DateFormat), interval, d.TSaveTime, interval/d.TSaveTime))
  186. }
  187. }
  188. //温度
  189. temperatureInterval := current.Tt - next.Tt
  190. if temperatureInterval > thresholdTemperature {
  191. util.SendWarning(d.Tsn, int(sensorId), d.TDevName, sensor[1], fmt.Sprintf("设备[%s]探头[%d] [%s~%s]超出温度变化范围,变化差值[%.2f]", d.Tsn, sensorId, next.TTime.Format(DateFormat), current.TTime.Format(DateFormat), temperatureInterval))
  192. }
  193. //湿度
  194. humidityInterval := current.Trh - next.Trh
  195. if humidityInterval > thresholdHumidity {
  196. util.SendWarning(d.Tsn, int(sensorId), d.TDevName, sensor[1], fmt.Sprintf("设备[%s]探头[%d] [%s~%s]超出湿度变化范围,变化差值[%.2f]", d.Tsn, sensorId, next.TTime.Format(DateFormat), current.TTime.Format(DateFormat), humidityInterval))
  197. }
  198. }
  199. }