From f3ef2ca2eb887079328d53225fe4336c05d87697 Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Mon, 7 Oct 2019 10:07:49 +0200 Subject: [PATCH] luci-base: ui.js: add pingDevice() and awaitReconnect() functions Import these functions from the flash.js for use with other views that might trigger a device reboot. Signed-off-by: Jo-Philipp Wich --- .../htdocs/luci-static/resources/ui.js | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/modules/luci-base/htdocs/luci-static/resources/ui.js b/modules/luci-base/htdocs/luci-static/resources/ui.js index a4050cd3f..caae81281 100644 --- a/modules/luci-base/htdocs/luci-static/resources/ui.js +++ b/modules/luci-base/htdocs/luci-static/resources/ui.js @@ -2214,6 +2214,44 @@ return L.Class.extend({ } }), + /* Reconnect handling */ + pingDevice: function(proto, ipaddr) { + var target = '%s://%s%s?%s'.format(proto || 'http', ipaddr || window.location.host, L.resource('icons/loading.gif'), Math.random()); + + return new Promise(function(resolveFn, rejectFn) { + var img = new Image(); + + img.onload = resolveFn; + img.onerror = rejectFn; + + window.setTimeout(rejectFn, 1000); + + img.src = target; + }); + }, + + awaitReconnect: function(/* ... */) { + var ipaddrs = arguments.length ? arguments : [ window.location.host ]; + + window.setTimeout(L.bind(function() { + L.Poll.add(L.bind(function() { + var tasks = [], reachable = false; + + for (var i = 0; i < 2; i++) + for (var j = 0; j < ipaddrs.length; j++) + tasks.push(this.pingDevice(i ? 'https' : 'http', ipaddrs[j]) + .then(function(ev) { reachable = ev.target.src.replace(/^(https?:\/\/[^\/]+).*$/, '$1/') }, function() {})); + + return Promise.all(tasks).then(function() { + if (reachable) { + L.Poll.stop(); + window.location = reachable; + } + }); + }, this)); + }, this), 5000); + }, + /* UCI Changes */ changes: L.Class.singleton({ init: function() { -- 2.25.1