platformAlarmService.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package service
  2. import (
  3. "iot_manager_service/app/warn/dao"
  4. edgeService "iot_manager_service/app/warn/edge_service"
  5. "iot_manager_service/app/warn/model"
  6. "iot_manager_service/util/common"
  7. "time"
  8. )
  9. var PlatformAlarmService = new(platformAlarmService)
  10. type platformAlarmService struct{}
  11. // GetList 报警列表
  12. func (s platformAlarmService) GetList(tenantId string, filter model.RequestPlatFormAlartFilter) ([]model.PlatFormAlartData, int64, error) {
  13. var list []model.PlatFormAlartData
  14. dao := dao.PlatformAlarm{
  15. TenantId: tenantId,
  16. }
  17. levelArr := make(map[int]string)
  18. levelArr[1] = "紧急"
  19. levelArr[2] = "严重"
  20. levelArr[3] = "普通"
  21. gets, i, err := dao.Gets(filter)
  22. for _, alart := range gets {
  23. list = append(list, model.PlatFormAlartData{
  24. ID: alart.ID,
  25. ArmDeviceName: alart.ArmDeviceName,
  26. ArmDeviceSn: alart.ArmDeviceSn,
  27. ArmDeviceTypeName: alart.ArmDeviceTypeName,
  28. ArmContent: alart.ArmContent,
  29. LampPoleName: alart.LampPoleName,
  30. LampPoleSn: alart.LampPoleSn,
  31. ArmTime: alart.ArmTime.String(),
  32. ArmEndTime: alart.ArmEndTime.String(),
  33. ArmLevel: alart.ArmLevel,
  34. ArmLevelName: levelArr[alart.ArmLevel],
  35. LampPoleLocation: alart.LampPoleLocation,
  36. ArmHandleTime: alart.ArmHandleTime.String(),
  37. OpsStatus: alart.ArmHandle,
  38. })
  39. }
  40. return list, i, err
  41. }
  42. // Refresh 同步边缘接口报警数据
  43. func (s platformAlarmService) Refresh(tenantId string) error {
  44. viewDao := dao.ViewsAllCode{TenantId: tenantId}
  45. alls, err := viewDao.GetAlls()
  46. if err != nil {
  47. return err
  48. }
  49. newAllCode := make(map[string]dao.ViewsAllCode)
  50. for _, viewsAllCode := range alls {
  51. if viewsAllCode.Sn != "" {
  52. newAllCode[viewsAllCode.Sn] = viewsAllCode
  53. }
  54. }
  55. edgeService := edgeService.RecordAlarmRecord{}
  56. maxId, maxUpDateTime := dao.PlatformAlarm{}.GetMaxIdAndUpDateTime()
  57. list, err := edgeService.SyncAlartRecord(maxId, maxUpDateTime)
  58. if err != nil {
  59. return err
  60. }
  61. alarms := []dao.PlatformAlarm{}
  62. for _, data := range list {
  63. viewInfo := newAllCode[data.Code]
  64. parse := common.TimeStringToGoTime(data.Tend)
  65. ArmTime := common.TimeStringToGoTime(data.Tstart)
  66. ArmSource := 1
  67. if data.Cid != 0 {
  68. ArmSource = 2
  69. }
  70. alarms = append(alarms, dao.PlatformAlarm{
  71. ID: data.ID,
  72. TenantId: tenantId,
  73. ArmDeviceType: viewInfo.DeviceType,
  74. ArmDeviceTypeName: viewInfo.DeviceTypeName,
  75. ArmDeviceId: viewInfo.DeviceId,
  76. ArmDeviceName: viewInfo.DeviceName,
  77. ArmDeviceSn: data.Code,
  78. LampPoleId: viewInfo.LampPoleId,
  79. LampPoleName: viewInfo.LampPoleName,
  80. LampPoleSn: viewInfo.LampPoleSn,
  81. LampPoleLocation: viewInfo.Address,
  82. ArmSource: ArmSource,
  83. ArmEndTime: common.Time(parse),
  84. ArmTime: common.Time(ArmTime),
  85. ArmHandle: 1,
  86. ArmLevel: data.Level,
  87. ArmContent: data.Content,
  88. Status: 0, //告警发送
  89. Cid: uint(data.Cid),
  90. Cname: data.Cname,
  91. Sid: data.Sid,
  92. Threshold: float64(data.Threshold),
  93. })
  94. }
  95. err = dao.PlatformAlarm{}.BatchCreate(alarms)
  96. return err
  97. }
  98. // HandleAlarm 告警处理
  99. func (s platformAlarmService) HandleAlarm(tenant_id string, id int, armHandle int, remark string) error {
  100. dao := dao.PlatformAlarm{
  101. ID: id,
  102. ArmHandle: armHandle,
  103. ArmHanleRemark: remark,
  104. ArmHandleTime: common.Time(time.Now()),
  105. }
  106. return dao.Update()
  107. }
  108. // GetWarnCount 根据设备类型 取
  109. func (s platformAlarmService) GetWarnCount(tenantId string, deviceType int) (int64, error) {
  110. alarm := dao.PlatformAlarm{TenantId: tenantId, ArmDeviceType: deviceType}
  111. return alarm.GetWarnCount()
  112. }
  113. // GetNotNotifyAlarm 返回最近几天没有发送告警通知的警告
  114. func (s platformAlarmService) GetNotNotifyAlarm(day int) ([]dao.PlatformAlarm, error) {
  115. start := time.Now().AddDate(0, 0, -day).Format("2006-01-02- 15:04:05")
  116. return dao.PlatformAlarm{}.GetsAlarmByTime(start)
  117. }