IF API connection tutorial


#1

Hey everyone,

Lately I’ve had a few requests for help from developers trying to get a connection up and running with the IF local API.
This is exciting since it means more add-ons are on the horizon! :-)

Best thing is to examine and learn from Cam’s awesome open source LiveFlight Connect (that’s how I learned back when I first wrote the first prototype of In-Flight Assistant):

As you can see, you will want to open a socket to localhost, port 10111 (if you’re going to be on the same device).

The key method is this one:

    private void WriteObject<T>(T state)
    {
        var stateString = Serializer.SerializeJson(state);
        var data = UTF8Encoding.UTF8.GetBytes(stateString);
        byte[] size = BitConverter.GetBytes(data.Length);
        NetworkStream.Write(size, 0, size.Length);
        NetworkStream.Write(data, 0, data.Length);
    }

Note that you first need to send the LENGTH of the JSON’s content that you are trying to send. And THEN you send the JSON itself. The JSON depends on the command you are trying to send.

Note that you need to send the data in Little Endian format (the language you are writing in will have its own way of doing this). If you don’t know what that means, you’re perhaps not quite ready to be programming IF stuff yet (but maybe you can teach yourself!)…

Here’s the counterpart, the reading method:

    private string ReadCommand()
    {
        var sizeToRead = ReadInt();
        var buffer = new byte[sizeToRead];
        var offset = 0;

        while (sizeToRead != 0)
        {
            var read = NetworkStream.Read(buffer, offset, sizeToRead);
            offset += read;
            sizeToRead -= read;
        }

        string str = Encoding.UTF8.GetString(buffer);
        return str;
    }

Pretty much the same thing just the other way around. The data will be coming in and you will need to parse it.

Any questions? Post them in here, that way everyone can see the answers. 🙂

Yep, there is, send the command “ListCommands” and you get the following list back:

Full command list: 105 commands

help: Show the help
listcommands: Lists all commands
airplane.getstate: Retrieves the full airplane state
airplane.getenginesstate: Retrieves the full airplane engines state
airplane.getfuelstate: Retrieves the full airplane engines state
airplane.getinfo: Retrieves the full airplane info
airplane.getlightsstate: Retrieves the full airplane lights state
airplane.setlightsstate: Set lights statestate
camera.virtualcockpit.setviewangle: Set the camera view angle (Radians)
camera.virtualcockpit.resetviewangle: Disabled the view angle override
va: Set the camera view angle (Radians) (Shortcut)
rv: Disabled the view angle override (Shortcut)
autopilot.getstate: Retrieves the AP State
infiniteflight.getstatus: Get the status of the application
networkjoystick.setaxisvalue: Lists all commands
networkjoystick.setbuttonstate: Lists all commands
networkjoystick.setpovstate: Lists all commands
networkjoystick.setnetworkjoystickaxes: Sets all the flight control axes to \“Network Joystick\”.
live.getweather: Lists all commands
live.gettraffic: Lists all commands
live.atcfacilities: Lists all commands
live.getcurrentcomfrequencies: Lists all commands
live.tunetofrequency: Lists all commands
live.enableatcmessagelistupdated: Lists all commands
flightplan.get: Gets the Flight Plan Info
flightplan.load: Gets the Flight Plan Info
commands.elevatortrimup: Elevator Trim Up
commands.elevatortrimdown: Elevator Trim Down
commands.throttleupcommand: Throttle Up
commands.throttledowncommand: Throttle Down
commands.setcockpitcamera: Cockpit Camera
commands.setvirtualcockpitcameracommand: Virtual Cockpit Camera
commands.setfollowcameracommand: Follow Camera
commands.setflybycamera: Fly By Camera
commands.setonboardcameracommand: Onboard Camera
commands.settowercameracommand: Tower Camera
commands.nextcamera: Next Camera
commands.prevcamera: Previous Camera
commands.cameramoveleft: Move Camera Left
commands.cameramoveright: Move Camera Right
commands.cameramovedown: Move Camera Down
commands.cameramoveup: Move Camera Up
commands.cameramovehorizontal: Move Camera Left\/Right
commands.cameramovevertical: Move Camera Up\/Down
commands.camerazoomin: Zoom In
commands.camerazoomout: Zoom Out
commands.showatcwindowcommand: Toggle ATC Window
commands.atcentry1: Select ATC Entry 1
commands.atcentry2: Select ATC Entry 2
commands.atcentry3: Select ATC Entry 3
commands.atcentry4: Select ATC Entry 4
commands.atcentry5: Select ATC Entry 5
commands.atcentry6: Select ATC Entry 6
commands.atcentry7: Select ATC Entry 7
commands.atcentry8: Select ATC Entry 8
commands.atcentry9: Select ATC Entry 9
commands.atcentry10: Select ATC Entry 10
commands.live.setcomfrequencies: SetCOMFrequencies
commands.flightplan.addwaypoints:
commands.flightplan.clear:
commands.flightplan.activateleg:
commands.brakes: Brakes
commands.parkingbrakes: Parking Brakes
commands.flapsdown: Flaps Down (1 Step)
commands.flapsup: Flaps Up (1 Step)
commands.flapsfulldown: Flaps Down (Full)
commands.flapsfullup: Flaps Up (Full)
commands.aircraft.setflapstate:
commands.spoilers: Toggle Spoilers
commands.landinggear: Landing Gear
commands.pushback: Pushback
commands.fueldump: Fuel Dump
commands.reversethrust: ReverseThrust
commands.landinglights: Landing Lights
commands.taxilights: Taxi Lights
commands.strobelights: Strobes
commands.beaconlights: Beacon Lights
commands.navlights: Navigation Lights
commands.setlandinglightsstate:
commands.settaxilightsstate:
commands.setstrobelightsstate:
commands.setbeaconlightsstate:
commands.setnavlightsstate:
commands.autopilot.toggle: Engage\/Disengage Autopilot
commands.autopilot.setstate: SetState
commands.autopilot.setheading: SetHeading
commands.autopilot.setaltitude: SetAltitude
commands.autopilot.setvs: SetVS
commands.autopilot.setspeed: SetSpeed
commands.autopilot.setheadingstate: SetHeadingState
commands.autopilot.setaltitudestate: SetAltitudeState
commands.autopilot.setvsstate: SetVSState
commands.autopilot.setspeedstate: SetSpeedState
commands.autopilot.setapproachmodestate: SetApproachModeState
commands.togglehud: Toggle HUD
commands.togglepause: Pause
commands.start recording:
commands.stop recording:
commands.engine.start: Start Specified Engine
commands.engine.stop: Stop Specified Engine
a+: Increase altitude 1,000ft
a++: Increase altitude 10,000ft
recenter: Recenter World
fpl.add: Add Flight Plan Item
infiniteflight.state: Increase altitude 10,000ft


