Read time/energy calc variables from profile

and ensure profile text is loaded before updating route and straight line stats
This commit is contained in:
Norbert Renner 2022-02-15 19:30:49 +01:00
parent 9abf4b94c4
commit 4d44153316
5 changed files with 57 additions and 20 deletions

View file

@ -38,11 +38,12 @@ BR.Profile = L.Evented.extend({
button.blur();
},
update: function (options) {
update: function (options, cb) {
var profileName = options.profile,
profileUrl,
empty = !this.editor.getValue(),
clean = this.editor.isClean();
clean = this.editor.isClean(),
loading = false;
if (profileName && BR.conf.profilesUrl) {
// only synchronize profile editor/parameters with selection if no manual changes in full editor,
@ -51,11 +52,13 @@ BR.Profile = L.Evented.extend({
this.profileName = profileName;
if (!(profileName in this.cache)) {
profileUrl = BR.conf.profilesUrl + profileName + '.brf';
loading = true;
BR.Util.get(
profileUrl,
L.bind(function (err, profileText) {
if (err) {
console.warn('Error getting profile from "' + profileUrl + '": ' + err);
if (cb) cb();
return;
}
@ -65,6 +68,7 @@ BR.Profile = L.Evented.extend({
if (!this.profileName || this.profileName === profileName) {
this._setValue(profileText);
}
if (cb) cb();
}, this)
);
} else {
@ -80,6 +84,8 @@ BR.Profile = L.Evented.extend({
}
}
}
if (cb && !loading) cb();
},
show: function () {

View file

@ -226,9 +226,12 @@
}
routingOptions = new BR.RoutingOptions();
routingOptions.on('update', updateRoute);
routingOptions.on('update', function (evt) {
profile.update(evt.options);
if (urlHash.movingMap) return;
profile.update(evt.options, () => {
updateRoute(evt);
});
});
BR.NogoAreas.MSG_BUTTON = i18next.t('keyboard.generic-shortcut', {
@ -290,7 +293,7 @@
routingPathQuality = new BR.RoutingPathQuality(map, layersControl);
routing = new BR.Routing({
routing = new BR.Routing(profile, {
routing: {
router: L.bind(router.getRouteSegment, router),
},
@ -399,11 +402,12 @@
// initial option settings (after controls are added and initialized with onAdd)
router.setOptions(nogos.getOptions());
router.setOptions(routingOptions.getOptions());
// (check before hash plugin init)
if (!location.hash) {
profile.update(routingOptions.getOptions());
// restore active layers from local storage when called without hash
// (check before hash plugin init)
if (!location.hash) {
layersControl.loadActiveLayers();
}
@ -427,13 +431,16 @@
router.setOptions(opts);
routingOptions.setOptions(opts);
nogos.setOptions(opts);
profile.update(opts);
const optsOrDefault = Object.assign({}, routingOptions.getOptions(), opts);
profile.update(optsOrDefault, () => {
if (opts.lonlats) {
routing.draw(false);
routing.clear();
routing.setWaypoints(opts.lonlats, opts.beelineFlags);
}
});
if (opts.pois) {
pois.setMarkers(opts.pois);
}

View file

@ -39,6 +39,12 @@ BR.Routing = L.Routing.extend({
},
},
initialize: function (profile, options) {
L.Routing.prototype.initialize.call(this, options);
this.profile = profile;
},
onAdd: function (map) {
this.options.tooltips.waypoint = i18next.t('map.route-tooltip-waypoint');
this.options.tooltips.segment = i18next.t('map.route-tooltip-segment');
@ -484,7 +490,7 @@ BR.Routing = L.Routing.extend({
},
_computeKinematic: function (distance, deltaHeight) {
const rc = new BR.RoutingContext();
const rc = new BR.RoutingContext(this.profile);
const stdPath = new BR.StdPath();
stdPath.computeKinematic(rc, distance, deltaHeight, true);

View file

@ -11,18 +11,30 @@
}
class BExpressionContext {
constructor(profile) {
this.profile = profile;
}
getVariableValue(name, defaultValue) {
return defaultValue;
let value = this.profile?.getProfileVar(name) ?? defaultValue;
if (value === 'true') {
value = 1;
} else if (value === 'false') {
value = 0;
}
return +value;
}
}
// from BRouter btools.router.RoutingContext
class RoutingContext {
constructor() {
this.expctxGlobal = new BExpressionContext();
constructor(profile) {
this.expctxGlobal = new BExpressionContext(profile);
this.expctxWay = new BExpressionContextWay();
this.bikeMode = true;
this.footMode = false;
this.bikeMode = 0 !== this.expctxGlobal.getVariableValue('validForBikes', 0);
this.footMode = 0 !== this.expctxGlobal.getVariableValue('validForFoot', 0);
this.totalMass = this.expctxGlobal.getVariableValue('totalMass', 90.0);
this.maxSpeed = this.expctxGlobal.getVariableValue('maxSpeed', this.footMode ? 6.0 : 45.0) / 3.6;
this.S_C_x = this.expctxGlobal.getVariableValue('S_C_x', 0.5 * 0.45);

View file

@ -7,7 +7,13 @@ const geoJson = require('../format/data/track.json');
test('simple track', () => {
const coordinates = geoJson.features[0].geometry.coordinates;
const properties = geoJson.features[0].properties;
const rc = new BR.RoutingContext();
const dummyProfileVars = {
getProfileVar(name) {
const vars = { validForBikes: 1 };
return vars[name];
},
};
const rc = new BR.RoutingContext(dummyProfileVars);
const stdPath = new BR.StdPath();
for (let i = 0; i < coordinates.length; i++) {