platformAlarmService.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. "strconv"
  8. "time"
  9. )
  10. var PlatformAlarmService = new(platformAlarmService)
  11. type platformAlarmService struct{}
  12. func (s platformAlarmService) GetList(tenantId int, filter model.RequestPlatFormAlartFilter) ([]model.PlatFormAlartData, int64, error) {
  13. var list []model.PlatFormAlartData
  14. dao := dao.PlatformAlarm{
  15. TenantId: strconv.Itoa(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. func (s platformAlarmService) Refresh(tenantId int) error {
  43. viewDao := dao.ViewsAllCode{TenantId: tenantId}
  44. alls, err := viewDao.GetAlls()
  45. if err != nil {
  46. return err
  47. }
  48. newAllCode := make(map[string]dao.ViewsAllCode)
  49. for _, viewsAllCode := range alls {
  50. if viewsAllCode.Sn != "" {
  51. newAllCode[viewsAllCode.Sn] = viewsAllCode
  52. }
  53. }
  54. //fmt.Printf("newAllCode = %v", newAllCode)
  55. edgeService := edgeService.RecordAlarmRecord{}
  56. maxId, maxUpDateTime := dao.PlatformAlarm{}.GetMaxIdAndUpDateTime()
  57. //fmt.Printf("maxId = %v\n", maxId)
  58. //fmt.Printf("maxUpDateTime = %v\n", maxUpDateTime)
  59. list, err := edgeService.SyncAlartRecord(maxId, maxUpDateTime)
  60. if err != nil {
  61. return err
  62. }
  63. alarms := []dao.PlatformAlarm{}
  64. for _, data := range list {
  65. viewInfo := newAllCode[data.Code]
  66. parse := common.TimeStringToGoTime(data.Tend)
  67. ArmTime := common.TimeStringToGoTime(data.Tstart)
  68. ArmSource := 1
  69. if data.Cid != 0 {
  70. ArmSource = 2
  71. }
  72. alarms = append(alarms, dao.PlatformAlarm{
  73. ID: data.ID,
  74. TenantId: strconv.Itoa(tenantId),
  75. ArmDeviceType: viewInfo.DeviceType,
  76. ArmDeviceTypeName: viewInfo.DeviceTypeName,
  77. ArmDeviceId: viewInfo.DeviceId,
  78. ArmDeviceName: viewInfo.DeviceName,
  79. ArmDeviceSn: data.Code,
  80. LampPoleId: viewInfo.LampPoleId,
  81. LampPoleName: viewInfo.LampPoleName,
  82. LampPoleSn: viewInfo.LampPoleSn,
  83. LampPoleLocation: viewInfo.Address,
  84. ArmSource: ArmSource,
  85. ArmEndTime: common.Time(parse),
  86. ArmTime: common.Time(ArmTime),
  87. ArmHandle: 1,
  88. ArmLevel: data.Level,
  89. ArmContent: data.Content,
  90. Status: 0, //告警发送
  91. Cid: uint(data.Cid),
  92. Cname: data.Cname,
  93. Sid: data.Sid,
  94. Threshold: float64(data.Threshold),
  95. })
  96. }
  97. err = dao.PlatformAlarm{}.BatchCreate(alarms)
  98. return err
  99. }
  100. func (s platformAlarmService) HannleAlart(tenant_id int, id int, armHandle int, remark string) error {
  101. dao := dao.PlatformAlarm{
  102. ID: id,
  103. ArmHandle: armHandle,
  104. ArmHanleRemark: remark,
  105. ArmHandleTime: common.Time(time.Now()),
  106. }
  107. return dao.Update()
  108. }
  109. // GetWarnCount 根据设备类型 取
  110. func (s platformAlarmService) GetWarnCount(tenantId int, deviceType int) (int64, error) {
  111. alarm := dao.PlatformAlarm{TenantId: strconv.Itoa(tenantId), ArmDeviceType: deviceType}
  112. return alarm.GetWarnCount()
  113. }