123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- // Copyright 2019 github.com. All rights reserved.
- // Use of this source code is governed by github.com.
- package v1
- import (
- "context"
- "dust-monitor/consts"
- "dust-monitor/model"
- "dust-monitor/pb"
- "dust-monitor/pb/v1"
- "dust-monitor/timetask"
- "dust-monitor/util"
- "encoding/json"
- "strings"
- "github.com/jaryhe/gopkgs/logger"
- "go.uber.org/zap"
- )
- var dustAlarmMap = map[string]string{
- "P": "电源故障",
- "F": "排放源停运",
- "C": "校验",
- "M": "维护",
- "T": "超标",
- "D": "故障",
- "S": "设定值",
- }
- var dustAlarmCode = map[string]string{
- "P": "1",
- "F": "2",
- "C": "3",
- "M": "4",
- "T": "5",
- "D": "6",
- "S": "7",
- }
- var dustAlarmCodePre = map[string]string{
- "Pm10": "Y01",
- "Pm25": "Y02",
- "噪声": "Y03",
- "湿度": "Y04",
- "气压": "Y05",
- "雨量": "Y06",
- "温度": "Y07",
- "Tsp": "Y08",
- "风向": "Y09",
- "风速": "Y10",
- }
- func handleAlarm(ctx context.Context, alram *v1.AlarmAddRequest, title, flag string) error {
- alram.AlarmReason = title + dustAlarmMap[flag]
- alram.AlarmCode = dustAlarmCodePre[title] + dustAlarmCode[flag]
- alram.DeviceCode = consts.DustDeviceCode
- // 添加告警
- _, err := pb.SmartAlarm.AlarmAdd(ctx, alram)
- if err != nil {
- reqByte, _ := json.Marshal(*alram)
- logger.Error("rpc",
- zap.String("call", "pb.SmartAlarm.AlarmAdd"),
- zap.String("args", string(reqByte)),
- zap.String("error", err.Error()))
- return err
- }
- return nil
- }
- func handleDustAlarmReason(ctx context.Context, req *v1.DustData, projectId int64, deviceName string) error {
- alram := &v1.AlarmAddRequest{
- Date: req.DateTime,
- ProjectId: projectId,
- Sn: req.Mn,
- DeviceName: deviceName,
- DeviceType: consts.DustDeviceName,
- }
- if !strings.Contains("CSN", req.Pm10Flag) && req.Pm10Flag != "" {
- handleAlarm(ctx, alram, "Pm10", req.Pm10Flag)
- }
- if !strings.Contains("CSN", req.Pm25Flag) && req.Pm25Flag != "" {
- handleAlarm(ctx, alram, "Pm25", req.Pm25Flag)
- }
- if !strings.Contains("CSN", req.B03Flag) && req.B03Flag != "" {
- handleAlarm(ctx, alram, "噪声", req.B03Flag)
- }
- /*
- if !strings.Contains("CSN", req.H01Flag) && req.H01Flag != "" {
- handleAlarm(ctx, alram, "湿度", req.H01Flag)
- }
- if !strings.Contains("CSN", req.P01Flag) && req.P01Flag != "" {
- handleAlarm(ctx, alram, "气压", req.P01Flag)
- }
- if !strings.Contains("CSN", req.R01Flag) && req.R01Flag != "" {
- handleAlarm(ctx, alram, "雨量", req.R01Flag)
- }
- if !strings.Contains("CSN", req.T01Flag) && req.T01Flag != "" {
- handleAlarm(ctx, alram, "温度", req.T01Flag)
- }
- if !strings.Contains("CSN", req.TspFlag) && req.TspFlag != "" {
- handleAlarm(ctx, alram, "Tsp", req.TspFlag)
- }
- if !strings.Contains("CSN", req.W01Flag) && req.W01Flag != "" {
- handleAlarm(ctx, alram, "风向", req.W01Flag)
- }
- if !strings.Contains("CSN", req.W02Flag) && req.W02Flag != "" {
- handleAlarm(ctx, alram, "风速", req.W02Flag)
- }
- if !strings.Contains("CSN", req.W03Flag) && req.W03Flag != "" {
- handleAlarm(ctx, alram, "风力", req.W03Flag)
- }*/
- return nil
- }
- func DustDataHandle(ctx context.Context, req *v1.DustData) error {
- // 获取设备信息
- deviceInfo, err := util.GetDeviceInfo(req.Mn)
- if err != nil {
- return err
- }
- tags := map[string]string{"sn": req.Mn}
- fields := map[string]interface{}{"pm10": req.Pm10Avg,
- "pm25": req.Pm25Avg,
- "tsp": req.TspAvg,
- "t01": req.T01Avg,
- "h01": req.H01Avg,
- "w01": req.W01Avg,
- "w02": req.W02Avg,
- "r01": req.R01Avg,
- "p01": req.P01Avg,
- "b03": req.B03Avg,
- "w03": req.W03Avg,
- "lng": req.Lng,
- "lat": req.Lat}
- /*
- bytes, _ := json.Marshal(req)
- json.Unmarshal(bytes, &fields)
- */
- go timetask.UpdateDustTime(req.Mn)
- t := util.ParseTime(req.DateTime)
- // 写数据
- err = model.WriteDustData(req.Mn, tags, fields, t, req.Cn)
- if err != nil {
- return err
- }
- // 处理扬尘告警
- handleDustAlarmReason(ctx, req, deviceInfo.ProjectId, deviceInfo.Name)
- return nil
- }
|