Skip to content

Commit

Permalink
Merge pull request #15 from XYOracleNetwork/develop
Browse files Browse the repository at this point in the history
release v 1.0.3
  • Loading branch information
klivin authored Nov 13, 2019
2 parents 5090ac2 + d40079b commit effb04b
Show file tree
Hide file tree
Showing 25 changed files with 306 additions and 223 deletions.
4 changes: 2 additions & 2 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ PODS:
- sdk-objectmodel-swift (~> 3.0)
- secp256k1.swift
- sdk-objectmodel-swift (3.0.2)
- sdk-xyo-swift (1.0.2):
- sdk-xyo-swift (1.0.3):
- sdk-xyobleinterface-swift
- sdk-xyobleinterface-swift (3.0.8):
- PromisesSwift (~> 1.2.4)
Expand Down Expand Up @@ -39,7 +39,7 @@ SPEC CHECKSUMS:
PromisesSwift: 37bad6f4daddb02f7c9c531efe91e8b21c13ee2f
sdk-core-swift: 6b089045fe8483388bd3c82d2d1375d796c29745
sdk-objectmodel-swift: 8d880c7871ab0ce16fed958d782c737fd495dd53
sdk-xyo-swift: c17ffeaf281221059be46a9c282a8afa6a32bf74
sdk-xyo-swift: 65aa6396b1650d320db6cb24b94adc1f9f24f1bc
sdk-xyobleinterface-swift: ba891f790841a9c9a503f60548586f3efadc8b23
secp256k1.swift: a7e7a214f6db6ce5db32cc6b2b45e5c4dd633634
XyBleSdk: 8d63d5cba164fa7d05d662fca0231f065fddccbe
Expand Down
2 changes: 1 addition & 1 deletion Example/XyoSdkExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
324EA14AF326520A70B6AE7C /* LICENSE */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text; name = LICENSE; path = ../LICENSE; sourceTree = "<group>"; };
4CE61CB1F1B5AFE9D3917B17 /* Pods_iOSExample.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_iOSExample.framework; sourceTree = BUILT_PRODUCTS_DIR; };
5882E7006B1D2655A2EA82FD /* Pods-iOSExample.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-iOSExample.release.xcconfig"; path = "Target Support Files/Pods-iOSExample/Pods-iOSExample.release.xcconfig"; sourceTree = "<group>"; };
94D24B3FC73A22A86214F8B7 /* sdk-xyo-swift.podspec */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text; name = "sdk-xyo-swift.podspec"; path = "../sdk-xyo-swift.podspec"; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.ruby; };
94D24B3FC73A22A86214F8B7 /* sdk-xyo-swift.podspec */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text; name = "sdk-xyo-swift.podspec"; path = "../sdk-xyo-swift.podspec"; sourceTree = "<group>"; };
B5819FDF30B92E7EE6E1C232 /* Pods-iOSExample.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-iOSExample.debug.xcconfig"; path = "Target Support Files/Pods-iOSExample/Pods-iOSExample.debug.xcconfig"; sourceTree = "<group>"; };
FC0196377632BCEBE33FBDF3 /* README.md */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = net.daringfireball.markdown; name = README.md; path = ../README.md; sourceTree = "<group>"; };
/* End PBXFileReference section */
Expand Down
14 changes: 7 additions & 7 deletions Example/iOSExample/XyoExampleViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,18 @@ class XyoExampleViewController: UIViewController {
}
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) {
print("Xyo VC going away, remove retain cycle")
XyoSdk.nodes.removeAll()
xyoNode = nil
}
Expand Down Expand Up @@ -162,6 +165,9 @@ extension XyoExampleViewController : BoundWitnessDelegate {
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()

Expand All @@ -178,12 +184,6 @@ extension XyoExampleViewController : BoundWitnessDelegate {
print("Errored BW with \(String(describing: withDeviceId)) \(String(describing: withError))")
}

func getPayloadData() -> [UInt8]? {
if isClient {
return [UInt8]("Hey, I'm client".utf8)
}
return [UInt8]("Yo, I'm the server".utf8)
}
}


Expand Down
42 changes: 14 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Include the library in your Podfile

```Podfile
target 'YourAppName' do
pod 'sdk-xyo-swift', '1.0.2'
pod 'sdk-xyo-swift', '1.0.3'
```

