1
0
Fork 0

- save aircraft data before the aircraft's Nasal files are executed

- make 0 a valid saving interval, and 'nil' or no arg stop the loop
  (this is for consistency with settimer() or aircraft.timer() intervals
- smaller fixes, cleanup
This commit is contained in:
mfranz 2007-01-22 16:49:28 +00:00
parent f376ea5adb
commit 97771b69ae

View file

@ -380,10 +380,11 @@ settimer(func { HUD = HUDControl.new() }, 0);
# data.add(<properties>); # data.add(<properties>);
# data.save([<interval>]) # data.save([<interval>])
# #
# interval ... save all <interval> minutes, or only now if 0 or empty
# properties ... about any combination of property nodes (props.Node) # properties ... about any combination of property nodes (props.Node)
# or path name strings, or lists or hashes of them, # or path name strings, or lists or hashes of them,
# lists of lists of them, etc. # lists of lists of them, etc.
# interval ... save in <interval> minutes intervals, or only once
# if 'nil' or empty (and again at reinit/exit)
# #
# SIGNALS: # SIGNALS:
# /sim/signals/save ... set to 'true' right before saving. Can be used # /sim/signals/save ... set to 'true' right before saving. Can be used
@ -418,19 +419,18 @@ Data = {
m.loop_id = 0; m.loop_id = 0;
m.interval = 0; m.interval = 0;
settimer(func { m.load() }, 0);
setlistener("/sim/signals/exit", func { m._save_() });
setlistener("/sim/signals/reinit", func { cmdarg().getBoolValue() and m._save_() }); setlistener("/sim/signals/reinit", func { cmdarg().getBoolValue() and m._save_() });
setlistener("/sim/signals/exit", func { m._save_() });
return m; return m;
}, },
load : func { load : func {
printlog("warn", "trying to load aircraft data from ", me.path, " (OK if not found)"); printlog("warn", "trying to load aircraft data from ", me.path, " (OK if not found)");
fgcommand("load", props.Node.new({ "file": me.path })); fgcommand("load", props.Node.new({ "file": me.path }));
}, },
save : func(v = 0) { save : func(v = nil) {
me.loop_id += 1; me.loop_id += 1;
me.interval = 60 * v; me.interval = 60 * v;
v == 0 ? me._save_() : me._loop_(me.loop_id); v == nil ? me._save_() : me._loop_(me.loop_id);
}, },
_loop_ : func(id) { _loop_ : func(id) {
id == me.loop_id or return; id == me.loop_id or return;
@ -440,7 +440,7 @@ Data = {
_save_ : func { _save_ : func {
size(me.catalog) or return; size(me.catalog) or return;
me.signal.setBoolValue(1); me.signal.setBoolValue(1);
var tmp = "_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_"; var tmp = "_-_-_-_-_-_aircraft.Data_-_-_-_-_-_";
printlog("info", "saving aircraft data to ", me.path); printlog("info", "saving aircraft data to ", me.path);
props.globals.removeChildren(tmp); props.globals.removeChildren(tmp);
var root = props.globals.getNode(tmp, 1); var root = props.globals.getNode(tmp, 1);
@ -455,29 +455,32 @@ Data = {
}, },
add : func { add : func {
foreach (var a; arg) { foreach (var a; arg) {
var t = typeof(a);
if (isa(a, props.Node)) { if (isa(a, props.Node)) {
append(me.catalog, a.getPath()); append(me.catalog, a.getPath());
} elsif (typeof(a, "scalar")) { } elsif (t == "scalar") {
append(me.catalog, a); append(me.catalog, a);
} elsif (typeof(a, "vector")) { } elsif (t == "vector") {
foreach (var i; a) { foreach (var i; a) {
me.add(i); me.add(i);
} }
} elsif (typeof(a, "hash")) { } elsif (t == "hash") {
foreach (var i; keys(a)) { foreach (var i; keys(a)) {
me.add(a[i]); me.add(a[i]);
} }
} else { } else {
die("aircraft.Data.add(): invalid item of type " ~ typeof(a)); die("aircraft.data.add(): invalid item of type " ~ t);
} }
} }
}, },
}; };
var data = nil; var data = nil;
_setlistener("/sim/signals/nasal-dir-initialized", func { _setlistener("/sim/signals/nasal-dir-initialized", func {
data = Data.new(); data = Data.new();
Data.new = func { die("illegal attempt to call Data.new()") } Data.new = nil;
data.load();
}); });
@ -489,20 +492,20 @@ _setlistener("/sim/signals/nasal-dir-initialized", func {
# is done automatically by the aircraft.Data class. # is done automatically by the aircraft.Data class.
# #
# SYNOPSIS: # SYNOPSIS:
# timer.new(<property>, <resolution:double> [, <save:bool>]) # timer.new(<property> [, <resolution:double> [, <save:bool>]])
# #
# <property> ... property path or props.Node hash that holds the timer value # <property> ... property path or props.Node hash that holds the timer value
# <resolution> ... timer resolution (interval in seconds in which the timer # <resolution> ... timer update resolution -- interval in seconds in which the
# is updated (default: 1 s) # timer property is updated while running (default: 1 s)
# <save> ... bool that defines whether the timer value should be saved # <save> ... bool that defines whether the timer value should be saved
# and restored next time, as needed for Hobbs meters # and restored next time, as needed for Hobbs meters
# (default: 1) # (default: 1)
# #
# EXAMPLE: # EXAMPLES:
# var hobbs_turbine = aircraft.timer.new("/sim/time/hobbs/turbine[0]", 60); # var hobbs_turbine = aircraft.timer.new("/sim/time/hobbs/turbine[0]", 60);
# hobbs_turbine.start(); # hobbs_turbine.start();
# #
# aircraft.timer.new("/sim/time/hobbs/battery", 60).start(); # anonymous # aircraft.timer.new("/sim/time/hobbs/battery", 60).start(); # anonymous timer
# #
timer = { timer = {
new : func(prop, res = 1, save = 1) { new : func(prop, res = 1, save = 1) {
@ -518,21 +521,21 @@ timer = {
m.running = 0; m.running = 0;
if (save) { if (save) {
data.add(m.timeN); data.add(m.timeN);
m.listener = setlistener("/sim/signals/save", func { m._save_() }); m.saveL = setlistener("/sim/signals/save", func { m._save_() });
} else { } else {
m.listener = nil; m.saveL = nil;
} }
return m; return m;
}, },
del : func { del : func {
if (me.listener != nil) { if (me.saveL != nil) {
removelistener(me.listener); removelistener(me.saveL);
} }
}, },
start : func { start : func {
me.running and return; me.running and return;
me.last_systime = me.systimeN.getValue(); me.last_systime = me.systimeN.getValue();
me._loop_(me.loop_id += 1); me.interval != nil and me._loop_(me.loop_id);
me.running = 1; me.running = 1;
me; me;
}, },
@ -541,12 +544,10 @@ timer = {
me.running = 0; me.running = 0;
me.loop_id += 1; me.loop_id += 1;
me._apply_(); me._apply_();
me;
}, },
reset : func { reset : func {
me.timeN.setDoubleValue(0); me.timeN.setDoubleValue(0);
me.last_systime = me.systimeN.getValue(); me.last_systime = me.systimeN.getValue();
me;
}, },
_apply_ : func { _apply_ : func {
var sys = me.systimeN.getValue(); var sys = me.systimeN.getValue();