166 lines
6.4 KiB
Objective-C
166 lines
6.4 KiB
Objective-C
//
|
||
// SettingResetViewController.m
|
||
// Ifish
|
||
//
|
||
// Created by Minghao Xue on 2018/7/24.
|
||
// Copyright © 2018年 lianlian. All rights reserved.
|
||
//
|
||
|
||
#import "SettingResetViewController.h"
|
||
#import "IFishHotpotUDPHelper.h"
|
||
#import "ConnectHotspotTipViewController.h"
|
||
#import "DeviceCameraModel.h"
|
||
#import "DeviceModel.h"
|
||
#import "MBProgressHUD.h"
|
||
#import<SystemConfiguration/CaptiveNetwork.h>
|
||
|
||
@interface SettingResetViewController ()<IFishHotpotUDPHelperDelegate>
|
||
|
||
@property (nonatomic, strong) DeviceModel *currentDevice;
|
||
@property (nonatomic, strong) MBProgressHUD *HUD;
|
||
@property (nonatomic, assign) BOOL isDeleteingDevice;
|
||
|
||
@end
|
||
|
||
@implementation SettingResetViewController
|
||
|
||
- (void)viewDidLoad {
|
||
[super viewDidLoad];
|
||
self.title = @"恢复出厂设置";
|
||
|
||
}
|
||
|
||
- (IBAction)reset:(id)sender {
|
||
if (![[self currentWifiSSID].lowercaseString hasPrefix:@"ifish"]) {
|
||
[self.view makeToast:@"请先将手机连接到wifi:ifish-xxxx"];
|
||
return;
|
||
}
|
||
[IFishHotpotUDPHelper sharedInstance].delegate = self;
|
||
[[IFishHotpotUDPHelper sharedInstance] broadCastRestCommand];
|
||
}
|
||
- (IBAction)tipBtnClicked:(id)sender {
|
||
ConnectHotspotTipViewController *vc = [[ConnectHotspotTipViewController alloc] initWithNibName:nil bundle:nil];
|
||
[self.navigationController pushViewController:vc animated:YES];
|
||
}
|
||
|
||
#pragma mark - IFishHotpotUDPHelperDelegate
|
||
|
||
- (void)udpHelperCommandExecutedSuccess:(IFishUDPHelperBackMsgModel *)backModel {
|
||
self.HUD.labelText = @"设备即将重置";
|
||
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
|
||
NSArray*deviceArry=[[DataCenter defaultDtacenter]valueForKey:@"deviceInfo"];
|
||
[deviceArry enumerateObjectsUsingBlock:^(DeviceModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
|
||
if ([obj.macAddress isEqualToString:backModel.senderMacAddress]) {
|
||
self.currentDevice = obj;
|
||
self.HUD.labelText = @"请切当前Ifish-xxxx到其他网络,以进行后续操作";
|
||
*stop = YES;
|
||
}
|
||
}];
|
||
|
||
[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
|
||
BOOL networkOk = NO;
|
||
if (status == AFNetworkReachabilityStatusReachableViaWiFi) {
|
||
if (![[self currentWifiSSID].lowercaseString hasPrefix:@"ifish"]) {
|
||
networkOk = YES;
|
||
}
|
||
} else if (status == AFNetworkReachabilityStatusReachableViaWWAN) {
|
||
networkOk = YES;
|
||
}
|
||
if (!networkOk) {
|
||
return;
|
||
}
|
||
if (self.isDeleteingDevice) {
|
||
return;
|
||
}
|
||
self.isDeleteingDevice = YES;
|
||
[self deletDevice];
|
||
}];
|
||
[[AFNetworkReachabilityManager sharedManager] startMonitoring];
|
||
});
|
||
|
||
|
||
}
|
||
|
||
- (void)udpHelperMessage:(NSString *)msg {
|
||
self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
|
||
self.HUD.mode = MBProgressHUDModeIndeterminate;
|
||
self.HUD.labelText = msg;
|
||
}
|
||
|
||
#pragma mark - 删除设备
|
||
|
||
- (NSString *)currentWifiSSID {
|
||
NSString *ssid = @"Not Found";
|
||
CFArrayRef myArray = CNCopySupportedInterfaces();
|
||
if (myArray != nil) {
|
||
CFDictionaryRef myDict = CNCopyCurrentNetworkInfo(CFArrayGetValueAtIndex(myArray, 0));
|
||
if (myDict != nil) {
|
||
NSDictionary *dict = (NSDictionary *)CFBridgingRelease(myDict);
|
||
ssid = [dict valueForKey:@"SSID"];
|
||
}
|
||
}
|
||
return ssid;
|
||
}
|
||
|
||
-(void)deletDevice{
|
||
self.HUD.labelText = @"开始删除设备";
|
||
//先判断此设备是否有关联 摄像头
|
||
//DeviceModel*deviceModel=[_deviceArr objectAtIndex:indexpath.row];
|
||
//要删除的设设备id
|
||
NSMutableArray *guanxiiArr= [[DataCenter defaultDtacenter] valueForKey:@"devicamerArr"];
|
||
for (DeviceCameraModel *model in guanxiiArr) {
|
||
if (self.currentDevice.deviceId == model.deviceId ) {
|
||
[guanxiiArr removeObject:model];
|
||
//保存新关系数组
|
||
[[DataCenter defaultDtacenter] setValue:guanxiiArr forKey:@"devicamerArr"];
|
||
}
|
||
}
|
||
[self deleteDeviceRequset];
|
||
}
|
||
|
||
-(void)deleteDeviceRequset{
|
||
AFHTTPRequestOperationManager*mannager=[AFHTTPRequestOperationManager manager];
|
||
mannager.responseSerializer=[AFHTTPResponseSerializer serializer];
|
||
NSMutableDictionary * para = [NSMutableDictionary dictionary];
|
||
NSString *priId=self.currentDevice.deviceId;
|
||
NSString*userId=self.currentDevice.userId;
|
||
[para setValue:priId forKey:@"priId.deviceId"];
|
||
[para setValue:userId forKey:@"priId.userId"];
|
||
// __weak typeof (self)weakSelf=self;
|
||
[mannager POST:kDeleteDeviceUser parameters:para success:^(AFHTTPRequestOperation *operation, id responseObject) {
|
||
if (responseObject) {
|
||
NSDictionary*resultDic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
|
||
NSLog(@"result:%@",resultDic[@"result"]);
|
||
if ([resultDic[@"result"] isEqualToString:@"100"]) {
|
||
self.HUD.labelText = @"删除成功";
|
||
//重置数据库
|
||
NSArray*deviceArry=[[DataCenter defaultDtacenter]valueForKey:@"deviceInfo"];
|
||
NSMutableArray *newArr = [NSMutableArray arrayWithArray:deviceArry];
|
||
|
||
for (DeviceModel *model in deviceArry) {
|
||
if ([model.deviceId isEqual:self.currentDevice.deviceId]) {
|
||
[newArr removeObject:model];
|
||
}
|
||
}
|
||
[[DataCenter defaultDtacenter] setValue: newArr forKey:@"deviceInfo"];
|
||
}else if ([resultDic[@"result"] isEqualToString:@"101"]){
|
||
self.HUD.labelText = @"删除失败";
|
||
}else if ([resultDic[@"result"] isEqualToString:@"301"]){
|
||
self.HUD.labelText = @"请求验证失败,请重新登陆";
|
||
}else if ([resultDic[@"result"] isEqualToString:@"302"]){
|
||
self.HUD.labelText = @"请求被舍弃,未执行";
|
||
}
|
||
[self.HUD hide:YES afterDelay:2];
|
||
} else {
|
||
self.HUD.labelText = @"删除失败";
|
||
[self.HUD hide:YES afterDelay:2];
|
||
}
|
||
|
||
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
|
||
self.HUD.labelText = @"请求异常";
|
||
[self.HUD hide:YES afterDelay:2];
|
||
}];
|
||
}
|
||
|
||
@end
|