Loading...
Searching...
No Matches
NavigineSdkExample.kt
Go to the documentation of this file.
1package com.navigine.examples
2
3import com.navigine.idl.java.*
4
5/**
6 * NavigineSDK usage example for Kotlin
7 * Demonstrates SDK initialization and access to various managers
8 */
9class NavigineSdkExample {
10
11 private var sdk: NavigineSdk? = null
12 private var locationManager: LocationManager? = null
13 private var navigationManager: NavigationManager? = null
14 private var zoneManager: ZoneManager? = null
15 private var routeManager: RouteManager? = null
16 private var asyncRouteManager: AsyncRouteManager? = null
17 private var notificationManager: NotificationManager? = null
18 private var measurementManager: MeasurementManager? = null
19 private var locationListManager: LocationListManager? = null
20 private var storageManager: StorageManager? = null
21 private var mqttSession: MqttSession? = null
22 private var locationWindow: LocationWindow? = null
23 private var routeLayer: RouteLayer? = null
24 private var guidanceCamera: GuidanceCamera? = null
25
26 init {
27 initializeSdk()
28 }
29
30 /**
31 * SDK initialization and basic parameter setup
32 */
33 private fun initializeSdk() {
34 try {
35 // [kotlin_NavigineSdk_getInstance]
36 // Get SDK instance
37 sdk = NavigineSdk.getInstance()
38 // [kotlin_NavigineSdk_getInstance]
39
40 // [kotlin_NavigineSdk_getVersion]
41 // Get SDK version
42 println("Navigine SDK Version: ${NavigineSdk.getVersion()}")
43 // [kotlin_NavigineSdk_getVersion]
44
45 // [kotlin_NavigineSdk_getDeviceId]
46 // Get device ID
47 println("Device ID: ${NavigineSdk.getDeviceId()}")
48 // [kotlin_NavigineSdk_getDeviceId]
49
50 // [kotlin_NavigineSdk_getRelativeTime]
51 // Get relative time
52 println("Relative Time: ${NavigineSdk.getRelativeTime()}")
53 // [kotlin_NavigineSdk_getRelativeTime]
54
55 // [kotlin_NavigineSdk_setUserHash]
56 // Set user hash (authorization token)
57 sdk?.setUserHash("XXXX-XXXX-XXXX-XXXX")
58 // [kotlin_NavigineSdk_setUserHash]
59
60
61 // [kotlin_NavigineSdk_getUserAgent]
62 println("User-Agent: ${NavigineSdk.getUserAgent()}")
63 // [kotlin_NavigineSdk_getUserAgent]
64
65 // [kotlin_NavigineSdk_reset]
66 sdk!!.reset()
67 println("SDK reset to initial state")
68 // [kotlin_NavigineSdk_reset]
69
70 // [kotlin_NavigineSdk_setServer]
71 // Set server URL (optional)
72 sdk?.setServer("https://custom.navigine.com")
73 // [kotlin_NavigineSdk_setServer]
74
75 // Initialize managers
76 initializeManagers()
77
78 } catch (e: Exception) {
79 System.err.println("SDK initialization error: ${e.message}")
80 }
81 }
82
83 /**
84 * Initialize all SDK managers
85 */
86 private fun initializeManagers() {
87 sdk?.let { sdkInstance ->
88 // [kotlin_NavigineSdk_getLocationManager]
89 // Get LocationManager for working with locations
90 locationManager = sdkInstance.locationManager
91 locationManager?.let {
92 println("LocationManager successfully initialized")
93 }
94 // [kotlin_NavigineSdk_getLocationManager]
95
96 // [kotlin_NavigineSdk_getNavigationManager]
97 // Get NavigationManager for navigation
98 locationManager?.let { locationManager ->
99 navigationManager = sdkInstance.getNavigationManager(locationManager)
100 navigationManager?.let {
101 println("NavigationManager successfully initialized")
102 }
103 }
104 // [kotlin_NavigineSdk_getNavigationManager]
105
106 // [kotlin_NavigineSdk_getZoneManager]
107 // Get ZoneManager for working with zones
108 navigationManager?.let { navigationManager ->
109 zoneManager = sdkInstance.getZoneManager(navigationManager)
110 zoneManager?.let {
111 println("ZoneManager successfully initialized")
112 }
113 }
114 // [kotlin_NavigineSdk_getZoneManager]
115
116 // [kotlin_NavigineSdk_getRouteManager]
117 // Get RouteManager for building routes
118 locationManager?.let { locationManager ->
119 navigationManager?.let { navigationManager ->
120 routeManager = sdkInstance.getRouteManager(locationManager, navigationManager)
121 routeManager?.let {
122 println("RouteManager successfully initialized")
123 }
124 }
125 }
126 // [kotlin_NavigineSdk_getRouteManager]
127
128 // [kotlin_NavigineSdk_getAsyncRouteManager]
129 // Get AsyncRouteManager for async route operations
130 locationManager?.let { locationManager ->
131 navigationManager?.let { navigationManager ->
132 asyncRouteManager = sdkInstance.getAsyncRouteManager(locationManager, navigationManager)
133 asyncRouteManager?.let {
134 println("AsyncRouteManager successfully initialized")
135 }
136 }
137 }
138 // [kotlin_NavigineSdk_getAsyncRouteManager]
139
140 // [kotlin_NavigineSdk_getNotificationManager]
141 // Get NotificationManager for notifications
142 locationManager?.let { locationManager ->
143 notificationManager = sdkInstance.getNotificationManager(locationManager)
144 notificationManager?.let {
145 println("NotificationManager successfully initialized")
146 }
147 }
148 // [kotlin_NavigineSdk_getNotificationManager]
149
150 // [kotlin_NavigineSdk_getMeasurementManager]
151 // Get MeasurementManager for measurements
152 locationManager?.let { locationManager ->
153 measurementManager = sdkInstance.getMeasurementManager(locationManager)
154 measurementManager?.let {
155 println("MeasurementManager successfully initialized")
156 }
157 }
158 // [kotlin_NavigineSdk_getMeasurementManager]
159
160 // [kotlin_NavigineSdk_getLocationListManager]
161 // Get LocationListManager for location list operations
162 locationListManager = sdkInstance.locationListManager
163 locationListManager?.let {
164 println("LocationListManager successfully initialized")
165 }
166 // [kotlin_NavigineSdk_getLocationListManager]
167
168 // [kotlin_NavigineSdk_getStorageManager]
169 // Get StorageManager for working with storages
170 storageManager = sdkInstance.storageManager
171 storageManager?.let {
172 println("StorageManager successfully initialized")
173 }
174 // [kotlin_NavigineSdk_getStorageManager]
175
176 // [kotlin_NavigineSdk_getRouteLayer]
177 // Create RouteLayer for building and drawing routes on the map
178 if (locationWindow != null) {
179 routeLayer = sdkInstance.getRouteLayer(locationWindow!!)
180 routeLayer?.let {
181 println("RouteLayer successfully initialized")
182 }
183 }
184 // [kotlin_NavigineSdk_getRouteLayer]
185
186 // [kotlin_NavigineSdk_getGuidanceCamera]
187 if (locationWindow != null && routeLayer != null) {
188 guidanceCamera = sdkInstance.getGuidanceCamera(locationWindow!!, routeLayer!!)
189 // [kotlin_GuidanceCamera_setMode]
190 guidanceCamera?.setMode(GuidanceCameraMode.FOLLOWING)
191 // [kotlin_GuidanceCamera_setMode]
192 // [kotlin_GuidanceCamera_set2DMode]
193 guidanceCamera?.set2DMode(false)
194 // [kotlin_GuidanceCamera_set2DMode]
195 // [kotlin_GuidanceCamera_mode]
196 println("Guidance mode: ${guidanceCamera?.mode}")
197 // [kotlin_GuidanceCamera_mode]
198 // [kotlin_GuidanceCamera_instruction]
199 val next = guidanceCamera?.instruction
200 println("Next instruction: ${next?.title} ${next?.distance} m")
201 // [kotlin_GuidanceCamera_instruction]
202 // [kotlin_GuidanceCamera_addListener]
203 guidanceCamera?.addListener(object : GuidanceListener() {
204 override fun onCameraModeChanged(mode: GuidanceCameraMode) {
205 println("Guidance camera mode: $mode")
206 }
207
208 override fun onInstructionChanged(instruction: RouteInstruction?) {
209 println("Next: ${instruction?.title} ${instruction?.distance} m")
210 }
211 })
212 // [kotlin_GuidanceCamera_addListener]
213 }
214 // [kotlin_NavigineSdk_getGuidanceCamera]
215
216 // [kotlin_NavigineSdk_getRouteSimulator]
217 if (locationWindow != null) {
218 val routeSimulator = sdkInstance.getRouteSimulator(locationWindow!!)
219 // [kotlin_RouteSimulator_setGeometry]
220 val walk = LocationPolyline(
221 listOf(GlobalPoint(55.751, 37.618), GlobalPoint(55.752, 37.618)),
222 null,
223 )
224 routeSimulator.setGeometry(listOf(walk))
225 // [kotlin_RouteSimulator_setGeometry]
226 // [kotlin_RouteSimulator_setSpeed]
227 routeSimulator.setSpeed(1.4f)
228 // [kotlin_RouteSimulator_setSpeed]
229 // [kotlin_RouteSimulator_start]
230 val started = routeSimulator.start()
231 println("Route walk started: $started")
232 // [kotlin_RouteSimulator_start]
233 // [kotlin_RouteSimulator_sample]
234 val sample = routeSimulator.sample
235 println("Walk advance: ${sample?.advance} m")
236 // [kotlin_RouteSimulator_sample]
237 }
238 // [kotlin_NavigineSdk_getRouteSimulator]
239
240 // [kotlin_NavigineSdk_getMqttSession]
241 // Get MqttSession for publishing position data via MQTT
242 navigationManager?.let { navigationManager ->
243 mqttSession = sdkInstance.getMqttSession(navigationManager)
244 mqttSession?.let {
245 println("MqttSession successfully initialized")
246 }
247 }
248 // [kotlin_NavigineSdk_getMqttSession]
249 }
250 }
251
252 /**
253 * Get information about current SDK state
254 */
255 fun printSdkInfo() {
256 println("=== Navigine SDK Information ===")
257 println("SDK Version: ${NavigineSdk.getVersion()}")
258 println("Device ID: ${NavigineSdk.getDeviceId()}")
259 println("Relative Time: ${NavigineSdk.getRelativeTime()}")
260 println("LocationManager: ${if (locationManager != null) "available" else "unavailable"}")
261 println("NavigationManager: ${if (navigationManager != null) "available" else "unavailable"}")
262 println("ZoneManager: ${if (zoneManager != null) "available" else "unavailable"}")
263 println("RouteManager: ${if (routeManager != null) "available" else "unavailable"}")
264 println("AsyncRouteManager: ${if (asyncRouteManager != null) "available" else "unavailable"}")
265 println("NotificationManager: ${if (notificationManager != null) "available" else "unavailable"}")
266 println("MeasurementManager: ${if (measurementManager != null) "available" else "unavailable"}")
267 println("LocationListManager: ${if (locationListManager != null) "available" else "unavailable"}")
268 println("StorageManager: ${if (storageManager != null) "available" else "unavailable"}")
269 println("MqttSession: ${if (mqttSession != null) "available" else "unavailable"}")
270 }
271
272 /**
273 * Demonstrate usage of all managers
274 */
275 fun demonstrateManagersUsage() {
276 println("=== Manager Usage Demonstration ===")
277
278 // LocationManager usage example
279 locationManager?.let {
280 println("LocationManager ready for location operations")
281 // Add code here for loading locations, working with them, etc.
282 }
283
284 // NavigationManager usage example
285 navigationManager?.let {
286 println("NavigationManager ready for positioning")
287 // Add code here for getting position, setting up listeners, etc.
288 }
289
290 // ZoneManager usage example
291 zoneManager?.let {
292 println("ZoneManager ready for zone operations")
293 // Add code here for setting up zones, handling enter/exit events, etc.
294 }
295
296 // RouteManager usage example
297 routeManager?.let {
298 println("RouteManager ready for route building")
299 // Add code here for building routes, setting destination points, etc.
300 }
301
302 // AsyncRouteManager usage example
303 asyncRouteManager?.let {
304 println("AsyncRouteManager ready for async route operations")
305 // Add code here for creating routing sessions, etc.
306 }
307
308 // NotificationManager usage example
309 notificationManager?.let {
310 println("NotificationManager ready for notifications")
311 // Add code here for setting up beacon notifications, etc.
312 }
313
314 // MeasurementManager usage example
315 measurementManager?.let {
316 println("MeasurementManager ready for measurements")
317 // Add code here for managing measurement generators, etc.
318 }
319
320 // LocationListManager usage example
321 locationListManager?.let {
322 println("LocationListManager ready for location list operations")
323 // Add code here for getting available locations list, etc.
324 }
325
326 // StorageManager usage example
327 storageManager?.let {
328 println("StorageManager ready for storage operations")
329 // Add code here for creating storages, managing data, etc.
330 }
331
332 // MqttSession usage example
333 mqttSession?.let {
334 println("MqttSession ready for publishing position data via MQTT")
335 // Add code here for connecting to MQTT broker, publishing positions, etc.
336 }
337 }
338
339 /**
340 * Demonstrate Kotlin features usage
341 */
342 fun demonstrateKotlinFeatures() {
343 println("=== Kotlin Features Demonstration ===")
344
345 // Using let for safe calls
346 sdk?.let { sdkInstance ->
347 sdkInstance.setUserHash("YYYY-YYYY-YYYY-YYYY")
348 println("User hash updated")
349 }
350
351 // Using run for code block execution
352 val sdkInfo = run {
353 mapOf(
354 "version" to NavigineSdk.getVersion(),
355 "deviceId" to NavigineSdk.getDeviceId(),
356 "relativeTime" to NavigineSdk.getRelativeTime()
357 )
358 }
359 println("SDK Information: $sdkInfo")
360
361 // Using with for object operations
362 with(locationManager) {
363 this?.let {
364 println("LocationManager available for operations")
365 // Call LocationManager methods here
366 }
367 }
368
369 // Using apply for object configuration
370 sdk?.apply {
371 setUserHash("ZZZZ-ZZZZ-ZZZZ-ZZZZ")
372 setServer("https://custom.navigine.com")
373 }
374 println("SDK settings updated")
375 }
376
377 /**
378 * Main function for demonstration
379 */
380 companion object {
381 @JvmStatic
382 fun main(args: Array<String>) {
383 val example = NavigineSdkExample()
384 example.printSdkInfo()
385 example.demonstrateManagersUsage()
386 example.demonstrateKotlinFeatures()
387 }
388 }
389}