@implementation MTCLocationManager
@synthesize manager;
@synthesize lastLocation;
@synthesize timer;
@synthesize wsUserName;
@synthesize xmlParser;
@synthesize webData;
@synthesize soapResults;
@synthesize wsUserPassword;
@synthesize glbData;
@synthesize vAktarDelegate;
NSDate *firstDate;
NSUserDefaults *defaults;
bool isUploadingDataNow;
NSString *strUserName;
NSString *strPassword;
NSString *strUrl;
CLLocation *lastLocation;
int saveUserApplicationStatusCalled;
bool sendOnly1Record = false;
-(id)init{
self = [super init];
if (self) {
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[self setLocationManager:locationManager withDistanceFilter:100.0f];
lastLocation = [[CLLocation alloc]init];
firstDate =[NSDate date];
glbData = [[NSMutableString alloc] init];
defaults = [NSUserDefaults standardUserDefaults];
strUserName= [defaults stringForKey:@"username"];
strPassword= [defaults stringForKey:@"password"];
strUrl = [defaults stringForKey:@"url"];
isUploadingDataNow = false;
saveUserApplicationStatusCalled = 0;
}
return self;
}
-(void) setSendOnly1Record:(BOOL) s{
sendOnly1Record = s;
}
-(void)enteredBackground:(id)sender{
[self.manager startMonitoringSignificantLocationChanges];
}
-(void)startUpdate{
[self.manager startUpdatingLocation];
}
-(void) stopLocationListener
{
[self.manager stopMonitoringSignificantLocationChanges];
}
-(void)post1Record:(CLLocation *)newLocation STATUS:(int) iStat{
NSLog(@"\n\nPOST1RECORD");
NSString *latitude = [[NSString alloc] init];
NSString *longitude = [[NSString alloc] init];
latitude = [NSString stringWithFormat:@"%f", [newLocation coordinate].latitude];
longitude = [NSString stringWithFormat:@"%f", [newLocation coordinate].longitude];
NSDate *currdate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *dateString = [[NSString alloc] init];
dateString = [dateFormatter stringFromDate:currdate];
NSString* batString = @"";
@try {
[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
float batteryLevel = [[UIDevice currentDevice] batteryLevel];
NSLog(@"battery level %f",batteryLevel);
batteryLevel *= 100;
int batLevel = (int) batteryLevel;
batString= [NSString stringWithFormat:@"%i", abs(batLevel)];
}
@catch (NSException *exception) {
}
@finally {
}
NSMutableString *strTemp = [[NSMutableString alloc] init];
NSString* strStat = [NSString stringWithFormat:@"%i", iStat];
[strTemp appendString:dateString];
[strTemp appendString:@"|"];
[strTemp appendString:latitude];
[strTemp appendString:@"|"];
[strTemp appendString:longitude];
[strTemp appendString:@"|"];
[strTemp appendString:batString];
[strTemp appendString:@"|"];
[strTemp appendString:strStat];
[strTemp appendString:@"|"];
[self postMULTILocation:strTemp];
}
- (void)locationManager:(CLLocationManager *)theLocationManager didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation{
lastLocation = newLocation;
if(sendOnly1Record) {
sendOnly1Record = false;
saveUserApplicationStatusCalled =1;
[self post1Record:newLocation STATUS:0];
}
else if (!isUploadingDataNow) {
NSString *latitude = [[NSString alloc] init];
NSString *longitude = [[NSString alloc] init];
latitude = [NSString stringWithFormat:@"%f", [newLocation coordinate].latitude];
longitude = [NSString stringWithFormat:@"%f", [newLocation coordinate].longitude];
NSDate *currdate = [NSDate date];
int intervall = (int) [currdate timeIntervalSinceDate: firstDate] / 60;
NSLog(@"time interval %d",intervall);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *dateString = [[NSString alloc] init];
dateString = [dateFormatter stringFromDate:currdate];
if(intervall > 10) {
//update location
isUploadingDataNow = true;
[self postMULTILocation:glbData];
firstDate = [NSDate date];
}else{
NSString* batString = @"";
@try {
[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
float batteryLevel = [[UIDevice currentDevice] batteryLevel];
//NSLog(@"battery level %f",batteryLevel);
batteryLevel *= 100;
int batLevel = (int) batteryLevel;
batString= [NSString stringWithFormat:@"%i", abs(batLevel)];
}
@catch (NSException *exception) {
}
@finally {
}
NSString* strSaveUserApplicationStatusCalled = [NSString stringWithFormat:@"%i", saveUserApplicationStatusCalled];
[glbData appendString:dateString];
[glbData appendString:@"|"];
[glbData appendString:latitude];
[glbData appendString:@"|"];
[glbData appendString:longitude];
[glbData appendString:@"|"];
[glbData appendString:batString];
[glbData appendString:@"|"];
[glbData appendString:strSaveUserApplicationStatusCalled];
[glbData appendString:@"|"];
}
if (!saveUserApplicationStatusCalled) {
saveUserApplicationStatusCalled=1;
}
[defaults setObject:glbData forKey:@"glbData"];
[defaults synchronize];
}
}
-(void)postMULTILocation:(NSString *) strData{
strUserName= [defaults stringForKey:@"username"];
strPassword= [defaults stringForKey:@"password"];
strUrl = [defaults stringForKey:@"url"];
///parameters
AFHTTPRequestOperation *operation =[[AFHTTPRequestOperation alloc] initWithRequest:theRequest];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject){
//NSLog(@"%@",[operation responseString]);
NSLog(@"RESPONSE:\n%@",[operation responseString]);
NSData* data = [[operation responseString] dataUsingEncoding:NSUTF8StringEncoding];
data = [data subdataWithRange:NSMakeRange(0, [data length] - 1)];
NSXMLParser *nsXmlParser = [[NSXMLParser alloc] initWithData:data];
// create and init our delegate
MultiPostParser *parser = [[MultiPostParser alloc] initXMLParser];
[nsXmlParser setDelegate:parser];
// parsing...
BOOL success = [nsXmlParser parse];
if([parser responseStr] != nil && [parser responseStr].length>0){
[self resetDefaults ];
glbData = [[NSMutableString alloc] init];
[self.vAktarDelegate didSend:true];
}else{
[self.vAktarDelegate didSend:false];
}
isUploadingDataNow = false;
}failure:^(AFHTTPRequestOperation *operation, NSError *error) {
isUploadingDataNow = false;
[self.vAktarDelegate didSend:false];
NSLog(@"failed %@",error.localizedDescription);
}];
[operation start];
}
- (void)resetDefaults {
[defaults removeObjectForKey:@"glbData"];
[defaults synchronize];
}
- (void)setLocationManager:(CLLocationManager*)locationManager withDistanceFilter:(CLLocationDistance)distanceFilter {
self.manager.delegate = nil;
self.manager = locationManager;
self.manager.pausesLocationUpdatesAutomatically = NO;
self.manager.delegate = self;
self.manager.distanceFilter = distanceFilter;
[self.manager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error{
}
@结尾