ipBroadcastService.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. package service
  2. import (
  3. "iot_manager_service/app/device/dao"
  4. "iot_manager_service/app/device/edge_service"
  5. "iot_manager_service/app/device/model"
  6. "iot_manager_service/app/system/service"
  7. "iot_manager_service/util/common"
  8. "iot_manager_service/util/logger"
  9. "time"
  10. )
  11. var IpBroadcastService = new(ipBroadcastService)
  12. type ipBroadcastService struct{}
  13. func (s *ipBroadcastService) Get(id int) (*model.IpBroadcastDetail, *common.Errors) {
  14. // 创建查询实例
  15. device := &dao.IpBroadcast{
  16. ID: id,
  17. }
  18. err := device.GetDevice()
  19. if err != nil {
  20. return nil, common.FailResponse(err.Error(), nil)
  21. }
  22. terms, err := edge_service.IpCastControlService{}.GetTermIds()
  23. if err != nil {
  24. return nil, common.FailResponse(err.Error(), nil)
  25. }
  26. //endTime, state := cache.GetDeviceState(device.CastSn)
  27. info, ok := terms[device.CastSn]
  28. state := "2"
  29. if ok {
  30. device.PlayState = "不在线"
  31. if info.Online == 1 {
  32. state = "1"
  33. device.SoundVolume = info.Vol
  34. device.IPAddress = info.IP
  35. device.PlayState = "空闲"
  36. if info.State == 2 {
  37. device.PlayState = "文件广播[" + info.From + "]"
  38. }
  39. }
  40. }
  41. return &model.IpBroadcastDetail{
  42. IpBroadcast: *device,
  43. RunState: state,
  44. NetworkState: state,
  45. //EndLineTime: endTime,
  46. }, nil
  47. }
  48. func (s *ipBroadcastService) CreateOrUpdate(userId int, tenantId string, req dao.IpBroadcast) *common.Errors {
  49. // 创建查询实例
  50. device := req
  51. device.TenantId = tenantId
  52. device.UpdateUser = userId
  53. device.UpdateTime = time.Now()
  54. if req.ID == 0 {
  55. device.CreateTime = time.Now()
  56. device.CreateUser = userId
  57. if device.IsExistedBySN() {
  58. logger.Logger.Errorf("Create GetDeviceID err \n")
  59. return common.ParamsInvalidResponse("编码不能重复,请重新填写!", nil)
  60. }
  61. if err := device.Create(); err != nil {
  62. logger.Logger.Errorf("Create err = %s \n", err.Error())
  63. return common.FailResponse(err.Error(), nil)
  64. }
  65. service.OperationHisService.Save(userId, tenantId, common.OperationCreate, common.ModuleTypeDevice,
  66. common.DeviceTypeIPBroadcast, common.GetDeviceObject(device.ID, device.CastName), common.OperationSuccess)
  67. return common.SuccessResponse(common.Succeeded, nil)
  68. }
  69. if device.IsExistedByNameAndCode() {
  70. return common.ParamsInvalidResponse("编码不能重复,请重新填写!", nil)
  71. }
  72. if err := device.Update(); err != nil {
  73. logger.Logger.Errorf("Update err = %s \n", err.Error())
  74. return common.FailResponse(err.Error(), nil)
  75. }
  76. service.OperationHisService.Save(userId, tenantId, common.OperationUpdate, common.ModuleTypeDevice,
  77. common.DeviceTypeIPBroadcast, common.GetDeviceObject(device.ID, device.CastName), common.OperationSuccess)
  78. return common.SuccessResponse(common.Succeeded, nil)
  79. }
  80. func (s *ipBroadcastService) List(searchValue string, current, size int) ([]model.IpBroadcastDetail, int64, *common.Errors) {
  81. device := dao.IpBroadcast{}
  82. terms, err := edge_service.IpCastControlService{}.GetTermIds()
  83. if err != nil {
  84. return nil, 0, nil
  85. }
  86. if searchValue != "" {
  87. device.CastSn = searchValue
  88. device.CastName = searchValue
  89. }
  90. offset := (current - 1) * size
  91. limit := size
  92. devices, total, err := device.GetDevices(offset, limit)
  93. if err != nil {
  94. return nil, 0, common.FailResponse(err.Error(), nil)
  95. }
  96. var details []model.IpBroadcastDetail
  97. for _, d := range devices {
  98. //endTime, state := cache.GetDeviceState(d.CastSn)
  99. info, ok := terms[d.CastSn]
  100. state := "2"
  101. if ok {
  102. d.PlayState = "不在线"
  103. if info.Online == 1 {
  104. state = "1"
  105. d.SoundVolume = info.Vol
  106. d.IPAddress = info.IP
  107. d.PlayState = "空闲"
  108. if info.State == 2 {
  109. d.PlayState = "文件广播[" + info.From + "]"
  110. }
  111. }
  112. }
  113. details = append(details, model.IpBroadcastDetail{
  114. IpBroadcast: d,
  115. RunState: state,
  116. NetworkState: state,
  117. //EndLineTime: endTime,
  118. })
  119. }
  120. return details, total, nil
  121. }
  122. func (s *ipBroadcastService) Remove(userId int, tenantId string, id int) *common.Errors {
  123. // 创建查询实例
  124. device := &dao.IpBroadcast{
  125. ID: id,
  126. IsDeleted: 1,
  127. UpdateUser: userId,
  128. UpdateTime: time.Now(),
  129. }
  130. err := device.Delete()
  131. if err != nil {
  132. return common.FailResponse(err.Error(), nil)
  133. }
  134. service.OperationHisService.Save(userId, tenantId, common.OperationRemove, common.ModuleTypeDevice,
  135. common.DeviceTypeIPBroadcast, common.GetDeviceObject(device.ID, device.CastName), common.OperationSuccess)
  136. return nil
  137. }
  138. func (s *ipBroadcastService) GetList(tenantId string) ([]dao.IpBroadcast, *common.Errors) {
  139. device := &dao.IpBroadcast{
  140. TenantId: tenantId,
  141. IsDeleted: 0,
  142. }
  143. devices, err := device.GetAllDevices()
  144. if err != nil {
  145. return nil, common.FailResponse(err.Error(), nil)
  146. }
  147. return devices, nil
  148. }
  149. func (s *ipBroadcastService) GetByGateway(id int) []dao.IpBroadcast {
  150. // 创建查询实例
  151. device := &dao.IpBroadcast{
  152. GatewayId: id,
  153. }
  154. return device.GetDevicesByGateway()
  155. }
  156. func (s *ipBroadcastService) GetByLampPole(id int) []dao.IpBroadcast {
  157. // 创建查询实例
  158. device := &dao.IpBroadcast{
  159. LampPoleId: id,
  160. }
  161. return device.GetDevicesByLampPole()
  162. }
  163. func (s *ipBroadcastService) GetAll(tenantId string) []dao.IpBroadcast {
  164. device := &dao.IpBroadcast{
  165. TenantId: tenantId,
  166. IsDeleted: 0,
  167. }
  168. return device.GetAll()
  169. }
  170. func (s *ipBroadcastService) GetByIds(tenantId string, ids string) []dao.IpBroadcast {
  171. device := &dao.IpBroadcast{
  172. TenantId: tenantId,
  173. IsDeleted: 0,
  174. }
  175. return device.GetByIds(ids)
  176. }
  177. func (s *ipBroadcastService) DeviceCount() dao.DeviceStatus {
  178. var rsp dao.DeviceStatus
  179. d := dao.IpBroadcast{}
  180. rsp.Set(d.DeviceCount1())
  181. return rsp
  182. }
  183. func (s *ipBroadcastService) ListDevices() []dao.Device {
  184. d := dao.IpBroadcast{}
  185. return d.ListDevices1()
  186. }