Discussion best way to “decode” status.json programatically?

G’day,

Outcome: best way to process and convert the “Flags” value into variables.

I am not a professional programmer, rather a bit of a hack with a bit of an idea.

I’ve written my own TARGET script for my Warthog (inspired by Aussiedroid’s script).

I want to do some state tracking. However, this is not easy within TARGET since it isn’t any way aligned to the game...hence the question.

Now, where I’m at is that I can read the status.json into TARGET, and can isolate and convert the ‘Flags’ value into an integer. My current solution is simply a bunch (up to 28....1 for each bit) of ‘if” statements one after the other.

Eg.
If (Flags >= number) {
...assign to a discrete boolean variable such as “LightsOn” or “Supercruise” etc
Flags = Flags - number;
}
...next If statement etc down until I get to Flags = 1.

This works, obviously and is pretty quick but, I’m not happy with it as a solution...I’m sure there’s a better way.

I’m thinking of creating an array with 28 elements and using a loop instead of the 28 ‘if’ statements, then doing the assignment somehow after that, but my feable mind keeps telling me this would be just as much work.

I’m hoping some clever programmer type can let me in on some method that’s elegent simple, effective etc. that I might be able to try convert into TARGET...which by the way seems like either a variation or subset of either “C” or javascript.

Cheers
Clicker
 
Hi,

I'm not entirely sure what you want to accomplish here, but maybe using bitwise operators will help.

Code:
if (flags & 0x0400 != 0) {
	// running silently
}
 
Hi,

I'm not entirely sure what you want to accomplish here, but maybe using bitwise operators will help.

Code:
if (flags & 0x0400 != 0) {
	// running silently
}

Thanks for the reply.

What I’m actually trying to accomplish is shorter, more efficient code.
I see 28 individual, multiline ‘if’ statements as being ‘something a noob would do’ despite the fact that it works.

I think your suggestion is a bit more elegant than integer subtraction method, so I’m going to use it (thanks!).

Clicker

p.s. Just tried this method and it works beautifully, thankyou.
 
Last edited:

Robert Maynard

Volunteer Moderator
In my StatusDisplay app, I use the following to set boolean variables from the raw flags value taken from status.json:

Code:
#define ED_Docked               0x0000000000000001ULL
#define ED_Landed               0x0000000000000002ULL
#define ED_LandingGearDown      0x0000000000000004ULL
#define ED_ShieldsUp            0x0000000000000008ULL
#define ED_Supercruise          0x0000000000000010ULL
#define ED_FlightAssistOff      0x0000000000000020ULL
#define ED_HardpointsDeployed   0x0000000000000040ULL
#define ED_InWing               0x0000000000000080ULL

#define ED_LightsOn             0x0000000000000100ULL
#define ED_CargoScoopDeployed   0x0000000000000200ULL
#define ED_SilentRunning        0x0000000000000400ULL
#define ED_ScoopingFuel         0x0000000000000800ULL
#define ED_SRVHandbrake         0x0000000000001000ULL
#define ED_SRVTurret            0x0000000000002000ULL
#define ED_SRVTurretRetracted   0x0000000000004000ULL
#define ED_SRVDriveAssist       0x0000000000008000ULL

#define ED_FSDMassLocked        0x0000000000010000ULL
#define ED_FSDCharging          0x0000000000020000ULL
#define ED_FSDCooldown          0x0000000000040000ULL
#define ED_LowFuel              0x0000000000080000ULL
#define ED_OverHeating          0x0000000000100000ULL
#define ED_HasLatLong           0x0000000000200000ULL
#define ED_IsInDanger           0x0000000000400000ULL
#define ED_BeingInterdicted     0x0000000000800000ULL

