iOS/Sources/Shared/API/Models/WebhookUpdateLocation.swift
David Beitey 7c052a9f1d
Update repo name and URL in CONTRIBUTING docs (#2210)
<!-- 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 -->
This clarifies in the contributing docs which project is being
contributed to and which repo to work against - previously it said just
"Home Assistant" which could be confusing.

This also updates the repo URL in the contributing docs and at the same
time several code comments that also used the older repo URL.

## 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. -->
2022-09-30 16:27:39 +00:00

120 lines
3.7 KiB
Swift

import CoreLocation
import CoreMotion
import Foundation
import ObjectMapper
public enum LocationNames: String {
case Home = "home"
case NotHome = "not_home"
}
public struct WebhookUpdateLocation: ImmutableMappable {
public var horizontalAccuracy: CLLocationAccuracy?
public var battery: Int?
public var location: CLLocationCoordinate2D?
public var locationName: String?
public var speed: CLLocationSpeed?
public var altitude: CLLocationDistance?
public var course: CLLocationDirection?
public var verticalAccuracy: CLLocationAccuracy?
// Not sent
public var trigger: LocationUpdateTrigger
public init(trigger: LocationUpdateTrigger) {
self.trigger = trigger
if let battery = Current.device.batteries().first {
self.battery = battery.level
}
}
public init(trigger: LocationUpdateTrigger, usingNameOf zone: RLMZone?) {
self.init(trigger: trigger)
self.locationName = zone?.deviceTrackerName ?? LocationNames.NotHome.rawValue
}
public init(trigger: LocationUpdateTrigger, location: CLLocation?, zone: RLMZone?) {
self.init(trigger: trigger)
let useLocation: Bool
switch trigger {
case .BeaconRegionExit, .BeaconRegionEnter:
useLocation = false
default:
useLocation = true
}
if let location = location, useLocation {
self.location = location.coordinate
if location.speed > -1 {
self.speed = location.speed
}
if location.course > -1 {
self.course = location.course
}
if location.altitude > -1 {
self.altitude = location.altitude
}
if location.verticalAccuracy > -1 {
self.verticalAccuracy = location.verticalAccuracy
}
if location.horizontalAccuracy > -1 {
self.horizontalAccuracy = location.horizontalAccuracy
}
} else if let zone = zone {
if trigger != .BeaconRegionExit {
self.location = zone.center
self.horizontalAccuracy = zone.Radius
}
#if os(iOS)
// https://github.com/home-assistant/iOS/issues/32
if let currentSSID = Current.connectivity.currentWiFiSSID(), zone.SSIDTrigger.contains(currentSSID) {
self.location = zone.center
self.locationName = zone.Name
return
}
#endif
if zone.isHome {
switch trigger {
case .RegionEnter, .GPSRegionEnter, .BeaconRegionEnter:
self.locationName = LocationNames.Home.rawValue
case .RegionExit, .GPSRegionExit:
self.locationName = LocationNames.NotHome.rawValue
default:
break
}
} else {
switch trigger {
case .BeaconRegionEnter where !zone.isPassive:
self.locationName = zone.Name
case .BeaconRegionExit:
break
default:
break
}
}
}
}
// Mappable
public init(map: Map) throws {
fatalError()
}
public func mapping(map: Map) {
battery >>> map["battery"]
location >>> (map["gps"], CLLocationCoordinate2DTransform())
horizontalAccuracy >>> map["gps_accuracy"]
locationName >>> map["location_name"]
speed >>> map["speed"]
altitude >>> map["altitude"]
course >>> map["course"]
verticalAccuracy >>> map["vertical_accuracy"]
}
}