Loading...
Searching...
No Matches
MqttSessionExample.m
Go to the documentation of this file.
1#import <Foundation/Foundation.h>
2#import "NCNavigineSdk.h"
5#import "NCMqttSession.h"
7
12@interface MqttSessionExample : NSObject <NCMqttSessionListener>
13
14@property (nonatomic, strong) NCNavigineSdk *sdk;
15@property (nonatomic, strong) NCLocationManager *locationManager;
16@property (nonatomic, strong) NCNavigationManager *navigationManager;
17@property (nonatomic, strong) NCMqttSession *mqttSession;
18
19@end
20
21@implementation MqttSessionExample
22
23- (instancetype)init {
24 self = [super init];
25 if (self) {
26 [self initializeSdk];
27 }
28 return self;
29}
30
34- (void)initializeSdk {
35 @try {
36 // [objc_NavigineSdk_getInstance]
37 // Get SDK instance
38 self.sdk = [NCNavigineSdk getInstance];
39 // [objc_NavigineSdk_getInstance]
40
41 // [objc_NavigineSdk_setUserHash]
42 // Set user hash
43 [self.sdk setUserHash:@"USER-HASH-HERE"];
44 // [objc_NavigineSdk_setUserHash]
45
46 // [objc_NavigineSdk_setServer]
47 // Set server URL (optional)
48 [self.sdk setServer:@"https://custom.navigine.com"];
49 // [objc_NavigineSdk_setServer]
50
51 // [objc_NavigineSdk_getLocationManager]
52 // Get LocationManager for working with locations
53 self.locationManager = [self.sdk getLocationManager];
54 // [objc_NavigineSdk_getLocationManager]
55
56 // [objc_NavigineSdk_getNavigationManager]
57 // Get NavigationManager for working with navigation
58 self.navigationManager = [self.sdk getNavigationManager:self.locationManager];
59 // [objc_NavigineSdk_getNavigationManager]
60
61 // [objc_NavigineSdk_getMqttSession]
62 // Create MQTT session for publishing position data
63 self.mqttSession = [self.sdk getMqttSession:self.navigationManager];
64 // [objc_NavigineSdk_getMqttSession]
65
66 if (self.mqttSession != nil) {
67 NSLog(@"MqttSession successfully initialized");
68 }
69 } @catch (NSException *exception) {
70 NSLog(@"Error initializing SDK: %@", exception.reason);
71 }
72}
73
77- (void)demonstrateMqttSessionMethods {
78 if (self.mqttSession == nil) {
79 NSLog(@"MqttSession not initialized");
80 return;
81 }
82
83 // [objc_MqttSession_addListener]
84 // Add MQTT session listener
85 [self.mqttSession addListener:self];
86 // [objc_MqttSession_addListener]
87
88 // [objc_MqttSession_setSubTopic]
89 // Set MQTT sub-topic for publishing position data
90 // Final topic will be "navigine/mobile/positions/" + subTopic + "/" + deviceId
91 [self.mqttSession setSubTopic:@"location1"];
92 // [objc_MqttSession_setSubTopic]
93
94 // [objc_MqttSession_connect]
95 // Connect to MQTT broker
96 // Server: MQTT broker hostname or IP address
97 // Port: MQTT broker port (typically 1883 for non-SSL, 8883 for SSL)
98 // Username: MQTT broker username for authentication
99 // Password: MQTT broker password for authentication
100 [self.mqttSession connect:@"mqtt.example.com" port:1883 username:@"username" password:@"password"];
101 // [objc_MqttSession_connect]
102
103 // Wait for connection
104 [NSThread sleepForTimeInterval:2.0];
105
106 // [objc_MqttSession_publish]
107 // Publish a custom message to a specific MQTT topic
108 // The message will be sent asynchronously and the result will be notified through listener callbacks
109 NSString *customTopic = @"custom/device/status";
110 NSString *customMessage = [NSString stringWithFormat:@"{\"status\": \"online\", \"timestamp\": %lld}", (long long)([[NSDate date] timeIntervalSince1970] * 1000)];
111 [self.mqttSession publish:customTopic message:customMessage];
112 // [objc_MqttSession_publish]
113}
114
118- (void)demonstrateMqttSessionConfigurations {
119 if (self.mqttSession == nil) {
120 NSLog(@"MqttSession not initialized");
121 return;
122 }
123
124 NSLog(@"=== MQTT Session Configurations ===");
125
126 // Example 1: Connect with default port
127 // [objc_MqttSession_setSubTopic]
128 // Set sub-topic. Final topic will be "navigine/mobile/positions/location1/{deviceId}"
129 [self.mqttSession setSubTopic:@"location1"];
130 // [objc_MqttSession_setSubTopic]
131 // [objc_MqttSession_connect]
132 // Connect to MQTT broker with default port 1883
133 [self.mqttSession connect:@"mqtt.example.com" port:1883 username:@"user" password:@"pass"];
134 // [objc_MqttSession_connect]
135
136 // Wait a bit
137 [NSThread sleepForTimeInterval:1.0];
138
139 // Disconnect before reconnecting with different config
140 // [objc_MqttSession_disconnect]
141 // Disconnect from MQTT broker
142 [self.mqttSession disconnect];
143 // [objc_MqttSession_disconnect]
144
145 // Example 2: Connect with SSL port
146 // [objc_MqttSession_setSubTopic]
147 // Set sub-topic. Final topic will be "navigine/mobile/positions/building-a/{deviceId}"
148 [self.mqttSession setSubTopic:@"building-a"];
149 // [objc_MqttSession_setSubTopic]
150 // [objc_MqttSession_connect]
151 // Connect to MQTT broker with SSL port 8883
152 [self.mqttSession connect:@"mqtt-secure.example.com" port:8883 username:@"user" password:@"pass"];
153 // [objc_MqttSession_connect]
154
155 // Wait a bit
156 [NSThread sleepForTimeInterval:1.0];
157
158 // Example 3: Connect with different sub-topic
159 // [objc_MqttSession_setSubTopic]
160 // Set different sub-topic. Final topic will be "navigine/mobile/positions/floor_2/{deviceId}"
161 [self.mqttSession setSubTopic:@"floor_2"];
162 // [objc_MqttSession_setSubTopic]
163 // [objc_MqttSession_connect]
164 // Connect to MQTT broker
165 [self.mqttSession connect:@"mqtt.example.com" port:1883 username:@"user" password:@"pass"];
166 // [objc_MqttSession_connect]
167}
168
172- (void)demonstrateListenerManagement {
173 if (self.mqttSession == nil) {
174 NSLog(@"MqttSession not initialized");
175 return;
176 }
177
178 NSLog(@"=== Listener Management ===");
179
180 // Create listener objects
181 MqttSessionListenerImpl *listener1 = [[MqttSessionListenerImpl alloc] initWithName:@"Listener 1"];
182 MqttSessionListenerImpl *listener2 = [[MqttSessionListenerImpl alloc] initWithName:@"Listener 2"];
183
184 // [objc_MqttSession_addListener]
185 // Add first listener
186 [self.mqttSession addListener:listener1];
187 // [objc_MqttSession_addListener]
188
189 // [objc_MqttSession_addListener]
190 // Add second listener
191 [self.mqttSession addListener:listener2];
192 // [objc_MqttSession_addListener]
193
194 // Connect to trigger callbacks
195 // [objc_MqttSession_setSubTopic]
196 [self.mqttSession setSubTopic:@"location1"];
197 // [objc_MqttSession_setSubTopic]
198 // [objc_MqttSession_connect]
199 [self.mqttSession connect:@"mqtt.example.com" port:1883 username:@"user" password:@"pass"];
200 // [objc_MqttSession_connect]
201
202 // Wait for connection
203 [NSThread sleepForTimeInterval:2.0];
204
205 // [objc_MqttSession_publish]
206 // Publish custom messages to different topics
207 [self.mqttSession publish:@"custom/events" message:@"{\"event\": \"user_action\"}"];
208 [self.mqttSession publish:@"custom/logs" message:@"Application started"];
209 // [objc_MqttSession_publish]
210
211 // Wait for messages to be published
212 [NSThread sleepForTimeInterval:1.0];
213
214 // [objc_MqttSession_removeListener]
215 // Remove first listener
216 [self.mqttSession removeListener:listener1];
217 // [objc_MqttSession_removeListener]
218
219 // [objc_MqttSession_removeListener]
220 // Remove second listener
221 [self.mqttSession removeListener:listener2];
222 // [objc_MqttSession_removeListener]
223}
224
228- (void)cleanup {
229 if (self.mqttSession != nil) {
230 // [objc_MqttSession_removeListener]
231 // Remove MQTT session listener
232 [self.mqttSession removeListener:self];
233 // [objc_MqttSession_removeListener]
234
235 // [objc_MqttSession_disconnect]
236 // Disconnect from MQTT broker
237 [self.mqttSession disconnect];
238 // [objc_MqttSession_disconnect]
239 }
240}
241
245- (void)runExample {
246 NSLog(@"=== MqttSession Example ===");
247
248 [self demonstrateMqttSessionMethods];
249 [self demonstrateMqttSessionConfigurations];
250 [self demonstrateListenerManagement];
251
252 // Wait a bit for connection events
253 [NSThread sleepForTimeInterval:3.0];
254
255 [self cleanup];
256 NSLog(@"=== Example completed ===");
257}
258
259#pragma mark - NCMqttSessionListener
260
261// [objc_MqttSessionListener_onConnected]
262- (void)onConnected {
263 NSLog(@"MQTT session connected successfully");
264}
265// [objc_MqttSessionListener_onConnected]
266
267// [objc_MqttSessionListener_onError]
268- (void)onError:(NSError *)error {
269 NSLog(@"MQTT session error: %@", error.localizedDescription);
270}
271// [objc_MqttSessionListener_onError]
272
273// [objc_MqttSessionListener_onMessagePublished]
274- (void)onMessagePublished {
275 NSLog(@"Message published successfully");
276}
277// [objc_MqttSessionListener_onMessagePublished]
278
279@end
280
284@interface MqttSessionListenerImpl : NSObject <NCMqttSessionListener>
285
286@property (nonatomic, strong) NSString *name;
287
288- (instancetype)initWithName:(NSString *)name;
289
290@end
291
292@implementation MqttSessionListenerImpl
293
294- (instancetype)initWithName:(NSString *)name {
295 self = [super init];
296 if (self) {
297 self.name = name;
298 }
299 return self;
300}
301
302- (void)onConnected {
303 NSLog(@"%@: Connected", self.name);
304}
305
306- (void)onError:(NSError *)error {
307 NSLog(@"%@: Error - %@", self.name, error.localizedDescription);
308}
309
310- (void)onMessagePublished {
311 NSLog(@"%@: Message published", self.name);
312}
313
314@end
315
319int main(int argc, const char * argv[]) {
320 @autoreleasepool {
321 MqttSessionExample *example = [[MqttSessionExample alloc] init];
322 [example runExample];
323 }
324 return 0;
325}