Skip to content

Instantly share code, notes, and snippets.

@alaeddineG
Created June 30, 2016 17:05
Show Gist options
  • Save alaeddineG/7afae608ab703344a0ee6f29b72a8f4e to your computer and use it in GitHub Desktop.
Save alaeddineG/7afae608ab703344a0ee6f29b72a8f4e to your computer and use it in GitHub Desktop.
- (BOOL)handleMessage:(Connection *)connection data:(NSData *)data onTopic:(NSString *)topic retained:(BOOL)retained {
DDLogVerbose(@"handleMessage");
if (![[OwnTracking sharedInstance] processMessage:topic data:data retained:retained context:self.queueManagedObjectContext]) {
return false;
}
NSArray *baseComponents = [[Settings theGeneralTopic] componentsSeparatedByString:@"/"];
NSArray *topicComponents = [[Settings theGeneralTopic] componentsSeparatedByString:@"/"];
NSString *device = @"";
BOOL ownDevice = true;
for (int i = 0; i < [baseComponents count]; i++) {
if (i > 0) {
device = [device stringByAppendingString:@"/"];
}
if (i < topicComponents.count) {
device = [device stringByAppendingString:topicComponents[i]];
if (![baseComponents[i] isEqualToString:topicComponents [i]]) {
ownDevice = false;
}
} else {
ownDevice = false;
}
}
DDLogVerbose(@"device %@", device);
if (ownDevice) {
NSError *error;
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
if (json && [json isKindOfClass:[NSDictionary class]]) {
NSDictionary *dictionary = json;
if ([@"cmd" saveEqual:dictionary[@"_type"]]) {
#ifdef DEBUG
if (true /* dirty work around not being able to set simulator .otrc */) {
#else
if ([Settings boolForKey:@"cmd_preference"]) {
#endif
if ([@"dump" saveEqual:dictionary[@"action"]]) {
[self dump];
} else if ([@"reportLocation" saveEqual:dictionary[@"action"]]) {
if ([LocationManager sharedInstance].monitoring == LocationMonitoringSignificant ||
[LocationManager sharedInstance].monitoring == LocationMonitoringMove ||
[Settings boolForKey:@"allowremotelocation_preference"]) {
[self publishLocation:[LocationManager sharedInstance].location trigger:@"r"];
}
} else if ([@"reportSteps" saveEqual:dictionary[@"action"]]) {
[self stepsFrom:[NSNumber saveCopy:dictionary[@"from"]]
to:[NSNumber saveCopy:dictionary[@"to"]]];
} else if ([@"waypoints" saveEqual:dictionary[@"action"]]) {
[self waypoints];
} else if ([@"action" saveEqual:dictionary[@"action"]]) {
NSString *content = [NSString saveCopy:[dictionary objectForKey:@"content"]];
NSString *url = [NSString saveCopy:[dictionary objectForKey:@"url"] ];
NSString *notificationMessage = [NSString saveCopy:[dictionary objectForKey:@"notification"]];
NSNumber *external = [NSNumber saveCopy:[dictionary objectForKey:@"extern"]];
notificationMessage = [NSString stringWithFormat:@"%@ | Received at %@", notificationMessage, [NSDate date]];
content = [NSString stringWithFormat:@"%@ | Received at %@", content, [NSDate date]];
[Settings setString:content forKey:SETTINGS_ACTION];
[Settings setString:url forKey:SETTINGS_ACTIONURL];
[Settings setBool:[external boolValue] forKey:SETTINGS_ACTIONEXTERN];
if (notificationMessage) {
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.alertBody = notificationMessage;
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:1.0];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
NSString* messageuuid = [NSString saveCopy:[dictionary objectForKey:@"message_uuid"]];
if(messageuuid){
NSMutableDictionary* mutableDico = [dictionary mutableCopy];
mutableDico[@"received_on"] = [self localCurrentDate];
NSData *data = [self jsonToData:mutableDico];
[self.connection sendData:data
topic:[NSString stringWithFormat:@"debug-auto"]
qos:2
retain:YES];
}
if (content || url) {
if (url && ![url isEqualToString:self.action]) {
self.action = url;
} else {
if (content && ![content isEqualToString:self.action]) {
self.action = content;
}
}
} else {
self.action = nil;
}
} else if ([@"setWaypoints" saveEqual:dictionary[@"action"]]) {
NSDictionary *payload = [NSDictionary saveCopy:dictionary[@"payload"]];
[Settings waypointsFromDictionary:payload];
} else {
DDLogVerbose(@"unknown action %@", dictionary[@"action"]);
}
}
} else {
DDLogVerbose(@"unhandled record type %@", dictionary[@"_type"]);
}
} else {
DDLogVerbose(@"illegal json %@ %@ %@", error.localizedDescription, error.userInfo, data.description);
}
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment