-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathViewController.swift
286 lines (228 loc) · 11 KB
/
ViewController.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
//
// ViewController.swift
// Geofence
//
// Created by Matthew Goulet on 4/27/23.
//
import UIKit
import MapKit
import CoreLocation
import CocoaMQTT
class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
setupMapView()
setupLocationManager()
setupLocationButton()
setupGeofenceButton()
setupGeofenceSlider()
setupGeofenceRadiusLabel()
setupMQTTClient()
}
private var mapView: MKMapView!
private var locationManager: CLLocationManager!
private var geofenceButton: UIButton!
private var geofenceSlider: UISlider!
private var geofenceOverlay: MKCircle?
private var geofenceRadiusLabel: UILabel!
private var mqttClient: CocoaMQTT?
private func setupMapView() {
mapView = MKMapView()
mapView.translatesAutoresizingMaskIntoConstraints = false
mapView.showsUserLocation = true
mapView.delegate = self // Set the delegate to self
view.addSubview(mapView)
NSLayoutConstraint.activate([
mapView.topAnchor.constraint(equalTo: view.topAnchor),
mapView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
mapView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
mapView.trailingAnchor.constraint(equalTo: view.trailingAnchor)
])
}
private func setupMQTTClient() {
let clientID = "iPhones" // Replace this with a unique client ID
let mqttHost = "4451d218b08f49b0adf1e5d0357e53e6.s1.eu.hivemq.cloud" // Replace this with your MQTT host address
let mqttPort: UInt16 = 8883 // Replace this with your MQTT host port (default SSL/TLS port is 8883)
mqttClient = CocoaMQTT(clientID: clientID, host: mqttHost, port: mqttPort)
mqttClient?.delegate = self
// Enable SSL/TLS
mqttClient?.enableSSL = true
// Allow untrusted CA certificates (not recommended for production)
// Only use this option for testing purposes or if you have a self-signed certificate
mqttClient?.allowUntrustCACertificate = true
// Set the username and password
mqttClient?.username = "Snake10" // Replace this with your MQTT username
mqttClient?.password = "quzvy0-xakhaM-peqkyh" // Replace this with your MQTT password
mqttClient?.connect()
}
private func subscribeToTopic() {
let topic = "#" // Replace this with the topic you want to subscribe to
mqttClient?.subscribe(topic, qos: .qos1)
}
private func setupGeofenceRadiusLabel() {
geofenceRadiusLabel = UILabel()
geofenceRadiusLabel.translatesAutoresizingMaskIntoConstraints = false
geofenceRadiusLabel.isHidden = true
geofenceRadiusLabel.text = "Geofence Radius: \(Int(geofenceSlider.value))m"
geofenceRadiusLabel.backgroundColor = UIColor.white
geofenceRadiusLabel.layer.cornerRadius = 5
geofenceRadiusLabel.clipsToBounds = true
geofenceRadiusLabel.textAlignment = .center
geofenceRadiusLabel.numberOfLines = 0
geofenceRadiusLabel.setContentCompressionResistancePriority(.required, for: .horizontal)
geofenceRadiusLabel.setContentHuggingPriority(.required, for: .horizontal)
geofenceRadiusLabel.setContentHuggingPriority(.required, for: .vertical)
geofenceRadiusLabel.setContentCompressionResistancePriority(.required, for: .vertical)
let padding = UIEdgeInsets(top: 4, left: 8, bottom: 4, right: 8)
geofenceRadiusLabel.drawText(in: geofenceRadiusLabel.bounds.inset(by: padding))
view.addSubview(geofenceRadiusLabel)
NSLayoutConstraint.activate([
geofenceRadiusLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor),
geofenceRadiusLabel.bottomAnchor.constraint(equalTo: geofenceSlider.topAnchor, constant: -8)
])
}
private func setupLocationManager() {
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
private func setupLocationButton() {
let locationButton = UIButton(type: .system)
locationButton.setTitle("My Location", for: .normal)
locationButton.addTarget(self, action: #selector(centerMapOnUserLocation), for: .touchUpInside)
locationButton.translatesAutoresizingMaskIntoConstraints = false
// Customizations
locationButton.backgroundColor = UIColor.systemBlue
locationButton.setTitleColor(UIColor.white, for: .normal)
locationButton.layer.cornerRadius = 10
locationButton.contentEdgeInsets = UIEdgeInsets(top: 8, left: 16, bottom: 8, right: 16)
view.addSubview(locationButton)
NSLayoutConstraint.activate([
locationButton.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 16),
locationButton.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -16)
])
}
private func setupGeofenceButton() {
geofenceButton = UIButton(type: .system)
geofenceButton.setTitle("Activate Geofence", for: .normal)
geofenceButton.addTarget(self, action: #selector(toggleGeofence), for: .touchUpInside)
geofenceButton.translatesAutoresizingMaskIntoConstraints = false
// Customizations
geofenceButton.backgroundColor = UIColor.systemBlue
geofenceButton.setTitleColor(UIColor.white, for: .normal)
geofenceButton.layer.cornerRadius = 10
geofenceButton.contentEdgeInsets = UIEdgeInsets(top: 8, left: 16, bottom: 8, right: 16)
view.addSubview(geofenceButton)
NSLayoutConstraint.activate([
geofenceButton.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -16),
geofenceButton.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -16)
])
}
private func setupGeofenceSlider() {
geofenceSlider = UISlider()
geofenceSlider.minimumValue = 5
geofenceSlider.maximumValue = 100
geofenceSlider.addTarget(self, action: #selector(geofenceRadiusChanged), for: .valueChanged)
geofenceSlider.translatesAutoresizingMaskIntoConstraints = false
geofenceSlider.isHidden = true
view.addSubview(geofenceSlider)
NSLayoutConstraint.activate([
geofenceSlider.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 16),
geofenceSlider.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -16),
geofenceSlider.bottomAnchor.constraint(equalTo: geofenceButton.topAnchor, constant: -16)
])
}
@objc private func centerMapOnUserLocation() {
guard let location = locationManager.location else { return }
let region = MKCoordinateRegion(center: location.coordinate, latitudinalMeters: 250, longitudinalMeters: 250)
mapView.setRegion(region, animated: true)
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.last else { return }
let region = MKCoordinateRegion(center: location.coordinate, latitudinalMeters: 250, longitudinalMeters: 250)
mapView.setRegion(region, animated: true)
}
@objc private func toggleGeofence() {
if geofenceSlider.isHidden {
geofenceButton.setTitle("Deactivate Geofence", for: .normal)
geofenceSlider.isHidden = false
geofenceRadiusLabel.isHidden = false
addGeofenceOverlay(radius: CLLocationDistance(geofenceSlider.value))
} else {
geofenceButton.setTitle("Activate Geofence", for: .normal)
geofenceSlider.isHidden = true
geofenceRadiusLabel.isHidden = true
removeGeofenceOverlay()
}
}
@objc private func geofenceRadiusChanged() {
geofenceRadiusLabel.text = "Geofence Radius: \(Int(geofenceSlider.value))m"
updateGeofenceOverlay(radius: CLLocationDistance(geofenceSlider.value))
}
private func addGeofenceOverlay(radius: CLLocationDistance) {
guard let location = locationManager.location else { return }
let circle = MKCircle(center: location.coordinate, radius: radius)
mapView.addOverlay(circle)
geofenceOverlay = circle
}
private func updateGeofenceOverlay(radius: CLLocationDistance) {
removeGeofenceOverlay()
addGeofenceOverlay(radius: radius)
}
private func removeGeofenceOverlay() {
if let overlay = geofenceOverlay {
mapView.removeOverlay(overlay)
geofenceOverlay = nil
}
}
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
if let circleOverlay = overlay as? MKCircle {
let circleRenderer = MKCircleRenderer(overlay: circleOverlay)
circleRenderer.fillColor = UIColor.black.withAlphaComponent(0.1)
circleRenderer.strokeColor = UIColor.black
circleRenderer.lineWidth = 2
return circleRenderer
}
return MKOverlayRenderer(overlay: overlay)
}
}
extension ViewController: CocoaMQTTDelegate {
func mqtt(_ mqtt: CocoaMQTT, didSubscribeTopics success: NSDictionary, failed: [String]) {
}
func mqtt(_ mqtt: CocoaMQTT, didConnectAck ack: CocoaMQTTConnAck) {
print("Connected to MQTT server: \(ack)")
if ack == .accept {
subscribeToTopic()
}
}
func mqtt(_ mqtt: CocoaMQTT, didStateChangeTo state: CocoaMQTTConnState) {
print("MQTT connection state changed: \(state)")
}
func mqtt(_ mqtt: CocoaMQTT, didPublishMessage message: CocoaMQTTMessage, id: UInt16) {
print("Message published: \(message.string ?? "")")
}
func mqtt(_ mqtt: CocoaMQTT, didPublishAck id: UInt16) {
print("Published message acknowledged: \(id)")
}
func mqtt(_ mqtt: CocoaMQTT, didReceiveMessage message: CocoaMQTTMessage, id: UInt16 ) {
print("Message received: \(message.string ?? "")")
}
func mqtt(_ mqtt: CocoaMQTT, didUnsubscribeTopics topics: [String]) {
print("Subscribed to topic(s): \(topics)")
}
func mqtt(_ mqtt: CocoaMQTT, didUnsubscribeTopic topic: String) {
print("Unsubscribed from topic: \(topic)")
}
func mqttDidPing(_ mqtt: CocoaMQTT) {
print("Sent PING")
}
func mqttDidReceivePong(_ mqtt: CocoaMQTT) {
print("Received PONG")
}
func mqttDidDisconnect(_ mqtt: CocoaMQTT, withError err: Error?) {
print("MQTT client disconnected: \(err?.localizedDescription ?? "No error")")
}
}