4d8bbdc7ef
Add to Git the current app version (the same available in the OriginalZip folder)
464 lines
16 KiB
Objective-C
464 lines
16 KiB
Objective-C
//
|
|
// EQNManager.m
|
|
// Earthquake Network
|
|
//
|
|
// Created by Luca Beretta on 17/09/18.
|
|
// Copyright © 2018 Luca Beretta. All rights reserved.
|
|
//
|
|
|
|
#import "EQNManager.h"
|
|
#import <UIKit/UIKit.h>
|
|
#import "EQNCalibrazione.h"
|
|
#import "EQNRilevamento.h"
|
|
#import "EQNUser.h"
|
|
#import "Costanti.h"
|
|
#import "ServerRequest.h"
|
|
#import "EQNGeneratoreURLServer.h"
|
|
#import "EQNReteSmartphone.h"
|
|
#import "EQNDatoGrafico.h"
|
|
#import "EQNPastquakes.h"
|
|
#import "EQNSegnalazione.h"
|
|
#import "EQNSisma.h"
|
|
#import "EQNUtility.h"
|
|
#import "Tsunami.h"
|
|
//#import <CoreLocation/CoreLocation.h>
|
|
|
|
@interface EQNManager()
|
|
|
|
@property (nonatomic, strong) EQNCalibrazione *calibrazione;
|
|
|
|
@property (nonatomic, strong) NSDate *timeStamp;
|
|
@property (nonatomic, assign) BOOL inCalibrazione;
|
|
@property (nonatomic, assign) BOOL inRilevamento;
|
|
|
|
@end
|
|
|
|
@implementation EQNManager
|
|
|
|
static EQNManager *_sharedInstance = nil;
|
|
|
|
+(EQNManager *) defaultManager{
|
|
static EQNManager *instance = nil;
|
|
static dispatch_once_t onceToken;
|
|
dispatch_once(&onceToken, ^{
|
|
instance = [[self alloc]init];
|
|
[instance recuperaDati];
|
|
// [instance scaricaDatiGrafico];
|
|
// [instance scaricaAreaCheck];
|
|
// [instance scaricaPastquakes];
|
|
|
|
});
|
|
return instance;
|
|
}
|
|
|
|
-(void)sincronizza{
|
|
|
|
[self scaricaDatiReteSmartphone];
|
|
|
|
}
|
|
|
|
-(void)scaricaDatiReteSmartphone{
|
|
|
|
[[ServerRequest defaultServerConnectionSingleton] inviaInformazioniAlServerWithURL:[NSURL URLWithString:URL_SERVER_DOWNLOAD] richiesta:downloadDati success:^(id result) {
|
|
|
|
NSLog(@"%@\nresult segnalazioni %@",[NSURL URLWithString:URL_SERVER_DOWNLOAD] ,result);
|
|
self.rete_smartphone = [[EQNReteSmartphone alloc] initWithInfo:result];
|
|
|
|
[self performSelectorOnMainThread:@selector(scaricaDatiGrafico) withObject:nil waitUntilDone:YES];
|
|
|
|
} failure:^(NSError * error) {
|
|
|
|
// NSLog(@"Errore scaricaDatiReteSmartphone %@", error);
|
|
|
|
}];
|
|
}
|
|
|
|
-(void)scaricaDatiGrafico{
|
|
|
|
[[ServerRequest defaultServerConnectionSingleton] inviaInformazioniAlServerWithURL:[NSURL URLWithString:URL_SERVER_GEAFICO_UTENTI] richiesta:graficoSmartPhone success:^(id result) {
|
|
|
|
NSMutableArray *arryTemp = [NSMutableArray array];
|
|
for (NSDictionary *dic in result)
|
|
[arryTemp addObject:[[EQNDatoGrafico alloc] initWithInfo:dic]];
|
|
|
|
self.datiGraficoUtente = arryTemp;
|
|
|
|
// NSLog(@"result scaricaDatiGrafico %@", result);
|
|
|
|
[self performSelectorOnMainThread:@selector(scaricaAreaCheck) withObject:nil waitUntilDone:YES];
|
|
|
|
} failure:^(NSError * error) {
|
|
|
|
// NSLog(@"Errore scaricaDatiGrafico %@", error);
|
|
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
-(void)scaricaAreaCheck{
|
|
|
|
[[ServerRequest defaultServerConnectionSingleton] inviaInformazioniAlServerWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?lat=%f&lon=%f", URL_SERVER_AREA_CHECK, [EQNUser defaultUser].lastPosition.coordinate.latitude, [EQNUser defaultUser].lastPosition.coordinate.longitude]] richiesta:areaCheck success:^(id result) {
|
|
|
|
self.area_check = [[EQNAreaCheck alloc] initWithInfo:result];
|
|
|
|
NSLog(@"%@ \nresult scaricaAreaCheck %@",[NSString stringWithFormat:@"%@?lat%f&lon=%f", URL_SERVER_AREA_CHECK, [EQNUser defaultUser].lastPosition.coordinate.latitude, [EQNUser defaultUser].lastPosition.coordinate.longitude] ,result);
|
|
|
|
[self performSelectorOnMainThread:@selector(scaricaPastquakes) withObject:nil waitUntilDone:YES];
|
|
|
|
} failure:^(NSError * error) {
|
|
|
|
// NSLog(@"Errore scaricaAreaCheck %@", error);
|
|
|
|
}];
|
|
}
|
|
|
|
-(void)scaricaPastquakes{
|
|
|
|
[[ServerRequest defaultServerConnectionSingleton] inviaInformazioniAlServerWithURL:[NSURL URLWithString:URL_SERVER_PAST_QUAKES] richiesta:pastquakes success:^(id result) {
|
|
|
|
NSMutableArray *arryTemp = [NSMutableArray array];
|
|
for (NSDictionary *dic in result)
|
|
[arryTemp addObject:[[EQNPastquakes alloc] initWithInfo:dic]];
|
|
self.datiPastQuakes = arryTemp;
|
|
|
|
// NSLog(@"result scaricaPastquakes %@", result);
|
|
|
|
[self performSelectorOnMainThread:@selector(scaricaSegnalazioniManuali) withObject:nil waitUntilDone:YES];
|
|
|
|
|
|
} failure:^(NSError * error) {
|
|
|
|
// NSLog(@"Errore scaricaPastquakes %@", error);
|
|
|
|
|
|
}];
|
|
}
|
|
|
|
-(void)scaricaSegnalazioniManuali{
|
|
|
|
[[ServerRequest defaultServerConnectionSingleton] inviaInformazioniAlServerWithURL:[NSURL URLWithString:URL_SERVER_DOWNLOAD_SEGNALAZIONI_UTENTE] richiesta:segnalazioneManuale success:^(id result) {
|
|
|
|
NSMutableArray *array = [NSMutableArray array];
|
|
NSArray *tempArray = (NSArray *)result;
|
|
for (NSDictionary *dict in tempArray) {
|
|
|
|
[array addObject:[[EQNSegnalazione alloc] initWithInfo:dict]];
|
|
|
|
}
|
|
|
|
self.elencoSelagnazioniManuali = [NSArray arrayWithArray:array];
|
|
|
|
[self performSelectorOnMainThread:@selector(scaricaReteSismica) withObject:nil waitUntilDone:YES];
|
|
|
|
} failure:^(NSError * error) {
|
|
|
|
// NSLog(@"Errore scaricaSegnalazioniManuali %@", error);
|
|
|
|
}];
|
|
}
|
|
|
|
|
|
-(void)scaricaReteSismica{
|
|
|
|
[[ServerRequest defaultServerConnectionSingleton] inviaInformazioniAlServerWithURL:[NSURL URLWithString:URL_SERVER_DOWNLOAD_RETI_SISMICHE] richiesta:downloadDati success:^(id result) {
|
|
|
|
NSMutableArray *array = [NSMutableArray array];
|
|
for (NSDictionary *dic in result)
|
|
[array addObject:[[EQNSisma alloc] initWithInfo:dic]];
|
|
|
|
self.retiSismiche = [NSArray arrayWithArray:array];
|
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICA_DOWNLOAD_TERMINATO object:nil userInfo:nil];
|
|
|
|
|
|
[self performSelectorOnMainThread:@selector(scaricaTsunami) withObject:nil waitUntilDone:YES];
|
|
[self salvaDati];
|
|
|
|
} failure:^(NSError * error) {
|
|
|
|
// NSLog(@"Errore scaricaReteSismica %@", error);
|
|
|
|
}];
|
|
}
|
|
|
|
-(void)scaricaTsunami{
|
|
|
|
[[ServerRequest defaultServerConnectionSingleton] inviaInformazioniAlServerWithURL:[NSURL URLWithString:URL_SERVER_DOWNLOAD_TSUNAMI] richiesta:segnalazioneManuale success:^(id result) {
|
|
|
|
NSMutableArray *array = [NSMutableArray array];
|
|
NSArray *tempArray = (NSArray *)result;
|
|
for (NSDictionary *dict in tempArray) {
|
|
[array addObject:[[Tsunami alloc] initWithInfo:dict]];
|
|
}
|
|
|
|
// NSLog(@"result scaricaTsunami %@", result);
|
|
|
|
self.listaTsunami = [NSArray arrayWithArray:array];
|
|
|
|
[self performSelectorOnMainThread:@selector(scaricaTempoDisponibile) withObject:nil waitUntilDone:YES];
|
|
|
|
|
|
} failure:^(NSError * error) {
|
|
|
|
// NSLog(@"Errore scaricaTsunami %@", error);
|
|
|
|
}];
|
|
}
|
|
|
|
-(void)scaricaTempoDisponibile{
|
|
|
|
[[ServerRequest defaultServerConnectionSingleton] inviaInformazioniAlServerWithURL:[EQNGeneratoreURLServer urlTempoDisponibile] richiesta:tempoDisponibile success:^(id result) {
|
|
|
|
NSLog(@"%@", result);
|
|
NSLog(@"result scaricaTempoDisponibile %@", result);
|
|
|
|
// [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICA_DOWNLOAD_TERMINATO object:nil userInfo:nil];
|
|
|
|
} failure:^(NSError * error) {
|
|
|
|
NSLog(@"Errore scaricaTempoDisponibile %@", error);
|
|
|
|
|
|
}];
|
|
|
|
|
|
}
|
|
|
|
|
|
/*
|
|
-(void)registerforDeviceLockNotif {
|
|
|
|
//Alert notification
|
|
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),
|
|
NULL, // observer
|
|
displayStatusChanged, // callback
|
|
CFSTR("com.apple.springboard.showingAlertItem"), // event name
|
|
NULL, // object
|
|
CFNotificationSuspensionBehaviorDeliverImmediately);
|
|
|
|
|
|
}
|
|
*/
|
|
|
|
-(void)avviaManager{
|
|
|
|
self.isBackground = YES;
|
|
self.timeStamp = [NSDate date];
|
|
|
|
}
|
|
-(void)stopManager{
|
|
|
|
self.isBackground = NO;;
|
|
self.timeStamp = nil;
|
|
|
|
}
|
|
|
|
-(void)controllaStatoApplicazione{
|
|
|
|
[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
|
|
|
|
switch ([[UIDevice currentDevice] batteryState]) {
|
|
case UIDeviceBatteryStateUnknown:
|
|
[EQNUser defaultUser].inCarica = NO;
|
|
break;
|
|
case UIDeviceBatteryStateUnplugged:
|
|
[EQNUser defaultUser].inCarica = NO;
|
|
break;
|
|
case UIDeviceBatteryStateCharging:
|
|
[EQNUser defaultUser].inCarica = YES;
|
|
break;
|
|
case UIDeviceBatteryStateFull:
|
|
[EQNUser defaultUser].inCarica = YES;
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
NSLog(@"Loanciato controllo batteria ");
|
|
|
|
if ([EQNUser defaultUser].inCarica){
|
|
|
|
// NSLog(@"Tempo Manager %f", [[NSDate date] timeIntervalSinceDate:self.timeStamp]);
|
|
|
|
NSTimeInterval timer = [[NSDate date] timeIntervalSinceDate:self.timeStamp];
|
|
NSLog(@"Timer %f", timer);
|
|
|
|
//[[NSNotificationCenter defaultCenter] postNotificationName:@"AGGIORNA_LOG" object:nil userInfo:@{@"messaggio": [NSString stringWithFormat:@"timer %f ", timer]}];
|
|
|
|
if (!self.calibrazione) {
|
|
if (!self.inCalibrazione) {
|
|
|
|
if(timer > TEMPO_LATENZA_CALIBRAZIONE){
|
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:@"AGGIORNA_LOG" object:nil userInfo:@{@"messaggio": @"Avvio Calibrazione"}];
|
|
|
|
NSLog(@"Avvio Calibrazione");
|
|
self.inCalibrazione = YES;
|
|
[self avviaCalibrazione];
|
|
}
|
|
}
|
|
}else{
|
|
if(timer > TEMPO_RIPETIZIONE_CALIBRAZIONE){
|
|
[self annullaCalibrazione];
|
|
self.inCalibrazione = YES;
|
|
[self avviaCalibrazione];
|
|
}
|
|
|
|
if (!self.inRilevamento && self.calibrazione.stato == calibrato) {
|
|
|
|
[self avviaRilevamento];
|
|
}
|
|
}
|
|
}
|
|
|
|
else
|
|
[self annullaCalibrazione];
|
|
|
|
}
|
|
|
|
-(void)avviaCalibrazione{
|
|
|
|
[EQNCalibrazione startCalibrazione:^(EQNCalibrazione *cal) {
|
|
self.calibrazione = cal;
|
|
|
|
// [self iniviaMessaggioAlServer:nil];
|
|
NSLog(@"Media %f stato %li", cal.media, (long)cal.stato );
|
|
[self performSelectorOnMainThread:@selector(iniviaMessaggioAlServer:)
|
|
withObject:nil
|
|
waitUntilDone:YES];
|
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:@"AGGIORNA_LOG" object:nil userInfo:@{@"messaggio": [NSString stringWithFormat:@"Media %f stato %li", cal.media, (long)cal.stato ]}];
|
|
|
|
|
|
if (self.calibrazione.stato == nonCalibrato){
|
|
|
|
self.inCalibrazione = YES;
|
|
[self performSelectorOnMainThread:@selector(avviaCalibrazione)
|
|
withObject:nil
|
|
waitUntilDone:YES];
|
|
}else{
|
|
|
|
self.inCalibrazione = NO;
|
|
[self performSelectorOnMainThread:@selector(avviaRilevamento)
|
|
withObject:nil
|
|
waitUntilDone:YES];
|
|
|
|
}
|
|
}];
|
|
}
|
|
|
|
-(void)annullaCalibrazione{
|
|
|
|
self.calibrazione = nil;
|
|
self.inRilevamento = NO;
|
|
self.inCalibrazione = NO;
|
|
self.timeStamp = [NSDate date];
|
|
|
|
}
|
|
|
|
-(void)avviaRilevamento{
|
|
|
|
if (self.calibrazione) {
|
|
self.inRilevamento = YES;
|
|
[EQNRilevamento startRilevamentoWithCalibrazione:self.calibrazione result:^(EQNRilevamento *ril) {
|
|
|
|
NSLog(@"Rilevamento data %@ stato %li deviaizone %f ", ril.timestamp, (long)ril.rilievo, ril.deviazione);
|
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:@"AGGIORNA_LOG" object:nil userInfo:@{@"messaggio": [NSString stringWithFormat:@"Rilevamento data %@ stato %li deviaizone %f deviazione calibrazione %f", ril.timestamp, (long)ril.rilievo, ril.deviazione, self.calibrazione.deviazione]}];
|
|
|
|
if (ril == NULL){
|
|
[self performSelectorOnMainThread:@selector(avviaCalibrazione)
|
|
withObject:nil
|
|
waitUntilDone:YES];
|
|
return;
|
|
}
|
|
|
|
switch (ril.rilievo) {
|
|
case positivo:{
|
|
[self performSelectorOnMainThread:@selector(iniviaMessaggioAlServer:)
|
|
withObject:ril
|
|
waitUntilDone:YES];
|
|
|
|
self.calibrazione = nil;
|
|
}
|
|
break;
|
|
case negativo:
|
|
/* [self performSelectorOnMainThread:@selector(riavviaRilevamento)
|
|
withObject:nil
|
|
waitUntilDone:YES];*/
|
|
|
|
self.inRilevamento = NO;
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}];
|
|
}
|
|
}
|
|
|
|
-(void)riavviaRilevamento{
|
|
|
|
if (self.calibrazione)
|
|
[self avviaRilevamento];
|
|
}
|
|
|
|
-(void)iniviaMessaggioAlServer:(EQNRilevamento *)anRilevamento{
|
|
|
|
EQNTipoChiamata tipo = calibrazione;
|
|
EQNCalibrazione *anCalibrazione = [self.calibrazione copy];
|
|
if (anRilevamento.rilievo == positivo){
|
|
tipo = rilevamento;
|
|
[self annullaCalibrazione];
|
|
}
|
|
|
|
[[ServerRequest defaultServerConnectionSingleton] inviaInformazioniAlServerWithURL:[EQNGeneratoreURLServer urlMessagioStatoSmartPhone:anCalibrazione rilevamento:anRilevamento] richiesta:tipo success:^(id result) {
|
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:@"AGGIORNA_LOG" object:nil userInfo:@{@"messaggio": [NSString stringWithFormat:@"invio informazione al server tipo %ld inviato ", (long)tipo]}];
|
|
|
|
} failure:^(NSError *error) {
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:@"AGGIORNA_LOG" object:nil userInfo:@{@"messaggio": [NSString stringWithFormat:@"invio informazione al server tipo %ld fallito ", (long)tipo]}];
|
|
}];
|
|
}
|
|
|
|
// Salvataggio dati
|
|
|
|
-(void)salvaDati{
|
|
|
|
/*
|
|
[EQNUtility saveCustomObject:self.retiSismiche key:MANAGER_DATA_RETE_SMARTPHONE];
|
|
[EQNUtility saveCustomObject:self.area_check key:MANAGER_DATA_AREA_CHECK];
|
|
|
|
[EQNUtility writeArrayWithCustomObjToUserDefaults:MANAGER_DATA_DATI_GRAFICO_UTENTI withArray:self.datiGraficoUtente];
|
|
[EQNUtility writeArrayWithCustomObjToUserDefaults:MANAGER_DATA_DATI_PASTQUAKES withArray:self.datiPastQuakes];
|
|
[EQNUtility writeArrayWithCustomObjToUserDefaults:MANAGER_DATA_ELENCO_SEGNALAZIONI_MANUALI withArray:self.elencoSelagnazioniManuali];
|
|
[EQNUtility writeArrayWithCustomObjToUserDefaults:MANAGER_DATA_RETI_SISMICHE withArray:self.retiSismiche];
|
|
[EQNUtility writeArrayWithCustomObjToUserDefaults:MANAGER_DATA_LISTA_TSUNAMI withArray:self.listaTsunami];
|
|
*/
|
|
}
|
|
|
|
-(void)recuperaDati{
|
|
/*
|
|
self.retiSismiche = [EQNUtility readArrayWithCustomObjFromUserDefaults:MANAGER_DATA_RETI_SISMICHE];
|
|
|
|
self.datiGraficoUtente = [EQNUtility readArrayWithCustomObjFromUserDefaults:MANAGER_DATA_DATI_GRAFICO_UTENTI];
|
|
|
|
self.datiPastQuakes = [EQNUtility readArrayWithCustomObjFromUserDefaults:MANAGER_DATA_DATI_PASTQUAKES];
|
|
|
|
self.elencoSelagnazioniManuali = [EQNUtility readArrayWithCustomObjFromUserDefaults:MANAGER_DATA_ELENCO_SEGNALAZIONI_MANUALI];
|
|
|
|
self.listaTsunami = [EQNUtility readArrayWithCustomObjFromUserDefaults:MANAGER_DATA_LISTA_TSUNAMI];
|
|
|
|
self.rete_smartphone = (EQNReteSmartphone *)[EQNUtility loadCustomObjectWithKey:MANAGER_DATA_RETE_SMARTPHONE];
|
|
|
|
self.area_check = (EQNAreaCheck *)[EQNUtility loadCustomObjectWithKey:MANAGER_DATA_AREA_CHECK];
|
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICA_DOWNLOAD_TERMINATO object:nil userInfo:nil];
|
|
*/
|
|
}
|
|
|
|
@end
|
|
|