Skip to content

Commit

Permalink
move from callback-based to promise-based requests on client pages
Browse files Browse the repository at this point in the history
  • Loading branch information
rand256 committed Jul 20, 2019
1 parent 59ade83 commit 95375b2
Show file tree
Hide file tree
Showing 17 changed files with 689 additions and 889 deletions.
13 changes: 6 additions & 7 deletions client/forbidden-markers-configuration-map.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,14 @@
const persistentData = {virtual_walls: map.getLocations().virtualWalls, no_go_areas: map.getLocations().forbiddenZones};
loadingBarSaveMarkers.setAttribute("indeterminate", "indeterminate");
saveButton.setAttribute("disabled", "disabled");
fn.requestWithPayload("api/persistent_data", JSON.stringify(persistentData), "PUT", function (err) {
fn.prequestWithPayload("api/persistent_data", JSON.stringify(persistentData), "PUT")
.then(() => {
ons.notification.toast("Successfully saved forbidden markers!", { buttonLabel: 'Dismiss', timeout: window.fn.toastOKTimeout });
fn.popPage();
}, (err) => ons.notification.toast(err, { buttonLabel: 'Dismiss', timeout: window.fn.toastErrorTimeout }))
.finally(() => {
loadingBarSaveMarkers.removeAttribute("indeterminate");
saveButton.removeAttribute("disabled");
if (err) {
ons.notification.toast(err, { buttonLabel: 'Dismiss', timeout: window.fn.toastErrorTimeout })
} else {
ons.notification.toast("Successfully saved forbidden markers!", { buttonLabel: 'Dismiss', timeout: window.fn.toastOKTimeout });
fn.popPage();
}
});
}
</script>
Expand Down
231 changes: 118 additions & 113 deletions client/home.html

Large diffs are not rendered by default.

65 changes: 29 additions & 36 deletions client/manualcontrol.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,21 @@
//API / Manual Control Handling
function manualMoveRobot(angle, velocity) {
manualControlLoadingBar.setAttribute("indeterminate", "indeterminate");
fn.requestWithPayload("api/set_manual_control", JSON.stringify({
fn.prequestWithPayload("api/set_manual_control", JSON.stringify({
angle: angle,
velocity: velocity,
//move for twice the interval we're updating at
//to keep on track if one package got lost
duration: manualControlDurationMS*2,
sequenceId: manualControlSequenceId++
}), "PUT", function (err) {
if (err) {
ons.notification.toast(err, { buttonLabel: 'Dismiss', timeout: window.fn.toastErrorTimeout })
}
}), "PUT")
.then(
_ => {},
err => ons.notification.toast(err, { buttonLabel: 'Dismiss', timeout: window.fn.toastErrorTimeout })
)
.finally(() => {
manualControlLoadingBar.removeAttribute("indeterminate");
})
});
}

function _startManualControl() {
Expand Down Expand Up @@ -84,30 +86,24 @@
function startManualControl() {
if (!manualControlEnabled) {
manualControlLoadingBar.setAttribute("indeterminate", "indeterminate");
fn.request("api/start_manual_control", "PUT", function (err) {
if (err) {
ons.notification.toast(err, { buttonLabel: 'Dismiss', timeout: window.fn.toastErrorTimeout })
} else {
_startManualControl();
postponeRefreshManualControlMode();
}
manualControlLoadingBar.removeAttribute("indeterminate");
})
fn.prequest("api/start_manual_control", "PUT")
.then(() => {
_startManualControl();
postponeRefreshManualControlMode();
}, (err) => ons.notification.toast(err, { buttonLabel: 'Dismiss', timeout: window.fn.toastErrorTimeout }))
.finally(() => manualControlLoadingBar.removeAttribute("indeterminate"));
}
}

function stopManualControl() {
if (manualControlEnabled) {
manualControlLoadingBar.setAttribute("indeterminate", "indeterminate");
fn.request("api/stop_manual_control", "PUT", function (err) {
if (err) {
ons.notification.toast(err, { buttonLabel: 'Dismiss', timeout: window.fn.toastErrorTimeout })
} else {
_stopManualControl();
postponeRefreshManualControlMode();
}
manualControlLoadingBar.removeAttribute("indeterminate");
});
fn.prequest("api/stop_manual_control", "PUT")
.then(() => {
_stopManualControl();
postponeRefreshManualControlMode();
}, (err) => ons.notification.toast(err, { buttonLabel: 'Dismiss', timeout: window.fn.toastErrorTimeout }))
.finally(() => manualControlLoadingBar.removeAttribute("indeterminate"));
}
}

Expand Down Expand Up @@ -270,23 +266,20 @@
}
}
}
}
};

