v0.8.20
This commit is contained in:
+22
-2
@@ -34,11 +34,31 @@ function showHistoryTab(tab, {push=true, load=true}={}) {
|
||||
if (load) loadHistory();
|
||||
}
|
||||
|
||||
async function sendDeviceCommand(id, command) {
|
||||
async function sendDeviceCommand(id, commandOrFactory) {
|
||||
const previous = app.deviceControlQueue[id] || Promise.resolve();
|
||||
const request = previous.catch(() => {}).then(() => {
|
||||
const current = app.devices.find(device => device.id === id);
|
||||
const command = typeof commandOrFactory === 'function' ? commandOrFactory(current) : commandOrFactory;
|
||||
return api(`/api/devices/${encodeURIComponent(id)}/command`, {method:'POST', body:command});
|
||||
});
|
||||
app.deviceControlQueue[id] = request;
|
||||
try {
|
||||
const device = await api(`/api/devices/${encodeURIComponent(id)}/command`, {method:'POST', body:command});
|
||||
const device = await request;
|
||||
updateDevice(device); renderAll();
|
||||
} catch (error) { toast(error.message, true); }
|
||||
finally {
|
||||
if (app.deviceControlQueue[id] === request) delete app.deviceControlQueue[id];
|
||||
}
|
||||
}
|
||||
|
||||
function enqueueClimateControlTask(task) {
|
||||
const previous = app.climateControlQueue || Promise.resolve();
|
||||
const request = previous.catch(() => {}).then(task);
|
||||
app.climateControlQueue = request;
|
||||
request.finally(() => {
|
||||
if (app.climateControlQueue === request) app.climateControlQueue = null;
|
||||
}).catch(() => {});
|
||||
return request;
|
||||
}
|
||||
|
||||
function updateDevice(device) {
|
||||
|
||||
Reference in New Issue
Block a user