zkv1000 / Nasal / core.nas /
Sébastien MARQUE add autopwer
a27abf6 4 years ago
1 contributor
241 lines | 10.767kb
var setListeners = func {
    var prop = '/instrumentation/nav/nav-id';
    data.listeners[prop] = setlistener(prop,
            func (n) {
                var val = n.getValue();
                foreach (var c; keys(flightdeck))
                    flightdeck[c].display.updateNAV({'nav-id': 1, val: val});
            }, 1, 2);

    prop = '/instrumentation/nav[1]/nav-id';
    data.listeners[prop] = setlistener(prop,
            func (n) {
                var val = n.getValue();
                foreach (var c; keys(flightdeck))
                    flightdeck[c].display.updateNAV({'nav-id': 2, val: val});
            }, 1, 2);

    # keep this listener as long as the code is to heavy to be modified in multiple places
    prop = afcs.getNode('selected-alt-ft').getPath();
    data.listeners[prop] = setlistener(prop,
            func (n) {
                var val = n.getValue();
                if (val != nil)
                    foreach (var c; keys(flightdeck))
                        if (flightdeck[c].role == 'PFD') {
                            if (! flightdeck[c].display.screenElements['SelectedALT'].getVisible()) {
                                flightdeck[c].display.screenElements['SelectedALT'].show();
                                flightdeck[c].display.screenElements['SelectedALT-text'].show();
                                flightdeck[c].display.screenElements['SelectedALT-symbol'].show();
                                flightdeck[c].display.screenElements['SelectedALT-bug'].show();
                                flightdeck[c].display.screenElements['SelectedALT-bg'].show();
                            }
                            flightdeck[c].display.updateSelectedALT();
                        }
            }, 0, 2);

    prop = '/gear/gear/wow';
    data.listeners[prop] = setlistener(prop,
            func foreach (var c; keys(flightdeck))
                if (flightdeck[c].role == 'PFD')
                    flightdeck[c].display.updateXPDR(),
            0, 0);

    prop = misc.getNode('alt-setting-inhg').getPath();
    data.listeners[prop] = setlistener(prop,
            func foreach (var c; keys(flightdeck))
                if (flightdeck[c].role == 'PFD')
                    flightdeck[c].display.updateBARO(), 0, 2);

    prop = '/autopilot/route-manager/signals/edited';
    data.listeners[prop] = setlistener(prop,
            func foreach (var c; keys(flightdeck))
                flightdeck[c].map.layers.route.onFlightPlanChange(),
                0, 1);

    prop = '/autopilot/route-manager/current-wp';
    data.listeners[prop] = setlistener(prop,
            func (n) {
                var wp_idx = n.getValue();
                if (wp_idx > 0) {
                    var wp_path = '/autopilot/route-manager/route/wp[' ~ wp_idx ~ ']/';
                    var wp = findNavaidsByID(getprop(wp_path ~ 'latitude-deg'),
                                             getprop(wp_path ~ 'longitude-deg'),
                                             getprop(wp_path ~ 'id'));
                    call(func {return wp[0].frequency}, [], nil, nil, var errors = []);
#                    if (!size(errors)) debuginfo ~ sprintf('; freq: %d', wp[0].frequency);
                    call(func {return wp[0].type}, [], nil, nil, var errors = []);
#                    if (!size(errors)) debuginfo ~ sprintf('; type: %s', wp[0].type);
                }
                var delay = maketimer(2, func {
                    foreach (var c; keys(flightdeck))
                        flightdeck[c].map.layers.route.onCurrentWaypointChange(
                                props.globals.getNode('/autopilot/route-manager/current-wp'));
                    });
                delay.singleShot = 1;
                delay.start();
            }, 0, 1);

    prop = '/autopilot/route-manager/active';
    data.listeners[prop] = setlistener(prop,
            func foreach (var c; keys(flightdeck))
                    flightdeck[c].map.layers.route.onCurrentWaypointChange(props.globals.getNode('/autopilot/route-manager/current-wp')),
                0, 1);

    prop = zkv.getPath() ~ '/serviceable';
    data.listeners[prop] = setlistener(prop, func (n) if (!n.getValue()) zkv1000.powerOff, 0, 0);

    # the timer isn't necessary anymore
    data.timers.listeners.stop();
    delete(data.timers, 'listeners');
}

