captureUintService.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 CaptureUintService = new(captureUintService)
  13. type captureUintService struct{}
  14. // 捕获提交
  15. func (s *captureUintService) CaptureSubmit(userId int, tenantId string, req *model.CaptureDetail) *common.Errors {
  16. device := req.CaptureUnit
  17. device.TenantId = tenantId
  18. device.UpdateUser = userId
  19. device.UpdateTime = time.Now()
  20. if gateway, err := GatewayService.GetOne(device.GatewayId); err == nil {
  21. device.GatewayName = gateway.GatewayName
  22. device.GatewaySn = gateway.GatewaySn
  23. }
  24. if device.ID == 0 {
  25. device.CreateTime = time.Now()
  26. device.CreateUser = userId
  27. if device.IsExistedBySN() {
  28. logger.Logger.Errorf("Create IsExistedBySN \n")
  29. return common.ParamsInvalidResponse(model.RepeatedPrompts, nil)
  30. }
  31. logger.Logger.Errorf("device = %+v \n", device)
  32. if err := device.Create(); err != nil {
  33. logger.Logger.Errorf("Create err = %s \n", err.Error())
  34. return common.FailResponse(err.Error(), nil)
  35. }
  36. service.OperationHisService.Save(userId, tenantId, common.OperationCreate, common.ModuleTypeDevice,
  37. common.DeviceTypeCaptureUnit, common.GetDeviceObject(device.ID, device.GatewayName), common.OperationSuccess)
  38. return common.SuccessResponse(common.Succeeded, nil)
  39. }
  40. if err := device.Update(); err != nil {
  41. logger.Logger.Errorf("Update err = %s \n", err.Error())
  42. return common.FailResponse(err.Error(), nil)
  43. }
  44. service.OperationHisService.Save(userId, tenantId, common.OperationUpdate, common.ModuleTypeDevice,
  45. common.DeviceTypeCaptureUnit, common.GetDeviceObject(device.ID, device.GatewayName), common.OperationSuccess)
  46. return common.SuccessResponse(common.Succeeded, nil)
  47. }
  48. func (s *captureUintService) GetCapture(id int) (*model.CaptureDetail, *common.Errors) {
  49. // 创建查询实例
  50. device := &dao.CaptureUnit{
  51. ID: id,
  52. }
  53. err := device.GetDevice()
  54. if err != nil {
  55. return nil, common.FailResponse(err.Error(), nil)
  56. }
  57. detail := &model.CaptureDetail{CaptureUnit: *device}
  58. endTime, state := cache.GetDeviceState(device.CaptureSn)
  59. detail.NetworkState = state
  60. detail.EndLineTime = endTime
  61. return detail, nil
  62. }
  63. func (s *captureUintService) CaptureList(searchValue string, current, size int) ([]model.CaptureDetail, int64, *common.Errors) {
  64. var details []model.CaptureDetail
  65. device := dao.CaptureUnit{}
  66. if searchValue != "" {
  67. device.CaptureSn = searchValue
  68. }
  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. for _, d := range devices {
  76. endTime, state := cache.GetDeviceState(d.CaptureSn)
  77. details = append(details, model.CaptureDetail{
  78. CaptureUnit: d,
  79. EndLineTime: endTime,
  80. NetworkState: state,
  81. })
  82. }
  83. return details, total, nil
  84. }
  85. func (s *captureUintService) CaptureGetList(tenantId string) ([]*dao.CaptureUnit, *common.Errors) {
  86. device := &dao.CaptureUnit{
  87. TenantId: tenantId,
  88. IsDeleted: 0,
  89. }
  90. devices, err := device.GetAllDevices()
  91. if err != nil {
  92. return nil, common.FailResponse(err.Error(), nil)
  93. }
  94. for _, d := range devices {
  95. d.CaptureName = d.CaptureName + "(" + d.CaptureSn + ")"
  96. }
  97. return devices, nil
  98. }
  99. func (s *captureUintService) GetPoint(id int) (*dao.CheckPoint, *common.Errors) {
  100. // 创建查询实例
  101. device := &dao.CheckPoint{
  102. ID: id,
  103. }
  104. err := device.GetDevice()
  105. if err != nil {
  106. return nil, common.FailResponse(err.Error(), nil)
  107. }
  108. return device, nil
  109. }
  110. func (s *captureUintService) PointSubmit(userId int, tenantId string, req *dao.CheckPoint) *common.Errors {
  111. device := req
  112. device.TenantId = tenantId
  113. device.UpdateUser = userId
  114. device.UpdateTime = time.Now()
  115. if device.ID == 0 {
  116. device.CreateTime = time.Now()
  117. device.CreateUser = userId
  118. if device.IsExistedBySN() {
  119. logger.Logger.Errorf("Create IsExistedBySN \n")
  120. return common.ParamsInvalidResponse(model.RepeatedPrompts, nil)
  121. }
  122. logger.Logger.Errorf("device = %+v \n", device)
  123. if err := device.Create(); err != nil {
  124. logger.Logger.Errorf("Create err = %s \n", err.Error())
  125. return common.FailResponse(err.Error(), nil)
  126. }
  127. service.OperationHisService.Save(userId, tenantId, common.OperationCreate, common.ModuleTypeDevice,
  128. common.DeviceTypePoint, common.GetDeviceObject(device.ID, device.PointName), common.OperationSuccess)
  129. return common.SuccessResponse(common.Succeeded, nil)
  130. }
  131. if err := device.Update(); err != nil {
  132. logger.Logger.Errorf("Update err = %s \n", err.Error())
  133. return common.FailResponse(err.Error(), nil)
  134. }
  135. service.OperationHisService.Save(userId, tenantId, common.OperationUpdate, common.ModuleTypeDevice,
  136. common.DeviceTypePoint, common.GetDeviceObject(device.ID, device.PointName), common.OperationSuccess)
  137. return common.SuccessResponse(common.Succeeded, nil)
  138. }
  139. func (s *captureUintService) PointList(searchValue string, current, size int) ([]dao.CheckPoint, int64, *common.Errors) {
  140. device := dao.CheckPoint{}
  141. if searchValue != "" {
  142. device.PointSN = searchValue
  143. }
  144. offset := (current - 1) * size
  145. limit := size
  146. devices, total, err := device.GetDevices(offset, limit)
  147. if err != nil {
  148. return nil, 0, common.FailResponse(err.Error(), nil)
  149. }
  150. return devices, total, nil
  151. }
  152. func (s *captureUintService) PointGetList(tenantId string) ([]*dao.CheckPoint, *common.Errors) {
  153. device := &dao.CheckPoint{
  154. TenantId: tenantId,
  155. IsDeleted: 0,
  156. }
  157. devices, err := device.GetAllDevices()
  158. if err != nil {
  159. return nil, common.FailResponse(err.Error(), nil)
  160. }
  161. for _, d := range devices {
  162. d.PointName = d.PointName + "(" + d.PointSN + ")"
  163. }
  164. return devices, nil
  165. }
  166. func (s *captureUintService) RemoveCapture(userId int, tenantId string, id int) *common.Errors {
  167. // 创建查询实例
  168. device := &dao.CaptureUnit{
  169. ID: id,
  170. IsDeleted: 1,
  171. UpdateUser: userId,
  172. UpdateTime: time.Now(),
  173. }
  174. err := device.Delete()
  175. if err != nil {
  176. return common.FailResponse(err.Error(), nil)
  177. }
  178. service.OperationHisService.Save(userId, tenantId, common.OperationRemove, common.ModuleTypeDevice,
  179. common.DeviceTypeCaptureUnit, common.GetDeviceObject(device.ID, device.CaptureName), common.OperationSuccess)
  180. return nil
  181. }
  182. func (s *captureUintService) RemovePoint(userId int, tenantId string, id int) *common.Errors {
  183. // 创建查询实例
  184. device := &dao.CheckPoint{
  185. ID: id,
  186. IsDeleted: 1,
  187. UpdateUser: userId,
  188. UpdateTime: time.Now(),
  189. }
  190. err := device.Delete()
  191. if err != nil {
  192. return common.FailResponse(err.Error(), nil)
  193. }
  194. service.OperationHisService.Save(userId, tenantId, common.OperationRemove, common.ModuleTypeDevice,
  195. common.DeviceTypePoint, common.GetDeviceObject(device.ID, device.PointName), common.OperationSuccess)
  196. return nil
  197. }
  198. func (s *captureUintService) GetCaptureByGateway(id int) []dao.CaptureUnit {
  199. // 创建查询实例
  200. device := &dao.CaptureUnit{
  201. GatewayId: id,
  202. }
  203. return device.GetDevicesByGateway()
  204. }
  205. func (s *captureUintService) GetByLampPole(id int) []dao.CaptureUnit {
  206. // 创建查询实例
  207. device := &dao.CaptureUnit{
  208. LampPoleId: id,
  209. }
  210. return device.GetDevicesByLampPole()
  211. }
  212. func (s *captureUintService) DeviceCount() dao.DeviceStatus {
  213. var rsp dao.DeviceStatus
  214. d := dao.CaptureUnit{}
  215. rsp.Set(d.DeviceCount1())
  216. return rsp
  217. }
  218. func (s *captureUintService) ListDevices() []dao.Device {
  219. d := dao.CaptureUnit{}
  220. return d.ListDevices1()
  221. }