platformAlarmService.go 4.2 KB

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