1
0
Fork 0

Switch live WXR layer to new API after wunderground API was taken down

This commit is contained in:
legoboyvdlp R 2020-10-22 16:39:44 +01:00 committed by James Turner
parent 85b5c817b0
commit 850b6910b0
2 changed files with 25 additions and 45 deletions

View file

@ -3,6 +3,7 @@
var name = 'WXR_live';
var parents = [SymbolLayer.Controller];
var __self__ = caller(0)[0];
SymbolLayer.Controller.add(name, __self__);
SymbolLayer.add(name, {
parents: [MultiSymbolLayer],
@ -24,14 +25,9 @@ var new = func(layer) {
};
layer.searcher._equals = func(l,r) l.equals(r);
m.addVisibilityListener();
#io.read_properties(getprop("/sim/fg-root") ~ "/Nasal/canvas/wxr_api.xml", wxr_tree);
var saved_conf = getprop("/sim/fg-home") ~ "/Export/wxr_api.xml";
if(io.stat(saved_conf) != nil)
io.read_properties(saved_conf, wxr_tree);
else
io.read_properties(getprop("/sim/fg-root") ~ "/Nasal/canvas/wxr_api.xml", wxr_tree);
return m;
};
var del = func() {
#print(name~".lcontroller.del()");
foreach (var l; me.listeners)
@ -40,9 +36,6 @@ var del = func() {
var searchCmd = func {
if(me.map.getRange() == nil) return [];
var api_key = getprop(wxr_tree~"/api-key");
if(!api_key or api_key == '' or api_key == 'YOUR API KEY')
return [];
var lat = getprop(wxr_tree~"/center/latitude-deg");
var lon = getprop(wxr_tree~"/center/longitude-deg");
@ -55,14 +48,9 @@ var searchCmd = func {
var result = geo.Coord.new();
result.set_latlon(lat, lon);
result.rangeNm = me.map.getRange();
result.key = api_key;
result.res = getprop(wxr_tree~"/resolution");
result.ltype = getprop(wxr_tree~"/layer-type");
result.smooth = getprop(wxr_tree~"/smooth");
result.fetchRad = getprop(wxr_tree~"/fetch-radius");
result.fetchRad = 184;
result.equals = func(r){
me.ltype == r.ltype and me.fetchRad == r.fetchRad and
me.lat == r.lat and me.lon == r.lon
me.fetchRad == r.fetchRad and me.lat == r.lat and me.lon == r.lon
};
return [result];

View file

@ -11,19 +11,18 @@ var getWXRImageFilePath = func(){
var home = getprop('sim/fg-home');
var aircraft = getprop('sim/aircraft');
var idx = me.layer.getCanvas()._node.getIndex();
return home ~ '/Export/' ~ aircraft ~ '-wxr-'~me.ltype~'-'~idx~'.png';
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];
};
var getWXRAPIUrl = func(lat, lon){
var res = me.res;
if(me.ltype == "radar"){
return "http://api.wunderground.com/api/"~me.key~
"/radar/image.png?centerlat="~lat~"&centerlon="~lon~
"&radius="~me.fetchRad~"&width="~res~"&height="~res~
"&smooth="~me.smooth;
} else {
return nil;
}
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";
};
var fetchWXRMap = func(size){
@ -34,12 +33,14 @@ var fetchWXRMap = func(size){
var url = me.getWXRAPIUrl(lat, lon);
var filePath = me.getWXRImageFilePath();
if(url == nil) {
print(me.name~': No URL!');
logprint(LOG_INFO,me.name,": No URL!");
return;
}
me.fetching = 1;
http.save(url, filePath)
.fail(func(){print(me.name~': Download failed!');})
.fail(func(){
logprint(LOG_INFO,me.name,":Download failed!");}
)
.done(func(){
var sz = size * 2;
var transl = -size;
@ -54,28 +55,28 @@ var fetchWXRMap = func(size){
setprop("/instrumentation/wxr/center/latitude-deg", lat);
setprop("/instrumentation/wxr/center/longitude-deg", lon);
me.fetching = 0;
print("[WXR] Live Layer Request: "~url); # Debugging
logprint(LOG_DEBUG,me.name,": Layer Request: ",url);
});
};
var init = func {
#print('WXR init');
me.fetching = 0;
me.key = me.model.key; # API Key from Wunderground API Subscription
me.res = me.model.res; # Resolution of image to fetch (default 2048)
me.ltype = me.model.ltype; # Layer Type - radar, satellite
me.smooth = me.model.smooth; # If we should fetch a smoothened image or the original
me.timeStamp = nil;
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();
me.last_request = -210;
me.update_interval = 240;
me.last_request = -600;
me.update_interval = 600;
var r_scaled = (me.fetchRad*me.viewport_radius)/me.range;
me.fetchWXRMap(r_scaled);
me.timeStamp = nil;
http.load("https://api.rainviewer.com/public/maps.json").done(func(r) me.timeStamp = processRequest(r));
};
var draw = func {
@ -83,15 +84,12 @@ var draw = func {
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*670)/me.range;
var r_scaled = (me.fetchRad*me.viewport_radius)/me.range;
var hdg = me.layer.map.getHdg();
var rot = 0 - hdg;
if(rot < 0) rot = 360 + rot;
if(rot < 0) rot = 360 + rot + -getprop("/environment/magnetic-variation-deg");
me.element.setRotation(rot*D2R);
if(update_size){
print('WXR draw range:'~ me.range);
print('Update size: '~r_scaled~'x2 = '~(r_scaled*2));
me.wxlayer.hide();
me.wxlayer.setSize(2*r_scaled, 2*r_scaled)
.setTranslation(-r_scaled, -r_scaled);
@ -99,12 +97,6 @@ var draw = func {
}
if(getprop("/sim/time/elapsed-sec") - me.last_request > me.update_interval) {
#print('Fetching WXR map...');
me.key = me.model.key; # API Key from Wunderground API Subscription
me.res = me.model.res; # Resolution of image to fetch (default 2048)
me.ltype = me.model.ltype; # Layer Type - radar, satellite
me.smooth = me.model.smooth; # If we should fetch a smoothened image or the original
me.fetchWXRMap(r_scaled);
}
};