Skip to content

JanC/XPConnectKit

Repository files navigation

XPConnectKit

Swift Version License Carthage compatiblePlatform

XPConnectKit is a swift wrapper around the XPlaneConnect client.

Requirements

  • iOS 11.0+
  • Xcode 9.0

Installation

Carthage

Create a Cartfile that lists the framework and run carthage update. Follow the instructions to add $(SRCROOT)/Carthage/Build/iOS/XPConnectKit.framework to an iOS project.

github "JanC/XPConnectKit" "master"

Usage example

The base class is the XPLConnector that you initialize with the ip or host name of the X-Plane running the XPlaneConnect server:

let connector = XPLConnector(host: "192.168.1.10")

Single DREFs

To get a signle data ref, call the connector.get(dref:"...") method. To facilitate the parsing into expected types, you have to supply a Parser that will parse the float array into the type you expect:

let connector = connector(host: "192.168.1.10")

do {
    // The com1_freq_hz values is an Int e.g. 11800 so we pass IntParser
    let com1 = try connector.get(dref: "sim/cockpit/radios/com1_freq_hz", parser: IntParser())
    print("com1: \(com1)")
    
    // The acf_tailnum values is an String so we pass StringParser
    let tailnum = try connector.get(dref: "sim/aircraft/view/acf_tailnum", parser: StringParser())
    print("tailnum: \(tailnum)")
    
} catch {
    print("error: \(error)")
}

Polling multiple DREF

You can use the XPLConnector to poll regurarly for a set of data refs. The closure with the result values will be called regurarly and result is a two dimentional array of Floats. You can use again a Parser to get the expected value:

let connector = XPLConnector(host: "192.168.1.10")

let radioDrefs = [
    "sim/cockpit/radios/com1_freq_hz",
    "sim/cockpit/radios/com1_stdby_freq_hz",
    "sim/cockpit/radios/nav1_freq_hz",
    "sim/cockpit/radios/nav1_stdby_freq_hz",
    ]

let parser = IntParser()
connector.startRequesting(drefs: radioDrefs) { (values) in
    do {
        var index = 0
        self.com1Label.text = try parser.parse(values: values[index]).formattedFrequency
        index += 1
        
        self.com1LabelStby.text = try parser.parse(values: values[index]).formattedFrequency
        index += 1
        
        self.nav1Label.text = try parser.parse(values: values[index]).formattedFrequency
        index += 1
        
        self.nav1LabelStby.text = try parser.parse(values: values[index]).formattedFrequency
        index += 1
    } catch {
        print("Could not parse dref: \(error)")
    }
}

To stop polling all currently requested drefs, call:

connector.stopRequestingDataRefs()

XPDiscovery

The XPConnector contains another module XPDiscoveryKit which is used to discover X-Plane instances running with XPlaneConnect plugin installed.

Installation

Once you build the Carthage modules, you need to import XPDiscoveryKit.framework as well as its dependency CocoaAsyncSocket.framework in your project.

Usage

let discovery = XPDiscovery()
discovery.delegate = self

// start the discovery
try? discovery.start()

// MARK: - XPDiscoveryDelegate

func discovery(_ discovery: XPDiscovery, didDiscoverNode node: XPLNode) {
    print("Discovered XPlane version \(node.beacon.xplaneVersion) - \(node.beacon.xplaneConnectVersion): \(node.address):\(node.port)")
}

func discovery(_ dicovery: XPDiscovery, didLostNode node: XPLNode) {
	print("Connection lost. No beacon received from \(node.address) after \(discovery.timeout)s")
}

Contribute

We would love you for the contribution to XPConnectKit, check the LICENSE file for more info.

Meta

Jan Chaloupecky – @TexTwil

Distributed under the XYZ license. See LICENSE for more information.

About

Swift wrapper for the NASA XPlaneConnect client

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published