lightControlService.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. package service
  2. import (
  3. "fmt"
  4. "iot_manager_service/app/dao"
  5. "iot_manager_service/app/model"
  6. "iot_manager_service/app/utils"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. // 中间件管理服务
  12. var LightControlService = new(lightControlService)
  13. type lightControlService struct{}
  14. func (s *lightControlService) Get(id int) (*dao.LightControl, *utils.Errors) {
  15. // 创建查询实例
  16. device := &dao.LightControl{
  17. ID: id,
  18. }
  19. err := device.GetDevice()
  20. if err != nil {
  21. return nil, utils.FailResponse(err.Error(), nil)
  22. }
  23. //todo runstate 需要使用各个设备的状态来处理
  24. return device, nil
  25. }
  26. func (s *lightControlService) GetRelevanceDetail(id int) (*model.LightControlDetail, *utils.Errors) {
  27. // 创建查询实例
  28. device := &dao.LightControl{
  29. ID: id,
  30. }
  31. err := device.GetDevice()
  32. if err != nil {
  33. return nil, utils.FailResponse(err.Error(), nil)
  34. }
  35. //todo get gateway ipBroadcast lightcontroller... list
  36. return &model.LightControlDetail{
  37. LightControl: *device,
  38. AlarmTerminalList: nil,
  39. CameraList: nil,
  40. CaptureUnitList: nil,
  41. GatewayList: nil,
  42. InfoBoardList: nil,
  43. IpBroadcastList: nil,
  44. LightControlList: nil,
  45. SensorList: nil,
  46. ZigbeeList: nil,
  47. }, nil
  48. }
  49. func (s *lightControlService) CreateOrUpdate(req *dao.LightControl) *utils.Errors {
  50. device := req
  51. if device.TenantId == "" {
  52. device.TenantId = "000000" // todo: 使用登录态
  53. }
  54. device.UpdateUser = "TODO" // todo: 使用登录态
  55. device.UpdateTime = time.Now()
  56. if device.LampPoleId != 0 {
  57. lampPole, err := LampPoleService.GetOne(device.LampPoleId)
  58. if err == nil {
  59. device.LampLat = lampPole.PoleLat
  60. device.LampLng = lampPole.PoleLng
  61. device.LampPoleName = lampPole.PoleName
  62. device.LampPoleSN = lampPole.PoleSN
  63. device.LampPoleLocation = lampPole.InstallLocation
  64. device.GroupId = lampPole.GroupId
  65. } else {
  66. fmt.Printf("LampPoleService.GetOne err = %v \n", err)
  67. }
  68. }
  69. if device.ControlType == model.ControlType_ZigBee {
  70. if device.ZigbeeId == 0 {
  71. return utils.ParamsInvalidResponse(model.ZigbeeSelect, nil)
  72. }
  73. if device.IsOnDemand == 0 {
  74. if device.ControlNO == "" {
  75. return utils.ParamsInvalidResponse(model.ControlNONull, nil)
  76. }
  77. if !checkControlNoIsCompliance(device.ControlNO) {
  78. return utils.ParamsInvalidResponse(model.ControlNOInvalid, nil)
  79. }
  80. }
  81. //todo zigbee
  82. // Zigbee zigbee = zigbeeService.getById(lightControl.getZigbeeId());
  83. // lightControl.setZigbeeName(zigbee.getName());
  84. // lightControl.setZigbeeSn(zigbee.getSn());
  85. // lightControl.setChanelNum(zigbee.getChanelNum());
  86. // lightControl.setNetworkNum(zigbee.getNetworkNum());
  87. }
  88. if device.ID == 0 {
  89. device.CreateTime = time.Now()
  90. device.CreateUser = "TODO" // todo: 使用登录态
  91. if device.IsExistedBySN() {
  92. fmt.Printf("Create IsExistedBySN \n")
  93. return utils.ParamsInvalidResponse(model.RepeatedPrompts, nil)
  94. }
  95. fmt.Printf("device = %+v \n", device)
  96. if err := device.Create(); err != nil {
  97. fmt.Printf("Create err = %s \n", err.Error())
  98. return utils.FailResponse(err.Error(), nil)
  99. }
  100. return utils.SuccessResponse(utils.Succeeded, nil)
  101. }
  102. if err := device.Update(); err != nil {
  103. fmt.Printf("Update err = %s \n", err.Error())
  104. return utils.FailResponse(err.Error(), nil)
  105. }
  106. //todo operation record
  107. return utils.SuccessResponse(utils.Succeeded, nil)
  108. }
  109. func (s *lightControlService) List(searchValue, controlType, zigbeeId string, current, size int) ([]dao.LightControl,
  110. *utils.Errors) {
  111. device := dao.LightControl{}
  112. if searchValue != "" {
  113. device.SN = searchValue
  114. }
  115. if controlType != "" {
  116. cType, err := strconv.Atoi(controlType)
  117. if err == nil {
  118. device.ControlType = cType
  119. }
  120. }
  121. if zigbeeId != "" {
  122. zId, err := strconv.Atoi(zigbeeId)
  123. if err == nil {
  124. device.ZigbeeId = zId
  125. }
  126. }
  127. offset := (current - 1) * size
  128. limit := size
  129. devices, err := device.GetDevices(offset, limit)
  130. if err != nil {
  131. return nil, utils.FailResponse(err.Error(), nil)
  132. }
  133. return devices, nil
  134. }
  135. func (s *lightControlService) Remove(id int) *utils.Errors {
  136. // 创建查询实例
  137. device := &dao.LightControl{
  138. ID: id,
  139. IsDeleted: 1,
  140. UpdateUser: "TODO", // todo 使用登录态
  141. UpdateTime: time.Now(),
  142. }
  143. //todo
  144. // service.lampPoleService.CountRelation()
  145. //todo operation record
  146. err := device.Delete()
  147. if err != nil {
  148. return utils.FailResponse(err.Error(), nil)
  149. }
  150. return nil
  151. }
  152. func (s *lightControlService) GetList() ([]*dao.LightControl, *utils.Errors) {
  153. // todo use redis cache
  154. device := &dao.LightControl{
  155. TenantId: "000000", // todo 使用登录态
  156. IsDeleted: 0,
  157. }
  158. devices, err := device.GetAllDevices()
  159. if err != nil {
  160. return nil, utils.FailResponse(err.Error(), nil)
  161. }
  162. return devices, nil
  163. }
  164. func (s *lightControlService) Enable(id, status int) *utils.Errors {
  165. // 创建查询实例
  166. device := &dao.LightControl{
  167. ID: id,
  168. IsEnable: status,
  169. }
  170. err := device.UpdateEnable()
  171. if err != nil {
  172. return utils.FailResponse(err.Error(), nil)
  173. }
  174. return nil
  175. }
  176. func (s *lightControlService) GetOne(id int) (*dao.LightControl, *utils.Errors) {
  177. device := &dao.LightControl{
  178. ID: id,
  179. }
  180. err := device.GetDevice()
  181. if err != nil {
  182. return nil, utils.FailResponse(err.Error(), nil)
  183. }
  184. return device, nil
  185. }
  186. //检查灯控编号是否合规1-1——255-255
  187. func checkControlNoIsCompliance(controlNo string) bool {
  188. arr := strings.Split(controlNo, "-")
  189. if len(arr) != 2 {
  190. return false
  191. }
  192. one, err := strconv.Atoi(arr[0])
  193. if err != nil || one < 1 || one > 255 {
  194. return false
  195. }
  196. two, err := strconv.Atoi(arr[1])
  197. if err != nil || two < 1 || two > 255 {
  198. return false
  199. }
  200. return true
  201. }