Skip to content

Commit

Permalink
Additional Tracker Improvements. (#835)
Browse files Browse the repository at this point in the history
* Additional Tracker Improvements.
Removes AlamoFire
Adds Tracker to Notifications
Fixes switching to/from demo mode
Remves unused HTTPClient functions in favor of upcoming Swift/OpenAPI work

Signed-off-by: Dan Cunningham <[email protected]>

* Fixes background http calls

Signed-off-by: Dan Cunningham <[email protected]>

---------

Signed-off-by: Dan Cunningham <[email protected]>
  • Loading branch information
digitaldan authored Oct 3, 2024
1 parent 9579371 commit c69b28b
Show file tree
Hide file tree
Showing 7 changed files with 358 additions and 317 deletions.
92 changes: 61 additions & 31 deletions NotificationService/NotificationService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
//
// SPDX-License-Identifier: EPL-2.0

import Combine
import Foundation
import OpenHABCore
import os.log
Expand All @@ -18,6 +19,7 @@ import UserNotifications
class NotificationService: UNNotificationServiceExtension {
var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?
var cancellables = Set<AnyCancellable>()

override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
Expand Down Expand Up @@ -142,9 +144,23 @@ class NotificationService: UNNotificationServiceExtension {
self.attachFile(localURL: localURL, mimeType: response?.mimeType, completion: completion)
}
if url.starts(with: "/") {
client.downloadFile(baseURLs: [Preferences.localUrl, Preferences.remoteUrl], path: url, completionHandler: downloadCompletionHandler)
} else {
client.downloadFile(url: url, completionHandler: downloadCompletionHandler)
let connection1 = ConnectionConfiguration(
url: Preferences.localUrl,
priority: 0
)
let connection2 = ConnectionConfiguration(
url: Preferences.remoteUrl,
priority: 1
)
NetworkTracker.shared.startTracking(connectionConfigurations: [connection1, connection2], username: Preferences.username, password: Preferences.password, alwaysSendBasicAuth: Preferences.alwaysSendCreds)
NetworkTracker.shared.waitForActiveConnection { activeConnection in
if let openHABUrl = activeConnection?.configuration.url, let uurl = URL(string: openHABUrl) {
client.downloadFile(url: uurl.appendingPathComponent(url), completionHandler: downloadCompletionHandler)
}
}
.store(in: &cancellables)
} else if let uurl = URL(string: url) {
client.downloadFile(url: uurl, completionHandler: downloadCompletionHandler)
}
}

Expand All @@ -158,39 +174,53 @@ class NotificationService: UNNotificationServiceExtension {
let itemName = String(itemURI.absoluteString.dropFirst(scheme.count + 1))

let client = HTTPClient(username: Preferences.username, password: Preferences.password, alwaysSendBasicAuth: Preferences.alwaysSendCreds)
client.getItem(baseURLs: [Preferences.localUrl, Preferences.remoteUrl], itemName: itemName) { item, error in
guard let item else {
os_log("Could not find item %{PUBLIC}@", log: .default, type: .info, itemName)
completion(nil)
return
}
if let state = item.state {
// Extract MIME type and base64 string
let pattern = /^data:(.*?);base64,(.*)$/
if let firstMatch = state.firstMatch(of: pattern) {
let mimeType = String(firstMatch.1)
let base64String = String(firstMatch.2)
if let imageData = Data(base64Encoded: base64String) {
// Create a temporary file URL
let tempDirectory = FileManager.default.temporaryDirectory
let tempFileURL = tempDirectory.appendingPathComponent(UUID().uuidString)
do {
try imageData.write(to: tempFileURL)
os_log("Image saved to temporary file: %{PUBLIC}@", log: .default, type: .info, tempFileURL.absoluteString)
self.attachFile(localURL: tempFileURL, mimeType: mimeType, completion: completion)
return
} catch {
os_log("Failed to write image data to file: %{PUBLIC}@", log: .default, type: .error, error.localizedDescription)
let connection1 = ConnectionConfiguration(
url: Preferences.localUrl,
priority: 0
)
let connection2 = ConnectionConfiguration(
url: Preferences.remoteUrl,
priority: 1
)
NetworkTracker.shared.startTracking(connectionConfigurations: [connection1, connection2], username: Preferences.username, password: Preferences.password, alwaysSendBasicAuth: Preferences.alwaysSendCreds)
NetworkTracker.shared.waitForActiveConnection { activeConnection in
if let openHABUrl = activeConnection?.configuration.url, let url = URL(string: openHABUrl) {
client.getItem(baseURL: url, itemName: itemName) { item, error in
guard let item else {
os_log("Could not find item %{PUBLIC}@", log: .default, type: .info, itemName)
completion(nil)
return
}
if let state = item.state {
// Extract MIME type and base64 string
let pattern = /^data:(.*?);base64,(.*)$/
if let firstMatch = state.firstMatch(of: pattern) {
let mimeType = String(firstMatch.1)
let base64String = String(firstMatch.2)
if let imageData = Data(base64Encoded: base64String) {
// Create a temporary file URL
let tempDirectory = FileManager.default.temporaryDirectory
let tempFileURL = tempDirectory.appendingPathComponent(UUID().uuidString)
do {
try imageData.write(to: tempFileURL)
os_log("Image saved to temporary file: %{PUBLIC}@", log: .default, type: .info, tempFileURL.absoluteString)
self.attachFile(localURL: tempFileURL, mimeType: mimeType, completion: completion)
return
} catch {
os_log("Failed to write image data to file: %{PUBLIC}@", log: .default, type: .error, error.localizedDescription)
}
} else {
os_log("Failed to decode base64 string to Data", log: .default, type: .error)
}
} else {
os_log("Failed to parse data: %{PUBLIC}@", log: .default, type: .error, error?.localizedDescription ?? "")
}
} else {
os_log("Failed to decode base64 string to Data", log: .default, type: .error)
}
} else {
os_log("Failed to parse data: %{PUBLIC}@", log: .default, type: .error, error?.localizedDescription ?? "")
completion(nil)
}
}
completion(nil)
}
.store(in: &cancellables)
}

func attachFile(localURL: URL, mimeType: String?, completion: @escaping (UNNotificationAttachment?) -> Void) {
Expand Down
Loading

0 comments on commit c69b28b

Please sign in to comment.