-
Notifications
You must be signed in to change notification settings - Fork 3
/
XyoExampleViewController.swift
188 lines (150 loc) · 5.19 KB
/
XyoExampleViewController.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
//
// XyoExampleViewController.swift
// iOSExample
//
// Created by Kevin Weiler on 10/17/19.
// Copyright © 2019 CocoaPods. All rights reserved.
//
import UIKit
import sdk_xyo_swift
import sdk_core_swift
import XyBleSdk
struct BoundWitnessResult {
var device: String
var dataString: String?
var debugString: String?
}
class XyoExampleViewController: UIViewController {
@IBOutlet weak var scanLabel: UILabel!
@IBOutlet weak var tableView: UITableView!
private weak var scanner = XYSmartScan.instance
public var isClient: Bool = true
var isLockedOnBottom: Bool = true
var isScrolling = false
var boundWitnesses : [BoundWitnessResult] = []
var xyoNode : XyoNode?
override func viewDidLoad() {
super.viewDidLoad()
title = isClient ? "Client" : "Server"
scanLabel.text = isClient ? "Scan" : "Listen"
let builder = XyoNodeBuilder()
builder.setBoundWitnessDelegate(self)
do {
xyoNode = try builder.build()
}
catch {
print("Caught Error Building Xyo Node\(error)")
}
if var bleClient = (xyoNode?.networks["ble"] as? XyoBleNetwork)?.client {
bleClient.pollingInterval = 10
bleClient.stringHeuristic = "Hi I'm Client"
}
if var bleServer = (xyoNode?.networks["ble"] as? XyoBleNetwork)?.server {
bleServer.stringHeuristic = "Yo I'm Server"
}
// Start client/server scanning and listening
setupNodeScanningListening(on: true)
updateBridging(on: false)
}
override func viewWillDisappear(_ animated: Bool) {
XyoSdk.nodes.removeAll()
xyoNode = nil
}
deinit {
print("XyoExampleViewController deinit")
}
@IBAction func autoBridgeToggled(_ sender: UISwitch) {
updateBridging(on: sender.isOn)
}
@IBAction func scanListenToggled(_ sender: UISwitch) {
setupNodeScanningListening(on: sender.isOn)
}
func setupNodeScanningListening(on: Bool) {
let ble = xyoNode?.networks["ble"] as? XyoBleNetwork
if isClient {
ble?.client?.scan = on
} else {
ble?.server?.listen = on
}
}
func updateBridging(on: Bool) {
let tcp = xyoNode?.networks["tcpip"] as? XyoTcpipNetwork
if isClient {
tcp?.client?.autoBridge = on
} else {
tcp?.server?.autoBridge = on
}
}
}
extension XyoExampleViewController : UITableViewDelegate, UITableViewDataSource {
/// Table view datasource/delegate
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return boundWitnesses.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "BoundWitnessCelId", for: indexPath)
cell.textLabel?.text = boundWitnesses[indexPath.item].device;
var detail = ""
if let data = boundWitnesses[indexPath.item].dataString {
detail.append(data)
}
cell.detailTextLabel?.text = detail;
return cell;
}
/// Scroll view delegation
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
isScrolling = true
}
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
if !decelerate { scrollViewDidEndScrolling(scrollView) }
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
scrollViewDidEndScrolling(scrollView)
}
func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
scrollViewDidEndScrolling(scrollView)
}
func scrollViewDidEndScrolling(_ scrollView: UIScrollView) {
isScrolling = false
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let scrollViewHeight = scrollView.frame.size.height
let scrollContentSizeHeight = scrollView.contentSize.height
let scrollOffset = scrollView.contentOffset.y
if (scrollOffset + scrollViewHeight + 100 >= scrollContentSizeHeight)
{
isLockedOnBottom = true
} else {
isLockedOnBottom = false
}
}
}
extension XyoExampleViewController : BoundWitnessDelegate {
func boundWitness(started withDeviceId: String) {
print("Started BW with \(withDeviceId)")
}
func boundWitness(completed withDeviceId: String, withBoundWitness: XyoBoundWitness?) {
print("Completed BW with \(withDeviceId)")
var dataStr = ""
if let resolveStr = withBoundWitness?.resolveString(forParty: 0) {
dataStr += "Server: " + resolveStr
}
if let resolveStr1 = withBoundWitness?.resolveString(forParty: 1) {
dataStr += " Client: " + resolveStr1
}
if let rssistr = withBoundWitness?.resolveRssiPayload(forParty: 1) {
dataStr += " Rssi: " + rssistr
}
boundWitnesses.append(BoundWitnessResult(device: withDeviceId, dataString: dataStr, debugString: withBoundWitness.debugDescription))
tableView.reloadData()
// Scroll to bottom
if (isLockedOnBottom && !isScrolling) {
DispatchQueue.main.async {
self.tableView.scrollToRow(at: IndexPath(item: self.boundWitnesses.count-1, section: 0), at: UITableView.ScrollPosition.bottom, animated: true)
}
}
}
func boundWitness(failed withDeviceId: String?, withError: XyoError) {
print("Errored BW with \(String(describing: withDeviceId)) \(String(describing: withError))")
}
}