lightControlService.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. package service
  2. import (
  3. "fmt"
  4. "iot_manager_service/app/device/dao"
  5. "iot_manager_service/app/device/model"
  6. "iot_manager_service/app/system/service"
  7. "iot_manager_service/util"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. // 中间件管理服务
  13. var LightControlService = new(lightControlService)
  14. type lightControlService struct{}
  15. func (s *lightControlService) Get(id int) (*model.LightControlDetail, *util.Errors) {
  16. // 创建查询实例
  17. device := &dao.LightControl{
  18. ID: id,
  19. }
  20. err := device.GetDevice()
  21. if err != nil {
  22. return nil, util.FailResponse(err.Error(), nil)
  23. }
  24. state := util.GetDeviceState(device.Sn)
  25. detail := model.LightControlDetail{
  26. LightControl: *device,
  27. EndLineTime: "",
  28. NetworkState: state,
  29. RunState: state,
  30. }
  31. return &detail, nil
  32. }
  33. func (s *lightControlService) CreateOrUpdate(userId int64, tenantId int, req *dao.LightControl) *util.Errors {
  34. device := req
  35. device.TenantId = tenantId
  36. device.UpdateUser = userId
  37. device.UpdateTime = time.Now()
  38. if device.LampPoleId != 0 {
  39. lampPole, err := LampPoleService.GetOne(device.LampPoleId)
  40. if err == nil {
  41. device.LampLat = lampPole.PoleLat
  42. device.LampLng = lampPole.PoleLng
  43. device.LampPoleName = lampPole.PoleName
  44. device.LampPoleSn = lampPole.PoleSN
  45. device.LampPoleLocation = lampPole.InstallLocation
  46. device.GroupId = lampPole.GroupId
  47. } else {
  48. fmt.Printf("LampPoleService.GetOne err = %v \n", err)
  49. }
  50. }
  51. if device.ControlType == model.ControlType_ZigBee {
  52. if device.ZigbeeId == 0 {
  53. return util.ParamsInvalidResponse(model.ZigbeeSelect, nil)
  54. }
  55. if device.IsOnDemand == 0 {
  56. if device.ControlNo == "" {
  57. return util.ParamsInvalidResponse(model.ControlNONull, nil)
  58. }
  59. if !checkControlNoIsCompliance(device.ControlNo) {
  60. return util.ParamsInvalidResponse(model.ControlNOInvalid, nil)
  61. }
  62. }
  63. zigbee, err := ZigbeeService.Get(device.ZigbeeId)
  64. if err != nil {
  65. return err
  66. }
  67. device.ZigbeeName = zigbee.Name
  68. device.ZigbeeSn = zigbee.Sn
  69. device.ChannelNum = zigbee.ChannelNum
  70. device.NetworkNum = zigbee.NetworkNum
  71. }
  72. if device.ID == 0 {
  73. device.CreateTime = time.Now()
  74. device.CreateUser = userId
  75. if device.IsExistedBySN() {
  76. fmt.Printf("Create IsExistedBySN \n")
  77. return util.ParamsInvalidResponse(model.RepeatedPrompts, nil)
  78. }
  79. fmt.Printf("device = %+v \n", device)
  80. if err := device.Create(); err != nil {
  81. fmt.Printf("Create err = %s \n", err.Error())
  82. return util.FailResponse(err.Error(), nil)
  83. }
  84. service.OperationHisService.Save(userId, tenantId, util.OperationCreate, util.ModuleTypeDevice,
  85. util.DeviceTypeLightControl, util.GetDeviceObject(device.ID, device.Name), util.OperationSuccess)
  86. return util.SuccessResponse(util.Succeeded, nil)
  87. }
  88. if err := device.Update(); err != nil {
  89. fmt.Printf("Update err = %s \n", err.Error())
  90. return util.FailResponse(err.Error(), nil)
  91. }
  92. service.OperationHisService.Save(userId, tenantId, util.OperationUpdate, util.ModuleTypeDevice,
  93. util.DeviceTypeLightControl, util.GetDeviceObject(device.ID, device.Name), util.OperationSuccess)
  94. return util.SuccessResponse(util.Succeeded, nil)
  95. }
  96. func (s *lightControlService) List(searchValue, controlType, zigbeeId string, current, size int) ([]dao.LightControl,
  97. *util.Errors) {
  98. device := dao.LightControl{}
  99. if searchValue != "" {
  100. device.Sn = searchValue
  101. }
  102. if controlType != "" {
  103. cType, err := strconv.Atoi(controlType)
  104. if err == nil {
  105. device.ControlType = cType
  106. }
  107. }
  108. if zigbeeId != "" {
  109. zId, err := strconv.Atoi(zigbeeId)
  110. if err == nil {
  111. device.ZigbeeId = zId
  112. }
  113. }
  114. offset := (current - 1) * size
  115. limit := size
  116. devices, err := device.GetDevices(offset, limit)
  117. if err != nil {
  118. return nil, util.FailResponse(err.Error(), nil)
  119. }
  120. return devices, nil
  121. }
  122. func (s *lightControlService) Remove(userId int64, tenantId int, id int) *util.Errors {
  123. // 创建查询实例
  124. device := &dao.LightControl{
  125. ID: id,
  126. IsDeleted: 1,
  127. UpdateUser: userId,
  128. UpdateTime: time.Now(),
  129. }
  130. err := device.Delete()
  131. if err != nil {
  132. return util.FailResponse(err.Error(), nil)
  133. }
  134. service.OperationHisService.Save(userId, tenantId, util.OperationRemove, util.ModuleTypeDevice,
  135. util.DeviceTypeLightControl, util.GetDeviceObject(device.ID, device.Name), util.OperationSuccess)
  136. return nil
  137. }
  138. func (s *lightControlService) GetList(tenantId int) ([]dao.LightControl, *util.Errors) {
  139. device := &dao.LightControl{
  140. TenantId: tenantId,
  141. IsDeleted: 0,
  142. }
  143. devices, err := device.GetAllDevices()
  144. if err != nil {
  145. return nil, util.FailResponse(err.Error(), nil)
  146. }
  147. return devices, nil
  148. }
  149. func (s *lightControlService) Enable(id, status int) *util.Errors {
  150. // 创建查询实例
  151. device := &dao.LightControl{
  152. ID: id,
  153. IsEnable: status,
  154. }
  155. err := device.UpdateEnable()
  156. if err != nil {
  157. return util.FailResponse(err.Error(), nil)
  158. }
  159. return nil
  160. }
  161. func (s *lightControlService) GetOne(id int) (*dao.LightControl, *util.Errors) {
  162. device := &dao.LightControl{
  163. ID: id,
  164. }
  165. err := device.GetDevice()
  166. if err != nil {
  167. return nil, util.FailResponse(err.Error(), nil)
  168. }
  169. return device, nil
  170. }
  171. func (s *lightControlService) GetByGroupId(groupId int) ([]dao.LightControl, *util.Errors) {
  172. // 创建查询实例
  173. device := &dao.LightControl{
  174. GroupId: groupId,
  175. }
  176. devices, err := device.GetDevicesByGroup()
  177. if err != nil {
  178. return nil, util.FailResponse(err.Error(), nil)
  179. }
  180. return devices, nil
  181. }
  182. func (s *lightControlService) GetByGateway(id int) []dao.LightControl {
  183. // 创建查询实例
  184. device := &dao.LightControl{
  185. GatewayId: id,
  186. }
  187. return device.GetDevicesByGateway()
  188. }
  189. func (s *lightControlService) GetByLampPole(id int) []dao.LightControl {
  190. // 创建查询实例
  191. device := &dao.LightControl{
  192. LampPoleId: id,
  193. }
  194. return device.GetDevicesByLampPole()
  195. }
  196. //检查灯控编号是否合规1-1——255-255
  197. func checkControlNoIsCompliance(controlNo string) bool {
  198. arr := strings.Split(controlNo, "-")
  199. if len(arr) != 2 {
  200. return false
  201. }
  202. one, err := strconv.Atoi(arr[0])
  203. if err != nil || one < 1 || one > 255 {
  204. return false
  205. }
  206. two, err := strconv.Atoi(arr[1])
  207. if err != nil || two < 1 || two > 255 {
  208. return false
  209. }
  210. return true
  211. }