4d8bbdc7ef
Add to Git the current app version (the same available in the OriginalZip folder)
155 lines
4.6 KiB
Objective-C
155 lines
4.6 KiB
Objective-C
//
|
|
// EQMAccelerometroManager.m
|
|
// Earthquake Network
|
|
//
|
|
// Created by Luca Beretta on 19/09/18.
|
|
// Copyright © 2018 Luca Beretta. All rights reserved.
|
|
//
|
|
|
|
#import "EQMAccelerometroManager.h"
|
|
#import <CoreMotion/CoreMotion.h>
|
|
#import "Costanti.h"
|
|
#import "EQNMath.h"
|
|
#import "EQNUser.h"
|
|
#import "EQNManager.h"
|
|
@interface EQMAccelerometroManager ()
|
|
@property (nonatomic, assign) BOOL posizioneRilevata;
|
|
|
|
@end
|
|
|
|
@implementation EQMAccelerometroManager
|
|
|
|
+(EQMAccelerometroManager *) sharedInstance
|
|
{
|
|
static EQMAccelerometroManager *instance = nil;
|
|
static dispatch_once_t onceToken;
|
|
dispatch_once(&onceToken, ^{
|
|
instance = [[self alloc]init];
|
|
});
|
|
return instance;
|
|
}
|
|
|
|
- (id)init {
|
|
self = [super init];
|
|
if(self != nil) {
|
|
self.locationManager = [[CLLocationManager alloc] init];
|
|
if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]){
|
|
[self.locationManager requestAlwaysAuthorization];
|
|
//[self.locationManager requestWhenInUseAuthorization];
|
|
}
|
|
self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
|
|
self.locationManager.delegate = self;
|
|
self.posizioneRilevata = NO;
|
|
|
|
}
|
|
return self;
|
|
}
|
|
|
|
-(void)getPosition{
|
|
|
|
[self.locationManager requestLocation];
|
|
}
|
|
|
|
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
|
|
|
|
switch (status) {
|
|
case kCLAuthorizationStatusAuthorizedAlways:{
|
|
|
|
if (![EQNUser defaultUser].user_ID)
|
|
[self getPosition];
|
|
|
|
}
|
|
break;
|
|
case kCLAuthorizationStatusDenied:{
|
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:@"" object:nil userInfo:@{@"auto" : @(kCLAuthorizationStatusDenied)}];
|
|
|
|
}break;
|
|
|
|
case kCLAuthorizationStatusAuthorizedWhenInUse:
|
|
|
|
break;
|
|
|
|
case kCLAuthorizationStatusRestricted:{
|
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:@"" object:nil userInfo:@{@"auto" : @(kCLAuthorizationStatusRestricted)}];
|
|
|
|
}break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
|
|
}
|
|
|
|
- (void)startUpdatingLocationBackground
|
|
{
|
|
NSLog(@"Starting location updates");
|
|
self.locationManager.allowsBackgroundLocationUpdates = TRUE;
|
|
[self.locationManager startUpdatingLocation];
|
|
}
|
|
|
|
- (void)stopUpdatingLocation
|
|
{
|
|
NSLog(@"Stop location updates");
|
|
[self.locationManager stopUpdatingLocation];
|
|
}
|
|
|
|
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
|
|
{
|
|
NSLog(@"Location service failed with error %@", error);
|
|
}
|
|
|
|
- (void)locationManager:(CLLocationManager *)manager
|
|
didUpdateLocations:(NSArray*)locations
|
|
{
|
|
CLLocation *location = [locations lastObject];
|
|
// verifica il tempo trascorso dall'ultimo rilevamento della posizione
|
|
if([location.timestamp timeIntervalSinceDate:[EQNUser defaultUser].lastPosition.timestamp] > TEMPO_AGGIORNAMENTO_POSIZIONE && [EQNUser defaultUser].lastPosition.coordinate.longitude > 0.000000){
|
|
self.posizioneRilevata = NO;
|
|
}
|
|
|
|
// per evitare il ripetersi dell'aggiornamento della posizione utilizzo la variabile posizioneRilevata
|
|
if (!self.posizioneRilevata) {
|
|
self.posizioneRilevata = YES;
|
|
self.currentLocation = location;
|
|
}
|
|
|
|
// se l'app è in background avvia il rilevamento dell'accellerometro
|
|
if ([EQNManager defaultManager].isBackground)
|
|
[[EQNManager defaultManager] controllaStatoApplicazione];
|
|
|
|
}
|
|
|
|
+(void)avviaLetturaAccellerometroisCal:(BOOL)isCal withResult:(void(^)(NSArray *dati)) success{
|
|
|
|
NSMutableArray *dati = [NSMutableArray array];
|
|
CMMotionManager *motionManager = [[CMMotionManager alloc] init];
|
|
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
|
|
motionManager.accelerometerUpdateInterval = kUpdateInterval;
|
|
__block NSTimeInterval cont = 0;
|
|
|
|
NSTimeInterval timer = SECONDI_RILIEVO;
|
|
if (isCal)
|
|
timer = SECONDI_CALIBRAZIONE;
|
|
|
|
[motionManager startAccelerometerUpdatesToQueue:queue withHandler:
|
|
^(CMAccelerometerData *accelerometerData, NSError *error) {
|
|
|
|
if(cont == 0)
|
|
cont = accelerometerData.timestamp;
|
|
|
|
[dati addObject:@([EQNMath powSum:accelerometerData])];
|
|
|
|
if (accelerometerData.timestamp-cont >= timer) {
|
|
[motionManager stopAccelerometerUpdates];
|
|
|
|
NSLog(@"time %f", accelerometerData.timestamp-cont);
|
|
|
|
success([NSArray arrayWithArray:dati]);
|
|
}
|
|
}];
|
|
}
|
|
|
|
@end
|