Files
iOS/Sources/Extensions/Watch/CameraNotificationController.swift
Zac West 4d9a530637 Reorganize files in repo, pull out build settings from pbxproj (#1140)
This is somewhat in prep of being able to make the project file generated, but also just organizes things into more concrete directory structures.

This pulls out _all_ of the build settings from the root level, and most from the target level, into xcconfigs.

The new directory structure looks like:

- Sources
  - App
    - (everything from HomeAssistant/)
  - WatchApp
  - Shared
  - MacBridge
  - Extensions
    - Intents
    - NotificationContent
    - NotificationService
    - Share
    - Today
    - Watch
    - Widgets
- Tests
  - App
  - UI
  - Shared

Somewhat intentionally, the file structure under these is not yet standardized/organized.

The project targets are now:

- App
- WatchApp
- Shared-iOS
- Shared-watchOS
- MacBridge
- Tests-App
- Tests-UI
- Tests-Shared
- Extension-Intents
- Extension-NotificationContent
- Extension-NotificationService
- Extension-Share
- Extension-Today
- Extension-Widget
- WatchExtension-Watch

This does not yet clean up resources vs. sources, nor does it handle some of the "it's in Sources/App but it's part of Shared" crossover directory issues.
2020-10-03 00:15:04 -07:00

119 lines
3.8 KiB
Swift

//
// CameraNotificationController.swift
// WatchAppExtension
//
// Created by Robert Trencheny on 2/27/19.
// Copyright © 2019 Robbie Trencheny. All rights reserved.
//
import WatchKit
import Foundation
import UserNotifications
import Shared
import Alamofire
import EMTLoadingIndicator
class CameraNotificationController: WKUserNotificationInterfaceController {
@IBOutlet weak var notificationTitleLabel: WKInterfaceLabel!
@IBOutlet weak var notificationSubtitleLabel: WKInterfaceLabel!
@IBOutlet weak var notificationAlertLabel: WKInterfaceLabel!
@IBOutlet weak var imageView: WKInterfaceImage!
var streamer: MJPEGStreamer?
var frameCount: Int = 0
var shouldPlay: Bool = true {
didSet {
if !self.shouldPlay {
Current.Log.verbose("Ending playback at frame #\(frameCount)")
self.streamer?.cancel()
self.streamer = nil
}
}
}
private var indicator: EMTLoadingIndicator?
// MARK: - WKUserNotificationInterfaceController
override func willActivate() {
super.willActivate()
indicator = EMTLoadingIndicator(interfaceController: self, interfaceImage: self.imageView, width: 40,
height: 40, style: .dot)
indicator?.prepareImagesForWait()
}
override func didAppear() {
indicator?.showWait()
}
override func didDeactivate() {
// This method is called when watch view controller is no longer visible
super.didDeactivate()
self.shouldPlay = false
}
override func didReceive(_ notification: UNNotification) {
self.notificationTitleLabel.setText(notification.request.content.title)
self.notificationSubtitleLabel.setText(notification.request.content.subtitle)
self.notificationAlertLabel!.setText(notification.request.content.body)
guard let entityId = notification.request.content.userInfo["entity_id"] as? String else {
Current.Log.error(L10n.Extensions.NotificationContent.Error.noEntityId)
return
}
guard let api = HomeAssistantAPI.authenticatedAPI() else {
Current.Log.error(HomeAssistantAPI.APIError.notConfigured.localizedDescription)
return
}
guard let streamer = api.VideoStreamer() else {
return
}
guard let connectionInfo = try? api.connectionInfo() else {
Current.Log.error("no connection info available")
return
}
self.streamer = streamer
let apiURL = connectionInfo.activeAPIURL
let queryUrl = apiURL.appendingPathComponent("camera_proxy_stream/\(entityId)", isDirectory: false)
streamer.streamImages(fromURL: queryUrl) { (image, error) in
if let error = error, let afError = error as? AFError {
Current.Log.error("Streaming image AFError: \(afError)")
var labelText = L10n.Extensions.NotificationContent.Error.Request.unknown
if let responseCode = afError.responseCode {
switch responseCode {
case 401:
labelText = L10n.Extensions.NotificationContent.Error.Request.authFailed
case 404:
labelText = L10n.Extensions.NotificationContent.Error.Request.entityNotFound(entityId)
default:
labelText = L10n.Extensions.NotificationContent.Error.Request.other(responseCode)
}
}
Current.Log.error(labelText)
}
if let image = image {
defer {
self.frameCount += 1
}
Current.Log.verbose("Frame #\(self.frameCount)")
self.imageView.setImage(image)
}
}
}
}