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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
//
//  Copyright © 2018年 Zhejiang Dahua Technology Co.,Ltd. All rights reserved.
//    AP配置:wifi选择界面
 
import UIKit
import LCBaseModule
 
public class DHAppWifiCell: UITableViewCell {
    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var qualityImageView: UIImageView!
    
    public func setup(wifiInfo: DHApWifiInfo) {
        nameLabel.text = wifiInfo.name
        nameLabel.textColor = UIColor.dhcolor_c2()
        var imageName: String = ""
        
        //强度不使用拼接方式,防止资源被清理
        if wifiInfo.linkQuality < 25 {
            imageName = wifiInfo.autoConnect ? "wifi_bad_unlock" : "wifi_bad_lock"
        } else if wifiInfo.linkQuality < 55 {
            imageName = wifiInfo.autoConnect ? "wifi_weak_unlock" : "wifi_weak_lock"
        } else if wifiInfo.linkQuality < 75 {
            imageName = wifiInfo.autoConnect ? "wifi_good_unlock" : "wifi_good_lock"
        } else {
            imageName = wifiInfo.autoConnect ?  "wifi_nice_unlock" : "wifi_nice_lock"
        }
        
        qualityImageView.image = UIImage(named: imageName)
    }
}
 
class DHApWifiSelectViewController: DHAddBaseViewController,
                                    UITableViewDelegate,
                                    UITableViewDataSource {
 
    public static func storyboardInstance() -> DHApWifiSelectViewController {
        let storyboard = UIStoryboard(name: "AddDevice", bundle: Bundle.dh_addDeviceBundle())
        if let controller = storyboard.instantiateViewController(withIdentifier: "DHApWifiSelectViewController") as? DHApWifiSelectViewController {
            return controller
        }
        
        return DHApWifiSelectViewController()
    }
    
    var loginHandle: Int = 0
    @IBOutlet weak var tableView: UITableView!
    
    /// WIFI列表,可由上级界面传入
    public var wifiList = [DHApWifiInfo]()
    public var devicePassword: String = ""
    
    private var headerView = LCApWifiHeaderView()
    private var isLoading = false
    private var abnormalView: UIView?
    private var ip: String?
    private var port: Int32 = 0
    
    public var scDeviceIsInited = false
    
    override func viewDidLoad() {
        super.viewDidLoad()
 
        
        tableView.tableFooterView = UIView()
        tableView.separatorColor = UIColor.dhcolor_c8()
        tableView.register(DHWiFiConfigHeader.self, forHeaderFooterViewReuseIdentifier: "DHWiFiConfigHeader")
        
        if wifiList.count == 0 {
            loadWifiList()
        }
    }
 
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func setupNaviRightItem() {
        btnNaviRight = UIButton(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
        btnNaviRight.contentHorizontalAlignment = .right
        btnNaviRight.setImage(UIImage(named: "common_image_nav_refresh"), for: .normal)
        btnNaviRight.setImage(UIImage(named: "common_image_nav_refresh"), for: .highlighted)
        btnNaviRight.setImage(UIImage(named: "common_image_nav_refresh_disable"), for: .disabled)
        btnNaviRight.addTarget(self, action: #selector(onRefreshAction), for: .touchUpInside)
        let item = UIBarButtonItem(customView: btnNaviRight)
        self.navigationItem.lc_rightBarButtonItems = [item]
        
        btnNaviRight.isHidden = self.isRightActionHidden()
    }
    
    @objc func onRefreshAction() {
        loadWifiList()
    }
    
    // MARK: LoadWifi
    private func loadWifiList() {
        if isLoading {
            return
        }
        
        isLoading = true
        btnNaviRight.dh_enable = false
        headerView.indicatorView.startAnimating()
        let deviceIp = DHAddDeviceManager.sharedInstance.getLocalDevice()?.deviceIP ?? ""
        let gatewayIp = UIDevice.lc_getRouterAddress()
        ip = deviceIp.count == 0 ? gatewayIp : deviceIp
        port = DHAddDeviceManager.sharedInstance.getLocalDevice()?.port ?? 37_777
        
//        print("gatewayIp :" + gatewayIp!)
        
        // IP没有获取到,延迟执行
        guard ip != nil else {
            DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
                self.isLoading = false
                self.btnNaviRight.dh_enable = true
                self.headerView.indicatorView.stopAnimating()
                
                if self.wifiList.count == 0 {
                    self.showAbnormalView()
                }
            }
            
            return
        }
        
        self.wifiList.removeAll()
        self.tableView.reloadData()
        self.hideAbnormalView()
        LCProgressHUD.show(on: self.view)
        if false == self.scDeviceIsInited && true == DHAddDeviceManager.sharedInstance.isSupportSC {
            DHNetSDKHelper.scDeviceApLoadWifiList(ip, port: Int(port)) { [weak self] (list, error) in
                LCProgressHUD.hideAllHuds(self?.view)
                self?.wifiListDidLoad(wifiList: list, error: error)
            }
        } else {
            if true == DHAddDeviceManager.sharedInstance.isSupportSC {
                devicePassword = DHAddDeviceManager.sharedInstance.initialPassword
            }
            
            DHNetSDKHelper.logoutDevice(loginHandle) {
                
            }
            
            DHNetSDKHelper.loginWithHighLevelSecurity(byIp: ip, port: Int(port), username: "admin", password: devicePassword, success: { [weak self] (handle) in
                LCProgressHUD.hideAllHuds(self?.view)
                self?.loginHandle = handle
                DHNetSDKHelper.loadWifiList(byLoginHandle: handle, complete: { (list, error) in
                    self?.wifiListDidLoad(wifiList: list, error: error)
                })
            }) { [weak self] _ in
                LCProgressHUD.hideAllHuds(self?.view)
                self?.isLoading = false
                self?.btnNaviRight.dh_enable = true
                self?.headerView.indicatorView.stopAnimating()
                self?.showAbnormalView()
            }
        }
        
    }
    
    func wifiListDidLoad(wifiList list: [DHApWifiInfo]?, error errorCode: NSInteger) {
        if list != nil {
            self.wifiList.append(contentsOf: list!)
        }
        
        self.tableView.reloadData()
        self.isLoading = false
        self.btnNaviRight.dh_enable = true
        self.headerView.indicatorView.stopAnimating()
        
        if errorCode != 0 {
            self.showAbnormalView()
        }
    }
    
    // MARK: Abnormal View
    func showAbnormalView() {
        let result = UIView()
        abnormalView = result
        result.backgroundColor = UIColor.dhcolor_c43()
        self.view.addSubview(result)
        
        result.snp.makeConstraints { make in
            make.edges.equalTo(self.view)
        }
        
        let imageView = UIImageView()
        imageView.image = UIImage(named: "common_pic_nointernet")
        result.addSubview(imageView)
        imageView.snp.makeConstraints({ (make) in
            make.centerX.equalTo(result)
            make.centerY.equalTo(result).offset(-70)
        })
        
        let tipLabel = UILabel()
        tipLabel.textAlignment = .center
        tipLabel.textColor = UIColor.dhcolor_c5()
        tipLabel.text = "add_device_connect_error_and_quit_retry".lc_T
        tipLabel.font = UIFont.dhFont_t4()
        tipLabel.numberOfLines = 0
        result.addSubview(tipLabel)
        
        tipLabel.snp.makeConstraints({ (make) in
            make.top.equalTo(imageView.snp.bottom)
            make.height.equalTo(30)
            make.left.right.equalTo(self.view)
        })
        self.view.addSubview(abnormalView!)
    }
    
    func hideAbnormalView() {
        abnormalView?.removeFromSuperview()
        abnormalView = nil
    }
        
    // MARK: - UITableViewDelegate
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return DHAddDeviceManager.sharedInstance.netConfigMode == .softAp ? wifiList.count + 1 : wifiList.count
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section == 0 {
            return 186.0
        } else {
            return 44
        }
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        if DHAddDeviceManager.sharedInstance.netConfigMode == .softAp && indexPath.row == wifiList.count {
            let vc = LCAddOtherWifiController(loginHandle: self.loginHandle)
            
            if true == DHAddDeviceManager.sharedInstance.isSupportSC && false == self.scDeviceIsInited {
                vc.devicePassword = DHAddDeviceManager.sharedInstance.initialPassword
            }
            else{
                vc.devicePassword = devicePassword
            }
            
            self.navigationController?.pushViewController(vc, animated: true)
        } else {
            let controller = DHWifiPasswordViewController.storyboardInstance()
            let presenter = DHApWifiPasswordPresenter(container: controller)
            presenter.devicePassword = devicePassword
            presenter.wifiSSID = wifiList[indexPath.row].name
            presenter.encryptionAuthority = wifiList[indexPath.row].encryptionAuthority
            presenter.netcardName = wifiList[indexPath.row].netcardName ?? ""
            presenter.scDeviceIsInited = self.scDeviceIsInited
            controller.setup(presenter: presenter)
            self.navigationController?.pushViewController(controller, animated: true)
        }
    }
    
    // MARK: - UITableViewDataSource
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if DHAddDeviceManager.sharedInstance.netConfigMode == .softAp && indexPath.row == wifiList.count {
            let cell = UITableViewCell()
            cell.textLabel?.text = "其他...".lc_T
            cell.textLabel?.textColor = UIColor.dhcolor_c0()
            cell.selectionStyle = .none
            return cell
        }
        if let cell = tableView.dequeueReusableCell(withIdentifier: "DHAppWifiCell", for: indexPath) as? DHAppWifiCell {
            let wifiInfo = wifiList[indexPath.row]
            cell.setup(wifiInfo: wifiInfo)
            return cell
        }
        
        return UITableViewCell()
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        guard let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "DHWiFiConfigHeader") as? DHWiFiConfigHeader else {
            return nil
        }
        header.isAddNetwordTitle = true
        header.delegate = self
        return header
    }
    
    // MARK: DHAddBaseVCProtocol
    override func leftActionType() -> DHAddBaseLeftAction {
        return .quit
    }
    
    override func isLeftActionShowAlert() -> Bool {
        return true
    }
}
 
extension DHApWifiSelectViewController: LCApWifiHeaderViewDelegate {
    func iconClicked(headView: LCApWifiHeaderView) {
        let vc = LCWifiInfoExplainController()
        self.navigationController?.pushViewController(vc, animated: true)
    }
}
 
extension DHApWifiSelectViewController: DHWiFiConfigHeaderDelegate {
    func iconDidClicked(type: DHWiFiConfigHeaderClickType) {
        switch type {
        case .fiveGDesc:
            let supportVc = DHWiFiUnsupportVC()
            self.navigationController?.pushViewController(supportVc, animated: true)
        case .wifiDesc:
            let vc = LCWifiInfoExplainController()
            self.navigationController?.pushViewController(vc, animated: true)
        }
    }
}