eqiupmentMonitoringV2.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. }
  68. // 处理sn
  69. func HandleTsn(devices []Device) {
  70. //查询所有设备探头
  71. var sensorId, sensorName string
  72. for i, v := range devices {
  73. sqlStatement := fmt.Sprintf("select t_id,t_name from device_sensor where t__state = 1 AND t_sn = '%s'", v.Tsn)
  74. rows, err := databases.Db.Query(sqlStatement)
  75. if err != nil {
  76. fmt.Println("查询", v.Tsn, "设备探头出错:", err.Error())
  77. }
  78. for rows.Next() {
  79. rows.Scan(&sensorId, &sensorName)
  80. // [设备名称,探头id,探头名称]
  81. v.TSensors = append(v.TSensors, [2]string{sensorId, sensorName})
  82. }
  83. devices[i] = v
  84. }
  85. fmt.Println("查询得到设备探头:", devices)
  86. //查询所有保存时间
  87. var tSaveT int64
  88. var devName any
  89. for i := 0; i < len(devices); i++ {
  90. 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)
  91. row := databases.Db.QueryRow(sqlStatement)
  92. row.Scan(&devName, &tSaveT)
  93. devices[i].TSaveTime = tSaveT
  94. devices[i].TDevName = fmt.Sprintf("%v", devName)
  95. if tSaveT == 0 {
  96. util.SendWarning(devices[i].Tsn, -1, devices[i].TDevName, "", "设备保存时间未设置!")
  97. continue
  98. }
  99. fmt.Println("设备:", devices[i].Tsn, "保存时间:", devices[i].TSaveTime)
  100. }
  101. //查询是否存在执行计划,判断sn 是否为移动设备;
  102. var now = time.Now()
  103. var lastTowHours = now.Add(time.Hour * -time.Duration(recordTime))
  104. var start, end string
  105. for i, v := range devices {
  106. 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))
  107. rows, err := databases.Db.Query(sqlStatement)
  108. if err != nil {
  109. fmt.Println(err)
  110. }
  111. //如果存在数据则表示是移动端
  112. for rows.Next() {
  113. rows.Scan(&start, &end)
  114. v.TTasks = append(v.TTasks, [2]string{start, end})
  115. }
  116. devices[i] = v
  117. }
  118. fmt.Println("查询得到设备任务:", devices)
  119. //公共的sql模板
  120. 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"
  121. var interval int64
  122. for _, v := range devices {
  123. //存在任务
  124. if len(v.TTasks) != 0 {
  125. for _, task := range v.TTasks {
  126. //计算时间差
  127. s, _ := time.Parse(DateFormat, task[0])
  128. e, _ := time.Parse(DateFormat, task[1])
  129. interval = e.Unix() - s.Unix()
  130. //循环查询各探头
  131. for _, sensor := range v.TSensors {
  132. sqlT := fmt.Sprintf(commonsStatement, v.Tsn, sensor[0], task[0], task[1])
  133. fmt.Println(sqlT)
  134. rows, err := databases.Db.Query(sqlT)
  135. if err != nil {
  136. fmt.Println("SOL查询错误!\t", err.Error())
  137. }
  138. handleDeviceData(v, sensor, interval, task[0], task[1], rows)
  139. }
  140. }
  141. continue
  142. }
  143. //不存在任务
  144. interval = now.Unix() - lastTowHours.Unix()
  145. for _, sensor := range v.TSensors {
  146. rows, err := databases.Db.Query(fmt.Sprintf(commonsStatement, v.Tsn, sensor[0], lastTowHours.Format(DateFormat), now.Format(DateFormat)))
  147. if err != nil {
  148. fmt.Println("SOL查询错误!\t", err.Error())
  149. }
  150. handleDeviceData(v, sensor, interval, lastTowHours.Format(DateFormat), now.Format(DateFormat), rows)
  151. }
  152. }
  153. }
  154. // 处理数据
  155. func handleDeviceData(d Device, sensor [2]string, interval int64, start, end string, rows *sql.Rows) {
  156. var sensorId, _ = strconv.ParseInt(sensor[0], 10, 64)
  157. fmt.Println("设备sn:", d.Tsn, "-", sensorId, " 任务时间:", start, end)
  158. //读取数据
  159. datas := make([]Data, 0)
  160. var t, rh float64
  161. var tTime string
  162. for rows.Next() {
  163. rows.Scan(&t, &rh, &tTime)
  164. ttime, _ := time.Parse(DateFormat, tTime)
  165. datas = append(datas, Data{t, rh, ttime})
  166. }
  167. fmt.Println("查询得到设备", d.Tsn, "数据:", len(datas), "最少条数:", d.TSaveTime, interval/d.TSaveTime, "条\t设备保存时间:")
  168. //
  169. ////时间内记录数小于应该保存的记录数,发出预警
  170. //if d.TSaveTime != 0 {
  171. // if int64(len(datas)) < (interval / d.TSaveTime) {
  172. // 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))))
  173. // }
  174. //}
  175. //处理数据
  176. var current, next Data
  177. for i := 0; i < len(datas)-1; i++ {
  178. current = datas[i]
  179. next = datas[i+1]
  180. //不在保存时间范围内
  181. if d.TSaveTime != 0 {
  182. interval := current.TTime.Unix() - next.TTime.Unix() - d.TSaveTime
  183. if interval >= d.TSaveTime {
  184. 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))
  185. }
  186. }
  187. //温度
  188. temperatureInterval := current.Tt - next.Tt
  189. if temperatureInterval > thresholdTemperature {
  190. 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))
  191. }
  192. //湿度
  193. humidityInterval := current.Trh - next.Trh
  194. if humidityInterval > thresholdHumidity {
  195. 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))
  196. }
  197. }
  198. }