156 lines
4.8 KiB
Objective-C
156 lines
4.8 KiB
Objective-C
//
|
|
// EQNAccelerometroManager.m
|
|
// Earthquake Network
|
|
//
|
|
// Created by Luca Beretta on 19/09/18.
|
|
// Copyright © 2018 Luca Beretta. All rights reserved.
|
|
//
|
|
|
|
#import "EQNAccelerometroManager.h"
|
|
#import <CoreMotion/CoreMotion.h>
|
|
#import "Costanti.h"
|
|
#import "EQNMath.h"
|
|
#import "EQNUser.h"
|
|
#import "EQNManager.h"
|
|
|
|
@interface EQNAccelerometroManager () <CLLocationManagerDelegate>
|
|
@property (nonatomic, assign) BOOL posizioneRilevata;
|
|
@end
|
|
|
|
@implementation EQNAccelerometroManager
|
|
|
|
#pragma mark - Singleton
|
|
|
|
+ (instancetype)sharedInstance
|
|
{
|
|
static EQNAccelerometroManager *instance = nil;
|
|
static dispatch_once_t onceToken;
|
|
dispatch_once(&onceToken, ^{
|
|
instance = [[self alloc]init];
|
|
});
|
|
return instance;
|
|
}
|
|
|
|
#pragma mark - Init
|
|
|
|
- (instancetype)init
|
|
{
|
|
self = [super init];
|
|
if (self) {
|
|
self.locationManager = [[CLLocationManager alloc] init];
|
|
if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]){
|
|
[self.locationManager requestAlwaysAuthorization];
|
|
}
|
|
self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
|
|
self.locationManager.delegate = self;
|
|
self.posizioneRilevata = NO;
|
|
}
|
|
return self;
|
|
}
|
|
|
|
#pragma mark - CLLocationManagerDelegate
|
|
|
|
- (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;
|
|
}
|
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:EQNAuthorizationStatusDidChangeNotification object:nil userInfo:@{}];
|
|
}
|
|
|
|
- (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];
|
|
}
|
|
}
|
|
|
|
#pragma mark - Public
|
|
|
|
- (void)getPosition
|
|
{
|
|
[self.locationManager requestLocation];
|
|
}
|
|
|
|
- (void)startUpdatingLocationBackground
|
|
{
|
|
NSLog(@"Starting location updates");
|
|
self.locationManager.allowsBackgroundLocationUpdates = TRUE;
|
|
[self.locationManager startUpdatingLocation];
|
|
}
|
|
|
|
- (void)stopUpdatingLocation
|
|
{
|
|
NSLog(@"Stop location updates");
|
|
[self.locationManager stopUpdatingLocation];
|
|
}
|
|
|
|
+ (void)avviaLetturaAccellerometroIsCalibrazione:(BOOL)isCalibrazione withCompletion:(void(^)(NSArray *dati))completion
|
|
{
|
|
__block 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 (isCalibrazione) {
|
|
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);
|
|
completion([NSArray arrayWithArray:dati]);
|
|
}
|
|
}];
|
|
}
|
|
|
|
@end
|