(function ($, document, window) {
function resetTuner(page, id) {
var message = Globalize.translate('MessageConfirmResetTuner');
Dashboard.confirm(message, Globalize.translate('HeaderResetTuner'), function (confirmResult) {
if (confirmResult) {
Dashboard.showLoadingMsg();
ApiClient.resetLiveTvTuner(id).done(function () {
Dashboard.hideLoadingMsg();
reload(page);
});
}
});
}
function renderTuners(page, tuners) {
var html = '';
for (var i = 0, length = tuners.length; i < length; i++) {
var tuner = tuners[i];
html += '
';
html += '';
html += tuner.Name;
html += ' ';
html += '';
html += tuner.SourceType;
html += ' ';
html += '';
if (tuner.Status == 'RecordingTv') {
if (tuner.ChannelName) {
html += '';
html += Globalize.translate('StatusRecordingProgram').replace('{0}', tuner.ChannelName);
html += ' ';
} else {
html += Globalize.translate('StatusRecording');
}
}
else if (tuner.Status == 'LiveTv') {
if (tuner.ChannelName) {
html += '';
html += Globalize.translate('StatusWatchingProgram').replace('{0}', tuner.ChannelName);
html += ' ';
} else {
html += Globalize.translate('StatusWatching');
}
}
else {
html += tuner.Status;
}
html += ' ';
html += '';
if (tuner.ProgramName) {
html += tuner.ProgramName;
}
html += ' ';
html += '';
html += tuner.Clients.join(' ');
html += ' ';
html += '';
html += '' + Globalize.translate('ButtonResetTuner') + ' ';
html += ' ';
html += ' ';
}
var elem = $('.tunersResultBody', page).html(html).parents('.tblTuners').table("refresh").trigger('create');
$('.btnResetTuner', elem).on('click', function () {
var id = this.getAttribute('data-tunerid');
resetTuner(page, id);
});
}
function loadPage(page, liveTvInfo) {
if (liveTvInfo.IsEnabled) {
$('.liveTvStatusContent', page).show();
$('.noLiveTvServices', page).hide();
} else {
$('.liveTvStatusContent', page).hide();
$('.noLiveTvServices', page).show();
}
var service = liveTvInfo.Services.filter(function (s) {
return s.Name == liveTvInfo.ActiveServiceName;
})[0] || {};
var serviceUrl = service.HomePageUrl || '#';
$('#activeServiceName', page).html('' + liveTvInfo.ActiveServiceName + ' ').trigger('create');
var versionHtml = service.Version || 'Unknown';
if (service.HasUpdateAvailable) {
versionHtml += ' ' + Globalize.translate('LiveTvUpdateAvailable') + ' ';
}
else {
versionHtml += ' ' + Globalize.translate('LabelVersionUpToDate');
}
$('#activeServiceVersion', page).html(versionHtml).trigger('create');
var status = liveTvInfo.Status;
if (liveTvInfo.Status == 'Ok') {
status = '' + status + ' ';
} else {
if (liveTvInfo.StatusMessage) {
status += ' (' + liveTvInfo.StatusMessage + ')';
}
status = '' + status + ' ';
}
$('#activeServiceStatus', page).html(status);
renderTuners(page, service.Tuners || []);
Dashboard.hideLoadingMsg();
}
function reload(page) {
Dashboard.showLoadingMsg();
ApiClient.getLiveTvInfo().done(function (liveTvInfo) {
loadPage(page, liveTvInfo);
});
}
function pollTasks(page) {
ApiClient.getScheduledTasks().done(function (tasks) {
updateTasks(page, tasks);
});
}
function updateTasks(page, tasks) {
$('.refreshGuidePanel', page).removeClass('hide');
var task = tasks.filter(function (t) {
return t.Name == 'Refresh Guide';
})[0];
$('.btnRefreshGuide', page).buttonEnabled(task.State == 'Idle').attr('data-taskid', task.Id);
var progress = (task.CurrentProgressPercentage || 0).toFixed(1);
var progressElem = $('.refreshGuideProgress', page).val(progress);
if (task.State == 'Running') {
progressElem.show();
} else {
progressElem.hide();
}
var lastResult = task.LastExecutionResult ? task.LastExecutionResult.Status : '';
if (lastResult == "Failed") {
$('.lastRefreshGuideResult', page).html('' + Globalize.translate('LabelFailed') + ' ');
}
else if (lastResult == "Cancelled") {
$('.lastRefreshGuideResult', page).html('' + Globalize.translate('LabelCancelled') + ' ');
}
else if (lastResult == "Aborted") {
$('.lastRefreshGuideResult', page).html('' + Globalize.translate('LabelAbortedByServerShutdown') + ' ');
} else {
$('.lastRefreshGuideResult', page).html(lastResult);
}
}
function onWebSocketMessage(e, msg) {
if (msg.MessageType == "ScheduledTasksInfo") {
var tasks = msg.Data;
var page = $.mobile.activePage;
updateTasks(page, tasks);
}
}
$(document).on('pageinit', "#liveTvStatusPage", function () {
var page = this;
$('.btnRefreshGuide', page).on('click', function () {
var button = this;
var id = button.getAttribute('data-taskid');
ApiClient.startScheduledTask(id).done(function () {
pollTasks(page);
});
});
}).on('pageshow', "#liveTvStatusPage", function () {
var page = this;
$('.refreshGuidePanel', page).addClass('hide');
reload(page);
pollTasks(page);
if (ApiClient.isWebSocketOpen()) {
ApiClient.sendWebSocketMessage("ScheduledTasksInfoStart", "1000,1000");
}
$(ApiClient).on("websocketmessage", onWebSocketMessage).on('websocketopen', function () {
if (ApiClient.isWebSocketOpen()) {
ApiClient.sendWebSocketMessage("ScheduledTasksInfoStart", "1000,1000");
}
});
}).on('pagehide', "#liveTvStatusPage", function () {
var page = this;
if (ApiClient.isWebSocketOpen()) {
ApiClient.sendWebSocketMessage("ScheduledTasksInfoStop");
}
$(ApiClient).off("websocketmessage", onWebSocketMessage);
});
})(jQuery, document, window);