Infinite Flight Connect API [Developers]
#2

Uhh, what is Little Endian format?


#3

That would take a while to explain, so best to read here:

https://betterexplained.com/articles/understanding-big-and-little-endian-byte-order/


#4

Very handy!

I’ve bookmarked it ;)


#5

What language is this C#, C++, etc?


#6

LiveFlight Connect is in C#.


#7

Nice. No wonder it looks familiar to me.


#8

This is really cool! I don’t know much about C#, but I should learn more about it since IF is written in the language


#9

Yep, there is, send the command “ListCommands” and you get the following list back:

Full command list: 105 commands

help: Show the help
listcommands: Lists all commands
airplane.getstate: Retrieves the full airplane state
airplane.getenginesstate: Retrieves the full airplane engines state
airplane.getfuelstate: Retrieves the full airplane engines state
airplane.getinfo: Retrieves the full airplane info
airplane.getlightsstate: Retrieves the full airplane lights state
airplane.setlightsstate: Set lights statestate
camera.virtualcockpit.setviewangle: Set the camera view angle (Radians)
camera.virtualcockpit.resetviewangle: Disabled the view angle override
va: Set the camera view angle (Radians) (Shortcut)
rv: Disabled the view angle override (Shortcut)
autopilot.getstate: Retrieves the AP State
infiniteflight.getstatus: Get the status of the application
networkjoystick.setaxisvalue: Lists all commands
networkjoystick.setbuttonstate: Lists all commands
networkjoystick.setpovstate: Lists all commands
networkjoystick.setnetworkjoystickaxes: Sets all the flight control axes to \“Network Joystick\”.
live.getweather: Lists all commands
live.gettraffic: Lists all commands
live.atcfacilities: Lists all commands
live.getcurrentcomfrequencies: Lists all commands
live.tunetofrequency: Lists all commands
live.enableatcmessagelistupdated: Lists all commands
flightplan.get: Gets the Flight Plan Info
flightplan.load: Gets the Flight Plan Info
commands.elevatortrimup: Elevator Trim Up
commands.elevatortrimdown: Elevator Trim Down
commands.throttleupcommand: Throttle Up
commands.throttledowncommand: Throttle Down
commands.setcockpitcamera: Cockpit Camera
commands.setvirtualcockpitcameracommand: Virtual Cockpit Camera
commands.setfollowcameracommand: Follow Camera
commands.setflybycamera: Fly By Camera
commands.setonboardcameracommand: Onboard Camera
commands.settowercameracommand: Tower Camera
commands.nextcamera: Next Camera
commands.prevcamera: Previous Camera
commands.cameramoveleft: Move Camera Left
commands.cameramoveright: Move Camera Right
commands.cameramovedown: Move Camera Down
commands.cameramoveup: Move Camera Up
commands.cameramovehorizontal: Move Camera Left\/Right
commands.cameramovevertical: Move Camera Up\/Down
commands.camerazoomin: Zoom In
commands.camerazoomout: Zoom Out
commands.showatcwindowcommand: Toggle ATC Window
commands.atcentry1: Select ATC Entry 1
commands.atcentry2: Select ATC Entry 2
commands.atcentry3: Select ATC Entry 3
commands.atcentry4: Select ATC Entry 4
commands.atcentry5: Select ATC Entry 5
commands.atcentry6: Select ATC Entry 6
commands.atcentry7: Select ATC Entry 7
commands.atcentry8: Select ATC Entry 8
commands.atcentry9: Select ATC Entry 9
commands.atcentry10: Select ATC Entry 10
commands.live.setcomfrequencies: SetCOMFrequencies
commands.flightplan.addwaypoints:
commands.flightplan.clear:
commands.flightplan.activateleg:
commands.brakes: Brakes
commands.parkingbrakes: Parking Brakes
commands.flapsdown: Flaps Down (1 Step)
commands.flapsup: Flaps Up (1 Step)
commands.flapsfulldown: Flaps Down (Full)
commands.flapsfullup: Flaps Up (Full)
commands.aircraft.setflapstate:
commands.spoilers: Toggle Spoilers
commands.landinggear: Landing Gear
commands.pushback: Pushback
commands.fueldump: Fuel Dump
commands.reversethrust: ReverseThrust
commands.landinglights: Landing Lights
commands.taxilights: Taxi Lights
commands.strobelights: Strobes
commands.beaconlights: Beacon Lights
commands.navlights: Navigation Lights
commands.setlandinglightsstate:
commands.settaxilightsstate:
commands.setstrobelightsstate:
commands.setbeaconlightsstate:
commands.setnavlightsstate:
commands.autopilot.toggle: Engage\/Disengage Autopilot
commands.autopilot.setstate: SetState
commands.autopilot.setheading: SetHeading
commands.autopilot.setaltitude: SetAltitude
commands.autopilot.setvs: SetVS
commands.autopilot.setspeed: SetSpeed
commands.autopilot.setheadingstate: SetHeadingState
commands.autopilot.setaltitudestate: SetAltitudeState
commands.autopilot.setvsstate: SetVSState
commands.autopilot.setspeedstate: SetSpeedState
commands.autopilot.setapproachmodestate: SetApproachModeState
commands.togglehud: Toggle HUD
commands.togglepause: Pause
commands.start recording:
commands.stop recording:
commands.engine.start: Start Specified Engine
commands.engine.stop: Stop Specified Engine
a+: Increase altitude 1,000ft
a++: Increase altitude 10,000ft
recenter: Recenter World
fpl.add: Add Flight Plan Item
infiniteflight.state: Increase altitude 10,000ft


