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
//
//  Copyright © 2018年 iblue. All rights reserved.
//    网络连接错误
 
import Foundation
 
/// 元组定义,按钮与操作类型对应
typealias DHNetConnectFailureTuple = (button: UIButton, operation: DHNetConnectFailureOperationType)
 
/// 网络连接按钮操作类型
enum DHNetConnectFailureOperationType {
    /// 输入WIFI密码
    case inputWifiPassword
    
    /// 红灯常亮详情
    case redLightConstantDetail
    
    /// 红灯旋转详情
    case readLightRotateDetail
    
    /// 红灯闪烁详情
    case redLightTwinkleDetail
    
    /// 继续等等(如重新计时)
    case continueToWait
    
    /// 再试一次(如连接路由器失败等)
    case tryAgain
    
    /// 切换到有线
    case switchToWired
    
    /// 进入设备密码检验
    case authDevicePassword
    
    /// 进入设备初始化界面
    case deviceInitialize
    
    /// 完成
    case complete
    
    /// 二维码扫描
    case qrCode
    
    /// 退出
    case quit
    
    /// 优化:A系的蓝灯长亮
    case blueConstantAction
    
    /// 优化:C系的绿灯长亮
    case greenConstantAction
}
 
/// 网络连接错误类型
enum DHNetConnectFailureType: String {
    
    case tp1     = "TP1"
    case tp1s    = "TP1S"
    case g1        = "G1"
    case door    = "K5"
    case ipcGeneral = "ipcGeneral"
    case overseasA    = "FamilyA"
    case overseasC    = "FamilyC"
    case overseasDoorbell    = "Doorbell"
    case commonWithWired    = "DahuaCommonWithWired"
    case commonWithoutWired = "DahuaCommonWithoutWired"
    case accessory = "Accessory"
    case cloudTimeout = "ConnectCloudTimeout"
    
    func buttonTuples() -> [DHNetConnectFailureTuple] {
        var buttons: [DHNetConnectFailureTuple]!
        
        //开放平台需求z,只显示一个"再试一次"按钮
        buttons = commonTypeButtons(supportWired: false)
        
//        switch self {
//        case .tp1:
//            buttons = tp1TypeButtons()
//        case .tp1s:
//            buttons = tp1sTypeButtons()
//        case .g1:
//            buttons = g1TypeButtons()
//        case .door:
//            buttons = k5TypeButtons()
////        case .ipcGeneral:
////            buttons = ipcGeneralTypeButtons()
//        case .overseasA:
//            buttons = overseasATypeButtons()
//        case .overseasC:
//            buttons = overseasCTypeButtons()
//        case .overseasDoorbell:
//            buttons = overseasDoorbellButtons()
//
//        case .accessory:
//            buttons = accessoryTypeButtons()
//
//        case .cloudTimeout:
//            buttons = cloudTimeoutTypeButtons()
//
//        case .commonWithoutWired:
//            buttons = commonTypeButtons(supportWired: false)
//
//        default:
//            buttons = commonTypeButtons(supportWired: true)
//        }
 
        
        return buttons
    }
    
    // MARK: Buttons with type
    private func tp1TypeButtons() -> [DHNetConnectFailureTuple] {
        let config: [(DHLightButtonType, DHNetConnectFailureOperationType)] = [(.redTwinkling, .inputWifiPassword),
                                                                                (.redConstant, .redLightConstantDetail),
                                                                                (.greenTwinkling, .inputWifiPassword),
                                                                                (.greenBlueConstant, .complete)]
        return lightButtons(config: config)
    }
    
    private func tp1sTypeButtons() -> [DHNetConnectFailureTuple] {
        
        let config: [(DHLightButtonType, DHNetConnectFailureOperationType)] = [(.yellowTwinkling, .inputWifiPassword),
                                                                                (.redRotate, .readLightRotateDetail),
                                                                                (.greenBlueConstant, .complete)]
        return lightButtons(config: config)
    }
    
    private func g1TypeButtons() -> [DHNetConnectFailureTuple] {
        let config: [(DHLightButtonType, DHNetConnectFailureOperationType)] = [(.redTwinkling, .redLightTwinkleDetail)]
        return lightButtons(config: config)
    }
    
    private func k5TypeButtons() -> [DHNetConnectFailureTuple] {
        let config: [(String, DHNetConnectFailureOperationType)] = [("add_device_connect_router_failed".lc_T, .tryAgain),
                                                                    ("add_device_connect_network_failed".lc_T, .continueToWait)]
        return DHNetConnectFailureType.commonButtons(config: config)
    }
    
//    private func ipcGeneralTypeButtons() -> [DHNetConnectFailureTuple] {
//        let config: [(String, DHNetConnectFailureOperationType)] = [("add_device_connect_router_failed".lc_T, .tryAgain),
//                                                                    ("add_device_add_by_wired".lc_T, .switchToWired)]
//        return DHNetConnectFailureType.commonButtons(config: config)
//    }
    
