...
|
...
|
@@ -6,6 +6,9 @@ |
|
|
// Copyright (c) 2015年 YOHO. All rights reserved.
|
|
|
//
|
|
|
|
|
|
#import <CoreTelephony/CTCarrier.h>
|
|
|
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
|
|
|
#import "Reachability.h"
|
|
|
#import "YHAssemblyAssistant.h"
|
|
|
//#import "YHErrorFactory.h"
|
|
|
//#import "YHEventFactory.h"
|
...
|
...
|
@@ -21,6 +24,12 @@ |
|
|
#define kPathLogSystem [kPathCaches stringByAppendingPathComponent:@"YHLogSystem"]
|
|
|
#define kYASApiMethod @"yas_mobile"
|
|
|
|
|
|
NSString * const kNetWorkStatusUnknow = @"0";
|
|
|
NSString * const kNetWorkStatusWIFI = @"1";
|
|
|
NSString * const kNetWorkStatus2G = @"2";
|
|
|
NSString * const kNetWorkStatus3G = @"3";
|
|
|
NSString * const kNetWorkStatus4G = @"4";
|
|
|
|
|
|
NSString * const JsonKeyDataTypeDevice = @"device";
|
|
|
NSString * const JsonKeyDataTypeStatus = @"status";
|
|
|
NSString * const JsonKeyDataTypeEvents = @"events";
|
...
|
...
|
@@ -39,18 +48,58 @@ static dispatch_queue_t persisting_queue() { |
|
|
|
|
|
@interface YHAssemblyAssistant ()
|
|
|
|
|
|
/**
|
|
|
* 定位服务
|
|
|
*/
|
|
|
@property (strong, nonatomic) CLLocationManager *locationManager;
|
|
|
/**
|
|
|
* 设备信息
|
|
|
*/
|
|
|
@property (strong, nonatomic) YHDevice *device;
|
|
|
/**
|
|
|
* 当前状态信息
|
|
|
*/
|
|
|
@property (strong, nonatomic) YHStatus *currentStatus;
|
|
|
@property (strong, nonatomic) NSMutableDictionary *immediUploadItemDic; // 用于立即发送策略下(device信息是相同的)
|
|
|
@property (assign, nonatomic) NSUInteger allEventsCount; // 本地持久化event的条数
|
|
|
/**
|
|
|
* 用于立即发送策略下(device信息是相同的)
|
|
|
*/
|
|
|
@property (strong, nonatomic) NSMutableDictionary *immediUploadItemDic;
|
|
|
/**
|
|
|
* 本地持久化event的条数
|
|
|
*/
|
|
|
@property (assign, nonatomic) NSUInteger allEventsCount;
|
|
|
/**
|
|
|
* 日志文件存储路径
|
|
|
*/
|
|
|
@property (strong, nonatomic) NSString *eventFilePath;
|
|
|
/**
|
|
|
* 日志文件名称
|
|
|
*/
|
|
|
@property (strong, nonatomic) NSString *eventFileName;
|
|
|
/**
|
|
|
* 文件管理服务
|
|
|
*/
|
|
|
@property (strong, nonatomic) NSFileManager *fileManager;
|
|
|
|
|
|
|
|
|
// 启动定位服务
|
|
|
- (void)startLocationService;
|
|
|
// 开启网络状态监测
|
|
|
- (void)startObserverNetworkReachabilityStatus;
|
|
|
// 将一个事件写入到文件
|
|
|
- (BOOL)writeEventToFile:(YHEvent *)eventData;
|
|
|
// 将一个错误写入到文件
|
|
|
- (BOOL)writeErrorToFile:(YHError *)errorData;
|
|
|
// 当触发预设的条件时,自动上传数据
|
|
|
- (void)autoUploadData;
|
|
|
// 删除本地持久化文件
|
|
|
- (void)removeLocalFile;
|
|
|
// 更新网络状态
|
|
|
- (BOOL)updateNetWorkStatus:(NSString *)status;
|
|
|
// 更新位置信息
|
|
|
- (BOOL)updateLocation:(CLLocation *)location;
|
|
|
// 更新立即上传信息
|
|
|
- (void)updateImmediUploadDic;
|
|
|
|
|
|
@end
|
|
|
|
...
|
...
|
@@ -110,6 +159,18 @@ static dispatch_queue_t persisting_queue() { |
|
|
return _eventFileName;
|
|
|
}
|
|
|
|
|
|
- (CLLocationManager *)locationManager
|
|
|
{
|
|
|
if (!_locationManager) {
|
|
|
_locationManager = [[CLLocationManager alloc]init];
|
|
|
_locationManager.delegate = self;
|
|
|
_locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 精度越高,越费电,根据需要设定
|
|
|
_locationManager.distanceFilter = 1000; // 位置移动1000米,发一次更新消息
|
|
|
}
|
|
|
|
|
|
return _locationManager;
|
|
|
}
|
|
|
|
|
|
#pragma mark - public method
|
|
|
|
|
|
- (BOOL)canPersisting
|
...
|
...
|
@@ -141,21 +202,10 @@ static dispatch_queue_t persisting_queue() { |
|
|
|
|
|
// 设置status
|
|
|
[self.immediUploadItemDic setObject: [[NSMutableArray alloc]initWithObjects:self.currentStatus.jsonDictionary, nil] forKey:JsonKeyDataTypeStatus];
|
|
|
|
|
|
}
|
|
|
|
|
|
- (void)updateImmediUploadDic
|
|
|
{
|
|
|
NSMutableArray *allStatus = [self.immediUploadItemDic objectForKey:JsonKeyDataTypeStatus];
|
|
|
if ([allStatus count]) {
|
|
|
[allStatus replaceObjectAtIndex:0 withObject:self.currentStatus.jsonDictionary];
|
|
|
[self.immediUploadItemDic setObject:allStatus forKey:JsonKeyDataTypeStatus];
|
|
|
} else {
|
|
|
NSException *exception = [NSException exceptionWithName:NSGenericException
|
|
|
reason:@"appId and sessionId do not be initialized. Please use prepareImmediUploadDic:sessionId: first."
|
|
|
userInfo:nil];
|
|
|
[exception raise];
|
|
|
}
|
|
|
|
|
|
[self startObserverNetworkReachabilityStatus];
|
|
|
|
|
|
[self startLocationService];
|
|
|
}
|
|
|
|
|
|
#pragma mark - 持久化数据
|
...
|
...
|
@@ -266,6 +316,93 @@ static dispatch_queue_t persisting_queue() { |
|
|
}
|
|
|
}
|
|
|
|
|
|
#pragma mark - private method
|
|
|
|
|
|
// 添加网络监测回调block
|
|
|
- (void)startObserverNetworkReachabilityStatus
|
|
|
{
|
|
|
/**
|
|
|
CTRadioAccessTechnologyGPRS - 2G
|
|
|
CTRadioAccessTechnologyEdge - 2G (sometimes called 2.5G)
|
|
|
CTRadioAccessTechnologyWCDMA - 3G
|
|
|
CTRadioAccessTechnologyHSDPA - 3G (sometimes called 3.5G)
|
|
|
CTRadioAccessTechnologyHSUPA - 3G
|
|
|
CTRadioAccessTechnologyCDMA1x - 2G
|
|
|
CTRadioAccessTechnologyCDMAEVDORev0 - 3G
|
|
|
CTRadioAccessTechnologyCDMAEVDORevA - 3G
|
|
|
CTRadioAccessTechnologyCDMAEVDORevB - 3G
|
|
|
CTRadioAccessTechnologyeHRPD - 3G (or 3.5 - eHRPD is to allow migration from CDMA EVDO to LTE)
|
|
|
CTRadioAccessTechnologyLTE - 4G (allowed to be called 4G by the ITU as mentioend above)
|
|
|
*/
|
|
|
[[YHNetworkAssistant sharedInstance] addReachabilityChangedHandler:^(AFNetworkReachabilityStatus networkStatus) {
|
|
|
NSString *net = kNetWorkStatusUnknow;
|
|
|
|
|
|
CTTelephonyNetworkInfo *netInfo = [[CTTelephonyNetworkInfo alloc] init];
|
|
|
switch (networkStatus) {
|
|
|
case AFNetworkReachabilityStatusUnknown:
|
|
|
case AFNetworkReachabilityStatusNotReachable:
|
|
|
net = kNetWorkStatusUnknow;
|
|
|
break;
|
|
|
|
|
|
case AFNetworkReachabilityStatusReachableViaWWAN:
|
|
|
{
|
|
|
if (NSFoundationVersionNumber >= NSFoundationVersionNumber_iOS_7_0) {
|
|
|
if ([netInfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyGPRS]|| [netInfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyEdge]) {
|
|
|
net = kNetWorkStatus2G;
|
|
|
} else if([netInfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyWCDMA]|| [netInfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyHSDPA] || [netInfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyHSUPA]|| [netInfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyeHRPD]) {
|
|
|
net = kNetWorkStatus3G;
|
|
|
} else if([netInfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyLTE]) {
|
|
|
net = kNetWorkStatus4G;
|
|
|
} else {
|
|
|
net = kNetWorkStatusUnknow;
|
|
|
}
|
|
|
} else {
|
|
|
net = kNetWorkStatusUnknow;
|
|
|
}
|
|
|
}
|
|
|
break;
|
|
|
case AFNetworkReachabilityStatusReachableViaWiFi:
|
|
|
net = kNetWorkStatusWIFI;
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
if ([self updateNetWorkStatus:net]) {
|
|
|
[self updateImmediUploadDic];
|
|
|
}
|
|
|
}];
|
|
|
}
|
|
|
|
|
|
// 启动定位服务
|
|
|
- (void)startLocationService
|
|
|
{
|
|
|
//判断用户定位服务是否开启
|
|
|
if ([CLLocationManager locationServicesEnabled]) {
|
|
|
[self.locationManager startUpdatingLocation];
|
|
|
} else {
|
|
|
//不能定位用户的位置
|
|
|
//1.提醒用户检查当前的网络状况
|
|
|
//2.提醒用户打开定位开关
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 更新网络状态
|
|
|
- (BOOL)updateNetWorkStatus:(NSString *)status
|
|
|
{
|
|
|
if (status == nil || [status length] == 0) {
|
|
|
return NO;
|
|
|
}
|
|
|
|
|
|
if ([status isEqualToString:self.currentStatus.net]) {
|
|
|
return NO;
|
|
|
}
|
|
|
|
|
|
self.currentStatus.net = status;
|
|
|
return YES;
|
|
|
}
|
|
|
|
|
|
- (BOOL)updateLocation:(CLLocation *)location
|
|
|
{
|
|
|
if (location == nil || ![location isKindOfClass:[CLLocation class]]) {
|
...
|
...
|
@@ -294,22 +431,21 @@ static dispatch_queue_t persisting_queue() { |
|
|
return YES;
|
|
|
}
|
|
|
|
|
|
- (BOOL)updateNetWorkStatus:(NSString *)status
|
|
|
// 更新立即上传信息
|
|
|
- (void)updateImmediUploadDic
|
|
|
{
|
|
|
if (status == nil || [status length] == 0) {
|
|
|
return NO;
|
|
|
}
|
|
|
|
|
|
if ([status isEqualToString:self.currentStatus.net]) {
|
|
|
return NO;
|
|
|
NSMutableArray *allStatus = [self.immediUploadItemDic objectForKey:JsonKeyDataTypeStatus];
|
|
|
if ([allStatus count]) {
|
|
|
[allStatus replaceObjectAtIndex:0 withObject:self.currentStatus.jsonDictionary];
|
|
|
[self.immediUploadItemDic setObject:allStatus forKey:JsonKeyDataTypeStatus];
|
|
|
} else {
|
|
|
NSException *exception = [NSException exceptionWithName:NSGenericException
|
|
|
reason:@"appId and sessionId do not be initialized. Please use prepareImmediUploadDic:sessionId: first."
|
|
|
userInfo:nil];
|
|
|
[exception raise];
|
|
|
}
|
|
|
|
|
|
self.currentStatus.net = status;
|
|
|
return YES;
|
|
|
}
|
|
|
|
|
|
#pragma mark - private method
|
|
|
|
|
|
// 将一个事件写入到文件
|
|
|
- (BOOL)writeEventToFile:(YHEvent *)event
|
|
|
{
|
...
|
...
|
@@ -452,4 +588,51 @@ static dispatch_queue_t persisting_queue() { |
|
|
self.allEventsCount = 0;
|
|
|
}
|
|
|
|
|
|
#pragma mark - CLLocationManagerDelegate
|
|
|
|
|
|
// 返回定位服务认证状态的委托方法
|
|
|
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
|
|
|
{
|
|
|
switch (status) {
|
|
|
case kCLAuthorizationStatusNotDetermined: {
|
|
|
if ([manager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
|
|
|
[manager requestAlwaysAuthorization];
|
|
|
}
|
|
|
}
|
|
|
break;
|
|
|
case kCLAuthorizationStatusRestricted:
|
|
|
//定位服务无法使用,请开启手机定位服务
|
|
|
break;
|
|
|
case kCLAuthorizationStatusDenied:
|
|
|
//程序定位服务未开启, 您需要去设置界面启用我们App定位服务
|
|
|
break;
|
|
|
default:
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//协议中的方法,作用是每当位置发生更新时会调用的委托方法
|
|
|
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
|
|
|
{
|
|
|
//locations数组里边存放的是CLLocation对象,一个CLLocation对象就代表着一个位置
|
|
|
CLLocation *newLocation = [locations firstObject];
|
|
|
|
|
|
if ([self updateLocation:newLocation]) {
|
|
|
[self updateImmediUploadDic];
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//当位置获取或更新失败会调用的方法
|
|
|
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
|
|
|
{
|
|
|
NSString *errorMsg = nil;
|
|
|
if ([error code] == kCLErrorDenied) {
|
|
|
errorMsg = @"定位功能访问被拒绝";
|
|
|
}
|
|
|
if ([error code] == kCLErrorLocationUnknown) {
|
|
|
errorMsg = @"获取位置信息失败";
|
|
|
}
|
|
|
NSLog(@"%@", errorMsg);
|
|
|
}
|
|
|
|
|
|
@end |
...
|
...
|
|