send.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright 2019 github.com. All rights reserved.
  2. // Use of this source code is governed by github.com.
  3. package alarm
  4. import (
  5. "context"
  6. "fmt"
  7. "smart-thirdparty/errors"
  8. "smart-thirdparty/parser"
  9. pb_v1 "smart-thirdparty/pb/v1"
  10. "smart-thirdparty/thirdparty"
  11. "github.com/jaryhe/gopkgs/logger"
  12. jsoniter "github.com/json-iterator/go"
  13. "go.uber.org/zap"
  14. "google.golang.org/grpc/status"
  15. )
  16. // 替换encoding/json包
  17. var json = jsoniter.ConfigCompatibleWithStandardLibrary
  18. // 发送短信告警
  19. func sendSmsAlarm(req *pb_v1.SendAlarmRequest) error {
  20. var data []interface{}
  21. data = append(data, parser.Conf.ThirdParty.RongLian.City)
  22. data = append(data, req.DeviceName)
  23. data = append(data, req.AlarmTime)
  24. data = append(data, req.AlarmReason)
  25. if err := thirdparty.SendTemplateSMS(req.PhoneNumber, data, parser.Conf.ThirdParty.RongLian.AlarmTemplateId); err != nil {
  26. logger.Error("func",
  27. zap.String("call", "ThirdParty.SendTemplateSMS"),
  28. zap.String("params", req.PhoneNumber),
  29. zap.String("error", err.Error()))
  30. return errors.SmsAlarmError
  31. }
  32. return nil
  33. }
  34. // 发送邮件告警
  35. func sendEmailAlarm(req *pb_v1.SendAlarmRequest) error {
  36. content := fmt.Sprintf("设备(%s) 于 %s 因 %s 告警", req.DeviceName, req.AlarmTime, req.AlarmReason)
  37. err := thirdparty.SendMail(parser.Conf.ThirdParty.MailUser,
  38. parser.Conf.ThirdParty.MailPass,
  39. parser.Conf.ThirdParty.MailHost,
  40. req.Email, "设备告警", nil, content)
  41. if err != nil {
  42. logger.Error("func",
  43. zap.String("call", "thirdparty.SendMail"),
  44. zap.String("params", req.Email),
  45. zap.String("error", err.Error()))
  46. return errors.SmsAlarmError
  47. }
  48. return nil
  49. }
  50. func SendAlarm(ctx context.Context, req *pb_v1.SendAlarmRequest) (reply *pb_v1.SendAlarmReply, err error) {
  51. reply = &pb_v1.SendAlarmReply{}
  52. // 捕获各个task中的异常并返回给调用者
  53. defer func() {
  54. if r := recover(); r != nil {
  55. err = fmt.Errorf("%+v", r)
  56. e := &status.Status{}
  57. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  58. logger.Error("err",
  59. zap.String("system_err", err.Error()),
  60. zap.Stack("stacktrace"))
  61. }
  62. }
  63. }()
  64. if req.PhoneNumber == "" && req.Email == "" {
  65. return nil, status.Error(10003, "参数错误,告警联系人为空")
  66. }
  67. if req.AlarmReason == "" {
  68. return nil, status.Error(10003, "参数错误,告警原因为空")
  69. }
  70. if req.AlarmTime == "" {
  71. return nil, status.Error(10003, "参数错误,告警时间为空")
  72. }
  73. if req.DeviceName == "" {
  74. return nil, status.Error(10003, "参数错误,设备名为空")
  75. }
  76. if req.PhoneNumber != "" {
  77. sendSmsAlarm(req)
  78. }
  79. if req.Email != "" {
  80. sendEmailAlarm(req)
  81. }
  82. return
  83. }