From 8768b658bf0089ca3e4dc51fdf5930e64c5ee7ac Mon Sep 17 00:00:00 2001 From: Garth Vander Houwen Date: Sat, 4 Mar 2023 11:11:34 -0800 Subject: [PATCH] Low battery notification --- Meshtastic.xcodeproj/project.pbxproj | 4 ++-- Meshtastic/Helpers/MeshPackets.swift | 25 ++++++++++++++++++++++--- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/Meshtastic.xcodeproj/project.pbxproj b/Meshtastic.xcodeproj/project.pbxproj index d6168a1e..0191e8c6 100644 --- a/Meshtastic.xcodeproj/project.pbxproj +++ b/Meshtastic.xcodeproj/project.pbxproj @@ -1187,7 +1187,7 @@ "$(inherited)", "@executable_path/Frameworks", ); - MARKETING_VERSION = 2.0.21; + MARKETING_VERSION = 2.0.22; PRODUCT_BUNDLE_IDENTIFIER = gvh.MeshtasticClient; PRODUCT_NAME = "$(TARGET_NAME)"; SUPPORTS_MACCATALYST = YES; @@ -1221,7 +1221,7 @@ "$(inherited)", "@executable_path/Frameworks", ); - MARKETING_VERSION = 2.0.21; + MARKETING_VERSION = 2.0.22; PRODUCT_BUNDLE_IDENTIFIER = gvh.MeshtasticClient; PRODUCT_NAME = "$(TARGET_NAME)"; SUPPORTS_MACCATALYST = YES; diff --git a/Meshtastic/Helpers/MeshPackets.swift b/Meshtastic/Helpers/MeshPackets.swift index 75b3fb2e..a2fc28de 100644 --- a/Meshtastic/Helpers/MeshPackets.swift +++ b/Meshtastic/Helpers/MeshPackets.swift @@ -681,23 +681,42 @@ func telemetryPacket(packet: MeshPacket, connectedNode: Int64, context: NSManage if connectedNode != Int64(packet.from) { print("💾 Telemetry Saved for Node: \(packet.from)") } else if telemetry.metricsType == 0 { - // Update our live activity if there is one running + // Connected Device Metrics + // ------------------------ + // Low Battery notification + if telemetry.batteryLevel > 0 && telemetry.batteryLevel < 6 { + let content = UNMutableNotificationContent() + content.title = "Critically Low Battery!" + content.body = "Time to charge your radio, there is less than 5% battery remaining." + let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false) + let uuidString = UUID().uuidString + let request = UNNotificationRequest(identifier: uuidString, content: content, trigger: trigger) + let notificationCenter = UNUserNotificationCenter.current() + notificationCenter.add(request) { (error) in + if error != nil { + // Handle any errors. + print("Error creating local low battery notification: \(error?.localizedDescription ?? "no description")") + } else { + print("Created local low battery notification.") + } + } + } + // Update our live activity if there is one running, not available on mac iOS >= 16.2 #if !targetEnvironment(macCatalyst) if #available(iOS 16.2, *) { let oneMinuteLater = Calendar.current.date(byAdding: .minute, value: (Int(1) ), to: Date())! let date = Date.now...oneMinuteLater let updatedMeshStatus = MeshActivityAttributes.MeshActivityStatus(timerRange: date, connected: true, channelUtilization: telemetry.channelUtilization, airtime: telemetry.airUtilTx, batteryLevel: UInt32(telemetry.batteryLevel)) - let alertConfiguration = AlertConfiguration(title: "Mesh activity update", body: "Updated Device Metrics Data.", sound: .default) let updatedContent = ActivityContent(state: updatedMeshStatus, staleDate: nil) - print("Update live activity.") let meshActivity = Activity.activities.first(where: { $0.attributes.nodeNum == connectedNode }) if meshActivity != nil { Task { await meshActivity?.update(updatedContent, alertConfiguration: alertConfiguration) //await meshActivity?.update(updatedContent) + print("Updated live activity.") } } }