iot_list.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package tower
  2. import (
  3. "context"
  4. "github.com/jaryhe/gopkgs/database"
  5. "smart-site-management/errors"
  6. "smart-site-management/model"
  7. "smart-site-management/pb/v1"
  8. )
  9. // iot 设备列表
  10. func IotList(ctx context.Context, req *v1.IotListRequest)(reply *v1.IotListReply, err error) {
  11. if req.Page == 0 {
  12. req.Page = 1
  13. }
  14. if req.PageSize == 0 {
  15. req.PageSize = 10
  16. }
  17. if req.DeviceCode == 0 {
  18. return nil, errors.ParamsError
  19. }
  20. if req.ProjectId == 0 {
  21. return nil, errors.ParamsError
  22. }
  23. // 总数
  24. reply = &v1.IotListReply{}
  25. p := model.TDevice{}
  26. where := map[string]interface{}{
  27. "device_code":req.DeviceCode,
  28. "verify_status":1,
  29. "project_id":req.ProjectId,
  30. }
  31. total, err := p.Count(database.DB(), where)
  32. if err != nil {
  33. return nil, errors.DataBaseError
  34. }
  35. reply.PageSize = req.PageSize
  36. reply.Page = req.Page
  37. reply.Total = total
  38. if total == 0{
  39. return reply, nil
  40. }
  41. // 在线离线数
  42. where["status"] = 1
  43. p.Id = 0
  44. online, err := p.Count(database.DB(), where)
  45. if err != nil {
  46. return nil, errors.DataBaseError
  47. }
  48. reply.Online = online
  49. reply.Offline = total - online
  50. p.Id = 0
  51. where = map[string]interface{}{
  52. "device_code":req.DeviceCode,
  53. "verify_status":1,
  54. "project_id":req.ProjectId,
  55. }
  56. // 列表
  57. list, err := p.List(database.DB(), where, req.Page, req.PageSize)
  58. if err != nil {
  59. return nil, errors.DataBaseError
  60. }
  61. reply.List = make([]*v1.IotListItem, len(list))
  62. for i, v := range list {
  63. item := &v1.IotListItem{
  64. State:int32(v.Status),
  65. Id:v.Id,
  66. Name:v.Name,
  67. Sn:v.Sn,
  68. }
  69. reply.List[i] = item
  70. }
  71. return reply, nil
  72. }