2020-07-26 04:10:32 -07:00
|
|
|
|
2020-08-13 23:46:34 -07:00
|
|
|
import serverNotifications from '../scripts/serverNotifications';
|
|
|
|
import globalize from '../scripts/globalize';
|
2020-10-17 11:08:56 -07:00
|
|
|
import ServerConnections from '../components/ServerConnections';
|
2022-10-14 07:53:16 -07:00
|
|
|
import Events from '../utils/events.ts';
|
|
|
|
|
|
|
|
import '../elements/emby-button/emby-button';
|
2020-07-17 03:30:39 -07:00
|
|
|
|
2021-06-11 13:59:57 -07:00
|
|
|
function taskbutton(options) {
|
2020-07-17 03:30:39 -07:00
|
|
|
function pollTasks() {
|
2020-10-17 11:08:56 -07:00
|
|
|
ServerConnections.getApiClient(serverId).getScheduledTasks({
|
2020-07-17 03:30:39 -07:00
|
|
|
IsEnabled: true
|
|
|
|
}).then(updateTasks);
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateTasks(tasks) {
|
2020-07-17 03:32:23 -07:00
|
|
|
const task = tasks.filter(function (t) {
|
2020-10-11 20:29:57 -07:00
|
|
|
return t.Key == options.taskKey;
|
2020-07-17 03:30:39 -07:00
|
|
|
})[0];
|
2019-10-08 09:54:02 -07:00
|
|
|
|
2020-07-17 03:30:39 -07:00
|
|
|
if (options.panel) {
|
|
|
|
if (task) {
|
|
|
|
options.panel.classList.remove('hide');
|
2019-10-08 09:54:02 -07:00
|
|
|
} else {
|
2020-07-17 03:30:39 -07:00
|
|
|
options.panel.classList.add('hide');
|
2018-10-22 15:05:09 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-17 03:30:39 -07:00
|
|
|
if (!task) {
|
|
|
|
return;
|
2018-10-22 15:05:09 -07:00
|
|
|
}
|
|
|
|
|
2020-07-17 03:30:39 -07:00
|
|
|
if (task.State == 'Idle') {
|
|
|
|
button.removeAttribute('disabled');
|
|
|
|
} else {
|
|
|
|
button.setAttribute('disabled', 'disabled');
|
2018-10-22 15:05:09 -07:00
|
|
|
}
|
|
|
|
|
2020-07-17 03:30:39 -07:00
|
|
|
button.setAttribute('data-taskid', task.Id);
|
2020-07-17 03:32:23 -07:00
|
|
|
const progress = (task.CurrentProgressPercentage || 0).toFixed(1);
|
2020-07-17 03:30:39 -07:00
|
|
|
|
|
|
|
if (options.progressElem) {
|
|
|
|
options.progressElem.value = progress;
|
|
|
|
|
|
|
|
if (task.State == 'Running') {
|
|
|
|
options.progressElem.classList.remove('hide');
|
|
|
|
} else {
|
|
|
|
options.progressElem.classList.add('hide');
|
2019-10-07 15:03:49 -07:00
|
|
|
}
|
2018-10-22 15:05:09 -07:00
|
|
|
}
|
|
|
|
|
2020-07-17 03:30:39 -07:00
|
|
|
if (options.lastResultElem) {
|
2020-07-17 03:32:23 -07:00
|
|
|
const lastResult = task.LastExecutionResult ? task.LastExecutionResult.Status : '';
|
2019-10-08 10:14:43 -07:00
|
|
|
|
2020-07-17 03:30:39 -07:00
|
|
|
if (lastResult == 'Failed') {
|
|
|
|
options.lastResultElem.html('<span style="color:#FF0000;">(' + globalize.translate('LabelFailed') + ')</span>');
|
|
|
|
} else if (lastResult == 'Cancelled') {
|
|
|
|
options.lastResultElem.html('<span style="color:#0026FF;">(' + globalize.translate('LabelCancelled') + ')</span>');
|
|
|
|
} else if (lastResult == 'Aborted') {
|
|
|
|
options.lastResultElem.html('<span style="color:#FF0000;">' + globalize.translate('LabelAbortedByServerShutdown') + '</span>');
|
|
|
|
} else {
|
|
|
|
options.lastResultElem.html(lastResult);
|
2019-10-07 15:03:49 -07:00
|
|
|
}
|
|
|
|
}
|
2020-07-17 03:30:39 -07:00
|
|
|
}
|
2019-10-07 15:03:49 -07:00
|
|
|
|
2020-07-17 03:30:39 -07:00
|
|
|
function onScheduledTaskMessageConfirmed(id) {
|
2020-10-17 11:08:56 -07:00
|
|
|
ServerConnections.getApiClient(serverId).startScheduledTask(id).then(pollTasks);
|
2020-07-17 03:30:39 -07:00
|
|
|
}
|
2019-10-08 10:14:43 -07:00
|
|
|
|
2020-07-17 03:30:39 -07:00
|
|
|
function onButtonClick() {
|
|
|
|
onScheduledTaskMessageConfirmed(this.getAttribute('data-taskid'));
|
|
|
|
}
|
|
|
|
|
|
|
|
function onScheduledTasksUpdate(e, apiClient, info) {
|
|
|
|
if (apiClient.serverId() === serverId) {
|
|
|
|
updateTasks(info);
|
2019-10-08 10:14:43 -07:00
|
|
|
}
|
2020-07-17 03:30:39 -07:00
|
|
|
}
|
2019-10-08 10:14:43 -07:00
|
|
|
|
2020-07-17 03:32:23 -07:00
|
|
|
let pollInterval;
|
|
|
|
const button = options.button;
|
|
|
|
const serverId = ApiClient.serverId();
|
2019-10-08 10:14:43 -07:00
|
|
|
|
2020-07-17 03:30:39 -07:00
|
|
|
function onPollIntervalFired() {
|
2020-10-17 11:08:56 -07:00
|
|
|
if (!ServerConnections.getApiClient(serverId).isMessageChannelOpen()) {
|
2020-07-17 03:30:39 -07:00
|
|
|
pollTasks();
|
2019-10-08 10:14:43 -07:00
|
|
|
}
|
2020-07-17 03:30:39 -07:00
|
|
|
}
|
2019-10-07 15:03:49 -07:00
|
|
|
|
2020-07-17 03:30:39 -07:00
|
|
|
function startInterval() {
|
2020-10-17 11:08:56 -07:00
|
|
|
const apiClient = ServerConnections.getApiClient(serverId);
|
2020-07-17 03:30:39 -07:00
|
|
|
|
|
|
|
if (pollInterval) {
|
|
|
|
clearInterval(pollInterval);
|
2019-10-07 15:03:49 -07:00
|
|
|
}
|
2020-07-17 03:30:39 -07:00
|
|
|
apiClient.sendMessage('ScheduledTasksInfoStart', '1000,1000');
|
|
|
|
pollInterval = setInterval(onPollIntervalFired, 5000);
|
|
|
|
}
|
2019-10-07 15:03:49 -07:00
|
|
|
|
2020-07-17 03:30:39 -07:00
|
|
|
function stopInterval() {
|
2020-10-17 11:08:56 -07:00
|
|
|
ServerConnections.getApiClient(serverId).sendMessage('ScheduledTasksInfoStop');
|
2020-07-17 03:30:39 -07:00
|
|
|
|
|
|
|
if (pollInterval) {
|
|
|
|
clearInterval(pollInterval);
|
2019-10-07 15:03:49 -07:00
|
|
|
}
|
2020-07-17 03:30:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (options.panel) {
|
|
|
|
options.panel.classList.add('hide');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.mode == 'off') {
|
|
|
|
button.removeEventListener('click', onButtonClick);
|
2020-09-07 23:05:02 -07:00
|
|
|
Events.off(serverNotifications, 'ScheduledTasksInfo', onScheduledTasksUpdate);
|
2020-07-17 03:30:39 -07:00
|
|
|
stopInterval();
|
|
|
|
} else {
|
|
|
|
button.addEventListener('click', onButtonClick);
|
|
|
|
pollTasks();
|
|
|
|
startInterval();
|
2020-09-07 23:05:02 -07:00
|
|
|
Events.on(serverNotifications, 'ScheduledTasksInfo', onScheduledTasksUpdate);
|
2020-07-17 03:30:39 -07:00
|
|
|
}
|
|
|
|
}
|
2021-06-11 13:59:57 -07:00
|
|
|
|
|
|
|
export default taskbutton;
|