hd_idle.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. 'use strict';
  2. 'require form';
  3. 'require fs';
  4. 'require view';
  5. 'require uci';
  6. function disk(devs, options, section_id) {
  7. var v = uci.get('hd-idle', section_id, 'disk') || '';
  8. var disk = devs.find(function(itm){ return itm.name == v; });
  9. var out = '';
  10. if(disk != undefined){
  11. out = options.map(function(opt){ return disk[opt].trim(); });
  12. out = out.filter(function(o){ return o != ''; });
  13. out = out.join(' ');
  14. }
  15. return E('span', out);
  16. }
  17. function prettytime(section_id) {
  18. return E('span', (uci.get('hd-idle', section_id, 'idle_time_interval') || '')
  19. + ' '
  20. + (uci.get('hd-idle', section_id, 'idle_time_unit') || ''));
  21. }
  22. return view.extend({
  23. load: function() {
  24. return fs.exec("/usr/bin/lsblk", ["-n", "-J", "-do", "NAME,TRAN,ROTA,RM,VENDOR,MODEL"]).then(function(res) {
  25. if( res.code )
  26. return [];
  27. var json = JSON.parse(res.stdout);
  28. return ( 'blockdevices' in json ) ? json['blockdevices'] : [];
  29. });
  30. },
  31. render: function(devs) {
  32. var m, s, o;
  33. m = new form.Map('hd-idle', _('HDD Idle'), _('HDD Idle is a utility program for spinning-down disks after a period of idle time.'));
  34. s = m.section(form.GridSection, 'hd-idle', _('Settings'));
  35. s.anonymous = true;
  36. s.addremove = true;
  37. s.sortable = true;
  38. s.addbtntitle = _('Add new hdd setting...');
  39. s.tab('general', _('Disk Settings'));
  40. o = s.taboption('general', form.Flag, 'enabled', _('Enable'));
  41. o.rmempty = false;
  42. o.editable = true;
  43. o = s.taboption('general', form.ListValue, 'disk', _('Disk'));
  44. devs.forEach(function(dev) {
  45. if( dev.rota ) {
  46. o.value(dev.name, `/dev/${dev.name} [${dev.tran}:${dev.vendor} ${dev.model}]`);
  47. }
  48. });
  49. o = s.taboption('general', form.Value, '_bus', _('Bus'));
  50. o.rawhtml = true;
  51. o.write = function() {};
  52. o.remove = function() {};
  53. o.modalonly = false;
  54. o.textvalue = disk.bind(o, devs, ['tran']);
  55. o = s.taboption('general', form.Value, '_vendorModel', _('Vendor / Model'));
  56. o.rawhtml = true;
  57. o.write = function() {};
  58. o.remove = function() {};
  59. o.modalonly = false;
  60. o.textvalue = disk.bind(o, devs, ['vendor', 'model'] );
  61. o = s.taboption('general', form.Value, 'idle_time_interval', _('Idle time'));
  62. o.modalonly = true;
  63. o.default = 10;
  64. o = s.taboption('general', form.ListValue, 'idle_time_unit', _('Idle time unit'));
  65. o.modalonly = true;
  66. o.value('seconds', _('seconds', 'Abbreviation for seconds'));
  67. o.value('minutes', _('minutes', 'Abbreviation for minutes'));
  68. o.value('hours', _('hours', 'Abbreviation for hours'));
  69. o.value('days', _('days', 'Abbreviation for days'));
  70. o.default = 'minutes';
  71. o = s.taboption('general', form.Value, '_prettytime', _('Idle time'));
  72. o.rawhtml = true;
  73. o.write = function() {};
  74. o.remove = function() {};
  75. o.modalonly = false;
  76. o.textvalue = prettytime.bind(o);
  77. return m.render();
  78. }
  79. });