-
Notifications
You must be signed in to change notification settings - Fork 9
/
ApplyDeviceConfigViewController.swift
202 lines (152 loc) · 6.6 KB
/
ApplyDeviceConfigViewController.swift
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
//
// ApplyDeviceConfigViewController.swift
// Developer Samples
//
// Created by Szymon Bobowiec on 24.01.2017.
// Copyright © 2017 kontakt.io. All rights reserved.
//
import UIKit
import KontaktSDK
class ApplyDeviceConfigViewController: UITableViewController {
// =========================================================================
// MARK: - Constants
let DEVICE_NOT_FOUND_TIMEOUT: TimeInterval! = TimeInterval(7.0)
// =========================================================================
// MARK: - Outlets
@IBOutlet weak var descriptionLabel: UILabel!
@IBOutlet weak var uniqueIdField: UITextField!
@IBOutlet weak var majorField: UITextField!
@IBOutlet weak var minorField: UITextField!
@IBOutlet weak var infoLabel: UILabel!
@IBOutlet weak var applyConfigButton: BorderedButton!
// =========================================================================
// MARK: - Vars
var devicesManager: KTKDevicesManager!
var configuration: KTKDeviceConfiguration!
var timer: Timer!
// =========================================================================
// MARK: - UIViewController
override func viewDidLoad() {
super.viewDidLoad()
setupView()
// Initialize Devices Manager
devicesManager = KTKDevicesManager(delegate: self)
}
// =========================================================================
// MARK: - Actions
@IBAction func applyConfig(_ sender: Any) {
if validInput() {
// Start scanning for device
showInfo(info: "Looking for device ...")
devicesManager.startDevicesDiscovery(withInterval: 2.0)
// Schedule timer - perform actions when device not found
timer = Timer.scheduledTimer(timeInterval: DEVICE_NOT_FOUND_TIMEOUT,
target: self,
selector: #selector(onDeviceNotFound),
userInfo: nil,
repeats: false);
// Disable button while applying config
applyConfigButton.isEnabled = false
}
}
@objc func onDeviceNotFound() {
devicesManager.stopDevicesDiscovery()
showError(error: "Device not found")
}
// =========================================================================
// MARK: - Private
private func setupView() {
// Setup nav bar
navigationItem.title = "Apply Config"
// Setup description label
descriptionLabel.textColor = UIColor.kontaktMediumGray
// Setup fields
uniqueIdField.textColor = UIColor.kontaktMediumGray
majorField.textColor = UIColor.kontaktMediumGray
majorField.keyboardType = UIKeyboardType.numberPad
minorField.textColor = UIColor.kontaktMediumGray
minorField.keyboardType = UIKeyboardType.numberPad
// Setup button
applyConfigButton.backgroundColor = UIColor.kontaktMainDarkBlue
applyConfigButton.layer.borderColor = UIColor.kontaktMainDarkBlue.cgColor
applyConfigButton.tintColor = UIColor.white
}
private func validInput() -> Bool {
// Validate if fields are not empty
if majorField.text == "" || minorField.text == "" || uniqueIdField.text == "" {
showError(error: "Fields not filled")
return false
}
// Validate unique ID length
let uniqueID = uniqueIdField.text!
if uniqueID.count >= 4 {
configuration = KTKDeviceConfiguration(uniqueID: uniqueID)
} else {
showError(error: "Unique ID must be 4-char length")
return false
}
// Validate major value
if let major = UInt16(majorField.text!) {
configuration.major = major as NSNumber?
} else {
showError(error: "Wrong major value")
return false
}
// Validate minor value
if let minor = UInt16(minorField.text!) {
configuration.minor = minor as NSNumber?
} else {
showError(error: "Wrong minor value")
return false
}
return true
}
// =========================================================================
// MARK: - Helper Methods
func showInfo(info: String!) {
infoLabel.text = info
infoLabel.textColor = UIColor.kontaktMediumGray
}
func showError(error: String!) {
infoLabel.text = error
infoLabel.textColor = UIColor.kontaktRed
applyConfigButton.isEnabled = true
}
func showSuccessInfo(info: String!) {
infoLabel.text = info
infoLabel.textColor = UIColor.kontaktMainGreen
applyConfigButton.isEnabled = true
}
}
// =========================================================================
// MARK: - KTKDevicesManagerDelegate
extension ApplyDeviceConfigViewController: KTKDevicesManagerDelegate {
func devicesManager(_ manager: KTKDevicesManager, didDiscover devices: [KTKNearbyDevice]) {
// Filter for desired device
if let device = devices.filter({$0.uniqueID == configuration.uniqueID}).first {
// Device found - stop discovery
devicesManager.stopDevicesDiscovery()
// Invalidate timer
timer.invalidate()
// Try to connect with device
let connection: KTKDeviceConnection? = KTKDeviceConnection(nearbyDevice: device)
// Check if connected
if let connection = connection {
showInfo(info: "Applying configuration ...")
// Write config if connected to device
connection.write(configuration) { synchronized, appliedConfiguration, error in
// Check if config applied successfully
if let _ = error {
self.showError(error: "Error while applying configuration")
} else {
self.showSuccessInfo(info: "Configuration applied")
}
}
}
}
}
func devicesManagerDidFail(toStartDiscovery manager: KTKDevicesManager, withError error: Error) {
print("Discovery did fail with error: \(String(describing: error))")
self.showError(error: "Device discovery did fail")
}
}