ipBroadcastService.go 5.6 KB

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