// // Device.m // myWatch // // Created by xTT on 15/11/5. // Copyright © 2015年 xTT. All rights reserved. // #import "Device.h" #import "myHttp.h" #import "User.h" #import "CJFileUtility.h" //#import "Clock.h" #import "Locate.h" //#import "bleObject.h" @implementation Device @synthesize name = _name; //一些默认选项 -(NSString *)name{ if(!_name){ _name = @"宝贝"; } return _name; } -(NSNumber *)vendor{ if(!_vendor){ _vendor = @(20000); } return _vendor; } -(NSString *)sex{ if (!_sex) { _sex = @"girl"; } return _sex; } -(NSString *)birthday{ if (!_birthday) { //存当前获取的时间 NSDateFormatter *formatter = [NSDateFormatter new]; [formatter setDateFormat:@"yyyy-MM-dd"]; NSDate *nowDate = [NSDate date]; NSString *nowDay = [formatter stringFromDate:nowDate]; _birthday = nowDay; } return _birthday; } -(NSString *)weight{ if(!_weight){ _weight = @"20"; } return _weight; } -(NSString *)height{ if(!_height){ _height = @"100"; } return _height; } -(NSString *)relationship{ if(!_relationship){ _relationship = @"亲人"; } return _relationship; } -(NSNumber *)relationship_image_id{ if(!_relationship_image_id){ _relationship_image_id = @(9); } return _relationship_image_id; } - (void)setUpDownNotice:(NSNumber *)upDownNotice{ if (upDownNotice) { _upDownNotice = upDownNotice; } } #pragma 头像 - (UIImage *)getImageWithRim:(CGSize)size{ if (!self.image) { return [myHelper createRoundedRectImage:[myHelper getImageWithName:@"圈成员设备用户默认头像"] addImage:[myHelper getImageWithName:@"默认头像外框"] size:size]; } return [myHelper createRoundedRectImage:self.image addImage:[myHelper getImageWithName:@"默认头像外框"] size:size]; } //- (void)setStatus:(NSNumber *)status{ // if (![_status isEqualToNumber:status]) { // [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadUserCircle" object:nil]; // } // _status = status; //} //+ (CBPeripheral *)connectPeripheralWithMac:(NSString *)blueMac{ // if (blueMac) { // NSString *UUID = [[NSUserDefaults standardUserDefaults] objectForKey:blueMac]; // if (UUID.length > 0) { // NSArray *arr = [[bleObject defaultManager].manager retrievePeripheralsWithIdentifiers:@[[CBUUID UUIDWithString:UUID]]]; // if (arr.count > 0) { // return arr[0]; // } // } // } // // return nil; //} #pragma 查询设备基本信息-激活用 + (void)getDeviceInfoWithParameters:(id)parameters success:(void (^)(id responseObject))success failure:(void (^)(id faiObject))failure{ NSString *urlStr = [MyHttp getURL:@"getway/devices/[imei]" objArr:@[[Device mj_objectWithKeyValues:parameters]]]; [xMyHttp URL:urlStr method:@"GET" parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) { if ([responseObject[@"code"] intValue] == HTTP_SUCCESS) { if(success){success(responseObject);} }else{ if(failure){failure(responseObject);} } } failure:^(NSURLSessionDataTask *task, NSError *error) { if (failure) { failure(error); } }]; } #pragma 获取配置文件 - (void)getDeviceConfigurationSuccess:(void (^)(id responseObject))success failure:(void (^)(id faiObject))failure{ NSString *version = @"produce"; #if defined(DEBUG)||defined(_DEBUG) version = @"develop"; #endif NSMutableDictionary *parameters = [NSMutableDictionary dictionaryWithDictionary:@{@"platform":@"ios", @"version":version, @"package":[[NSBundle mainBundle] bundleIdentifier]}]; NSString *urlStr = [MyHttp getURL:@"getway/vendors/[vendor]" objArr:@[self]]; WEAKSELF [xMyHttp URL:urlStr method:@"GET" parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) { if ([responseObject[@"code"] intValue] == HTTP_SUCCESS) { NSString *fileName = [NSString stringWithFormat:@"%@%@%@.plist",responseObject[@"plist_update_date"], responseObject[@"plist_version"], weakSelf.vendor]; NSString *path = [CJFileUtility documentsPathSubDir:@"vendor" andFile:fileName]; if (![CJFileUtility fileExists:path]) { //远程地址 NSURL *URL = [NSURL URLWithString:responseObject[@"plist_url"]]; NSURLSessionDownloadTask *downloadTask; //下载Task操作 downloadTask = [xMyHttp downloadTaskWithRequest:[NSURLRequest requestWithURL:URL] progress:^(NSProgress * _Nonnull downloadProgress) {} destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) { return [NSURL fileURLWithPath:path]; } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) { //设置下载完成操作 // filePath就是你下载文件的位置,你可以解压,也可以直接拿来使用 // [ConfigurationManager defaultManager].vendor = weakSelf.vendor; // [[ConfigurationManager defaultManager] processData:[NSMutableDictionary dictionaryWithContentsOfURL:filePath]]; if(success){success(responseObject);} }]; [downloadTask resume]; }else{ // [[ConfigurationManager defaultManager] processData:[NSMutableDictionary dictionaryWithContentsOfFile:path]]; if(success){success(responseObject);} } }else{ if(failure){failure(responseObject);} } } failure:^(NSURLSessionDataTask *task, NSError *error) { if (failure) { failure(error); } }]; } #pragma 获取设备信息 - (void)getDeviceInfoWithParameters:(id)parameters success:(void (^)(id responseObject))success failure:(void (^)(id faiObject))failure{ NSString *urlStr = [MyHttp getURL:HTTP_DEVICES__ objArr:@[self]]; [xMyHttp URL:urlStr method:@"GET" parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) { if ([responseObject[@"code"] intValue] == HTTP_SUCCESS) { [self mj_setKeyValues:responseObject]; [User saveToFile]; if(success){success(responseObject);} }else{ if(failure){failure(responseObject);} } } failure:^(NSURLSessionDataTask *task, NSError *error) { if (failure) { failure(error); } } showHUD:NO]; } #pragma 编辑设备信息 - (void)saveDeviceInfoSuccess:(void (^)(id responseObject))success failure:(void (^)(id faiObject))failure{ NSString *urlStr = [MyHttp getURL:HTTP_DEVICES__ objArr:@[self]]; NSMutableDictionary *parameters = [self mj_keyValues]; [parameters removeObjectForKey:@"image"]; [xMyHttp URL:urlStr method:@"PATCH" parameters:parameters constructingBodyWithBlock:^(id formData) { if (self.image) { [formData appendPartWithFileData:UIImageJPEGRepresentation(self.image, 0.1) name:@"image" fileName:@"image.jpg" mimeType:@"image/jpeg"]; } } success:^(NSURLSessionDataTask *task, id responseObject) { if ([responseObject[@"code"] intValue] == HTTP_SUCCESS) { [self mj_setKeyValues:responseObject]; if(success){success(responseObject);} [User saveToFile]; }else{ if(failure){failure(responseObject);} } } failure:^(NSURLSessionDataTask *task, NSError *error) { if (failure) { failure(error); } }]; } #pragma 获取管理员的与某设备的关系和手机号码 - (void)getAdminInfosuccess:(void (^)(id responseObject))success failure:(void (^)(id faiObject))failure{ NSString *urlStr = [MyHttp getURL:HTTP_DEVICES__OPERATION objArr:@[self]]; [xMyHttp URL:urlStr method:@"GET" parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) { if ([responseObject[@"code"] intValue] == HTTP_SUCCESS) { if(success){success(responseObject);} }else{ if(failure){failure(responseObject);} } } failure:^(NSURLSessionDataTask *task, NSError *error) { if (failure) { failure(error); } }]; } #pragma 编辑腕表信息 - (void)editOperationDeviceInfoWithParameters:(id)parameters success:(void (^)(id responseObject))success failure:(void (^)(id faiObject))failure{ NSString *urlStr = [MyHttp getURL:HTTP_DEVICES__OPERATION objArr:@[self]]; [xMyHttp URL:urlStr method:@"POST" parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) { if ([responseObject[@"code"] intValue] == HTTP_SUCCESS) { if(success){success(responseObject);} [User saveToFile]; }else{ if(failure){failure(responseObject);} } } failure:^(NSURLSessionDataTask *task, NSError *error) { if (failure) { failure(error); } }]; } #pragma 请求设备当前定位数据 - (void)getDeviceCurLocationSuccess:(void (^)(id responseObject))success failure:(void (^)(id faiObject))failure{ NSString *urlStr = [MyHttp getURL:HTTP_DEVICES__LOCATION_LAST objArr:@[self]]; [xMyHttp URL:urlStr method:@"POST" parameters:@{} success:^(NSURLSessionDataTask *task, id responseObject) { if ([responseObject[@"code"] intValue] == HTTP_SUCCESS) { if(success){success(responseObject);} }else{ if(failure){failure(responseObject);} } } failure:^(NSURLSessionDataTask *task, NSError *error) { if (failure) { failure(error); } } showHUD:NO]; } #pragma 获取设备注册码 - (void)getDeviceAuthCodeSuccess:(void (^)(id responseObject))success failure:(void (^)(id faiObject))failure{ NSString *urlStr = [MyHttp getURL:HTTP_DEVICES__AUTHCODE objArr:@[self]]; [xMyHttp URL:urlStr method:@"POST" parameters:@{} success:^(NSURLSessionDataTask *task, id responseObject) { if ([responseObject[@"code"] intValue] == HTTP_SUCCESS) { if(success){success(responseObject);} }else{ if(failure){failure(responseObject);} } } failure:^(NSURLSessionDataTask *task, NSError *error) { if (failure) { failure(error); } } showHUD:NO]; } #pragma 打电话功能 - (void)makePhoneCallWithViewController:(UIViewController *)vc{ if (self.phone.length == 0) { LGAlertView *alertView = [[LGAlertView alloc] initWithTitle:@"未设置设备号码,是否设置?" message:nil style:LGAlertViewStyleAlert buttonTitles:@[@"设置"] cancelButtonTitle:@"取消" destructiveButtonTitle:nil actionHandler:^(LGAlertView *alertView, NSString *title, NSUInteger index) { UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; UINavigationController *deviceVC = [sb instantiateViewControllerWithIdentifier:@"EditDeviceViewController"]; deviceVC.hidesBottomBarWhenPushed = YES; [vc setValue:@(NO) forKey:@"isAdd"]; [vc.navigationController pushViewController:deviceVC animated:YES]; } cancelHandler:nil destructiveHandler:nil]; [alertView showAnimated:YES completionHandler:nil]; }else{ NSString * string = [[NSMutableString alloc] initWithFormat:@"tel:%@",cUser.cDevice.phone]; if(cUser.cDevice.phone.length == 11){ string = [NSString stringWithFormat:@"tel:+86-%@", cUser.cDevice.phone]; } if(CurrentSystemVersion.doubleValue > 10){ [[UIApplication sharedApplication] openURL:[NSURL URLWithString:string] options:@{} completionHandler:nil]; }else{ [[UIApplication sharedApplication] openURL:[NSURL URLWithString:string]]; } } } #pragma 在线客服 - (void)getZuoYeBangServiceSuccess:(void (^)(id responseObject))success failure:(void (^)(id faiObject))failure{ NSString *urlStr = HTTP_ZUOYEBANG_SERVICE; [xMyHttp URL:urlStr method:@"GET" parameters:@{} success:^(NSURLSessionDataTask *task, id responseObject) { if ([responseObject[@"code"] intValue] == HTTP_SUCCESS) { if(success){success(responseObject);} }else{ if(failure){failure(responseObject);} } } failure:^(NSURLSessionDataTask *task, NSError *error) { if (failure) { failure(error); } } showHUD:NO]; } #pragma mark- 查询话费和流量接口 - (void)queryCostAndTrafficFunctionWithParameters:(id)parameters Success:(void (^)(id responseObject))success failure:(void (^)(id faiObject))failure{ [self costAndTrafficWithMethod:@"POST" Parameters:parameters Success:success failure:failure]; } #pragma mark- 删除 话费和流量记录 的接口 - (void)deleteCostAndTrafficFunctionWithSuccess:(void (^)(id responseObject))success failure:(void (^)(id faiObject))failure{ [self costAndTrafficWithMethod:@"DELETE" Parameters:nil Success:success failure:failure]; } #pragma mark- 查询话费和流量记录 的接口 - (void)getCostAndTrafficRecordWithParameters:(id)parameters Success:(void (^)(id responseObject))success failure:(void (^)(id faiObject))failure{ [self costAndTrafficWithMethod:@"GET" Parameters:parameters Success:success failure:failure]; } // 查询话费和流量的 公共接口 - (void)costAndTrafficWithMethod:(NSString*)method Parameters:(id)parameters Success:(void (^)(id responseObject))success failure:(void (^)(id faiObject))failure{ NSString *urlStr = [MyHttp getURL:HTTP_DEVICES__INQUIRE objArr:@[self]]; [xMyHttp URL:urlStr method:method parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) { if ([responseObject[@"code"] intValue] == HTTP_SUCCESS) { if(success){success(responseObject);} [User saveToFile]; }else{ if(failure){failure(responseObject);} } } failure:^(NSURLSessionDataTask *task, NSError *error) { if (failure) { failure(error); } }]; } #pragma mark ---获取设备视频通话成员列表--- - (void)getVideoUserListSuccess:(void (^)(id responseObject))success failure:(void (^)(void))failure{ NSString *urlStr = [MyHttp getURL:HTTP_DEVICES__VIDEO_USER_LIST objArr:@[self]]; [xMyHttp URL:urlStr method:@"GET" parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) { if ([responseObject[@"code"] intValue] == HTTP_SUCCESS) { success(responseObject); }else{ if (failure) { failure(); } } } failure:^(NSURLSessionDataTask *task, NSError *error) { if (failure) { failure(); } } showHUD:YES]; } #pragma mark ---添加设备视频通话成员列表--- - (void)addVideoUserListWithList:(NSArray*)list Success:(void (^)(id responseObject))success failure:(void (^)(void))failure{ NSString *urlStr = [MyHttp getURL:HTTP_DEVICES__VIDEO_USER_LIST objArr:@[self]]; [xMyHttp URL:urlStr method:@"POST" parameters:@{@"openids":[list mj_JSONString]} success:^(NSURLSessionDataTask *task, id responseObject) { if ([responseObject[@"code"] intValue] == HTTP_SUCCESS) { success(responseObject); }else{ if (failure) { failure(); } } } failure:^(NSURLSessionDataTask *task, NSError *error) { if (failure) { failure(); } } showHUD:YES]; } #pragma mark ---删除设备视频通话成员列表--- - (void)delectVideoUserListWithList:(NSArray*)list Success:(void (^)(id responseObject))success failure:(void (^)(void))failure{ NSString *urlStr = [MyHttp getURL:HTTP_DEVICES__VIDEO_USER_LIST objArr:@[self]]; [xMyHttp URL:urlStr method:@"DELETE" parameters:@{@"openids":[list mj_JSONString]} success:^(NSURLSessionDataTask *task, id responseObject) { if ([responseObject[@"code"] intValue] == HTTP_SUCCESS) { success(responseObject); }else{ if (failure) { failure(); } } } failure:^(NSURLSessionDataTask *task, NSError *error) { if (failure) { failure(); } } showHUD:YES]; } #pragma mark 同步表盘 - (void)updateMyDial:(NSMutableDictionary *)parameter success:(void (^)(void))success failure:(void (^)(NSError *error))failure { NSString *urlStr = HTTP_POST_DOWNDIAL; [xMyHttp URL:urlStr method:@"POST" parameters:parameter success:^(NSURLSessionDataTask *task, id responseObject) { if ([responseObject[@"code"] intValue] == 0) { success(); } else { if (failure) { NSString *domain = @"com.zuoyebang.iot.watch"; NSDictionary *userInfo = @{@"msg" : responseObject[@"msg"]}; NSError *err = [NSError errorWithDomain:domain code:-1 userInfo:userInfo]; failure(err); } } } failure:^(NSURLSessionDataTask *task, NSError *error) { if (failure) { failure(error); } }]; } #pragma mark 签到 - (void)SetAccountsSign:(void (^)(void))success failure:(void (^)(NSError *error))failure { NSMutableDictionary *parameter = [NSMutableDictionary dictionary]; [parameter setValue:cUser.openid forKey:@"openid"]; [parameter setValue:cUser.accesstoken forKey:@"accesstoken"]; NSString *urlStr = HTTP_POST_ACCOUNTSSIGN; [xMyHttp URL:urlStr method:@"POST" parameters:parameter success:^(NSURLSessionDataTask *task, id responseObject) { if ([responseObject[@"code"] intValue] == 0) { success(); } else { if (failure) { NSString *domain = @"com.zuoyebang.iot.watch"; NSDictionary *userInfo = @{@"msg" : responseObject[@"msg"]}; NSError *err = [NSError errorWithDomain:domain code:-1 userInfo:userInfo]; failure(err); } } } failure:^(NSURLSessionDataTask *task, NSError *error) { if (failure) { failure(error); } }]; } #pragma mark 查询签到 - (void)GetAccountsSignWithMonth:(NSString *)month Success:(void (^)(id responseObject))success failure:(void (^)(NSError *error))failure { NSMutableDictionary *parameter = [NSMutableDictionary dictionary]; [parameter setValue:cUser.openid forKey:@"openid"]; [parameter setValue:cUser.accesstoken forKey:@"accesstoken"]; [parameter setValue:month forKey:@"month"]; NSString *urlStr = HTTP_POST_ACCOUNTSSIGN; [xMyHttp URL:urlStr method:@"GET" parameters:parameter success:^(NSURLSessionDataTask *task, id responseObject) { if ([responseObject[@"code"] intValue] == 0) { success(responseObject); } else { if (failure) { NSString *domain = @"com.zuoyebang.iot.watch"; NSDictionary *userInfo = @{@"msg" : responseObject[@"msg"]}; NSError *err = [NSError errorWithDomain:domain code:-1 userInfo:userInfo]; failure(err); } } } failure:^(NSURLSessionDataTask *task, NSError *error) { if (failure) { failure(error); } }]; } /// 获取手表短信列表 - (void)getDevicesSmSList:(NSInteger)page success:(void (^)(id responseObject))success failure:(void (^)(id faiObject))failure { NSString *urlStr = [MyHttp getURL:HTTP_GET_DEVICESSMS objArr:@[self]]; NSMutableDictionary *parameters = [NSMutableDictionary dictionary]; [parameters setValue:@(page) forKey:@"page"]; [parameters setValue:@(20) forKey:@"num"]; [parameters setValue:cUser.accesstoken forKey:@"accesstoken"]; [parameters setValue:cUser.openid forKey:@"openid"]; [xMyHttp URL:urlStr method:@"GET" parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) { if ([responseObject[@"code"] intValue] == HTTP_SUCCESS) { if(success){success(responseObject);} }else{ if(failure){failure(responseObject);} } } failure:^(NSURLSessionDataTask *task, NSError *error) { if (failure) { failure(error); } } showHUD:NO]; } /// 删除或者已读短信 - (void)deleteAndPostDevicesSmSList:(NSString *)Id method:(NSString *)method success:(void (^)(id responseObject))success failure:(void (^)(id faiObject))failure { NSString *urlStr = [MyHttp getURL:HTTP_GET_DEVICESSMS objArr:@[self]]; NSMutableDictionary *parameters = [NSMutableDictionary dictionary]; [parameters setValue:Id forKey:@"id"]; [parameters setValue:cUser.accesstoken forKey:@"accesstoken"]; [parameters setValue:cUser.openid forKey:@"openid"]; [xMyHttp URL:urlStr method:method parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) { if ([responseObject[@"code"] intValue] == HTTP_SUCCESS) { if(success){success(responseObject);} }else{ if(failure){failure(responseObject);} } } failure:^(NSURLSessionDataTask *task, NSError *error) { if (failure) { failure(error); } } showHUD:NO]; } /// 位置纠偏 - (void)setLocationRectify:(NSMutableDictionary *)parameters success:(void (^)(id responseObject))success failure:(void (^)(id faiObject))failure { NSString *urlStr = [MyHttp getURL:HTTP_POST_LocationRectify objArr:@[self]]; [parameters setValue:cUser.accesstoken forKey:@"accesstoken"]; [parameters setValue:cUser.openid forKey:@"openid"]; [xMyHttp URL:urlStr method:@"POST" parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) { if ([responseObject[@"code"] intValue] == HTTP_SUCCESS) { if(success){success(responseObject);} }else{ if(failure){failure(responseObject);} } } failure:^(NSURLSessionDataTask *task, NSError *error) { if (failure) { failure(error); } } showHUD:NO]; } /// 获取用户所有单聊和群聊信息 - (void)GetChatGroupsWithSuccess:(void (^)(id responseObject))success failure:(void (^)(NSError *error))failure { NSMutableDictionary *parameter = [NSMutableDictionary dictionary]; [parameter setValue:cUser.openid forKey:@"openid"]; [parameter setValue:cUser.accesstoken forKey:@"accesstoken"]; NSString *urlStr = HTTP_GET_CHATGROUPSMESSAGE; [xMyHttp URL:urlStr method:@"GET" parameters:parameter success:^(NSURLSessionDataTask *task, id responseObject) { if ([responseObject[@"code"] intValue] == 0) { success(responseObject); } else { if (failure) { NSString *domain = @"com.zuoyebang.iot.watch"; NSDictionary *userInfo = @{@"msg" : responseObject[@"msg"]}; NSError *err = [NSError errorWithDomain:domain code:-1 userInfo:userInfo]; failure(err); } } } failure:^(NSURLSessionDataTask *task, NSError *error) { if (failure) { failure(error); } }]; } @end