Expand Down Expand Up @@ -90,20 +90,21 @@ class SomeViewController: UIViewController, BoundWitnessDelegate {

```

You can also get payload data from bound witness.
You can also set a string payload data on any node that gets passed in a bound witness

```swift
class SomeViewController: UIViewController, BoundWitnessDelegate {
...
func getPayloadData() {
if isClient {
return [UInt8]("Hey, I'm client".utf8)
if var bleClient = (xyoNode?.networks["ble"] as? XyoBleNetwork)?.client {
bleClient.pollingInterval = 10
bleClient.stringHeuristic = "Hi I'm Client"
}
return [UInt8]("Yo, I'm the server".utf8)

if var bleServer = (xyoNode?.networks["ble"] as? XyoBleNetwork)?.server {
bleServer.stringHeuristic = "Yo I'm Server"
}
}
```
This will return a byteArray.

The following extensions can be used to pull data from a bound witness. Party index 0 is the server, party 1 is the client.

Expand All @@ -121,33 +122,18 @@ Given the above example of passing strings, you can resolve those strings for cl
```


You can create your own data serializers and deserializers.
Or you can get all heuristics in a dictionary for a given bound witness

```swift
extension XyoBoundWitness {
func resolveTimePayload() {
let resolver = TimeResolver()
XyoHumanHeuristics.resolvers[XyoSchemas.UNIX_TIME.id] = resolver
let key = resolver.getHumanKey(partyIndex: 1)
return XyoHumanHeuristics.getHumanHeuristics(boundWitness: self).index(forKey: key).debugDescription
extension XyoBoundWitness {
func allHeuristics() : [String:String] {
return XyoHumanHeuristics.getAllHeuristics(self)
}
}
}
```

Create a bound witness RSSI parser

```swift
extension XyoBoundWitness {
func resolveRssiPayload() {
let resolver = RssiResolver()
XyoHumanHeuristics.resolvers[XyoSchemas.RSSI.id] = resolver
let key = resolver.getHumanKey(partyIndex: 1)
return XyoHumanHeuristics.getHumanHeuristics(boundWitness: self).index(forKey: key).debugDescription
}
}
```

You can see more heuristic resolvers in the source code:
You can see individual heuristic resolvers in the source code:

[GPS](./Heuristics/GpsResolver.swift)

Expand Down
50 changes: 37 additions & 13 deletions Source/Clients/XyoBleClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,23 @@ struct XyoBleClientError: Error {
}


class XyoBleClient: XyoClient {
class XyoBleClient: XyoClient, XyoHeuristicGetter {

static let DefaultPollingTime = 6 // seconds

var knownBridges: [String] = []
var relayNode: XyoRelayNode
var procedureCatalog: XyoProcedureCatalog
weak var delegate: BoundWitnessDelegate?

var acceptBridging: Bool = false
var autoBoundWitness: Bool = false
var autoBridge: Bool = false
var scan: Bool = false {
var stringHeuristic: String?
var enabledHeuristics: [XyoHeuristicEnum : XyoHeuristicGetter] = [:]

weak var delegate: BoundWitnessDelegate?

public var pollingInterval = DefaultPollingTime // seconds
public var scan: Bool = false {
didSet {
if (scan) {
startScanningForDevices()
Expand All @@ -43,15 +48,18 @@ class XyoBleClient: XyoClient {
}

private weak var scanner = XYSmartScan.instance
public var pollingInterval = DefaultPollingTime // seconds
private var currentRssi : Int? = nil

private var lastBoundWitnessTime = Date().addingTimeInterval(TimeInterval(-1 * DefaultPollingTime))

// private var semaphore = DispatchSemaphore(value: 1)
private var semaphore = true

required init(relayNode: XyoRelayNode, procedureCatalog: XyoProcedureCatalog) {
self.relayNode = relayNode
self.procedureCatalog = procedureCatalog
self.enableHeursitics(heuristics: [.time, .string], enabled: true)

XyoBluetoothDevice.family.enable(enable: true)
XyoBluetoothDeviceCreator.enable(enable: true)
}
Expand All @@ -66,13 +74,13 @@ class XyoBleClient: XyoClient {
func startScanningForDevices() {
scanner?.setDelegate(self, key: "xyo_client")
scanner?.start(mode: XYSmartScanMode.foreground)
relayNode.addHeuristic(key: "XyoBleClient", getter: self)
relayNode.addHeuristic(key: "rssi_client", getter: self)
}

func stopScanningForDevices() {
scanner?.removeDelegate(for: "xyo_client")
scanner?.stop()
relayNode.removeHeuristic(key: "XyoBleClient")
relayNode.removeHeuristic(key: "rssi_client")
}

deinit {
Expand All @@ -82,8 +90,9 @@ class XyoBleClient: XyoClient {
typealias BoundWitnessCallback = ((_ boundWitness: XyoBoundWitness?, _ device: XyoBluetoothDevice?, _ error: Error?) -> Void)?

func doBoundWitness(withDevice: XyoBluetoothDevice, withCompletion: BoundWitnessCallback) throws {

self.delegate?.boundWitness(started: withDevice.id)
// DispatchQueue.main.sync {

withDevice.connection {
[weak self] in
withDevice.connect()
Expand Down Expand Up @@ -111,7 +120,8 @@ class XyoBleClient: XyoClient {
strong.delegate?.boundWitness(failed: withDevice.id, withError: XyoError.RESPONSE_IS_NULL)
return
}



strong.delegate?.boundWitness(completed: withDevice.id, withBoundWitness: bw)
withCompletion?(bw, withDevice, nil)
}
Expand All @@ -124,10 +134,18 @@ class XyoBleClient: XyoClient {
print("CAUGHT ERROR \(err)")
withCompletion?(nil, nil, err)
}
// }
}
}

/// XyoHeuristicGetter
func getHeuristic() -> XyoObjectStructure? {
guard let rssi = currentRssi else { return nil }

let unsignedRssi = UInt8(bitPattern: Int8(rssi))
let rssiTag = XyoObjectStructure.newInstance(schema: XyoSchemas.RSSI, bytes: XyoBuffer().put(bits: (unsignedRssi)))
return rssiTag
}

}

extension XyoBleClient : XYSmartScanDelegate {
func smartScan(entered device: XYBluetoothDevice) {
Expand All @@ -148,14 +166,20 @@ extension XyoBleClient : XYSmartScanDelegate {
if let xyoDevice = device as? XyoBluetoothDevice {
if (Int(Date().timeIntervalSince(self.lastBoundWitnessTime)) > self.pollingInterval) {
self.semaphore = false
self.currentRssi = xyoDevice.rssi

print("Initiatiting auto-boundwithness from scan")

do {
self.lastBoundWitnessTime = Date()
try self.doBoundWitness(withDevice: xyoDevice)
{
[weak self]
_,_,_ in
_,_,error in
if let err = error {
print("Error completing bound witness \(err)")
}

self?.semaphore = true
}

Expand Down
2 changes: 1 addition & 1 deletion Source/Clients/XyoClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import Foundation
import sdk_core_swift

public protocol XyoClient: XyoBoundWitnessTarget, XyoHeuristicGetter {
public protocol XyoClient: XyoBoundWitnessTarget {
var pollingInterval: Int {get set}
var scan: Bool {get set}
var autoBoundWitness: Bool {get set}
Expand Down
7 changes: 6 additions & 1 deletion Source/Clients/XyoTcpipClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ struct TcpipClientError : Error {
let kind : ErrorKind
}

class XyoTcpipClient: XyoClient {
class XyoTcpipClient: XyoClient {
var stringHeuristic: String?

var enabledHeuristics: [XyoHeuristicEnum : XyoHeuristicGetter] = [:]

var knownBridges: [String] = []
var relayNode: XyoRelayNode
var procedureCatalog: XyoProcedureCatalog
Expand All @@ -39,6 +43,7 @@ class XyoTcpipClient: XyoClient {
required init(relayNode: XyoRelayNode, procedureCatalog: XyoProcedureCatalog) {
self.procedureCatalog = procedureCatalog
self.relayNode = relayNode
self.enableHeursitics(heuristics: [.time, .string], enabled: true)
}

convenience init(relayNode: XyoRelayNode, procedureCatalog: XyoProcedureCatalog, autoBridge: Bool, acceptBridging: Bool, autoBoundWitness: Bool) {
Expand Down
4 changes: 2 additions & 2 deletions Source/Heuristics/GpsResolver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public struct GpsResolver: XyoHumanHeuristicResolver {
}

extension XyoBoundWitness {
public func resolveGPSPayload(forParty: Int) -> String {
let resolver = RssiResolver()
public func resolveGPSPayload(forParty: Int) -> String? {
let resolver = GpsResolver()
XyoHumanHeuristics.resolvers[XyoSchemas.GPS.id] = resolver

return resolver.getName(forParty: forParty, boundWitness: self)
Expand Down
2 changes: 1 addition & 1 deletion Source/Heuristics/RssiResolver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public struct RssiResolver: XyoHumanHeuristicResolver {
}

extension XyoBoundWitness {
public func resolveRssiPayload(forParty: Int) -> String {
public func resolveRssiPayload(forParty: Int) -> String? {
let resolver = RssiResolver()
XyoHumanHeuristics.resolvers[XyoSchemas.RSSI.id] = resolver

Expand Down
4 changes: 2 additions & 2 deletions Source/Heuristics/StringResolver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ public struct StringResolver: XyoHumanHeuristicResolver {
if let blob = objectValue?.toByteArray() {
return String(bytes: blob, encoding: .utf8)
}
return ""
return nil
}
}

extension XyoBoundWitness {

public func resolveString(forParty: Int) -> String {
public func resolveString(forParty: Int) -> String? {
let resolver = StringResolver()
XyoHumanHeuristics.resolvers[XyoSchemas.BLOB.id] = resolver
return resolver.getName(forParty: forParty, boundWitness: self)
Expand Down
2 changes: 1 addition & 1 deletion Source/Heuristics/TimeResolver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public struct TimeResolver: XyoHumanHeuristicResolver {
}

extension XyoBoundWitness {
public func resolveTime(forParty: Int) -> String {
public func resolveTime(forParty: Int) -> String? {
let resolver = TimeResolver()
XyoHumanHeuristics.resolvers[XyoSchemas.UNIX_TIME.id] = resolver
return resolver.getName(forParty: forParty, boundWitness: self)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import sdk_core_swift
import sdk_objectmodel_swift
import CoreLocation

public struct XyoGps: XyoHueresticGetter {
public struct XyoGpsHeuristic: XyoHeuristicGetter {
let locManager = CLLocationManager()

init() {
Expand Down
Loading

0 comments on commit effb04b

Please sign in to comment.