hwmon.js 3.49 KB
Newer Older
1 2 3 4 5 6
$(function(){
	//init();
});

var interval_read_temperature;

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
7 8 9
var d0 = Array(600);
var d1 = Array(600);
var d2 = Array(600);
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
10

11 12
var ds = [Array(600),Array(600),Array(600),Array(600)];
var labels = ["CPU","389","/dev/sda","/dev/sdb"];
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
13

14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
var t = Array();

var level_shutdown;
var level_fan_on;
var level_fan_hyst;

var options = {
	lines: { show: true },
	points: { show: false },
	xaxis: { 
		tickDecimals: 0,
		tickSize: 1
	},
	yaxis: {
		min: 0,
		max: 100
	},
31 32 33 34 35 36 37 38 39
	colors: [
            'rgba(255,100,100,0.5)',
            'rgba(255,150, 20,0.5)',
            'rgba(100,255,100,0.5)',
            'rgba(0,0,0,1)',
            'rgba(100,100,200,1)',
            'rgba( 50,150, 50,1)',
            'rgba( 50,150,150,1)'
        ],
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
40 41 42
	legend:{
	    position: "nw"
	},
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
};

function init(){
	console.log("hwmon init()");
	
	var tc = $("#t_chart");
	
	tc.css({
		width: "800px",
		height: "600px",
		background:"rgba(100,100,100,0.0)"
	});
	
	read_params(init_fields);
}

function init_fields(){
	$("#t_shutdown").val(level_shutdown);
	$("#t_fan_on").val(level_fan_on);
	$("#t_fan_hyst").val(level_fan_hyst);
	$("#scan_period").val(scan_period);
	
	$(".pars").change(function(){
		update_params();
	});
	
	interval_read_temperature = setInterval(read_core_temp,scan_period*1000);
}

function update_params(){
	level_shutdown = parseFloat($("#t_shutdown").val());
	level_fan_on = parseFloat($("#t_fan_on").val());
	level_fan_hyst = parseFloat($("#t_fan_hyst").val());
	scan_period = parseFloat($("#scan_period").val());
	
	$.ajax({
		url: "hwmon.php?cmd=write&temp_sampling_time="+scan_period+"&temp_major="+level_shutdown+"&temp_minor="+level_fan_on+"&temp_hyst="+level_fan_hyst,
		complete: function(){
			console.log("parameters updated");
			clearInterval(interval_read_temperature);
			interval_read_temperature = setInterval(read_core_temp,scan_period*1000);
		}
	});
}

function read_params(callback){
	$.ajax({
		url: "hwmon.php?cmd=read",
		complete: function(data){
			var tmp_str = data.responseText;
			tmp_str = tmp_str.trim();
			var tmp_arr = tmp_str.split(/\n|: /);
			for (var i=0;i<tmp_arr.length;i++){
				switch(tmp_arr[i]){
					case "temp_sampling_time":
						scan_period = parseFloat(tmp_arr[i+1]);
						break;
					case "temp_major":
						level_shutdown = parseFloat(tmp_arr[i+1]);
						break;
					case "temp_minor":
						level_fan_on = parseFloat(tmp_arr[i+1]);
						break;
					case "temp_hyst":
						level_fan_hyst = parseFloat(tmp_arr[i+1]);
						break;
					default:
						break;
				}
			}
			callback();
		}
	});
}

function read_core_temp(){
	$.ajax({
		url: "hwmon.php?cmd=t",
		complete: function(data){
			console.log(data.responseText);
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
123 124 125
			
			temps = data.responseText.trim().split(" ");
			
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
126 127
			//t.push(d1.length);
			d0.splice(0,1);
128
			d0.push(level_shutdown);
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
129
			d1.splice(0,1);
130
			d1.push(level_fan_on);
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
131
			d2.splice(0,1);
132
			d2.push(level_fan_on-level_fan_hyst);
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
133
			
134
			options.xaxis.tickSize = Math.max(1,Math.round(d1.length)/10);
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
135 136
			
			points = [
137
				{label: "CPU shutdown level", data: get_data(t,d0)},
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
138 139
				{label: "CPU Fan-On level",  data: get_data(t,d1)},
				{label: "CPU Fan-Off level", data: get_data(t,d2)},
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
140 141 142
			];
			
			for(var i=0;i<temps.length;i++){
143
                            if (temps[i]!="-"){
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
144 145 146
				ds[i].splice(0,1);
				ds[i].push(temps[i]);
				points.push({label: labels[i], data: get_data(t,ds[i])});
147
                            }
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
148 149 150
			}
			
			$.plot($("#t_chart"), points, options);
151 152 153 154 155 156
		}
	});
}

function get_data(x,y){
	var d = Array();
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
157 158
	for (var i=0;i<y.length; i++){
        d.push([i, y[i]]);
159 160 161
	}
	return d;
}