#10

I’m able to connect to IF Connect and sending commands to them using Camerons source code. Commands like other camera, engine start/stop works fine. But get stuck when comes to receiving data. (Receiving the IP Adress and port works). Can somebody help me out with it? Swift/Objective-C.


#11

I am trying to send commands via the API but I fail everytime I try. This is the code below (Java):

static void connect() {
    try {

        Socket connection = new Socket(HOST, PORT);
        connection.setTcpNoDelay(true);
        OutputStream outputStream = new DataOutputStream(connection.getOutputStream());
        byte[] data = createData();
        byte[] size = getSizeObject(data);
        outputStream.write(size,0, size.length);
        outputStream.flush();
        outputStream.write(data,0, data.length);
        outputStream.flush();
        outputStream.close();
        connection.close();

    } catch (IOException e) {
        Log.e(TAG, "IOException thrown : Unable to connect to Infinite Flight Connect");
    }
}

createData method :

private static byte[] createData() {
    try {
        JSONObject root = new JSONObject();
        root.put("Command", "Commands.StrobeLights");
        root.put("Parameters", new JSONArray());
        ByteBuffer byteBuffer = ByteBuffer.wrap(root.toString().getBytes()).order(ByteOrder.LITTLE_ENDIAN);
        if (byteBuffer.hasArray()){
            return byteBuffer.array();
        }
        Log.e(TAG, "Failed to get the byte array");
    } catch (JSONException e) {
        Log.e(TAG, "JSON Exception thrown when trying to create the JSON Data to send");
    }
    Log.e(TAG, "Failure in the createData method. Returning null");
    return null;
}

getSize method:

private static byte[] getSize(byte[] data){
    String size = "" + data.length;
    return size.getBytes();
}

I will really appreciate if someone can help me find the issue.


#12

Sorry for the bump, but @epaga would you be able to help me with this? I’m using the IFCtest sample by @nicolas which is written in JavaScript. I’ve put it on a Node JS client, which he said would work, tried to connect to IF, but it just won’t do it. Here’s a link to the code, would you mind having a look through this and seeing what I’ve done wrong?


#13

This simply uses the API of the ifc.js library which I don’t know and have never used before so it seems like @nicolas would be the one to ask. Is he using those methods right?


#14

Not sure. Would you be able to provide a working (able to run commands) out-of-the-box Node JS sample?


#15

Nope, since I’ve only ever dabbled in Node.js…perhaps @Nicolas?


#16

He’s not very active on here, but I’ll try asking him on GitHub. Is there another language that supports PC that you could provide a sample in?


#17

Best example (which was extremely helpful when I was just getting started) is Cam’s own LiveFlight Connect, which uses C# and he graciously open-sourced. https://github.com/LiveFlightApp/Connect-Windows


#18

Is there a convenient way to see the arguments/parameters required for these commands? Some are toggles, but others, like commands.flightplan.activateleg: look like they would need specific data, in this case a waypoint name (I think).