CommandResponseViewController.swift 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. //
  2. // CommandResponseViewController.swift
  3. // RileyLinkKitUI
  4. //
  5. // Created by Pete Schwamb on 7/19/21.
  6. // Copyright © 2021 Pete Schwamb. All rights reserved.
  7. //
  8. import Foundation
  9. import LoopKitUI
  10. import RileyLinkBLEKit
  11. extension CommandResponseViewController {
  12. typealias T = CommandResponseViewController
  13. static func getStatistics(device: RileyLinkDevice) -> T {
  14. return T { (completionHandler) -> String in
  15. device.runSession(withName: "Get Statistics") { session in
  16. let response: String
  17. do {
  18. let stats = try session.getRileyLinkStatistics()
  19. response = String(describing: stats)
  20. } catch let error {
  21. response = String(describing: error)
  22. }
  23. DispatchQueue.main.async {
  24. completionHandler(response)
  25. }
  26. }
  27. return LocalizedString("Get Statistics…", comment: "Progress message for getting statistics.")
  28. }
  29. }
  30. static func setDiagnosticLEDMode(device: RileyLinkDevice, mode: RileyLinkLEDMode) -> T {
  31. return T { (completionHandler) -> String in
  32. device.setDiagnosticeLEDModeForBLEChip(mode)
  33. device.runSession(withName: "Update diagnostic LED mode") { session in
  34. let response: String
  35. do {
  36. try session.setCCLEDMode(mode)
  37. switch mode {
  38. case .on:
  39. response = "Diagnostic mode enabled"
  40. default:
  41. response = "Diagnostic mode disabled"
  42. }
  43. } catch let error {
  44. response = String(describing: error)
  45. }
  46. DispatchQueue.main.async {
  47. completionHandler(response)
  48. }
  49. }
  50. return LocalizedString("Updating diagnostic LEDs mode", comment: "Progress message for changing diagnostic LED mode")
  51. }
  52. }
  53. }