//Page Handling (refresh/update/onload/onhide)
function refreshManualControlMode() {
fn.request("api/current_status", "GET", function (err, res) {
if (err) {
ons.notification.toast(err, { buttonLabel: 'Dismiss', timeout: window.fn.toastErrorTimeout })
fn.prequest("api/current_status", "GET")
.then((res) => {
if (res.state === 7) { //7: manual
_startManualControl();
} else {
if (res.state === 7) { //7: manual
_startManualControl();
} else {
_stopManualControl();
}
_stopManualControl();
}
manualControlLoadingBar.removeAttribute("indeterminate");
});
}
}, (err) => ons.notification.toast(err, { buttonLabel: 'Dismiss', timeout: window.fn.toastErrorTimeout }))
.finally(() => manualControlLoadingBar.removeAttribute("indeterminate"));
};

ons.getScriptPage().onShow = function () {
manualControlLoadingBar.setAttribute("indeterminate", "indeterminate");
Expand Down
66 changes: 32 additions & 34 deletions client/map.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,29 +37,30 @@
resolve(map);
}
loadingBar.setAttribute("indeterminate", "indeterminate");
fn.request("api/map/latest", "GET", function (err, mapData) {
loadingBar.removeAttribute("indeterminate");
if (!err) {
if(map === null) {
map = new VacuumMap(document.getElementById('map-canvas'));
map.initCanvas(mapData);
} else {
map.updateMap(mapData);
}
map.initWebSocket();
resolve(map);
fn.prequest("api/map/latest", "GET")
.then(mapData => {
if(map === null) {
map = new VacuumMap(document.getElementById('map-canvas'));
map.initCanvas(mapData);
} else {
ons.notification.toast(err, { buttonLabel: 'Dismiss', timeout: window.fn.toastErrorTimeout })
reject();
map.updateMap(mapData);
}
map.initWebSocket();
resolve(map);
}, err => {
ons.notification.toast(err, { buttonLabel: 'Dismiss', timeout: window.fn.toastErrorTimeout })
reject();
})
.finally(() => {
loadingBar.removeAttribute("indeterminate");
});
});
};

