-
Notifications
You must be signed in to change notification settings - Fork 27
/
ScanViewController.swift
122 lines (102 loc) · 4.19 KB
/
ScanViewController.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
//
// ScanViewController.swift
// MetaWearApiTest
//
// Created by Stephen Schiffli on 10/18/16.
// Copyright © 2016 MbientLab. All rights reserved.
//
import Cocoa
import MetaWear
import MetaWearCpp
class ScanViewController: NSViewController, NSTableViewDelegate, NSTableViewDataSource {
@IBOutlet weak var tableView: NSTableView!
var scannerModel: ScannerModel!
// MARK: View Life Cycle
override func viewDidLoad() {
super.viewDidLoad()
tableView.target = self
tableView.doubleAction = #selector(ScanViewController.tableViewDoubleClick(sender:))
scannerModel = ScannerModel(delegate: self)
}
override func viewWillAppear() {
super.viewWillAppear()
scannerModel.isScanning = true
}
override func viewWillDisappear() {
super.viewWillDisappear()
scannerModel.isScanning = false
}
// MARK: NSTableViewDelegate
func numberOfRows(in tableView: NSTableView) -> Int {
return scannerModel.items.count
}
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
guard let cell = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "MetaWearCell"), owner: nil) as? NSTableCellView else {
return nil
}
let device = scannerModel.items[row].device
let uuid = cell.viewWithTag(1) as! NSTextField
uuid.stringValue = device.peripheral.identifier.uuidString
if let rssiNumber = device.averageRSSI() {
let rssi = cell.viewWithTag(2) as! NSTextField
rssi.stringValue = String(Int(rssiNumber.rounded()))
}
let connected = cell.viewWithTag(3) as! NSTextField
if device.isConnectedAndSetup {
connected.stringValue = "Connected!"
connected.isHidden = false
} else if scannerModel.items[row].isConnecting {
connected.stringValue = "Connecting..."
connected.isHidden = false
} else {
connected.isHidden = true
}
let name = cell.viewWithTag(4) as! NSTextField
name.stringValue = device.name
let signal = cell.viewWithTag(5) as! NSImageView
if let movingAverage = device.averageRSSI() {
if movingAverage < -80.0 {
signal.image = #imageLiteral(resourceName: "wifi_d1")
} else if movingAverage < -70.0 {
signal.image = #imageLiteral(resourceName: "wifi_d2")
} else if movingAverage < -60.0 {
signal.image = #imageLiteral(resourceName: "wifi_d3")
} else if movingAverage < -50.0 {
signal.image = #imageLiteral(resourceName: "wifi_d4")
} else if movingAverage < -40.0 {
signal.image = #imageLiteral(resourceName: "wifi_d5")
} else {
signal.image = #imageLiteral(resourceName: "wifi_d6")
}
} else {
signal.image = #imageLiteral(resourceName: "wifi_not_connected")
}
return cell
}
@objc func tableViewDoubleClick(sender: AnyObject) {
let device = scannerModel.items[tableView.clickedRow].device
guard !device.isConnectedAndSetup else {
device.flashLED(color: .red, intensity: 1.0, _repeat: 3)
mbl_mw_debug_disconnect(device.board)
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
self.tableView.reloadData()
}
return
}
scannerModel.items[tableView.clickedRow].toggleConnect()
tableView.reloadData()
}
}
extension ScanViewController: ScannerModelDelegate {
func scannerModel(_ scannerModel: ScannerModel, didAddItemAt idx: Int) {
tableView.reloadData()
}
func scannerModel(_ scannerModel: ScannerModel, confirmBlinkingItem item: ScannerModelItem, callback: @escaping (Bool) -> Void) {
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
callback(true)
self.tableView.reloadData()
}
}
func scannerModel(_ scannerModel: ScannerModel, errorDidOccur error: Error) {
}
}