alarmTerminalService.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package service
  2. import (
  3. "iot_manager_service/app/device/dao"
  4. "iot_manager_service/app/device/model"
  5. "iot_manager_service/app/system/service"
  6. "iot_manager_service/util/cache"
  7. "iot_manager_service/util/common"
  8. "iot_manager_service/util/logger"
  9. "time"
  10. )
  11. // 中间件管理服务
  12. var AlarmTerminalService = new(alarmTerminalService)
  13. type alarmTerminalService struct{}
  14. // 获取单个设备信息
  15. func (s *alarmTerminalService) Get(id int) (*model.AlarmTerminalDetail, *common.Errors) {
  16. // 创建查询实例
  17. device := &dao.AlarmTerminal{
  18. ID: id,
  19. }
  20. err := device.GetDevice()
  21. if err != nil {
  22. return nil, common.FailResponse(err.Error(), nil)
  23. }
  24. endTime, state := cache.GetDeviceState(device.TerminalSN)
  25. return &model.AlarmTerminalDetail{
  26. AlarmTerminal: *device,
  27. RunState: state,
  28. NetworkState: state,
  29. EndLineTime: endTime,
  30. }, nil
  31. }
  32. // 添加或者修改设备信息
  33. func (s *alarmTerminalService) CreateOrUpdate(userId int, tenantId string, req dao.AlarmTerminal) *common.Errors {
  34. device := req
  35. device.TenantId = tenantId
  36. device.UpdateUser = userId
  37. device.UpdateTime = time.Now()
  38. if req.ID == 0 {
  39. device.CreateTime = time.Now()
  40. device.CreateUser = userId
  41. if device.IsExistedBySN() {
  42. logger.Logger.Errorf("Create IsExistedBySN \n")
  43. return common.ParamsInvalidResponse(model.RepeatedName, nil)
  44. }
  45. if err := device.Create(); err != nil {
  46. logger.Logger.Errorf("Create err = %s \n", err.Error())
  47. return common.FailResponse(err.Error(), nil)
  48. }
  49. service.OperationHisService.Save(userId, tenantId, common.OperationCreate, common.ModuleTypeDevice,
  50. common.DeviceTypeAlarmTerminal, common.GetDeviceObject(device.ID, device.TerminalName), common.OperationSuccess)
  51. return common.SuccessResponse(common.Succeeded, nil)
  52. }
  53. if device.IsExistedByNameAndCode() {
  54. logger.Logger.Errorf("Update IsExistedByNameAndCode \n")
  55. return common.ParamsInvalidResponse(model.RepeatedName, nil)
  56. }
  57. if err := device.Update(); err != nil {
  58. logger.Logger.Errorf("Update err = %s \n", err.Error())
  59. return common.FailResponse(err.Error(), nil)
  60. }
  61. service.OperationHisService.Save(userId, tenantId, common.OperationUpdate, common.ModuleTypeDevice,
  62. common.DeviceTypeAlarmTerminal, common.GetDeviceObject(device.ID, device.TerminalName), common.OperationSuccess)
  63. return common.SuccessResponse(common.Succeeded, nil)
  64. }
  65. // 获取设备信息
  66. func (s *alarmTerminalService) List(poleGroupName string, current, size int) ([]model.AlarmTerminalDetail, int64, *common.Errors) {
  67. // 创建查询实例
  68. device := &dao.AlarmTerminal{}
  69. offset := (current - 1) * size
  70. limit := size
  71. devices, total, err := device.GetDevices(offset, limit)
  72. if err != nil {
  73. return nil, 0, common.FailResponse(err.Error(), nil)
  74. }
  75. var details []model.AlarmTerminalDetail
  76. for _, d := range devices {
  77. endTime, state := cache.GetDeviceState(d.TerminalSN)
  78. details = append(details, model.AlarmTerminalDetail{
  79. AlarmTerminal: d,
  80. RunState: state,
  81. NetworkState: state,
  82. EndLineTime: endTime,
  83. })
  84. }
  85. return details, total, nil
  86. }
  87. // 移除设备信息
  88. func (s *alarmTerminalService) Remove(userId int, tenantId string, id int) *common.Errors {
  89. // 创建查询实例
  90. device := &dao.AlarmTerminal{
  91. ID: id,
  92. IsDeleted: 1,
  93. UpdateUser: userId,
  94. UpdateTime: time.Now(),
  95. }
  96. err := device.Delete()
  97. if err != nil {
  98. return common.FailResponse(err.Error(), nil)
  99. }
  100. service.OperationHisService.Save(userId, tenantId, common.OperationRemove, common.ModuleTypeDevice,
  101. common.DeviceTypeAlarmTerminal, common.GetDeviceObject(device.ID, device.TerminalName), common.OperationSuccess)
  102. return nil
  103. }
  104. // 根据网关获取设备信息
  105. func (s *alarmTerminalService) GetByGateway(id int) []dao.AlarmTerminal {
  106. // 创建查询实例
  107. device := &dao.AlarmTerminal{
  108. GatewayId: id,
  109. }
  110. return device.GetDevicesByGateway()
  111. }
  112. // 根据灯杆设备获取设备信息
  113. func (s *alarmTerminalService) GetByLampPole(id int) []dao.AlarmTerminal {
  114. // 创建查询实例
  115. device := &dao.AlarmTerminal{
  116. LampPoleId: id,
  117. }
  118. return device.GetDevicesByLampPole()
  119. }
  120. // 获取设备的数量
  121. func (s *alarmTerminalService) DeviceCount() dao.DeviceStatus {
  122. var rsp dao.DeviceStatus
  123. d := dao.AlarmTerminal{}
  124. rsp.Set(d.DeviceCount1())
  125. return rsp
  126. }
  127. // 获取设备列表
  128. func (s *alarmTerminalService) ListDevices() []dao.Device {
  129. d := dao.AlarmTerminal{}
  130. return d.ListDevices1()
  131. }