var deviceClass = {
    new: func (name) {
        var m = { parents: [ deviceClass ] };
        m.name = name;
        m.role = substr(name, 0, 3);
        m.node = zkv.getNode(name, 1);
        m.data = {};
        m.timers = {};
        foreach (var v; ['Vx', 'Vy', 'Vr', 'Vglide'])
            m.data[v ~ '-visible'] = 1;
        foreach (var v; ['screen-object', 'screen-view', 'screen-size'])
            m.data[v] = getprop(zkv.getPath() ~ '/' ~ name ~ '/' ~ v);

        m.display  = displayClass.new(m);
        m.softkeys = softkeysClass.new(m);
        m.buttons  = buttonsClass.new(m);
        m.knobs    = knobsClass.new(m);
        m.map      = mapClass.new(m);
        m.windows  = pageClass.new(m);

        if (! contains(data.timers, 'alt-setting-inhg')) {
            data.timers['alt-setting-inhg'] = maketimer(0.1, m, func {
                    var inhg = getprop('/instrumentation/altimeter/setting-inhg');
                    if (inhg != misc.getValue('alt-setting-inhg'))
                        misc.getNode('alt-setting-inhg').setValue(inhg);
                });
            data.timers['alt-setting-inhg'].start();
        }
        if (! contains(data.timers, 'map')) {
            data.timers.map = maketimer(1, m, func {
                    foreach (var d; keys(flightdeck))
                        flightdeck[d].map.update();
                    var gspd = getprop('/velocities/groundspeed-kt');
                    if (gspd != 0)
                        var next = (me.data['range-nm']/(gspd/3600))/(me.display.display.get('view[1]')/2);
                    else
                        var next = 10;
                    if (next > 10)
                        next = 10;
                    data.timers.map.restart(next);
                });
            data.timers.map.singleShot = 1;
            data.timers.map.start();
        }
        if (getprop('/instrumentation/tcas/serviceable') != nil) {
            m.data.tcas = 0;
            if (!contains(data.timers, 'tcas')) {
                data.timers.tcas = maketimer ( 1,
                    func {
                        var tcas_dirty = [];
                        var _range = 6;
                        var _minLevel = -1;
                        foreach (var ac; props.globals.getNode("/ai/models").getChildren("multiplayer")) {
                            if (ac.getValue("valid")) {
                                var range = ac.getNode("radar/range-nm").getValue();
                                if (range != nil) {
                                    if (debug.isnan(range) == 0) {
                                        if ( (range > 0) and (range <= _range) ) {
                                            var nTcasThreat = ac.getNode("tcas/threat-level");
                                            if (nTcasThreat != nil) {
                                                var level = nTcasThreat.getValue();
                                                if (level > _minLevel) {
                                                    var lat     = ac.getNode("position/latitude-deg").getValue();
                                                    var lon     = ac.getNode("position/longitude-deg").getValue();
                                                    var aAlt    = ac.getNode("position/altitude-ft").getValue();
                                                    var vs      = ac.getNode("velocities/vertical-speed-fps").getValue();
                                                    var alt     = math.floor(((aAlt - data.alt) / 100) + 0.5);
                                                    if (debug.isnan(lat) == 0 and
                                                        debug.isnan(lon) == 0 and
                                                        debug.isnan(vs)  == 0 and
                                                        debug.isnan(alt) == 0) {
                                                            append(tcas_dirty, {
                                                                lat: lat,
                                                                lon: lon,
                                                                vs: vs,
                                                                alt: alt,
                                                                level: level,
                                                            });
                                                    } } } } } } } }
                        data.tcas = tcas_dirty;
                    }
                );
                data.timers.tcas.start();
            }
        }
        else {
            delete(m.softkeys.bindings.PFD.INSET, 'TRAFFIC');
            delete(m.softkeys.bindings.MFD.MAP,   'TRAFFIC');
        }
        m.display.showInitProgress();

        setprop(zkv.getPath() ~ '/' ~ m.name ~ '/status', 1);
        msg(m.name ~ ' switched on!');
        return m;
    },
    off: func {
        foreach (var timer; keys(me.timers)) {
            me.timers[timer].stop();
            delete(me.timers, timer);
        }
        foreach (var member; keys(me)) {
            if (contains(me[member], 'off')) me[class].off();
            delete(me, member);
        }
    },
};

var powerOff = func {
    foreach (var listener; keys(data.listeners))
        removelistener(data.listeners[listener]);

    foreach (var timer; keys(data.timers)) {
        data.timers[timer].stop();
        delete(data.timers, timer);
    }

    foreach (var name; keys(flightdeck)) {
        flightdeck[name].off();
        delete(flightdeck, name);
        setprop(zkv.getPath() ~ '/' ~ name ~ '/status', 0);
    }
}

var powerOn = func {
    if (!zkv.getValue('serviceable')) {
        msg('not yet serviceable (check power)!');
        return;
    }

    if (contains(data.listeners, 'autopower'))
        removelistener(data.listeners.autopower);

    foreach (var freq; keys(data.timers))
        data.timers[freq].start();

    autopilot = APClass.new();

    foreach (var name; keys(flightdeck))
        if (zkv.getNode(name) != nil)
#            thread.newthread(func {
                flightdeck[name] = deviceClass.new(name);
#            });

    if (! contains(data.timers, 'listeners')) {
        data.timers.listeners = maketimer(5, setListeners);
        data.timers.listeners.singleShot = 1;
        data.timers.listeners.start();
    }
}