Loading...
Searching...
No Matches
NavigineSdkExample.swift
Go to the documentation of this file.
1import Foundation
2import NavigineSDK
3
4/**
5 * NavigineSDK usage example for Swift
6 * Demonstrates SDK initialization and access to various managers
7 */
8class NavigineSdkExample {
9
10 private var sdk: NCNavigineSdk?
11 private var locationManager: NCLocationManager?
12 private var navigationManager: NCNavigationManager?
13 private var zoneManager: NCZoneManager?
14 private var routeManager: NCRouteManager?
15 private var asyncRouteManager: NCAsyncRouteManager?
16 private var notificationManager: NCNotificationManager?
17 private var measurementManager: NCMeasurementManager?
18 private var locationListManager: NCLocationListManager?
19 private var storageManager: NCStorageManager?
20 private var mqttSession: NCMqttSession?
21
22 init() {
23 initializeSdk()
24 }
25
26 /**
27 * SDK initialization and basic parameter setup
28 */
29 private func initializeSdk() {
30 do {
31 // [swift_NavigineSdk_getInstance]
32 // Get SDK instance
33 sdk = NCNavigineSdk.getInstance()
34 // [swift_NavigineSdk_getInstance]
35
36 guard let sdk = sdk else {
37 print("Error: failed to get NavigineSdk instance")
38 return
39 }
40
41 // [swift_NavigineSdk_getVersion]
42 // Get SDK version
43 print("Navigine SDK Version: \‍(NCNavigineSdk.getVersion())")
44 // [swift_NavigineSdk_getVersion]
45
46 // [swift_NavigineSdk_getDeviceId]
47 // Get device ID
48 print("Device ID: \‍(NCNavigineSdk.getDeviceId())")
49 // [swift_NavigineSdk_getDeviceId]
50
51 // [swift_NavigineSdk_getRelativeTime]
52 // Get relative time
53 print("Relative Time: \‍(NCNavigineSdk.getRelativeTime())")
54 // [swift_NavigineSdk_getRelativeTime]
55
56 // [swift_NavigineSdk_setUserHash]
57 // Set user hash (authorization token)
58 sdk.setUserHash("XXXX-XXXX-XXXX-XXXX")
59 // [swift_NavigineSdk_setUserHash]
60
61 // [swift_NavigineSdk_setServer]
62 // Set server URL (optional)
63 sdk.setServer("https://custom.navigine.com")
64 // [swift_NavigineSdk_setServer]
65
66 // Initialize managers
67 initializeManagers()
68
69 } catch {
70 print("SDK initialization error: \‍(error)")
71 }
72 }
73
74 /**
75 * Initialize all SDK managers
76 */
77 private func initializeManagers() {
78 guard let sdk = sdk else { return }
79
80 // [swift_NavigineSdk_getLocationManager]
81 // Get LocationManager for working with locations
82 locationManager = sdk.getLocationManager()
83 if locationManager != nil {
84 print("LocationManager successfully initialized")
85 }
86 // [swift_NavigineSdk_getLocationManager]
87
88 // [swift_NavigineSdk_getNavigationManager]
89 // Get NavigationManager for navigation
90 if let locationManager = locationManager {
91 navigationManager = sdk.getNavigationManager(locationManager)
92 if navigationManager != nil {
93 print("NavigationManager successfully initialized")
94 }
95 }
96 // [swift_NavigineSdk_getNavigationManager]
97
98 // [swift_NavigineSdk_getZoneManager]
99 // Get ZoneManager for working with zones
100 if let navigationManager = navigationManager {
101 zoneManager = sdk.getZoneManager(navigationManager)
102 if zoneManager != nil {
103 print("ZoneManager successfully initialized")
104 }
105 }
106 // [swift_NavigineSdk_getZoneManager]
107
108 // [swift_NavigineSdk_getRouteManager]
109 // Get RouteManager for building routes
110 if let locationManager = locationManager,
111 let navigationManager = navigationManager {
112 routeManager = sdk.getRouteManager(locationManager, navigationManager: navigationManager)
113 if routeManager != nil {
114 print("RouteManager successfully initialized")
115 }
116 }
117 // [swift_NavigineSdk_getRouteManager]
118
119 // [swift_NavigineSdk_getAsyncRouteManager]
120 // Get AsyncRouteManager for async route operations
121 if let locationManager = locationManager,
122 let navigationManager = navigationManager {
123 asyncRouteManager = sdk.getAsyncRouteManager(locationManager, navigationManager: navigationManager)
124 if asyncRouteManager != nil {
125 print("AsyncRouteManager successfully initialized")
126 }
127 }
128 // [swift_NavigineSdk_getAsyncRouteManager]
129
130 // [swift_NavigineSdk_getNotificationManager]
131 // Get NotificationManager for notifications
132 if let locationManager = locationManager {
133 notificationManager = sdk.getNotificationManager(locationManager)
134 if notificationManager != nil {
135 print("NotificationManager successfully initialized")
136 }
137 }
138 // [swift_NavigineSdk_getNotificationManager]
139
140 // [swift_NavigineSdk_getMeasurementManager]
141 // Get MeasurementManager for measurements
142 if let locationManager = locationManager {
143 measurementManager = sdk.getMeasurementManager(locationManager)
144 if measurementManager != nil {
145 print("MeasurementManager successfully initialized")
146 }
147 }
148 // [swift_NavigineSdk_getMeasurementManager]
149
150 // [swift_NavigineSdk_getLocationListManager]
151 // Get LocationListManager for location list operations
152 locationListManager = sdk.getLocationListManager()
153 if locationListManager != nil {
154 print("LocationListManager successfully initialized")
155 }
156 // [swift_NavigineSdk_getLocationListManager]
157
158 // [swift_NavigineSdk_getStorageManager]
159 // Get StorageManager for working with storages
160 storageManager = sdk.getStorageManager()
161 if storageManager != nil {
162 print("StorageManager successfully initialized")
163 }
164 // [swift_NavigineSdk_getStorageManager]
165
166 // [swift_NavigineSdk_getMqttSession]
167 // Get MqttSession for publishing position data via MQTT
168 if let navigationManager = navigationManager {
169 mqttSession = sdk.getMqttSession(navigationManager)
170 if mqttSession != nil {
171 print("MqttSession successfully initialized")
172 }
173 }
174 // [swift_NavigineSdk_getMqttSession]
175 }
176
177 /**
178 * Get information about current SDK state
179 */
180 func printSdkInfo() {
181 print("=== Navigine SDK Information ===")
182 print("SDK Version: \‍(NCNavigineSdk.getVersion())")
183 print("Device ID: \‍(NCNavigineSdk.getDeviceId())")
184 print("Relative Time: \‍(NCNavigineSdk.getRelativeTime())")
185 print("LocationManager: \‍(locationManager != nil ? "available" : "unavailable")")
186 print("NavigationManager: \‍(navigationManager != nil ? "available" : "unavailable")")
187 print("ZoneManager: \‍(zoneManager != nil ? "available" : "unavailable")")
188 print("RouteManager: \‍(routeManager != nil ? "available" : "unavailable")")
189 print("AsyncRouteManager: \‍(asyncRouteManager != nil ? "available" : "unavailable")")
190 print("NotificationManager: \‍(notificationManager != nil ? "available" : "unavailable")")
191 print("MeasurementManager: \‍(measurementManager != nil ? "available" : "unavailable")")
192 print("LocationListManager: \‍(locationListManager != nil ? "available" : "unavailable")")
193 print("StorageManager: \‍(storageManager != nil ? "available" : "unavailable")")
194 print("MqttSession: \‍(mqttSession != nil ? "available" : "unavailable")")
195 }
196
197 /**
198 * Demonstrate usage of all managers
199 */
200 func demonstrateManagersUsage() {
201 print("=== Manager Usage Demonstration ===")
202
203 // LocationManager usage example
204 if let locationManager = locationManager {
205 print("LocationManager ready for location operations")
206 // Add code here for loading locations, working with them, etc.
207 }
208
209 // NavigationManager usage example
210 if let navigationManager = navigationManager {
211 print("NavigationManager ready for positioning")
212 // Add code here for getting position, setting up listeners, etc.
213 }
214
215 // ZoneManager usage example
216 if let zoneManager = zoneManager {
217 print("ZoneManager ready for zone operations")
218 // Add code here for setting up zones, handling enter/exit events, etc.
219 }
220
221 // RouteManager usage example
222 if let routeManager = routeManager {
223 print("RouteManager ready for route building")
224 // Add code here for building routes, setting destination points, etc.
225 }
226
227 // AsyncRouteManager usage example
228 if let asyncRouteManager = asyncRouteManager {
229 print("AsyncRouteManager ready for async route operations")
230 // Add code here for creating routing sessions, etc.
231 }
232
233 // NotificationManager usage example
234 if let notificationManager = notificationManager {
235 print("NotificationManager ready for notifications")
236 // Add code here for setting up beacon notifications, etc.
237 }
238
239 // MeasurementManager usage example
240 if let measurementManager = measurementManager {
241 print("MeasurementManager ready for measurements")
242 // Add code here for managing measurement generators, etc.
243 }
244
245 // LocationListManager usage example
246 if let locationListManager = locationListManager {
247 print("LocationListManager ready for location list operations")
248 // Add code here for getting available locations list, etc.
249 }
250
251 // StorageManager usage example
252 if let storageManager = storageManager {
253 print("StorageManager ready for storage operations")
254 // Add code here for creating storages, managing data, etc.
255 }
256
257 // MqttSession usage example
258 if let mqttSession = mqttSession {
259 print("MqttSession ready for publishing position data via MQTT")
260 // Add code here for connecting to MQTT broker, publishing positions, etc.
261 }
262 }
263
264 /**
265 * Demonstrate Swift features usage
266 */
267 func demonstrateSwiftFeatures() {
268 print("=== Swift Features Demonstration ===")
269
270 // Using guard let for safe optional unwrapping
271 guard let sdk = sdk else {
272 print("SDK unavailable")
273 return
274 }
275
276 // Using defer for cleanup code execution
277 defer {
278 print("Swift features demonstration completed")
279 }
280
281 // Using map for data transformation
282 let sdkInfo = [
283 "version": NCNavigineSdk.getVersion(),
284 "deviceId": NCNavigineSdk.getDeviceId(),
285 "relativeTime": NCNavigineSdk.getRelativeTime()
286 ]
287
288 let infoStrings = sdkInfo.map { key, value in
289 "\‍(key): \‍(value)"
290 }
291 print("SDK Information: \‍(infoStrings.joined(separator: ", "))")
292
293 // Using compactMap for nil filtering
294 let managers = [
295 locationManager,
296 navigationManager,
297 zoneManager,
298 routeManager,
299 asyncRouteManager,
300 notificationManager,
301 measurementManager,
302 locationListManager,
303 storageManager,
304 mqttSession
305 ]
306
307 let availableManagers = managers.compactMap { $0 }
308 print("Available managers: \‍(availableManagers.count)")
309
310 // Using async/await (if supported)
311 Task {
312 await performAsyncOperation()
313 }
314 }
315
316 /**
317 * Async operation
318 */
319 @MainActor
320 private func performAsyncOperation() async {
321 // Simulate async operation
322 try? await Task.sleep(nanoseconds: 1_000_000_000) // 1 second
323 print("Async operation completed")
324 }
325
326 /**
327 * Demonstrate property wrappers usage
328 */
329 func demonstratePropertyWrappers() {
330 print("=== Property Wrappers Demonstration ===")
331
332 // Example of @Published usage (if this was an ObservableObject)
333 // @Published var sdkVersion = NCNavigineSdk.getVersion()
334
335 // Example of @State usage (for SwiftUI)
336 // @State private var isInitialized = false
337
338 print("Property wrappers demonstrated")
339 }
340
341 /**
342 * Main method for demonstration
343 */
344 func runExample() {
345 printSdkInfo()
346 demonstrateManagersUsage()
347 demonstrateSwiftFeatures()
348 demonstratePropertyWrappers()
349 }
350}
351
352/**
353 * Extension for additional functionality
354 */
355extension NavigineSdkExample {
356
357 /**
358 * Get status of all managers
359 */
360 func getManagersStatus() -> [String: Bool] {
361 return [
362 "LocationManager": locationManager != nil,
363 "NavigationManager": navigationManager != nil,
364 "ZoneManager": zoneManager != nil,
365 "RouteManager": routeManager != nil,
366 "AsyncRouteManager": asyncRouteManager != nil,
367 "NotificationManager": notificationManager != nil,
368 "MeasurementManager": measurementManager != nil,
369 "LocationListManager": locationListManager != nil,
370 "StorageManager": storageManager != nil,
371 "MqttSession": mqttSession != nil
372 ]
373 }
374
375 /**
376 * Check if SDK is ready for work
377 */
378 func isSdkReady() -> Bool {
379 return sdk != nil && locationManager != nil
380 }
381}
382
383/**
384 * Function to run the example
385 */
386func main() {
387 let example = NavigineSdkExample()
388 example.runExample()
389}
390
391// Run the example
392main()