#define ED_InMainShip           0x0000000001000000ULL
#define ED_InFighter            0x0000000002000000ULL
#define ED_InSRV                0x0000000004000000ULL
#define ED_HudInAnalysisMode    0x0000000008000000ULL
#define ED_NightVision          0x0000000010000000ULL

    Status.Flags.Valid = (Status.raw_flags != 0);

    Status.Flags.Docked =  Status.raw_flags & ED_Docked;
    Status.Flags.Landed = Status.raw_flags & ED_Landed;
    Status.Flags.LandingGearDown = Status.raw_flags & ED_LandingGearDown;
    Status.Flags.ShieldsUp = Status.raw_flags & ED_ShieldsUp;
    Status.Flags.Supercruise = Status.raw_flags & ED_Supercruise;
    Status.Flags.FlightAssistOff = Status.raw_flags & ED_FlightAssistOff;
    Status.Flags.HardpointsDeployed = Status.raw_flags & ED_HardpointsDeployed;
    Status.Flags.InWing = Status.raw_flags & ED_InWing;
    Status.Flags.LightsOn = Status.raw_flags & ED_LightsOn;
    Status.Flags.CargoScoopDeployed = Status.raw_flags & ED_CargoScoopDeployed;
    Status.Flags.SilentRunning = Status.raw_flags & ED_SilentRunning;
    Status.Flags.ScoopingFuel = Status.raw_flags & ED_ScoopingFuel;
    Status.Flags.SRVHandbrake = Status.raw_flags & ED_SRVHandbrake;
    Status.Flags.SRVTurret = Status.raw_flags & ED_SRVTurret;
    Status.Flags.SRVTurretRetracted = Status.raw_flags & ED_SRVTurretRetracted;
    Status.Flags.SRVDriveAssist = Status.raw_flags & ED_SRVDriveAssist;
    Status.Flags.FSDMassLocked = Status.raw_flags & ED_FSDMassLocked;
    Status.Flags.FSDCharging = Status.raw_flags & ED_FSDCharging;
    Status.Flags.FSDCooldown = Status.raw_flags & ED_FSDCooldown;
    Status.Flags.LowFuel = Status.raw_flags & ED_LowFuel;
    Status.Flags.OverHeating = Status.raw_flags & ED_OverHeating;
    Status.Flags.HasLatLong  = Status.raw_flags & ED_HasLatLong;
    Status.Flags.IsInDanger = Status.raw_flags & ED_IsInDanger;
    Status.Flags.BeingInterdicted = Status.raw_flags & ED_BeingInterdicted;
    Status.Flags.InMainShip = Status.raw_flags & ED_InMainShip;
    Status.Flags.InFighter = Status.raw_flags & ED_InFighter;
    Status.Flags.InSRV = Status.raw_flags & ED_InSRV;
    Status.Flags.AnalysisMode = Status.raw_flags & ED_HudInAnalysisMode;
    Status.Flags.NightVision = Status.raw_flags & ED_NightVision;
 
Last edited:
Thanks Robert.

What language are you using there? (C? Javascript? Other?)

Cheers
Clicker

EDIT: nevermind, I’ll download and take a look at your app!
 
Last edited:
Here's my Python code to convert the Flags field of status.json into class members:
Code:
import ctypes
import json
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler
c_uint8 = ctypes.c_uint8
c_uint32 = ctypes.c_uint32

# How to define a bit-field structure in Python:
# https://wiki.python.org/moin/BitManipulation
class StatusFlagsBits(ctypes.LittleEndianStructure):
# Definitions of bits:
# http://hosting.zaonce.net/community/journal/v16/Journal_Manual_v16.pdf
    _fields_ = [("docked",          c_uint8, 1),
        ("landed",          c_uint8, 1),
        ("landing_gear",    c_uint8, 1),
        ("shields_up",      c_uint8, 1),
        ("supercruise",     c_uint8, 1),
        ("fa_off",          c_uint8, 1),
        ("hardpoints",      c_uint8, 1),
        ("wing",            c_uint8, 1),
        ("lights",          c_uint8, 1),
        ("cargo_scoop",     c_uint8, 1),
        ("silent_run",      c_uint8, 1),
        ("fuel_scoop",      c_uint8, 1),
        ("srv_brake",       c_uint8, 1),
        ("srv_turret",      c_uint8, 1),
        ("srv_board",       c_uint8, 1),
        ("srv_da",          c_uint8, 1),
        ("fsd_masslock",    c_uint8, 1),
        ("fsd_charge",      c_uint8, 1),
        ("fsd_cool",        c_uint8, 1),
        ("low_fuel",        c_uint8, 1),
        ("overheat",        c_uint8, 1),
        ("has_lat_long",    c_uint8, 1),
        ("in_danger",       c_uint8, 1),
        ("interdiction",    c_uint8, 1),
        ("in_ship",         c_uint8, 1),
        ("in_fighter",      c_uint8, 1),
        ("in_srv",          c_uint8, 1)]

class StatusFlags(ctypes.Union):
    _anonymous_ = ("bits",)
    _fields_ = [("bits",        StatusFlagsBits),
        ("as_integer",  c_uint32)]

class MyEventHandler(PatternMatchingEventHandler):
    def on_modified(self, event):
        try:
            with open(event.src_path, 'r') as statusfile:
                status_json = statusfile.read()
                # The status file is cleared before writing new data, causing
                # two modify events. Check that the file isn't empty before
                # trying to parse it as JSON
                if status_json: 
                    status_dict = json.loads(status_json)
                    status_flags = StatusFlags()
                    status_flags.as_integer = status_dict['Flags']

                    # Set LEDs based on status flag values
                    danger_led.value = status_flags.in_danger
                    docked_led.value = status_flags.docked or status_flags.landed
                    fsd_led.value = status_flags.supercruise
                    scoop_led.value = status_flags.fuel_scoop
                    interdict_led.value = status_flags.interdiction
        except Exception as err:
            print(err)
Not a complete program, and this code hasn't been updated to match the current set of flags.
 
Thanks for the replies.

I’ve gone with the defines from Robert Maynard (thankyou) and am using the 28x ‘if’ statements using the bitwise test from Lombra (thankyou). Works a treat and is reasonably quick.

In case anyone is interested, I’ll post the TARGET script function here when I’m home on the PC.

Cheers
Clicker
 
Back
Top Bottom