2019-10-14 16:48:35 +00:00
|
|
|
# See: http://wiki.flightgear.org/MapStructure
|
|
|
|
# Class things:
|
|
|
|
var name = 'WXR_live';
|
|
|
|
var parents = [DotSym];
|
|
|
|
var __self__ = caller(0)[0];
|
|
|
|
DotSym.makeinstance( name, __self__ );
|
|
|
|
|
|
|
|
var element_type = "group";
|
|
|
|
|
|
|
|
var getWXRImageFilePath = func(){
|
|
|
|
var home = getprop('sim/fg-home');
|
|
|
|
var aircraft = getprop('sim/aircraft');
|
|
|
|
var idx = me.layer.getCanvas()._node.getIndex();
|
2020-10-22 15:25:45 +00:00
|
|
|
return home ~ '/Export/' ~ aircraft ~ '-wxr-'~idx~'.png';
|
|
|
|
};
|
|
|
|
|
|
|
|
var processRequest = func(r) {
|
|
|
|
var splitVector = split(",", r.response);
|
|
|
|
var timeStamp = split("]",splitVector[size(splitVector) - 1]);
|
|
|
|
return timeStamp[0];
|
2019-10-14 16:48:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
var getWXRAPIUrl = func(lat, lon){
|
2020-10-22 15:25:45 +00:00
|
|
|
if (me.timeStamp == nil) { return nil; }
|
|
|
|
return "https://tilecache.rainviewer.com/v2/radar/" ~ sprintf("%s",me.timeStamp) ~ "/512/5/" ~ lat ~ "/" ~ lon ~ "/7/0_0.png";
|
2019-10-14 16:48:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
var fetchWXRMap = func(size){
|
|
|
|
if(me.fetching) return;
|
|
|
|
var pos = geo.aircraft_position();
|
|
|
|
var lat = pos.lat();
|
|
|
|
var lon = pos.lon();
|
|
|
|
var url = me.getWXRAPIUrl(lat, lon);
|
|
|
|
var filePath = me.getWXRImageFilePath();
|
|
|
|
if(url == nil) {
|
2020-10-22 15:25:45 +00:00
|
|
|
logprint(3,me.name,": No URL!");
|
2019-10-14 16:48:35 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
me.fetching = 1;
|
|
|
|
http.save(url, filePath)
|
|
|
|
.fail(func(){
|
2020-10-22 15:25:45 +00:00
|
|
|
logprint(3,me.name,":Download failed!");}
|
|
|
|
)
|
2019-10-14 16:48:35 +00:00
|
|
|
.done(func(){
|
|
|
|
var sz = size * 2;
|
|
|
|
var transl = -size;
|
|
|
|
me.wxlayer.hide();
|
|
|
|
me.wxlayer.setFile(filePath)
|
|
|
|
.setSize(sz, sz)
|
|
|
|
.setTranslation(transl, transl);
|
|
|
|
me.wxlayer.show();
|
2020-02-07 16:10:54 +00:00
|
|
|
me.last_request = getprop("sim/time/elapsed-sec");
|
2019-10-14 16:48:35 +00:00
|
|
|
})
|
|
|
|
.always(func(){
|
2020-02-07 16:10:54 +00:00
|
|
|
setprop("instrumentation/wxr/center/latitude-deg", lat);
|
|
|
|
setprop("instrumentation/wxr/center/longitude-deg", lon);
|
2019-10-14 16:48:35 +00:00
|
|
|
me.fetching = 0;
|
2020-10-22 15:25:45 +00:00
|
|
|
logprint(4,me.name,": Layer Request: ",url);
|
2019-10-14 16:48:35 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
var init = func {
|
|
|
|
#print('WXR init');
|
|
|
|
me.fetching = 0;
|
2020-10-22 15:25:45 +00:00
|
|
|
me.timeStamp = nil;
|
2019-10-14 16:48:35 +00:00
|
|
|
me.fetchRad = me.model.fetchRad; # Radius of radar layer to fetch
|
|
|
|
me.range = me.model.rangeNm; # Range of Navigation Display
|
|
|
|
me.viewport_radius = me.getOption('viewport_radius', 670);
|
|
|
|
|
|
|
|
me.wxlayer = me.element.createChild("image").set("z-index", -100).hide();
|
|
|
|
|
2020-10-22 15:25:45 +00:00
|
|
|
me.last_request = -600;
|
|
|
|
me.update_interval = 600;
|
2019-10-14 16:48:35 +00:00
|
|
|
|
|
|
|
var r_scaled = (me.fetchRad*me.viewport_radius)/me.range;
|
|
|
|
me.fetchWXRMap(r_scaled);
|
2020-10-22 15:25:45 +00:00
|
|
|
|
|
|
|
me.timeStamp = nil;
|
|
|
|
http.load("https://api.rainviewer.com/public/maps.json").done(func(r) me.timeStamp = processRequest(r));
|
2019-10-14 16:48:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
var draw = func {
|
|
|
|
var range = me.layer.map.getRange(); # Range of Navigation Display
|
|
|
|
var update_size = (range != me.range);
|
|
|
|
me.range = range;
|
|
|
|
me.fetchRad = me.model.fetchRad; # Radius of radar layer to fetch
|
|
|
|
var r_scaled = (me.fetchRad*me.viewport_radius)/me.range;
|
|
|
|
var hdg = me.layer.map.getHdg();
|
|
|
|
var rot = 0 - hdg;
|
2020-10-22 15:25:45 +00:00
|
|
|
if(rot < 0) rot = 360 + rot + -getprop("/environment/magnetic-variation-deg");
|
2019-10-14 16:48:35 +00:00
|
|
|
me.element.setRotation(rot*D2R);
|
|
|
|
if(update_size){
|
|
|
|
me.wxlayer.hide();
|
|
|
|
me.wxlayer.setSize(2*r_scaled, 2*r_scaled)
|
|
|
|
.setTranslation(-r_scaled, -r_scaled);
|
|
|
|
me.wxlayer.show();
|
|
|
|
}
|
|
|
|
|
2020-02-07 16:10:54 +00:00
|
|
|
if(getprop("sim/time/elapsed-sec") - me.last_request > me.update_interval) {
|
2019-10-14 16:48:35 +00:00
|
|
|
me.fetchWXRMap(r_scaled);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|