taskWarnService.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package service
  2. import (
  3. "errors"
  4. "fmt"
  5. "iot_manager_service/app/warn/dao"
  6. "iot_manager_service/config"
  7. "iot_manager_service/util/notify"
  8. )
  9. var TaskWarnService = new(taskWarnService)
  10. // 定时任务处理的service
  11. type taskWarnService struct {
  12. }
  13. // HandlingAlarms 报警处理
  14. func (receiver taskWarnService) HandlingAlarms() {
  15. /*
  16. 1. 查出平台 platformAlarmService.go 最近3天的 status = 0 未处理的 报警记录
  17. 2. 根据报警 sn 查出设置的报警人相关信息,可能是多个报警方式
  18. 3. 执行发短信或邮件逻辑,发送内容
  19. 4. 保存相关日志到 noticeRecordService.go 下
  20. */
  21. alarm, _ := PlatformAlarmService.GetNotNotifyAlarm(3)
  22. for _, platformAlarm := range alarm {
  23. sn := platformAlarm.ArmDeviceSn
  24. deviceType := platformAlarm.ArmDeviceType
  25. armSource := 1
  26. receiveList, _ := NoticeSetService.GetReceiveList(deviceType, sn, armSource)
  27. armSourceName := "运维"
  28. if armSource == 2 {
  29. armSourceName = "业务"
  30. }
  31. for _, receive := range receiveList {
  32. //告警内容拼装
  33. sendMsgFormat := "%v,您好:%v的%v于%v检测到%v,请确认并执行维护。"
  34. name := "【" + platformAlarm.LampPoleName + "】" + platformAlarm.ArmDeviceName
  35. sendMsg := fmt.Sprintf(sendMsgFormat, receive.UserName, platformAlarm.LampPoleLocation, name, platformAlarm.ArmTime, platformAlarm.ArmContent)
  36. sendErr := errors.New("")
  37. //去发送消息
  38. SendTypeName := "邮件"
  39. switch receive.NoticeMode {
  40. case 1:
  41. SendTypeName = "短信"
  42. sendErr = notify.SendTencentSms([]string{"+86" + receive.NoticeReceiveAccount}, config.Instance().MonitNotice.Sms.Tencent.WarnSmsTemplateID, []string{platformAlarm.LampPoleName, platformAlarm.LampPoleSn, platformAlarm.ArmContent})
  43. case 2:
  44. sendErr = notify.SendEmail(sendMsg, armSourceName+"通知", receive.NoticeReceiveAccount)
  45. }
  46. sendStatus := 1
  47. if sendErr != nil {
  48. sendStatus = 0
  49. }
  50. //保存消息记录
  51. dao.NoticeRecord{
  52. NoticeSetId: receive.NoticeSetId,
  53. ArmClassify: armSource,
  54. Content: sendMsg,
  55. Status: sendStatus,
  56. SendType: receive.NoticeMode,
  57. SendTypeName: SendTypeName,
  58. SendValue: receive.NoticeReceiveAccount,
  59. SendName: receive.UserName,
  60. PlatformAlarmId: platformAlarm.ID,
  61. }.Create()
  62. //标记已发送
  63. dao.PlatformAlarm{ID: platformAlarm.ID, Status: 1}.Update()
  64. }
  65. }
  66. }