devicefarm.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "go-common/app/admin/ep/merlin/model"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. "github.com/satori/go.uuid"
  10. )
  11. //SyncMobileDeviceList Sync Device Farm List .
  12. func (s *Service) SyncMobileDeviceList(c context.Context) (uid string, err error) {
  13. uid = uuid.NewV4().String()
  14. s.deviceChan.Do(c, func(c context.Context) {
  15. var (
  16. startCnt int
  17. status int
  18. totalCnt int
  19. addCnt int
  20. updateCnt int
  21. delCnt int
  22. err error
  23. )
  24. if startCnt, err = s.dao.FindMobileSyncLogStartStatus(); err != nil {
  25. return
  26. }
  27. if startCnt > 0 {
  28. return
  29. }
  30. mobileSyncLog := &model.MobileSyncLog{
  31. UUID: uid,
  32. }
  33. s.dao.InsertMobileSyncLog(mobileSyncLog)
  34. if totalCnt, addCnt, updateCnt, delCnt, err = s.SyncMobileDeviceListInChan(uid); err != nil {
  35. status = -1
  36. } else {
  37. status = 1
  38. }
  39. mobileSyncLog = &model.MobileSyncLog{
  40. UUID: uid,
  41. TotalCnt: totalCnt,
  42. AddCnt: addCnt,
  43. UpdateCnt: updateCnt,
  44. DeleteCnt: delCnt,
  45. Status: status,
  46. }
  47. s.dao.UpdateMobileSyncLog(mobileSyncLog)
  48. })
  49. return
  50. }
  51. // SyncMobileDeviceListInChan Sync Mobile Device List In Chan.
  52. func (s *Service) SyncMobileDeviceListInChan(uuid string) (totalCnt, addCnt, updateCnt, delCnt int, err error) {
  53. var (
  54. resTotal map[string][]*model.Device
  55. )
  56. if resTotal, err = s.dao.MobileDeviceList(context.Background()); err != nil {
  57. log.Error("SyncMobileDeviceListInChan httpClient err(%+v)", err)
  58. err = nil
  59. }
  60. if len(resTotal) == 0 {
  61. return
  62. }
  63. for host, devices := range resTotal {
  64. totalCnt = totalCnt + len(devices)
  65. for _, device := range devices {
  66. var deviceType int
  67. var tmpMobileMachine *model.MobileMachine
  68. if device.IsSimulator {
  69. deviceType = model.MBSimulator
  70. }
  71. if tmpMobileMachine, err = s.dao.FindMobileMachineBySerial(device.Serial); err != nil {
  72. log.Error("d.SyncMobileDeviceList FindMobileMachineBySerial err(%+v)", err)
  73. continue
  74. }
  75. mobileMachine := &model.MobileMachine{
  76. Serial: device.Serial,
  77. Name: device.Name,
  78. CPU: device.CPU,
  79. Version: device.Version,
  80. Mode: device.Mode,
  81. State: device.State,
  82. Host: host,
  83. Type: deviceType,
  84. Action: model.MBOnline,
  85. UUID: uuid,
  86. }
  87. if tmpMobileMachine.ID == 0 {
  88. if err = s.dao.InsertMobileMachine(mobileMachine); err != nil {
  89. log.Error("d.SyncMobileDeviceList InsertMobileMachine err(%+v)", err)
  90. } else {
  91. addCnt = addCnt + 1
  92. }
  93. } else {
  94. if err = s.dao.UpdateMobileMachine(mobileMachine); err != nil {
  95. log.Error("d.SyncMobileDeviceList UpdateMobileMachine err(%+v)", err)
  96. } else {
  97. updateCnt = updateCnt + 1
  98. }
  99. }
  100. }
  101. }
  102. s.dao.DeleteMobileMachineNotInHost(s.c.DeviceFarm.HostList)
  103. delCnt, err = s.dao.DeleteMobileMachineByUUID(uuid)
  104. return
  105. }
  106. // QueryMobileDeviceList Query Mobile Device List.
  107. func (s *Service) QueryMobileDeviceList(c context.Context, queryRequest *model.QueryMobileDeviceRequest) (p *model.PaginateMobileMachines, err error) {
  108. var (
  109. total int64
  110. mobileMachines []*model.MobileMachine
  111. mobileMachinesResponse []*model.MobileMachineResponse
  112. )
  113. if total, mobileMachines, err = s.dao.FindMobileMachines(queryRequest); err != nil {
  114. return
  115. }
  116. for _, mobileMachine := range mobileMachines {
  117. var mobileMachineResponse model.MobileMachineResponse
  118. mobileMachineResponse.MobileMachine = mobileMachine
  119. mobileMachinesResponse = append(mobileMachinesResponse, &mobileMachineResponse)
  120. }
  121. if err = s.getMobileImages(mobileMachinesResponse); err != nil {
  122. return
  123. }
  124. p = &model.PaginateMobileMachines{
  125. PageNum: queryRequest.PageNum,
  126. PageSize: queryRequest.PageSize,
  127. Total: total,
  128. MobileMachines: mobileMachinesResponse,
  129. }
  130. return
  131. }
  132. // RefreshMobileDeviceDetail Refresh Mobile Device Detail.
  133. func (s *Service) RefreshMobileDeviceDetail(c context.Context, deviceID int64) (mobileMachineResponse *model.MobileMachineResponse, err error) {
  134. var (
  135. device *model.Device
  136. mobileMachine *model.MobileMachine
  137. )
  138. if mobileMachine, err = s.dao.FindMobileMachineByID(deviceID); err != nil {
  139. return
  140. }
  141. if device, err = s.dao.MobileDeviceDetail(c, mobileMachine.Host, mobileMachine.Serial); err != nil {
  142. return
  143. }
  144. //update db
  145. mobileMachine.State = device.State
  146. mobileMachine.Action = 1
  147. if err = s.dao.UpdateMobileMachine(mobileMachine); err != nil {
  148. log.Error("d.RefreshMobileDeviceDetail err(%+v)", err)
  149. return
  150. }
  151. mobileMachineResponse = &model.MobileMachineResponse{}
  152. mobileMachineResponse.MobileMachine = mobileMachine
  153. if err = s.getMobileImage(mobileMachineResponse); err != nil {
  154. return
  155. }
  156. return
  157. }
  158. // MobileMachineCategory Mobile Machine Category.
  159. func (s *Service) MobileMachineCategory(c context.Context, isShowOffline bool) (categoryList []*model.MobileDeviceCategoryResponse, err error) {
  160. var mobileCategory *model.MobileCategory
  161. if mobileCategory, err = s.dao.FindMobileMachineCategory(isShowOffline); err != nil {
  162. return
  163. }
  164. interfaceCPU := make([]interface{}, len(mobileCategory.CPUs))
  165. for i, v := range mobileCategory.CPUs {
  166. interfaceCPU[i] = v
  167. }
  168. categoryCPU := &model.MobileDeviceCategoryResponse{
  169. Name: "cpu",
  170. Label: "CPU",
  171. Values: interfaceCPU,
  172. }
  173. interfaceVersion := make([]interface{}, len(mobileCategory.Versions))
  174. for i, v := range mobileCategory.Versions {
  175. interfaceVersion[i] = v
  176. }
  177. categoryVersion := &model.MobileDeviceCategoryResponse{
  178. Name: "version",
  179. Label: "版本",
  180. Values: interfaceVersion,
  181. }
  182. interfaceMode := make([]interface{}, len(mobileCategory.Modes))
  183. for i, v := range mobileCategory.Modes {
  184. interfaceMode[i] = v
  185. }
  186. categoryMode := &model.MobileDeviceCategoryResponse{
  187. Name: "mode",
  188. Label: "型号",
  189. Values: interfaceMode,
  190. }
  191. interfaceType := make([]interface{}, len(mobileCategory.Types))
  192. for i, v := range mobileCategory.Types {
  193. interfaceType[i] = v
  194. }
  195. categoryType := &model.MobileDeviceCategoryResponse{
  196. Name: "type",
  197. Label: "是否真机",
  198. Values: interfaceType,
  199. }
  200. interfaceState := make([]interface{}, len(mobileCategory.States))
  201. for i, v := range mobileCategory.States {
  202. interfaceState[i] = v
  203. }
  204. categoryState := &model.MobileDeviceCategoryResponse{
  205. Name: "state",
  206. Label: "状态",
  207. Values: interfaceState,
  208. }
  209. interfaceUsage := make([]interface{}, len(mobileCategory.Usages))
  210. for i, v := range mobileCategory.Usages {
  211. interfaceUsage[i] = v
  212. }
  213. categoryUsage := &model.MobileDeviceCategoryResponse{
  214. Name: "usage",
  215. Label: "使用情况",
  216. Values: interfaceUsage,
  217. }
  218. categoryList = append(categoryList, categoryCPU)
  219. categoryList = append(categoryList, categoryVersion)
  220. categoryList = append(categoryList, categoryMode)
  221. categoryList = append(categoryList, categoryType)
  222. categoryList = append(categoryList, categoryState)
  223. categoryList = append(categoryList, categoryUsage)
  224. return
  225. }
  226. // BindMobileDevice Bind Mobile Device.
  227. func (s *Service) BindMobileDevice(c context.Context, deviceID int64, username string) (status int, err error) {
  228. var mobileMachine *model.MobileMachine
  229. status = -1
  230. if mobileMachine, err = s.dao.FindMobileMachineByID(deviceID); err != nil {
  231. return
  232. }
  233. if mobileMachine.Action < model.MBOnline {
  234. err = ecode.MerlinDeviceFarmMachineStatusErr
  235. return
  236. }
  237. //真机借出状态, 无法操作
  238. if mobileMachine.Type == model.MBReal && mobileMachine.IsLendOut == model.MBLendOut {
  239. err = ecode.MerlinDeviceIsLendOut
  240. return
  241. }
  242. if mobileMachine.Username != "" {
  243. return
  244. }
  245. mobileMachine.Username = username
  246. mobileMachine.EndTime = time.Now().AddDate(0, s.c.DeviceFarm.ApplyMonthTime, 0)
  247. mobileMachine.LastBindTime = time.Now()
  248. if err = s.dao.UpdateMobileMachine(mobileMachine); err != nil {
  249. return
  250. }
  251. if err = s.addMobileDeviceLog(deviceID, username, model.MBBindLog); err != nil {
  252. return
  253. }
  254. status = 0
  255. return
  256. }
  257. // ReleaseMobileDevice Release Mobile Device.
  258. func (s *Service) ReleaseMobileDevice(c context.Context, deviceID int64, username string) (status int, err error) {
  259. var mobileMachine *model.MobileMachine
  260. status = -1
  261. if mobileMachine, err = s.isAccessToMobileDevice(deviceID, username); err != nil {
  262. return
  263. }
  264. mobileMachine.Username = ""
  265. if mobileMachine.EndTime, err = time.ParseInLocation(model.TimeFormat, "1990-01-01 01:01:01", time.Local); err != nil {
  266. return
  267. }
  268. if err = s.dao.UpdateMobileMachineByRelease(mobileMachine); err != nil {
  269. return
  270. }
  271. if err = s.addMobileDeviceLog(deviceID, username, model.MBReleaseLog); err != nil {
  272. return
  273. }
  274. status = 0
  275. return
  276. }
  277. // LendOutMobileDevice Pull Out Mobile Device.
  278. func (s *Service) LendOutMobileDevice(c context.Context, deviceID int64, username string) (status int, err error) {
  279. var (
  280. mobileMachine *model.MobileMachine
  281. )
  282. status = -1
  283. if mobileMachine, err = s.isAccessToMobileDevice(deviceID, username); err != nil {
  284. return
  285. }
  286. //只有真机支持借出 真机type=0
  287. if mobileMachine.Type != model.MBReal {
  288. err = ecode.MerlinDeviceIsNotRealMachineErr
  289. return
  290. }
  291. if err = s.dao.LendOutMobileMachine(deviceID, mobileMachine.Serial, username); err != nil {
  292. return
  293. }
  294. status = 0
  295. return
  296. }
  297. // ReturnMobileDevice Return Mobile Device.
  298. func (s *Service) ReturnMobileDevice(c context.Context, deviceID int64, username string) (status int, err error) {
  299. var (
  300. mobileMachine *model.MobileMachine
  301. isSuperOwner = false
  302. )
  303. status = -1
  304. for _, superOwner := range s.c.DeviceFarm.SuperOwner {
  305. if username == superOwner {
  306. isSuperOwner = true
  307. break
  308. }
  309. }
  310. if !isSuperOwner {
  311. err = ecode.MerlinDeviceNoRight
  312. return
  313. }
  314. if mobileMachine, err = s.dao.FindMobileMachineByID(deviceID); err != nil {
  315. return
  316. }
  317. if mobileMachine.IsLendOut == model.MBOnSite {
  318. err = ecode.MerlinDeviceFarmErr
  319. return
  320. }
  321. if err = s.dao.ReturnMobileMachine(deviceID, mobileMachine.Serial, username); err != nil {
  322. return
  323. }
  324. status = 0
  325. return
  326. }
  327. // StartMobileDevice Start Mobile Device.
  328. func (s *Service) StartMobileDevice(c context.Context, deviceID int64, username string) (deviceBootData *model.DeviceBootData, err error) {
  329. var (
  330. mobileMachine *model.MobileMachine
  331. device *model.Device
  332. )
  333. if mobileMachine, err = s.isAccessToMobileDevice(deviceID, username); err != nil {
  334. return
  335. }
  336. if deviceBootData, err = s.dao.BootMobileDevice(c, mobileMachine.Host, mobileMachine.Serial); err != nil {
  337. return
  338. }
  339. if device, err = s.dao.MobileDeviceDetail(c, mobileMachine.Host, mobileMachine.Serial); err != nil {
  340. return
  341. }
  342. mobileMachine.State = device.State
  343. mobileMachine.WsURL = deviceBootData.WSRUL
  344. mobileMachine.UploadURL = deviceBootData.UploadURL
  345. if err = s.dao.UpdateMobileMachineWsurlAndState(mobileMachine); err != nil {
  346. return
  347. }
  348. err = s.addMobileDeviceLog(deviceID, username, model.MBStartLog)
  349. return
  350. }
  351. // ShutdownMobileDevice Shutdown Mobile Device.
  352. func (s *Service) ShutdownMobileDevice(c context.Context, deviceID int64, username string) (status int, err error) {
  353. var (
  354. mobileMachine *model.MobileMachine
  355. device *model.Device
  356. )
  357. status = -1
  358. if mobileMachine, err = s.isAccessToMobileDevice(deviceID, username); err != nil {
  359. return
  360. }
  361. if err = s.dao.ShutdownMobileDevice(c, mobileMachine.Host, mobileMachine.Serial); err != nil {
  362. return
  363. }
  364. if device, err = s.dao.MobileDeviceDetail(c, mobileMachine.Host, mobileMachine.Serial); err != nil {
  365. return
  366. }
  367. mobileMachine.State = device.State
  368. mobileMachine.WsURL = ""
  369. mobileMachine.UploadURL = ""
  370. if err = s.dao.UpdateMobileMachineWsurlAndState(mobileMachine); err != nil {
  371. return
  372. }
  373. if err = s.addMobileDeviceLog(deviceID, username, model.MBShutDownLog); err != nil {
  374. return
  375. }
  376. status = 0
  377. return
  378. }
  379. func (s *Service) isAccessToMobileDevice(deviceID int64, username string) (mobileMachine *model.MobileMachine, err error) {
  380. if mobileMachine, err = s.dao.FindMobileMachineByID(deviceID); err != nil {
  381. return
  382. }
  383. if mobileMachine.Action < model.MBOnline {
  384. err = ecode.MerlinDeviceFarmMachineStatusErr
  385. return
  386. }
  387. //真机借出状态, 无法操作
  388. if mobileMachine.Type == model.MBReal && mobileMachine.IsLendOut == model.MBLendOut {
  389. err = ecode.MerlinDeviceIsLendOut
  390. return
  391. }
  392. for _, superOwner := range s.c.DeviceFarm.SuperOwner {
  393. if username == superOwner {
  394. return
  395. }
  396. }
  397. if mobileMachine.Username != username {
  398. err = ecode.MerlinDeviceNotBind
  399. }
  400. return
  401. }
  402. func (s *Service) addMobileDeviceLog(deviceID int64, username, operateType string) (err error) {
  403. machineLog := &model.MobileMachineLog{
  404. OperateType: operateType,
  405. Username: username,
  406. MachineID: deviceID,
  407. OperateResult: model.OperationSuccessForMachineLog,
  408. }
  409. return s.dao.InsertMobileMachineLog(machineLog)
  410. }
  411. func (s *Service) getMobileImages(mobileMachinesResponse []*model.MobileMachineResponse) (err error) {
  412. var mobileImages []*model.MobileImage
  413. if mobileImages, err = s.dao.FindAllMobileImages(); err != nil {
  414. return
  415. }
  416. for _, mobileMachineResponse := range mobileMachinesResponse {
  417. for _, mobileImage := range mobileImages {
  418. if mobileImage.Mode == mobileMachineResponse.Mode {
  419. mobileMachineResponse.ImageSrc = mobileImage.ImageSrc
  420. continue
  421. }
  422. }
  423. }
  424. return
  425. }
  426. func (s *Service) getMobileImage(mobileMachinesResponse *model.MobileMachineResponse) (err error) {
  427. var mobileImage *model.MobileImage
  428. if mobileImage, err = s.dao.FindMobileImageByMode(mobileMachinesResponse.Mode); err != nil {
  429. return
  430. }
  431. mobileMachinesResponse.ImageSrc = mobileImage.ImageSrc
  432. return
  433. }
  434. // QueryMobileMachineLogs Query Mobile Machine Logs.
  435. func (s *Service) QueryMobileMachineLogs(c context.Context, queryRequest *model.QueryMobileMachineLogRequest) (p *model.PaginateMobileMachineLog, err error) {
  436. var (
  437. total int64
  438. machineLogs []*model.AboundMobileMachineLog
  439. )
  440. if total, machineLogs, err = s.dao.FindMobileMachineLogs(queryRequest); err != nil {
  441. return
  442. }
  443. p = &model.PaginateMobileMachineLog{
  444. PageNum: queryRequest.PageNum,
  445. PageSize: queryRequest.PageSize,
  446. Total: total,
  447. MachineLogs: machineLogs,
  448. }
  449. return
  450. }
  451. // QueryMobileMachineLendOutLogs Query Mobile Machine Lend Out Logs.
  452. func (s *Service) QueryMobileMachineLendOutLogs(c context.Context, username string, queryRequest *model.QueryMobileMachineLogRequest) (p *model.PaginateMobileMachineLendOutLog, err error) {
  453. var (
  454. total int64
  455. machineLogs []*model.AboundMobileMachineLog
  456. machineLendOutRecords []*model.MachineLendOutRecord
  457. mobileMachine *model.MobileMachine
  458. existMachineIDs []int64
  459. isSuperUser bool
  460. )
  461. isSuperUser = s.isSuperUser(username)
  462. queryRequest.OperateType = model.MBLendOutLog
  463. if total, machineLogs, err = s.dao.FindMobileMachineLogs(queryRequest); err != nil {
  464. return
  465. }
  466. for _, machineLog := range machineLogs {
  467. var (
  468. enableReturn bool
  469. isExistLendRecord bool
  470. )
  471. if mobileMachine, err = s.dao.FindMobileMachineByID(machineLog.MachineID); err != nil {
  472. return
  473. }
  474. for _, existMachineID := range existMachineIDs {
  475. if existMachineID == machineLog.MachineID {
  476. isExistLendRecord = true
  477. break
  478. }
  479. }
  480. //移动设备借出状态 且 用户是超级用户 才有资格归还手机 只有最新一条记录 才能点击返还
  481. if isExistLendRecord {
  482. machineLendOutRecord := &model.MachineLendOutRecord{
  483. MachineID: machineLog.MachineID,
  484. Lender: machineLog.Username,
  485. LendTime: machineLog.OperateTime,
  486. EnableReturn: false,
  487. Status: model.MBOnSite,
  488. }
  489. machineLendOutRecords = append(machineLendOutRecords, machineLendOutRecord)
  490. } else {
  491. if isSuperUser && (mobileMachine.IsLendOut == model.MBLendOut) {
  492. enableReturn = true
  493. }
  494. machineLendOutRecord := &model.MachineLendOutRecord{
  495. MachineID: machineLog.MachineID,
  496. Lender: machineLog.Username,
  497. LendTime: machineLog.OperateTime,
  498. EnableReturn: enableReturn,
  499. Status: mobileMachine.IsLendOut,
  500. }
  501. machineLendOutRecords = append(machineLendOutRecords, machineLendOutRecord)
  502. existMachineIDs = append(existMachineIDs, machineLog.MachineID)
  503. }
  504. }
  505. p = &model.PaginateMobileMachineLendOutLog{
  506. PageNum: queryRequest.PageNum,
  507. PageSize: queryRequest.PageSize,
  508. Total: total,
  509. MachineLendOutRecords: machineLendOutRecords,
  510. }
  511. return
  512. }
  513. // QueryMobileMachineErrorLogs Query Mobile Machine Error Logs.
  514. func (s *Service) QueryMobileMachineErrorLogs(c context.Context, queryRequest *model.QueryMobileMachineErrorLogRequest) (p *model.PaginateMobileMachineErrorLog, err error) {
  515. var (
  516. total int64
  517. machineLogs []*model.MobileMachineErrorLog
  518. )
  519. if total, machineLogs, err = s.dao.FindMobileMachineErrorLog(queryRequest); err != nil {
  520. return
  521. }
  522. p = &model.PaginateMobileMachineErrorLog{
  523. PageNum: queryRequest.PageNum,
  524. PageSize: queryRequest.PageSize,
  525. Total: total,
  526. MachineLogs: machineLogs,
  527. }
  528. return
  529. }
  530. // CallBackMobileDeviceError Call Back Mobile Device Error.
  531. func (s *Service) CallBackMobileDeviceError(c context.Context, serialName, errMsg string, errCode int) (err error) {
  532. var mobileMachine *model.MobileMachine
  533. if mobileMachine, err = s.dao.FindMobileMachineBySerial(serialName); err != nil {
  534. return
  535. }
  536. mobileMachineErrorLog := &model.MobileMachineErrorLog{
  537. MachineID: mobileMachine.ID,
  538. SerialName: serialName,
  539. ErrorMessage: errMsg,
  540. ErrorCode: errCode,
  541. }
  542. err = s.dao.InsertMobileMachineErrorLog(mobileMachineErrorLog)
  543. return
  544. }
  545. // ReportMobileDeviceError Report Mobile Device Error.
  546. func (s *Service) ReportMobileDeviceError(c context.Context, serialName, errMsg string, errCode int) (err error) {
  547. var mobileMachine *model.MobileMachine
  548. if mobileMachine, err = s.dao.FindMobileMachineBySerial(serialName); err != nil {
  549. return
  550. }
  551. if mobileMachine.ID == 0 {
  552. err = ecode.NothingFound
  553. return
  554. }
  555. content := fmt.Sprintf("通知\n设备ID: %d\n系列号: %s\n型号: %s\n系统版本: %s\n使用者: %s\n错误信息: %s\n错误码: %d", mobileMachine.ID, serialName, mobileMachine.Mode, mobileMachine.Version, mobileMachine.Username, errMsg, errCode)
  556. msgSendReq := &model.MsgSendReq{
  557. Chatid: s.c.WeChat.WeChatDeviceFarm.ChatID,
  558. Msgtype: s.c.WeChat.WeChatDeviceFarm.MsgType,
  559. Text: model.MsgSendReqText{Content: content},
  560. Safe: s.c.WeChat.WeChatDeviceFarm.Safe,
  561. }
  562. if _, err = s.dao.WeChatSendMessage(c, msgSendReq); err != nil {
  563. return
  564. }
  565. mobileMachineErrorLog := &model.MobileMachineErrorLog{
  566. MachineID: mobileMachine.ID,
  567. SerialName: serialName,
  568. ErrorMessage: errMsg,
  569. ErrorCode: errCode,
  570. }
  571. err = s.dao.InsertMobileMachineErrorLog(mobileMachineErrorLog)
  572. return
  573. }
  574. // DeviceFarmSuperUser Device Farm Super User.
  575. func (s *Service) DeviceFarmSuperUser() (responseMap map[string]interface{}) {
  576. responseMap = make(map[string]interface{})
  577. responseMap["superuser_list"] = s.c.DeviceFarm.SuperOwner
  578. return
  579. }
  580. // IsBindMobileDeviceByTheUser Is Bind Mobile Device By The User.
  581. func (s *Service) IsBindMobileDeviceByTheUser(c context.Context, deviceID int64, username string) (isBind bool, err error) {
  582. var mobileMachine *model.MobileMachine
  583. if mobileMachine, err = s.dao.FindMobileMachineByID(deviceID); err != nil {
  584. return
  585. }
  586. if mobileMachine.Username == username {
  587. isBind = true
  588. }
  589. return
  590. }
  591. func (s *Service) isSuperUser(username string) bool {
  592. for _, superOwner := range s.c.DeviceFarm.SuperOwner {
  593. if username == superOwner {
  594. return true
  595. }
  596. }
  597. return false
  598. }