// Register update function to be accessible outside of es6 module (see <script> below)
window.fn.updateMapPage = updateMapPage;
window.fn.cancelUpdateMap = () => {
if(map !== null) {
if (map !== null) {
map.closeWebSocket();
}
};
Expand All @@ -71,16 +72,16 @@
let button = document.getElementById("goto");
loadingBar.setAttribute("indeterminate", "indeterminate");
button.setAttribute("disabled", "disabled");
fn.requestWithPayload("api/go_to", JSON.stringify(point), "PUT", function (err) {
fn.prequestWithPayload("api/go_to", JSON.stringify(point), "PUT")
.then(
(res) => ons.notification.toast("Command successfully sent!", { buttonLabel: 'Dismiss', timeout: window.fn.toastOKTimeout }),
(err) => ons.notification.toast(err, { buttonLabel: 'Dismiss', timeout: window.fn.toastErrorTimeout })
)
.finally(() => {
loadingBar.removeAttribute("indeterminate");
button.removeAttribute("disabled");
if (err) {
ons.notification.toast(err, { buttonLabel: 'Dismiss', timeout: window.fn.toastErrorTimeout })
} else {
ons.notification.toast("Command successfully sent!", { buttonLabel: 'Dismiss', timeout: window.fn.toastOKTimeout });
}
});
}
};

/**
* Calls the zoned_cleanup api route with the currently set zone
Expand All @@ -89,16 +90,16 @@
let button = document.getElementById("start_zoned_cleanup");
loadingBar.setAttribute("indeterminate", "indeterminate");
button.setAttribute("disabled", "disabled");
fn.requestWithPayload("api/start_cleaning_zone", JSON.stringify(zones), "PUT", function (err) {
fn.prequestWithPayload("api/start_cleaning_zone", JSON.stringify(zones), "PUT")
.then(
(res) => ons.notification.toast("Command successfully sent!", { buttonLabel: 'Dismiss', timeout: window.fn.toastOKTimeout }),
(err) => ons.notification.toast(err, { buttonLabel: 'Dismiss', timeout: window.fn.toastErrorTimeout })
)
.finally(() => {
loadingBar.removeAttribute("indeterminate");
button.removeAttribute("disabled");
if (err) {
ons.notification.toast(err, { buttonLabel: 'Dismiss', timeout: window.fn.toastErrorTimeout })
} else {
ons.notification.toast("Command successfully sent!", { buttonLabel: 'Dismiss', timeout: window.fn.toastOKTimeout });
}
});
}
};

document.getElementById("goto").onclick = () => {
const gotoPoint = map.getLocations().gotoPoints[0];
Expand All @@ -121,12 +122,9 @@
}
document.getElementById("map_reload_button").onclick = () => {
loadingBar.setAttribute("indeterminate", "indeterminate");
fn.request("api/poll_map", "GET", function (err, data) {
setTimeout(() => {
loadingBar.removeAttribute("indeterminate");
updateMapPage();
}, 300);
});
fn.prequest("api/poll_map", "GET")
.then(null, (err) => ons.notification.toast(err, { buttonLabel: 'Dismiss', timeout: window.fn.toastErrorTimeout }))
.finally (() => loadingBar.removeAttribute("indeterminate"));
}
document.getElementById("map-canvas").addEventListener('zoneSelection', (e) => {
document.getElementById("promote_zone").style.display = e.detail.state ? "" : "none";
Expand Down
79 changes: 34 additions & 45 deletions client/settings-access-control.html
Original file line number Diff line number Diff line change
Expand Up @@ -115,65 +115,54 @@
};
function updateSettingsAccessControlPage() {
loadingBarSettingsAccessControl.setAttribute("indeterminate", "indeterminate");
fn.request("api/http_auth_config", "GET", function (err, res) {
if (!err) {
httpAuthInputEnabled.checked = res.enabled;
httpAuthInputUsername.value = res.username;
httpAuthInputPassword.value = "";
fn.request("api/get_ssh_keys", "GET", function (err, res) {
if (!err) {
sshKeysTextArea.value = res;

sshKeysTitle.style.display = "block";
sshKeysList.style.display = "block";
}
loadingBarSettingsAccessControl.removeAttribute("indeterminate");
});
} else {
ons.notification.toast(err, { buttonLabel: 'Dismiss', timeout: window.fn.toastErrorTimeout })
}
});
}
const getAuthCfg = fn.prequest("api/http_auth_config", "GET")
.then((res) => {
httpAuthInputEnabled.checked = res.enabled;
httpAuthInputUsername.value = res.username;
httpAuthInputPassword.value = "";
},(err) => ons.notification.toast(err, { buttonLabel: 'Dismiss', timeout: window.fn.toastErrorTimeout }));

const getSshCfg = fn.prequest("api/get_ssh_keys", "GET")
.then((res) => {
sshKeysTextArea.value = res;
sshKeysTitle.style.display = "block";
sshKeysList.style.display = "block";
},(err) => ons.notification.toast(err, { buttonLabel: 'Dismiss', timeout: window.fn.toastErrorTimeout }));

Promise.all([getAuthCfg, getSshCfg]).finally(() => loadingBarSettingsAccessControl.removeAttribute("indeterminate"));
};
function handleSSHKeysSettingsSaveButton() {
loadingBarSettingsAccessControl.setAttribute("indeterminate", "indeterminate");
fn.requestWithPayload("api/set_ssh_keys", JSON.stringify({
fn.prequestWithPayload("api/set_ssh_keys", JSON.stringify({
keys: sshKeysTextArea.value
}), "PUT", function (err) {
if (err) {
ons.notification.toast(err, { buttonLabel: 'Dismiss', timeout: window.fn.toastErrorTimeout })
}
loadingBarSettingsAccessControl.removeAttribute("indeterminate");
});
}
}), "PUT")
.then(null, (err) => ons.notification.toast(err, { buttonLabel: 'Dismiss', timeout: window.fn.toastErrorTimeout }))
.finally(() => loadingBarSettingsAccessControl.removeAttribute("indeterminate"));
};
function handleSSHKeysSettingsPermanentlyDisableButton() {
loadingBarSettingsAccessControl.setAttribute("indeterminate", "indeterminate");
fn.requestWithPayload("api/ssh_keys_permanently_disable", JSON.stringify({
fn.prequestWithPayload("api/ssh_keys_permanently_disable", JSON.stringify({
confirmation: sshKeysInputDisableConfirmation.value
}), "PUT", function (err) {
if (err) {
ons.notification.toast(err, { buttonLabel: 'Dismiss', timeout: window.fn.toastErrorTimeout })
} else {
sshKeysTitle.style.display = "none";
sshKeysList.style.display = "none";
}
loadingBarSettingsAccessControl.removeAttribute("indeterminate");
});
}
}), "PUT")
.then((res) => {
sshKeysTitle.style.display = "none";
sshKeysList.style.display = "none";
}, (err) => ons.notification.toast(err, { buttonLabel: 'Dismiss', timeout: window.fn.toastErrorTimeout }))
.finally(() => loadingBarSettingsAccessControl.removeAttribute("indeterminate"));
};
function handleHttpAuthSettingsSaveButton() {
loadingBarSettingsAccessControl.setAttribute("indeterminate", "indeterminate");
if (httpAuthInputPassword.value !== httpAuthInputPasswordConfirm.value) {
return ons.notification.toast("Passwords don't match", { buttonLabel: 'Dismiss', timeout: window.fn.toastErrorTimeout });
}
fn.requestWithPayload("api/http_auth_config", JSON.stringify({
fn.prequestWithPayload("api/http_auth_config", JSON.stringify({
enabled: httpAuthInputEnabled.checked === true,
username: httpAuthInputUsername.value,
password: httpAuthInputPassword.value,
}), "PUT", function (err) {
if (err) {
ons.notification.toast(err, { buttonLabel: 'Dismiss', timeout: window.fn.toastErrorTimeout })
}
loadingBarSettingsAccessControl.removeAttribute("indeterminate");
});
}
}), "PUT")
.then(null, (err) => ons.notification.toast(err, { buttonLabel: 'Dismiss', timeout: window.fn.toastErrorTimeout }))
.finally(() => loadingBarSettingsAccessControl.removeAttribute("indeterminate"));
};
</script>
</ons-page>
Loading

0 comments on commit 95375b2

Please sign in to comment.