501 lines
19 KiB
Objective-C
501 lines
19 KiB
Objective-C
//
|
|
// ServerRequest.m
|
|
// Telegea
|
|
//
|
|
// Created by Luca Beretta on 15/02/17.
|
|
// Copyright © 2017 Luca Beretta. All rights reserved.
|
|
//
|
|
|
|
#import "ServerRequest.h"
|
|
#import "Reachability.h"
|
|
#import "Costanti.h"
|
|
#import "EQNUtility.h"
|
|
#import "NSDictionary+BVJSONString.h"
|
|
|
|
//#import "User.h"
|
|
//#import "Portachiavi.h"
|
|
|
|
@interface ServerRequest (){
|
|
|
|
NSURLSession *session;
|
|
|
|
}
|
|
@property (nonatomic) Reachability *hostReachability;
|
|
@property (nonatomic) Reachability *internetReachability;
|
|
|
|
@end
|
|
|
|
@implementation ServerRequest
|
|
|
|
static ServerRequest *_sharedInstance = nil;
|
|
|
|
+(ServerRequest *) defaultServerConnectionSingleton{
|
|
|
|
@synchronized (_sharedInstance) {
|
|
|
|
if (_sharedInstance == nil){
|
|
_sharedInstance = [[ServerRequest alloc] init];
|
|
|
|
}
|
|
[_sharedInstance initSingleton];
|
|
|
|
return _sharedInstance;
|
|
}
|
|
}
|
|
|
|
-(void)initSingleton{
|
|
|
|
NSURLSessionConfiguration *config = [NSURLSessionConfiguration ephemeralSessionConfiguration];
|
|
session = [NSURLSession sessionWithConfiguration:config];
|
|
|
|
// registro notifiche rilevamento connessione
|
|
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
|
|
NSString *remoteHostName = @"http://srv.earthquakenetwork.it";
|
|
|
|
self.hostReachability = [Reachability reachabilityWithHostName:remoteHostName];
|
|
[self.hostReachability startNotifier];
|
|
[self setValue:@([self updateConnectionhReachability:self.hostReachability]) forKey:@"isConnect"];
|
|
// self.isConnect = [self updateConnectionhReachability:self.hostReachability];
|
|
|
|
self.internetReachability = [Reachability reachabilityForInternetConnection];
|
|
[self.internetReachability startNotifier];
|
|
// self.isConnect = [self updateConnectionhReachability:self.internetReachability];
|
|
[self setValue:@([self updateConnectionhReachability:self.internetReachability]) forKey:@"isConnect"];
|
|
|
|
}
|
|
|
|
/*!
|
|
* Called by Reachability whenever status changes.
|
|
*/
|
|
- (void) reachabilityChanged:(NSNotification *)note
|
|
{
|
|
Reachability* curReach = [note object];
|
|
NSParameterAssert([curReach isKindOfClass:[Reachability class]]);
|
|
// self.isConnect = [self updateConnectionhReachability:curReach];
|
|
|
|
[self setValue:@([self updateConnectionhReachability:curReach]) forKey:@"isConnect"];
|
|
}
|
|
|
|
- (BOOL)updateConnectionhReachability:(Reachability *)reachability
|
|
{
|
|
NetworkStatus netStatus = [reachability currentReachabilityStatus];
|
|
switch (netStatus) {
|
|
case NotReachable:
|
|
return NO;
|
|
break;
|
|
default:
|
|
return YES;
|
|
break;
|
|
}
|
|
|
|
}
|
|
|
|
-(void)inviaInformazioniAlServerWithURL:(NSURL *)url richiesta:(EQNTipoChiamata )chiamata success:(void(^)(id result)) success failure:(void(^)(NSError *))failure{
|
|
|
|
if (!self.isConnect) {
|
|
|
|
NSError *error = [NSError errorWithDomain:NSMachErrorDomain code:401 userInfo:@{MESSAGGIO : NSLocalizedString(@"Nessuna connessione", @"messaggio assenza connesione")}];
|
|
failure(error);
|
|
|
|
}
|
|
|
|
NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:url];
|
|
[urlRequest setHTTPMethod:@"GET"];
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
|
|
});
|
|
|
|
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:urlRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error){
|
|
|
|
NSHTTPURLResponse *httpResp = (NSHTTPURLResponse *)response;
|
|
|
|
|
|
if(!error){
|
|
|
|
NSError *jsonError;
|
|
// NSData *myRequestData = [response dataUsingEncoding:NSUTF8StringEncoding];
|
|
id JSON = [NSJSONSerialization JSONObjectWithData:data
|
|
options:NSJSONReadingAllowFragments
|
|
error:&jsonError];
|
|
|
|
if (!jsonError){
|
|
|
|
switch (httpResp.statusCode) {
|
|
case 200:
|
|
switch (chiamata) {
|
|
case registrazione:
|
|
success(JSON);
|
|
break;
|
|
case posizione:
|
|
success(@"success");
|
|
break;
|
|
case calibrazione:
|
|
success(JSON);
|
|
break;
|
|
case rilevamento:
|
|
success(JSON);
|
|
break;
|
|
case downloadDati:
|
|
success(JSON);
|
|
break;
|
|
case graficoSmartPhone:
|
|
success(JSON);
|
|
break;
|
|
case areaCheck:
|
|
success(JSON);
|
|
break;
|
|
case pastquakes:
|
|
success(JSON);
|
|
break;
|
|
case utentiDisponibili:
|
|
success(JSON);
|
|
break;
|
|
case segnalazioneManuale:
|
|
success(JSON);
|
|
break;
|
|
case tsunami:
|
|
success(JSON);
|
|
break;
|
|
case segnalazzioneTerremoto:
|
|
success(JSON);
|
|
break;
|
|
case commentoTerremoto:
|
|
success(JSON);
|
|
break;
|
|
case tempoDisponibile:
|
|
success(JSON);
|
|
break;
|
|
case impostazioniNotifiche:
|
|
success(JSON);
|
|
break;
|
|
case offerTimeRemaining:
|
|
success(JSON);
|
|
default:
|
|
break;
|
|
}
|
|
// NSLog(@"Risultato richiesta server %@", JSON);
|
|
// success(@"");
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
|
|
});
|
|
}
|
|
}
|
|
else{
|
|
|
|
NSString* newStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
|
|
if (newStr) {
|
|
if (chiamata == segnalazzioneTerremoto) {
|
|
|
|
NSLog(@"risultato segnalazione %@", [EQNUtility clearStringMessaggi:newStr]);
|
|
success([EQNUtility clearStringMessaggi:newStr]);
|
|
|
|
}
|
|
|
|
if (chiamata == rilevamento) {
|
|
|
|
/// NSLog(@"newStr %@", newStr);
|
|
|
|
success([EQNUtility clearStringMessaggi:newStr]);
|
|
|
|
|
|
}
|
|
|
|
if (chiamata == registrazione) {
|
|
|
|
NSLog(@"newStr %@", newStr);
|
|
|
|
}
|
|
if (chiamata == tempoDisponibile) {
|
|
|
|
NSLog(@"newStr %@", newStr);
|
|
|
|
}
|
|
|
|
if (chiamata == calibrazione) {
|
|
|
|
NSLog(@"calibrazione %@", newStr);
|
|
success(newStr);
|
|
|
|
}
|
|
if (chiamata == rilevamento) {
|
|
|
|
success(newStr);
|
|
|
|
}
|
|
if (chiamata == impostazioniNotifiche) {
|
|
|
|
NSLog(@"impostazioniNotifiche %@", newStr);
|
|
|
|
success(newStr);
|
|
|
|
}
|
|
if (chiamata == segnalazioneManuale) {
|
|
|
|
NSLog(@"segnalazioneManuale %@", newStr);
|
|
|
|
}
|
|
} else{
|
|
|
|
|
|
NSLog(@"Error %@", [jsonError description]);
|
|
|
|
NSError *error = [NSError errorWithDomain:NSMachErrorDomain code:0 userInfo:@{MESSAGGIO : NSLocalizedString(@"Username e Password sbagliati", @"messaggio errore inserimento credenziali")}];
|
|
failure(error);
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
|
|
});
|
|
|
|
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
[dataTask resume];
|
|
|
|
}
|
|
|
|
-(void)inviaRicevuta:(NSData *)ricevuta success:(void(^)(id result)) success failure:(void(^)(NSError *))failure{
|
|
|
|
|
|
NSString *jsonObjectString = [ricevuta base64EncodedStringWithOptions:0];
|
|
|
|
// NSString* BASEURl = @"XXXXXXXXX/XXX/XXXX";
|
|
// NSString *testReceipt= [BASEURl stringByAppendingString:@"/InApp/iOS/updatePurchase"];
|
|
NSDictionary *params = @{@"ricevuta" :jsonObjectString};
|
|
NSString *parametri = [params bv_jsonStringWithPrettyPrint:YES];
|
|
|
|
NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@""]];
|
|
[urlRequest setHTTPMethod:@"POST"];
|
|
[urlRequest addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
|
|
[urlRequest addValue:@"application/json" forHTTPHeaderField:@"Accept"];
|
|
[urlRequest setHTTPBody:[parametri dataUsingEncoding:NSUTF8StringEncoding]];
|
|
|
|
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:urlRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error){
|
|
|
|
NSHTTPURLResponse *httpResp = (NSHTTPURLResponse *)response;
|
|
|
|
if(!error){
|
|
|
|
NSError *jsonError;
|
|
// NSData *myRequestData = [response dataUsingEncoding:NSUTF8StringEncoding];
|
|
NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:data
|
|
options:NSJSONReadingAllowFragments
|
|
error:&jsonError];
|
|
|
|
if (!jsonError){
|
|
|
|
switch (httpResp.statusCode) {
|
|
case 200:
|
|
success(JSON);
|
|
|
|
break;
|
|
|
|
default:
|
|
NSLog(@"httpResp.statusCode %ld", (long)httpResp.statusCode);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
}
|
|
else{
|
|
|
|
failure(error.userInfo[@"NSLocalizedDescription"]);
|
|
NSLog(@"Error %@", error.userInfo);
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
[dataTask resume];
|
|
|
|
|
|
}
|
|
|
|
/*
|
|
-(void)sendGetMethod:(NSURLRequest *)request typeRequest:(TypeRequest)type success:(void(^)(id dictionary)) success failure:(void(^)(NSError *))failure{
|
|
|
|
if (!self.isConnect) {
|
|
|
|
NSError *error = [NSError errorWithDomain:NSMachErrorDomain code:401 userInfo:@{MESSAGGIO : NSLocalizedString(@"Nessuna connessione", @"messaggio assenza connesione")}];
|
|
failure(error);
|
|
|
|
}
|
|
|
|
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
|
|
|
|
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error){
|
|
|
|
NSHTTPURLResponse *httpResp = (NSHTTPURLResponse *)response;
|
|
|
|
if(!error){
|
|
|
|
NSError *jsonError;
|
|
|
|
NSDictionary *showJSON = [NSJSONSerialization JSONObjectWithData:data
|
|
options:NSJSONReadingAllowFragments
|
|
error:&jsonError];
|
|
if (!jsonError){
|
|
|
|
switch (httpResp.statusCode) {
|
|
case 200:
|
|
success(showJSON);
|
|
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
|
|
break;
|
|
}
|
|
}
|
|
else{
|
|
|
|
NSError *error;
|
|
switch (type) {
|
|
case login:
|
|
error = [NSError errorWithDomain:NSMachErrorDomain code:login userInfo:@{MESSAGGIO : NSLocalizedString(@"Username e Password sbagliati", @"messaggio errore inserimento credenziali")}];
|
|
break;
|
|
case reading:
|
|
error = [NSError errorWithDomain:NSMachErrorDomain code:reading userInfo:@{MESSAGGIO : NSLocalizedString(@"Errore! Impossibile accedere all'impianto", @"messaggio errore richiesta apikey impianto")}];
|
|
break;
|
|
case sending:
|
|
// error = [NSError errorWithDomain:NSMachErrorDomain code:httpResp.statusCode userInfo:@{MESSAGGIO : NSLocalizedString(@"sending", @"")}];
|
|
case zona:
|
|
error = [NSError errorWithDomain:NSMachErrorDomain code:zona userInfo:@{MESSAGGIO : NSLocalizedString(@"Sì è verificato un errore sulla centralina", @"messaggio errore richiesta dev_cmd")}];
|
|
break;
|
|
default:
|
|
error = [NSError errorWithDomain:NSMachErrorDomain code:httpResp.statusCode userInfo:@{MESSAGGIO : NSLocalizedString(@"Errore sconosciuto", @"")}];
|
|
|
|
break;
|
|
}
|
|
failure(error);
|
|
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
[dataTask resume];
|
|
}
|
|
|
|
|
|
|
|
-(void)caricaImpianto:(Plant *)plant success:(void(^)(BOOL))success failure:(void(^)(NSError *))failure{
|
|
|
|
NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:[self urlWithInfo:plant]];
|
|
[urlRequest setHTTPMethod:@"GET"];
|
|
|
|
|
|
[self sendGetMethod:urlRequest
|
|
typeRequest:login
|
|
success:^(NSDictionary *result){
|
|
|
|
plant.apikey = result[@"apikey"];
|
|
[self caricaZoneiWithPlant:plant success:^(BOOL result){
|
|
|
|
|
|
// caricamento zone
|
|
success(result);
|
|
|
|
}failure:^(NSError *error){
|
|
// caricamento zone
|
|
failure(error);
|
|
}];
|
|
}
|
|
failure:^(NSError *error){
|
|
|
|
// caricamento impianto
|
|
|
|
failure(error);
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
-(NSURL *)urlWithInfo:(Plant *)plant{
|
|
|
|
NSURL *url =[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@=%@&%@=%@&%@=%@", LOGIN_URL, USERNAME, [User defaultUser].nome, PSW, [Portachiavi getCredenziali:[User defaultUser].nome], PLANT_ID, plant.plant_id]];
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
-(void)caricaZoneiWithPlant:(Plant *)plant success:(void(^)(BOOL))success failure:(void(^)(NSError *))failure{
|
|
|
|
|
|
NSString *url = [NSString stringWithFormat:@"%@?%@=%@&%@=%@&%@=[{\"mod\":10,\"reg\":100}]", DEVICE_COMMANDS_URL, APIKEY, plant.apikey, PLANT_ID, plant.plant_id, DEV_CMD];
|
|
|
|
NSURL *theUrl = [NSURL URLWithString:[url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]]];
|
|
|
|
NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:theUrl];
|
|
[urlRequest setHTTPMethod:@"GET"];
|
|
[self sendGetMethod:urlRequest
|
|
typeRequest:reading
|
|
success:^(NSDictionary *result){
|
|
|
|
NSArray *valori = result[@"result"];
|
|
NSNumber *numeroZone;
|
|
|
|
if(valori.count == 0){
|
|
//numeroZone = @1;
|
|
NSError *error = [NSError errorWithDomain:NSMachErrorDomain code:0 userInfo:@{MESSAGGIO : NSLocalizedString(@"Errore! Impossibile accedere all'impianto", @"messaggio errore richiesta apikey impianto")}];
|
|
failure(error);
|
|
return ;
|
|
|
|
}
|
|
else if([[valori firstObject][@"val"] isEqual:[NSNull null]]){
|
|
//numeroZone = @1;
|
|
NSError *error = [NSError errorWithDomain:NSMachErrorDomain code:0 userInfo:@{MESSAGGIO : NSLocalizedString(@"Errore! Impossibile accedere all'impianto", @"messaggio errore richiesta apikey impianto")}];
|
|
failure(error);
|
|
return ;
|
|
|
|
}
|
|
else
|
|
numeroZone = [valori firstObject][@"val"];
|
|
|
|
|
|
[plant creaZone:[numeroZone intValue]];
|
|
success(YES);
|
|
}
|
|
|
|
failure:^(NSError *error){
|
|
failure(error);
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
-(UIAlertController *)mostraAllertWithMessage:(NSString *)messaggio close:(void(^)(BOOL terminato))terminato{
|
|
|
|
NSString *titolo = @"";
|
|
if ([messaggio isEqualToString:NSLocalizedString(@"Nessuna connessione", @"messaggio assenza connesione")])
|
|
titolo = NSLocalizedString(@"Attenzione", @"");
|
|
|
|
UIAlertController* alert = [UIAlertController alertControllerWithTitle:titolo//NSLocalizedString(@"Attenzione", @"")
|
|
message:messaggio
|
|
preferredStyle:UIAlertControllerStyleAlert];
|
|
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"")
|
|
style:UIAlertActionStyleDefault
|
|
handler:^(UIAlertAction * action) {
|
|
|
|
terminato(YES);
|
|
|
|
}];
|
|
[alert addAction:defaultAction];
|
|
|
|
return alert;
|
|
}
|
|
|
|
- (void)dealloc
|
|
{
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self name:kReachabilityChangedNotification object:nil];
|
|
}
|
|
*/
|
|
|
|
@end
|