devicefarm.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "go-common/app/admin/ep/merlin/model"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. )
  10. const (
  11. _deviceCode = 0
  12. _devicesListURL = "/apis/devices/list"
  13. _deviceURL = "/apis/devices/get"
  14. _deviceBootURL = "/apis/devices/boot"
  15. _deviceShutDownURL = "/apis/devices/shutdown"
  16. )
  17. // MobileDeviceList Get Device Farm List .
  18. func (d *Dao) MobileDeviceList(c context.Context) (resTotal map[string][]*model.Device, err error) {
  19. var (
  20. req *http.Request
  21. hostList = d.c.DeviceFarm.HostList
  22. )
  23. resTotal = make(map[string][]*model.Device)
  24. for _, host := range hostList {
  25. var res *model.DeviceListResponse
  26. url := fmt.Sprintf("http://%s", host+_devicesListURL)
  27. if req, err = d.newRequest(http.MethodGet, url, nil); err != nil {
  28. log.Error("d.MobileDeviceList newRequest url(%s) err(%+v)", _devicesListURL, err)
  29. continue
  30. }
  31. if err = d.httpClient.Do(c, req, &res); err != nil {
  32. log.Error("d.MobileDeviceList httpClient url(%s) err(%+v)", _devicesListURL, err)
  33. continue
  34. }
  35. if res.Status.Code != _deviceCode {
  36. err = ecode.MerlinDeviceFarmErr
  37. log.Error("Status url(%s) res(%s),err(%+v)", _devicesListURL, res, err)
  38. continue
  39. }
  40. resTotal[host] = res.Data.Devices
  41. }
  42. return
  43. }
  44. // MobileDeviceDetail Get Mobile Device Detail.
  45. func (d *Dao) MobileDeviceDetail(c context.Context, host, serial string) (device *model.Device, err error) {
  46. var (
  47. req *http.Request
  48. res *model.DeviceListDetailResponse
  49. )
  50. url := fmt.Sprintf("http://%s?serial=%s", host+_deviceURL, serial)
  51. if req, err = d.newRequest(http.MethodGet, url, nil); err != nil {
  52. log.Error("d.MobileDeviceDetail newRequest url(%s) err(%+v)", _deviceURL, err)
  53. return
  54. }
  55. if err = d.httpClient.Do(c, req, &res); err != nil {
  56. log.Error("d.MobileDeviceDetail url(%s) err(%+v)", _deviceURL, err)
  57. return
  58. }
  59. if res.Status.Code != _deviceCode {
  60. err = ecode.MerlinDeviceFarmErr
  61. log.Error("Status url(%s) res(%s) err(%+v)", _deviceURL, res, err)
  62. return
  63. }
  64. device = res.Data.Devices
  65. return
  66. }
  67. // BootMobileDevice Boot Mobile Device.
  68. func (d *Dao) BootMobileDevice(c context.Context, host, serial string) (deviceBootData *model.DeviceBootData, err error) {
  69. var (
  70. req *http.Request
  71. res *model.DeviceBootResponse
  72. )
  73. reqModel := &model.DeviceBootRequest{
  74. Serial: serial,
  75. }
  76. url := fmt.Sprintf("http://%s", host+_deviceBootURL)
  77. if req, err = d.newRequest(http.MethodPost, url, reqModel); err != nil {
  78. log.Error("d.BootMobileDevice newRequest url(%s) err(%+v)", _deviceBootURL, err)
  79. return
  80. }
  81. req.Header.Set("content-type", "application/json")
  82. if err = d.httpClient.Do(c, req, &res); err != nil {
  83. log.Error("d.BootMobileDevice url(%s) err(%+v)", _deviceBootURL, err)
  84. return
  85. }
  86. if res.Status.Code != _deviceCode {
  87. err = ecode.MerlinDeviceFarmErr
  88. log.Error("Status url(%s) res(%s) err(%+v)", _deviceBootURL, res, err)
  89. return
  90. }
  91. deviceBootData = res.Data
  92. return
  93. }
  94. // ShutdownMobileDevice Shutdown Mobile Device.
  95. func (d *Dao) ShutdownMobileDevice(c context.Context, host, serial string) (err error) {
  96. var (
  97. req *http.Request
  98. res *model.DeviceShutDownResponse
  99. )
  100. reqModel := &model.DeviceBootRequest{
  101. Serial: serial,
  102. }
  103. url := fmt.Sprintf("http://%s", host+_deviceShutDownURL)
  104. if req, err = d.newRequest(http.MethodPost, url, reqModel); err != nil {
  105. log.Error("d.ShutdownMobileDevice newRequest url(%s) err(%+v)", _deviceShutDownURL, err)
  106. return
  107. }
  108. req.Header.Set("content-type", "application/json")
  109. if err = d.httpClient.Do(c, req, &res); err != nil {
  110. log.Error("d.ShutdownMobileDevice url(%s) err(%+v)", _deviceShutDownURL, err)
  111. return
  112. }
  113. if res.Status.Code != _deviceCode {
  114. err = ecode.MerlinDeviceFarmErr
  115. log.Error("Status url(%s) res(%s) err(%+v)", _deviceShutDownURL, res, err)
  116. return
  117. }
  118. return
  119. }