Просмотр исходного кода

Trying to add link battery level in freeaps-x

(cherry picked from commit 294491495858676825bbb8272d8aed4266902a58)
Jon Mårtensson 5 лет назад
Родитель
Сommit
29ad9f8687

+ 45 - 0
Dependecies/rileylink_ios/RileyLinkBLEKit/PeripheralManager+RileyLink.swift

@@ -19,6 +19,7 @@ extension CBUUIDRawValue where RawValue == String {
 
 enum RileyLinkServiceUUID: String, CBUUIDRawValue {
     case main = "0235733B-99C5-4197-B856-69219C2A3845"
+    case battery = "180F"
 }
 
 enum MainServiceCharacteristicUUID: String, CBUUIDRawValue {
@@ -30,6 +31,12 @@ enum MainServiceCharacteristicUUID: String, CBUUIDRawValue {
     case ledMode         = "C6D84241-F1A7-4F9C-A25F-FCE16732F14E"
 }
 
+
+enum BatteryServiceCharacteristicUUID: String, CBUUIDRawValue {
+    case battery_level   = "2A19"
+}
+
+
 enum RileyLinkLEDMode: UInt8 {
     case off  = 0x00
     case on   = 0x01
@@ -48,6 +55,9 @@ extension PeripheralManager.Configuration {
                     MainServiceCharacteristicUUID.timerTick.cbUUID,
                     MainServiceCharacteristicUUID.firmwareVersion.cbUUID,
                     MainServiceCharacteristicUUID.ledMode.cbUUID
+            ],
+            RileyLinkServiceUUID.battery.cbUUID: [
+                BatteryServiceCharacteristicUUID.battery_level.cbUUID
                 ]
             ],
             notifyingCharacteristics: [
@@ -82,6 +92,18 @@ fileprivate extension CBPeripheral {
     }
 }
 
+fileprivate extension CBPeripheral {
+    func getBatteryCharacteristic(_ uuid: BatteryServiceCharacteristicUUID, serviceUUID: RileyLinkServiceUUID = .battery) -> CBCharacteristic? {
+        guard let service = services?.itemWithUUID(serviceUUID.cbUUID) else {
+            return nil
+        }
+
+        return service.characteristics?.itemWithUUID(uuid.cbUUID)
+    }
+}
+
+
+
 
 extension CBCentralManager {
     func scanForPeripherals(withOptions options: [String: Any]? = nil) {
@@ -90,6 +112,8 @@ extension CBCentralManager {
 }
 
 
+
+
 extension Command {
     /// Encodes a command's data by validating and prepending its length
     ///
@@ -290,9 +314,30 @@ extension PeripheralManager {
             throw RileyLinkDeviceError.peripheralManagerError(error)
         }
     }
+
+    func readBatteryLevel(timeout: TimeInterval) throws -> String {
+        guard let characteristic = peripheral.getBatteryCharacteristic(.battery_level) else {
+            throw RileyLinkDeviceError.peripheralManagerError(.unknownCharacteristic)
+        }
+
+        do {
+            guard let data = try readValue(for: characteristic, timeout: timeout) else {
+                // TODO: This is an "unknown value" issue, not a timeout
+                throw RileyLinkDeviceError.peripheralManagerError(.timeout)
+            }
+
+            let battery_level = "\(data[0])"
+
+            return battery_level
+        } catch let error as PeripheralManagerError {
+            throw RileyLinkDeviceError.peripheralManagerError(error)
+        }
+    }
 }
 
 
+
+
 // MARK: - Lower-level helper operations
 extension PeripheralManager {
 

+ 7 - 0
Dependecies/rileylink_ios/RileyLinkBLEKit/RileyLinkDevice.swift

@@ -93,6 +93,13 @@ extension RileyLinkDevice {
         manager.setCustomName(name)
     }
     
+    public func getBatterylevel() -> String {
+        do {
+            return try! manager.readBatteryLevel(timeout: 1)
+        } catch {
+        }
+    }
+       
     public func enableBLELEDs() {
         manager.setLEDMode(mode: .on)
     }

+ 37 - 0
Dependecies/rileylink_ios/RileyLinkKitUI/RileyLinkDeviceTableViewController.swift

@@ -42,6 +42,17 @@ public class RileyLinkDeviceTableViewController: UITableViewController {
         }
     }
     
+    private var battery: String? {
+        didSet {
+            guard isViewLoaded else {
+                return
+               }
+            
+            cellForRow(.battery)?.setDetailBatteryLevel(battery)
+        }
+    }
+       
+    
     private var frequency: Measurement<UnitFrequency>? {
         didSet {
             guard isViewLoaded else {
@@ -114,6 +125,19 @@ public class RileyLinkDeviceTableViewController: UITableViewController {
         }
     }
     
+    func updateBatteryLevel() {
+        device.runSession(withName: "Get battery level") { (session) in
+            do {
+                let batteryLevel = try self.device.getBatterylevel()
+                DispatchQueue.main.async {
+                    self.battery = batteryLevel
+                }
+            } catch {
+            }
+        }
+    }
+       
+    
     func updateFrequency() {
 
         device.runSession(withName: "Get base frequency") { (session) in
@@ -185,6 +209,8 @@ public class RileyLinkDeviceTableViewController: UITableViewController {
 
         updateUptime()
         
+        updateBatteryLevel()
+        
     }
     
     public override func viewWillDisappear(_ animated: Bool) {
@@ -237,6 +263,7 @@ public class RileyLinkDeviceTableViewController: UITableViewController {
         case connection
         case uptime
         case frequency
+        case battery
     }
 
     private func cellForRow(_ row: DeviceRow) -> UITableViewCell? {
@@ -289,6 +316,9 @@ public class RileyLinkDeviceTableViewController: UITableViewController {
             case .frequency:
                 cell.textLabel?.text = LocalizedString("Frequency", comment: "The title of the cell showing current rileylink frequency")
                 cell.setDetailFrequency(frequency, formatter: frequencyFormatter)
+            case .battery:
+                cell.textLabel?.text = NSLocalizedString("Battery level", comment: "The title of the cell showing battery level")
+                cell.setDetailBatteryLevel(battery)
             }
         case .commands:
             cell.accessoryType = .disclosureIndicator
@@ -410,4 +440,11 @@ private extension UITableViewCell {
         }
     }
 
+    func setDetailBatteryLevel(_ batteryLevel: String?) {
+        if let unwrappedBatteryLevel = batteryLevel {
+            detailTextLabel?.text = unwrappedBatteryLevel + " %"
+        } else {
+            detailTextLabel?.text = ""
+        }
+    }
 }