Files
iOS/Sources/Extensions/Watch/Notifications/NotificationSubControllerMJPEG.swift
Bruno Pantaleão Gonçalves 79d84045d6 Add more logging to connectivity errors (#3196)
<!-- Thank you for submitting a Pull Request and helping to improve Home
Assistant. Please complete the following sections to help the processing
and review of your changes. Please do not delete anything from this
template. -->

## Summary
<!-- Provide a brief summary of the changes you have made and most
importantly what they aim to achieve -->

## Screenshots
<!-- If this is a user-facing change not in the frontend, please include
screenshots in light and dark mode. -->

## Link to pull request in Documentation repository
<!-- Pull requests that add, change or remove functionality must have a
corresponding pull request in the Companion App Documentation repository
(https://github.com/home-assistant/companion.home-assistant). Please add
the number of this pull request after the "#" -->
Documentation: home-assistant/companion.home-assistant#

## Any other notes
<!-- If there is any other information of note, like if this Pull
Request is part of a bigger change, please include it here. -->
2024-11-26 14:06:57 +01:00

57 lines
1.6 KiB
Swift

import Foundation
import PromiseKit
import Shared
import UserNotifications
import WatchKit
class NotificationSubControllerMJPEG: NotificationSubController {
let entityId: String
let api: HomeAssistantAPI
required init?(api: HomeAssistantAPI, notification: UNNotification) {
guard let entityId = notification.request.content.userInfo["entity_id"] as? String,
entityId.starts(with: "camera.") else {
return nil
}
self.api = api
self.entityId = entityId
}
required init?(api: HomeAssistantAPI, url: URL) {
nil
}
private var streamer: MJPEGStreamer?
func start(with elements: NotificationElements) -> Promise<Void> {
elements.image.setHidden(true)
let streamer = api.VideoStreamer()
self.streamer = streamer
return Promise<Void> { seal in
guard let apiURL = api.server.info.connection.activeAPIURL() else {
seal.reject(ServerConnectionError.noActiveURL(api.server.info.name))
return
}
let queryUrl = apiURL.appendingPathComponent("camera_proxy_stream/\(entityId)", isDirectory: false)
streamer.streamImages(fromURL: queryUrl) { uiImage, error in
if let error {
seal.reject(error)
} else if let uiImage {
seal.fulfill(())
elements.image.setHidden(false)
elements.image.setImage(uiImage)
}
}
}
}
func stop() {
streamer?.cancel()
streamer = nil
}
}