-
Notifications
You must be signed in to change notification settings - Fork 277
/
DevicesMapViewController.swift
184 lines (152 loc) · 7.64 KB
/
DevicesMapViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
//
// DevicesMapViewController.swift
// HomeAssistant
//
// Created by Robbie Trencheny on 4/8/16.
// Copyright © 2016 Robbie Trencheny. All rights reserved.
//
import UIKit
import MapKit
import RealmSwift
enum MapType: Int {
case standard = 0
case hybrid
case satellite
}
class DeviceAnnotation: MKPointAnnotation {
var device: DeviceTracker?
}
class HACircle: MKCircle {
var type: String = "zone"
}
class DevicesMapViewController: UIViewController, MKMapViewDelegate {
var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Devices & Zones"
self.navigationController?.isToolbarHidden = false
let items = ["Standard", "Hybrid", "Satellite"]
let typeController = UISegmentedControl(items: items)
typeController.selectedSegmentIndex = 0
typeController.addTarget(self, action: #selector(DevicesMapViewController.switchMapType(_:)), for: .valueChanged)
let uploadIcon = getIconForIdentifier("mdi:upload", iconWidth: 30, iconHeight: 30, color: colorWithHexString("#44739E", alpha: 1))
self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: uploadIcon, style: .plain, target: self, action: #selector(DevicesMapViewController.sendCurrentLocation(_:)))
self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(DevicesMapViewController.closeMapView(_:)))
// Do any additional setup after loading the view.
mapView = MKMapView()
mapView.mapType = .standard
mapView.frame = view.frame
mapView.delegate = self
mapView.showsUserLocation = false
mapView.showsPointsOfInterest = false
view.addSubview(mapView)
let locateMeButton = MKUserTrackingBarButtonItem(mapView: mapView)
let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil)
let segmentedControlButtonItem = UIBarButtonItem(customView: typeController)
// let bookmarksButton = UIBarButtonItem(barButtonSystemItem: .Bookmarks, target: self, action: nil)
self.setToolbarItems([locateMeButton, flexibleSpace, segmentedControlButtonItem, flexibleSpace], animated: true)
for zone in realm.objects(Zone.self) {
let circle = HACircle.init(center: zone.locationCoordinates(), radius: CLLocationDistance(zone.Radius))
circle.type = "zone"
mapView.add(circle)
}
for device in realm.objects(DeviceTracker.self) {
if device.Latitude.value == nil || device.Longitude.value == nil {
continue
}
let dropPin = DeviceAnnotation()
dropPin.coordinate = device.locationCoordinates()
dropPin.title = device.Name
var subtitlePieces : [String] = []
// if let changedTime = device.LastChanged {
// subtitlePieces.append("Last seen: "+changedTime.toRelativeString(abbreviated: true, maxUnits: 1)!+" ago")
// }
if let battery = device.Battery.value {
subtitlePieces.append("Battery: "+String(battery)+"%")
}
dropPin.subtitle = subtitlePieces.joined(separator: " / ")
dropPin.device = device
mapView.addAnnotation(dropPin)
if let radius = device.GPSAccuracy.value {
let circle = HACircle.init(center: device.locationCoordinates(), radius: radius)
circle.type = "device"
mapView.add(circle)
}
}
var zoomRect:MKMapRect = MKMapRectNull
for index in 0..<mapView.annotations.count {
let annotation = mapView.annotations[index]
let aPoint:MKMapPoint = MKMapPointForCoordinate(annotation.coordinate)
let rect:MKMapRect = MKMapRectMake(aPoint.x, aPoint.y, 0.1, 0.1)
zoomRect = MKMapRectUnion(zoomRect, rect)
}
if let firstOverlay = mapView.overlays.first {
let rect = mapView.overlays.reduce(firstOverlay.boundingMapRect, {MKMapRectUnion($0, $1.boundingMapRect)})
mapView.setVisibleMapRect(MKMapRectUnion(zoomRect, rect), edgePadding: UIEdgeInsets(top: 10.0, left: 10.0, bottom: 10.0, right: 10.0), animated: true)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func switchMapType(_ sender: UISegmentedControl) {
let mapType = MapType(rawValue: sender.selectedSegmentIndex)
switch (mapType!) {
case .standard:
mapView.mapType = MKMapType.standard
case .hybrid:
mapView.mapType = MKMapType.hybrid
case .satellite:
mapView.mapType = MKMapType.satellite
}
}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if let annotation = annotation as? DeviceAnnotation {
let annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: annotation.device?.ID)
annotationView.animatesDrop = true
annotationView.canShowCallout = true
if let picture = annotation.device?.DownloadedPicture {
annotationView.leftCalloutAccessoryView = UIImageView(image: picture)
} else {
annotationView.leftCalloutAccessoryView = UIImageView(image: annotation.device!.EntityIcon)
}
// annotationView.rightCalloutAccessoryView = UIButton(type: .DetailDisclosure)
return annotationView
} else {
return nil
}
}
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
if let overlay = overlay as? HACircle {
let circle = MKCircleRenderer(overlay: overlay)
if overlay.type == "zone" {
circle.strokeColor = UIColor.red
circle.fillColor = UIColor.red.withAlphaComponent(0.1)
circle.lineWidth = 1
circle.lineDashPattern = [2, 5]
} else if overlay.type == "device" {
circle.strokeColor = UIColor.blue
circle.fillColor = UIColor.blue.withAlphaComponent(0.1)
circle.lineWidth = 1
}
return circle
} else {
return MKOverlayRenderer(overlay: overlay)
}
}
func closeMapView(_ sender: UIBarButtonItem) {
self.navigationController?.dismiss(animated: true, completion: nil)
}
func sendCurrentLocation(_ sender: UIBarButtonItem) {
HomeAssistantAPI.sharedInstance.sendOneshotLocation().then { success -> Void in
let alert = UIAlertController(title: "Location updated", message: "Successfully sent a one shot location to the server", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}.catch {error in
let nserror = error as NSError
let alert = UIAlertController(title: "Location failed to update", message: "Failed to send current location to server. The error was \(nserror.localizedDescription)", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
}