dust.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Copyright 2019 github.com. All rights reserved.
  2. // Use of this source code is governed by github.com.
  3. package v1
  4. import (
  5. "context"
  6. "dust-monitor/consts"
  7. "dust-monitor/model"
  8. "dust-monitor/pb"
  9. "dust-monitor/pb/v1"
  10. "dust-monitor/timetask"
  11. "dust-monitor/util"
  12. "encoding/json"
  13. "strings"
  14. "github.com/jaryhe/gopkgs/logger"
  15. "go.uber.org/zap"
  16. )
  17. var dustAlarmMap = map[string]string{
  18. "P": "电源故障",
  19. "F": "排放源停运",
  20. "C": "校验",
  21. "M": "维护",
  22. "T": "超标",
  23. "D": "故障",
  24. "S": "设定值",
  25. }
  26. var dustAlarmCode = map[string]string{
  27. "P": "1",
  28. "F": "2",
  29. "C": "3",
  30. "M": "4",
  31. "T": "5",
  32. "D": "6",
  33. "S": "7",
  34. }
  35. var dustAlarmCodePre = map[string]string{
  36. "Pm10": "Y01",
  37. "Pm25": "Y02",
  38. "噪声": "Y03",
  39. "湿度": "Y04",
  40. "气压": "Y05",
  41. "雨量": "Y06",
  42. "温度": "Y07",
  43. "Tsp": "Y08",
  44. "风向": "Y09",
  45. "风速": "Y10",
  46. }
  47. func handleAlarm(ctx context.Context, alram *v1.AlarmAddRequest, title, flag string) error {
  48. alram.AlarmReason = title + dustAlarmMap[flag]
  49. alram.AlarmCode = dustAlarmCodePre[title] + dustAlarmCode[flag]
  50. alram.DeviceCode = consts.DustDeviceCode
  51. // 添加告警
  52. _, err := pb.SmartAlarm.AlarmAdd(ctx, alram)
  53. if err != nil {
  54. reqByte, _ := json.Marshal(*alram)
  55. logger.Error("rpc",
  56. zap.String("call", "pb.SmartAlarm.AlarmAdd"),
  57. zap.String("args", string(reqByte)),
  58. zap.String("error", err.Error()))
  59. return err
  60. }
  61. return nil
  62. }
  63. func handleDustAlarmReason(ctx context.Context, req *v1.DustData, projectId int64, deviceName string) error {
  64. alram := &v1.AlarmAddRequest{
  65. Date: req.DateTime,
  66. ProjectId: projectId,
  67. Sn: req.Mn,
  68. DeviceName: deviceName,
  69. DeviceType: consts.DustDeviceName,
  70. }
  71. if !strings.Contains("CSN", req.Pm10Flag) && req.Pm10Flag != "" {
  72. handleAlarm(ctx, alram, "Pm10", req.Pm10Flag)
  73. }
  74. if !strings.Contains("CSN", req.Pm25Flag) && req.Pm25Flag != "" {
  75. handleAlarm(ctx, alram, "Pm25", req.Pm25Flag)
  76. }
  77. if !strings.Contains("CSN", req.B03Flag) && req.B03Flag != "" {
  78. handleAlarm(ctx, alram, "噪声", req.B03Flag)
  79. }
  80. /*
  81. if !strings.Contains("CSN", req.H01Flag) && req.H01Flag != "" {
  82. handleAlarm(ctx, alram, "湿度", req.H01Flag)
  83. }
  84. if !strings.Contains("CSN", req.P01Flag) && req.P01Flag != "" {
  85. handleAlarm(ctx, alram, "气压", req.P01Flag)
  86. }
  87. if !strings.Contains("CSN", req.R01Flag) && req.R01Flag != "" {
  88. handleAlarm(ctx, alram, "雨量", req.R01Flag)
  89. }
  90. if !strings.Contains("CSN", req.T01Flag) && req.T01Flag != "" {
  91. handleAlarm(ctx, alram, "温度", req.T01Flag)
  92. }
  93. if !strings.Contains("CSN", req.TspFlag) && req.TspFlag != "" {
  94. handleAlarm(ctx, alram, "Tsp", req.TspFlag)
  95. }
  96. if !strings.Contains("CSN", req.W01Flag) && req.W01Flag != "" {
  97. handleAlarm(ctx, alram, "风向", req.W01Flag)
  98. }
  99. if !strings.Contains("CSN", req.W02Flag) && req.W02Flag != "" {
  100. handleAlarm(ctx, alram, "风速", req.W02Flag)
  101. }
  102. if !strings.Contains("CSN", req.W03Flag) && req.W03Flag != "" {
  103. handleAlarm(ctx, alram, "风力", req.W03Flag)
  104. }*/
  105. return nil
  106. }
  107. func DustDataHandle(ctx context.Context, req *v1.DustData) error {
  108. // 获取设备信息
  109. deviceInfo, err := util.GetDeviceInfo(req.Mn)
  110. if err != nil {
  111. return err
  112. }
  113. tags := map[string]string{"sn": req.Mn}
  114. fields := map[string]interface{}{"pm10": req.Pm10Avg,
  115. "pm25": req.Pm25Avg,
  116. "tsp": req.TspAvg,
  117. "t01": req.T01Avg,
  118. "h01": req.H01Avg,
  119. "w01": req.W01Avg,
  120. "w02": req.W02Avg,
  121. "r01": req.R01Avg,
  122. "p01": req.P01Avg,
  123. "b03": req.B03Avg,
  124. "w03": req.W03Avg,
  125. "lng": req.Lng,
  126. "lat": req.Lat}
  127. /*
  128. bytes, _ := json.Marshal(req)
  129. json.Unmarshal(bytes, &fields)
  130. */
  131. go timetask.UpdateDustTime(req.Mn)
  132. t := util.ParseTime(req.DateTime)
  133. // 写数据
  134. err = model.WriteDustData(req.Mn, tags, fields, t, req.Cn)
  135. if err != nil {
  136. return err
  137. }
  138. // 处理扬尘告警
  139. handleDustAlarmReason(ctx, req, deviceInfo.ProjectId, deviceInfo.Name)
  140. return nil
  141. }