Menu="Device:2 New:2"
Title="Self-Test"
Tag="wrench"
Cond="strpos($disks[$name]['status'],'_NP')===false"
---
<?PHP
/* Copyright 2005-2022, Lime Technology
 * Copyright 2012-2022, Bergware International.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version 2,
 * as published by the Free Software Foundation.
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 */
?>
<?
$date = date('Ymd-Hi');
$file = $disk['id']."-$date.txt";
$zip  = str_replace(' ','_',strtolower($var['NAME']))."-smart-$date.zip";
$long = $disks[$name]['spindownDelay']==0 || ($disks[$name]['spindownDelay']==-1 && $var['spindownDelay']==0);
?>
_(Download SMART report)_:
: <input type='button' value="_(Download)_" onclick='saveSMART()'>

_(SMART self-test history)_:
: <input type="button" value="_(Show)_" id="toggle_log" onclick="selftestLog()">

:selftest_history_help:

<pre id="selftest" style="display:none"></pre>

_(SMART error log)_:
: <input type="button" value="_(Show)_" id="toggle_error" onclick="errorLog()">

:selftest_error_log_help:

<pre id="errorlog" style="display:none"></pre>

_(SMART short self-test)_:
: <input type='button' value="_(Start)_" id='short_test' onclick="startShortTest()">

:selftest_short_test_help:

_(SMART extended self-test)_:
: <input type='button' value="_(Start)_" id='long_test' onclick="startLongTest()"<?=$long ? ">" : " disabled> _(Disable **Spin down delay** to run a SMART extended self-test)_"?>

:selftest_long_test_help:

_(Last SMART test result)_:
: <span id="test_result"><i class='fa fa-circle-o-notch fa-spin fa-fw'></i></span>

:selftest_result_help:

<script>
function cleanUp() {
  if (document.hasFocus()) {
    $('input[value="_(Downloading)_..."]').val("_(Download)_").prop('disabled',false);
    $.post('/webGui/include/Download.php',{cmd:'delete',file:'<?=addslashes(htmlspecialchars($file))?>'});
    $.post('/webGui/include/Download.php',{cmd:'delete',file:'<?=addslashes(htmlspecialchars($zip))?>'});
  } else {
    setTimeout(cleanUp,4000);
  }
}
function saveSMART() {
  $('input[value="_(Download)_"]').val('_(Downloading)_...').prop('disabled',true);
  $.post('/webGui/include/SmartInfo.php',{cmd:'save',port:'<?=addslashes(htmlspecialchars($dev))?>',name:'<?=addslashes(htmlspecialchars($name))?>',file:'<?=addslashes(htmlspecialchars($file))?>'}, function() {
    $.post('/webGui/include/Download.php',{cmd:'save',source:'<?=addslashes(htmlspecialchars($file))?>',file:'<?=addslashes(htmlspecialchars($zip))?>'},function(zip) {
      location = zip;
      setTimeout(cleanUp,4000);
    });
  });
}
function testUpdate(init) {
  $.post('/webGui/include/SmartInfo.php',{cmd:'update',port:'<?=addslashes(htmlspecialchars($dev))?>',name:'<?=addslashes(htmlspecialchars($name))?>',type:'<?=addslashes(htmlspecialchars($type))?>',csrf:'<?=$var['csrf_token']?>'},function(data) {
    $('#test_result').html(data);
    if (data.indexOf('%')>=0) {
      if ($('#smart_selftest').length) {
        $('#smart_selftest').html('<i class="fa fa-camera"></i> _(SMART self-test in progress)_...');
      }
      if (!init) timers.testUpdate = setTimeout(testUpdate, 3000);
    } else {
      if ($('#smart_selftest').length) $('#smart_selftest').html('');
      $('#short_test').val("_(Start)_");
      $('#long_test').val("_(Start)_");
      if (!init) {
        $.removeCookie('test.<?=addslashes(htmlspecialchars($dev))?>');
      }
    }
  });
}
function selftestLog() {
  $('#selftest').toggle('slow');
  var text = $('#toggle_log').val()=="_(Show)_" ? "_(Hide)_" : "_(Show)_";
  $('#toggle_log').val(text);
}
function errorLog() {
  $('#errorlog').toggle('slow');
  var text = $('#toggle_error').val()=="_(Show)_" ? "_(Hide)_" : "_(Show)_";
  $('#toggle_error').val(text);
}
function startShortTest() {
  if ($('#short_test').val()=="_(Start)_") {
    $('#short_test').val("_(Stop)_");
    $('#long_test').attr('disabled','disabled');
    $.cookie('test.<?=addslashes(htmlspecialchars($dev))?>','short',{expires:365});
    $.post('/webGui/include/SmartInfo.php',{cmd:'short',port:'<?=addslashes(htmlspecialchars($dev))?>',name:'<?=addslashes(htmlspecialchars($name))?>'},function(){timers.testUpdate = setTimeout(testUpdate,0);});
  } else {
    clearTimeout(timers.testUpdate);
    $.removeCookie('test.<?=addslashes(htmlspecialchars($dev))?>');
    $.post('/webGui/include/SmartInfo.php',{cmd:'stop',port:'<?=addslashes(htmlspecialchars($dev))?>',name:'<?=addslashes(htmlspecialchars($name))?>'},function(){setTimeout(testUpdate,0);});
  }
}
function startLongTest() {
  if ($('#long_test').val()=="_(Start)_") {
    $('#long_test').val("_(Stop)_");
    $('#short_test').attr('disabled','disabled');
    $.cookie('test.<?=addslashes(htmlspecialchars($dev))?>','long',{expires:365});
    $.post('/webGui/include/SmartInfo.php',{cmd:'long',port:'<?=addslashes(htmlspecialchars($dev))?>',name:'<?=addslashes(htmlspecialchars($name))?>'},function(){timers.testUpdate = setTimeout(testUpdate,0);});
  } else {
    clearTimeout(timers.testUpdate);
    $.removeCookie('test.<?=addslashes(htmlspecialchars($dev))?>');
    $.post('/webGui/include/SmartInfo.php',{cmd:'stop',port:'<?=addslashes(htmlspecialchars($dev))?>',name:'<?=addslashes(htmlspecialchars($name))?>'},function(){setTimeout(testUpdate,0);});
  }
}
$(function() {
  var smarttest = $.cookie('test.<?=addslashes(htmlspecialchars($dev))?>');
  testUpdate(smarttest===undefined);
  if (smarttest !== undefined) $('#'+smarttest+'_test').val("_(Stop)_").removeAttr('disabled');
  $.post('/webGui/include/SmartInfo.php',{cmd:'selftest',port:'<?=addslashes(htmlspecialchars($dev))?>',name:'<?=addslashes(htmlspecialchars($name))?>'},function(data){$('#selftest').html(data);});
  $.post('/webGui/include/SmartInfo.php',{cmd:'errorlog',port:'<?=addslashes(htmlspecialchars($dev))?>',name:'<?=addslashes(htmlspecialchars($name))?>'},function(data){$('#errorlog').html(data);});
});
</script>