    private func overseasATypeButtons() -> [DHNetConnectFailureTuple] {
        let config: [(DHLightButtonType, DHNetConnectFailureOperationType)] = [(.yellowTwinkling, .inputWifiPassword),
                                                                                (.redRotate, .readLightRotateDetail),
                                                                                (.greenTwinkling, .inputWifiPassword),
                                                                                (.blueConstant, .blueConstantAction)] 
        return lightButtons(config: config)
    }
    
    private func overseasCTypeButtons() -> [DHNetConnectFailureTuple] {
        let config: [(DHLightButtonType, DHNetConnectFailureOperationType)] = [(.redTwinkling, .inputWifiPassword),
                                                                                (.redConstant, .redLightConstantDetail),
                                                                                (.greenTwinkling, .inputWifiPassword),
                                                                                (.greenConstant, .greenConstantAction)]
        return lightButtons(config: config)
    }
    
    private func overseasDoorbellButtons() -> [DHNetConnectFailureTuple] {
        let config: [(String, DHNetConnectFailureOperationType)] = [("add_device_try_again".lc_T, .tryAgain)]
        return DHNetConnectFailureType.commonButtons(config: config)
    }
    
    private func commonTypeButtons(supportWired: Bool) -> [DHNetConnectFailureTuple] {
//        var config: [(String, DHNetConnectFailureOperationType)] = [("add_device_try_again".lc_T, .tryAgain)]
//        if supportWired {
//            config.append(("add_device_add_by_wired".lc_T, .switchToWired))
//        }
        //demo调整为重新开始
        let config: [(String, DHNetConnectFailureOperationType)] = [("add_device_restart".lc_T, .quit)]
        
        return DHNetConnectFailureType.commonButtons(config: config)
    }
    
    private func accessoryTypeButtons() -> [DHNetConnectFailureTuple] {
        let config: [(String, DHNetConnectFailureOperationType)] = [("add_device_continue_to_wait".lc_T, .continueToWait),
                                                                 ("add_device_try_again".lc_T, .tryAgain),
                                                                 ("add_device_quit_add_process".lc_T, .quit)]
        return DHNetConnectFailureType.commonButtons(config: config)
    }
    
    private func cloudTimeoutTypeButtons() -> [DHNetConnectFailureTuple] {
        let config: [(String, DHNetConnectFailureOperationType)] = [("add_device_try_again".lc_T, .tryAgain),
                                                                    ("add_device_quit_add_process".lc_T, .quit)]
        return DHNetConnectFailureType.commonButtons(config: config)
    }
    
    // MARK: General DHLightButton
    private func lightButtons(config : [(type: DHLightButtonType, oper: DHNetConnectFailureOperationType)]) -> [DHNetConnectFailureTuple] {
        var tuples = [DHNetConnectFailureTuple]()
        
        for index in 0..<config.count {
            let button = DHLightButton()
            button.lightType = config[index].type
            tuples.append((button, config[index].oper))
        }
        
        return tuples
    }
    
    // MARK: General CommonButton
    public static func commonButtons(config: [(title: String, action: DHNetConnectFailureOperationType)]) -> [DHNetConnectFailureTuple] {
        var tuples = [DHNetConnectFailureTuple]()
        
        for index in 0..<config.count {
            let button = UIButton(type: .custom)
            button.setTitle(config[index].title, for: .normal)
            tuples.append((button, config[index].action))
            
            //最后一个按钮颜色配置
            //if index != 0, index == config.count - 1
            if DHAddDeviceManager.sharedInstance.netConfigMode == .softAp,
                (DHAddDeviceManager.sharedInstance.deviceModel.lowercased() == "k5" || DHAddDeviceManager.sharedInstance.deviceModel.lowercased() == "k8") {
                button.setTitleColor(UIColor.dhcolor_c2(), for: .normal)
                button.backgroundColor = UIColor.dhcolor_c43()
                button.layer.borderColor = UIColor.dhcolor_c5().cgColor
                button.layer.borderWidth = 0.5
                button.layer.masksToBounds = true
                button.layer.cornerRadius = DHModuleConfig.shareInstance().commonButtonCornerRadius()
                continue
            }
 
            if index != 0 {
                button.setTitleColor(UIColor.dhcolor_c2(), for: .normal)
                button.backgroundColor = UIColor.dhcolor_c43()
                button.layer.borderColor = UIColor.dhcolor_c5().cgColor
                button.layer.borderWidth = 0.5
                button.layer.masksToBounds = true
                button.layer.cornerRadius = DHModuleConfig.shareInstance().commonButtonCornerRadius()
            } else {
                button.setTitleColor(UIColor.dhcolor_c43(), for: .normal)
                button.backgroundColor = DHModuleConfig.shareInstance().commonButtonColor()
                button.layer.masksToBounds = true
                button.layer.cornerRadius = DHModuleConfig.shareInstance().commonButtonCornerRadius()
            }
        }
        
        return tuples
    }
}