JLChen
2021-05-18 a869383e163a18cdedcf587383c1eca043129754
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//
//  Copyright © 2018年 Zhejiang Dahua Technology Co.,Ltd. All rights reserved.
//
 
import UIKit
 
class DHApWifiPasswordPresenter: NSObject, DHWifiPasswordPresenterProtocol {
    
    var container: DHWifiPasswordViewController?
    
    /// 设备密码
    var devicePassword: String = "admin"
    
    /// 软AP WIFI列表中选中的WIFI
    var wifiSSID: String = ""
    
    /// 软AP WIFI列表选中WIFI的加密模式
    var encryptionAuthority: Int = 0
    
    /// 软AP 连接WIFI时的网卡,eth2或wlan0
    var netcardName: String = "eth2"
    
    var scDeviceIsInited: Bool = false
    
    required init(container: DHWifiPasswordViewController) {
        self.container = container
    }
    
    // MARK: DHWifiPasswordPresenterProtocol
    func updateContainerViewByNetwork() {
        
        if self.container?.nextButton == nil {
            // 可能还没有创建
            return
        }
        
        //软AP的WIFI名称是从上级界面选中的
        self.container?.wifiNameLabel.text = wifiSSID
        
        //【*】直接从保存的密码取:如果长度大于0,选中check,并填充密码
        if let password = DHUserManager.shareInstance().ssidPwd(by: wifiSSID), password.count > 0 {
            self.container?.checkButton.isSelected = true
            self.container?.passwordInputView.textField.text = password
        } else {
            self.container?.checkButton?.isSelected = false
            self.container?.passwordInputView.textField.text = nil
        }
    }
    
    func setupSupportView() {
        //软AP不显示5g提示及wifi检测
        self.container?.supportView.isHidden = true
        container?.checkWidthConstraint.constant = 250
        container?.wifiDetectButton.isHidden = true
    }
    
    func nextStepAction(wifiSSID: String, wifiPassword: String?) {
        //连接wifi
        let deviceId = DHAddDeviceManager.sharedInstance.deviceId
        let device = DHNetSDKSearchManager.sharedInstance().getNetInfo(byID: deviceId)
        guard device != nil else {
            LCProgressHUD.showMsg("add_device_connect_failed".lc_T)
            return
        }
        
        LCProgressHUD.show(on: self.container?.view)
        
        if true == DHAddDeviceManager.sharedInstance.isSupportSC && false == self.scDeviceIsInited {
            if let pwd = wifiPassword {
                DHNetSDKHelper.scDeviceApConnectWifi(wifiSSID, password: pwd, ip: device!.deviceIP, port: Int(device!.port), encryptionAuthority: Int32(self.encryptionAuthority)) { (error) in
         
                        LCProgressHUD.hideAllHuds(self.container?.view)
                        self.devicePassword = DHAddDeviceManager.sharedInstance.initialPassword
                        self.connectWifiSuccessProcess()
                        
                    }
                }
            
        } else {
            DHNetSDKHelper.loginWithHighLevelSecurity(byIp: device!.deviceIP, port: Int(device!.port), username: "admin", password: devicePassword, success: { (handle) in
                let ssid = wifiSSID
                let pwd = wifiPassword
                DHNetSDKHelper.connectWIFI(byDeviceId: DHAddDeviceManager.sharedInstance.deviceId, ssid: ssid, password: pwd, devicePwd: self.devicePassword, isSC: DHAddDeviceManager.sharedInstance.isSupportSC) { (success) in
                    LCProgressHUD.hideAllHuds(self.container?.view)
                    self.connectWifiSuccessProcess()
                }
                
            }) { (description) in
                LCProgressHUD.hideAllHuds(self.container?.view)
                LCProgressHUD.showMsg("add_device_connect_failed".lc_T)
            }
        }
    }
    
    private func connectWifiSuccessProcess() {
        //【*】新方案不区分p2p、非p2p,进入连接乐橙云平台界面
        //【*】其他设备,进入连接乐橙云平台界面
        self.container?.basePushToConnectCloudVC(devicePassword: devicePassword)
    }
}