treewide: import utility classes explicitly
[project/luci.git] / applications / luci-app-statistics / htdocs / luci-static / resources / view / statistics / plugins / sensors.js
1 'use strict';
2 'require baseclass';
3 'require fs';
4 'require form';
5
6 var sensorTypes = [
7 /^[0-9]+(?:\.[0-9]+)?v$/, 'voltage',
8 /^(?:ain|in|vccp|vdd|vid|vin|volt|voltbatt|vrm)[0-9]*$/, 'voltage',
9 /^(?:cpu_temp|remote_temp|temp)[0-9]*$/, 'temperature',
10 /^(?:fan)[0-9]*$/, 'fanspeed',
11 /^(?:power)[0-9]*$/, 'power'
12 ];
13
14 return baseclass.extend({
15 title: _('Sensors Plugin Configuration'),
16 description: _('The sensors plugin uses the Linux Sensors framework to gather environmental statistics.'),
17
18 addFormOptions: function(s) {
19 var o;
20
21 o = s.option(form.Flag, 'enable', _('Enable this plugin'));
22
23 o = s.option(form.DynamicList, 'Sensor', _('Sensor list'));
24 o.rmempty = true;
25 o.size = 18;
26 o.depends('enable', '1');
27 o.load = function(section_id) {
28 return fs.exec_direct('/usr/sbin/sensors', ['-j'], 'json').then(L.bind(function(output) {
29 for (var bus in output) {
30 for (var sensor in output[bus]) {
31 if (!L.isObject(output[bus][sensor]))
32 continue;
33
34 for (var j = 0; j < sensorTypes.length; j += 2) {
35 if (sensor.match(sensorTypes[j])) {
36 this.value('%s/%s-%s'.format(bus, sensorTypes[j + 1], sensor));
37 break;
38 }
39 }
40 }
41 }
42
43 return this.super('load', [section_id]);
44 }, this));
45 };
46
47 o = s.option(form.Flag, 'IgnoreSelected', _('Monitor all except specified'));
48 o.depends('enable', '1');
49 },
50
51 configSummary: function(section) {
52 var sensors = L.toArray(section.Sensor),
53 invert = section.IgnoreSelected == '1';
54
55 if (invert && sensors.length)
56 return N_(sensors.length, 'Monitoring all but one sensor', 'Monitoring all but %d sensors').format(sensors.length);
57 else if (sensors.length)
58 return N_(sensors.length, 'Monitoring one sensor', 'Monitoring %d sensors').format(sensors.length);
59 else
60 return _('Monitoring all sensors');
61 }
62 });