thermal_sensors.js 12 KB
Newer Older
Ad Schellevis's avatar
Ad Schellevis committed
1 2
/*
	$Id: thermal_sensors.js
3
	Description:
Ad Schellevis's avatar
Ad Schellevis committed
4 5
		Javascript functions to get and show thermal sensors data in thermal_sensors.widget.php.
		NOTE: depends on proper cofing in System >> Advanced >> Miscellaneous tab >> Thermal Sensors section.
6
	File location:
Ad Schellevis's avatar
Ad Schellevis committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
		\usr\local\www\widgets\javascript\
	Used by:
		\usr\local\www\widgets\widgets\thermal_sensors.widget.php

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions are met:

	1. Redistributions of source code must retain the above copyright notice,
	   this list of conditions and the following disclaimer.

	2. Redistributions in binary form must reproduce the above copyright
	   notice, this list of conditions and the following disclaimer in the
	   documentation and/or other materials provided with the distribution.

	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
	POSSIBILITY OF SUCH DAMAGE.
*/



//should be called from "thermal_sensors.widget.php"
function showThermalSensorsData() {

	//get data from thermal_sensors.widget.php
39
	url = "/widgets/widgets/thermal_sensors.widget.php?getThermalSensorsData=1"
Ad Schellevis's avatar
Ad Schellevis committed
40 41 42 43 44 45 46 47 48 49 50
			//IE fix to disable cache when using http:// , just append timespan
			+ new Date().getTime();

	jQuery.ajax(url, {
		type: 'get',
		success: function(data) {
			var thermalSensorsData = data || "";
			buildThermalSensorsData(thermalSensorsData);
		},
		error: function(jqXHR, status, error){
			buildThermalSensorsDataRaw(
51 52
				"Error getting data from [thermal_sensors.widget.php] - |" +
				"status: [" + (status || "") + "]|" +
Ad Schellevis's avatar
Ad Schellevis committed
53 54 55
				"error: [" + (error || "") + "]");
		}
	});
56

Ad Schellevis's avatar
Ad Schellevis committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
	//call itself in 11 seconds
	window.setTimeout(showThermalSensorsData, 11000);
}

function buildThermalSensorsData(thermalSensorsData) {
	//NOTE: variable thermal_sensors_widget_showRawOutput is declared/set in "thermal_sensors.widget.php"
	if (thermal_sensors_widget_showRawOutput) {
		buildThermalSensorsDataRaw(thermalSensorsData);
	}
	else {
		buildThermalSensorsDataGraph(thermalSensorsData);
	}
}

function buildThermalSensorsDataRaw(thermalSensorsData) {
72

Ad Schellevis's avatar
Ad Schellevis committed
73
	var thermalSensorsContent = "";
74

Ad Schellevis's avatar
Ad Schellevis committed
75 76 77 78
	if (thermalSensorsData && thermalSensorsData != "") {
		thermalSensorsContent = thermalSensorsData.replace(/\|/g, "<br />");
		//rawData = thermalSensorsData.split("|").join("<br />");
	}
79

Ad Schellevis's avatar
Ad Schellevis committed
80 81 82 83 84 85 86 87 88 89 90
	loadThermalSensorsContainer(thermalSensorsContent);
}

function loadThermalSensorsContainer (thermalSensorsContent) {

	if (thermalSensorsContent && thermalSensorsContent != "") {
		//load generated graph (or raw data) into thermalSensorsContainer (thermalSensorsContainer DIV defined in "thermal_sensors.widget.php")
		jQuery('#thermalSensorsContainer').html(thermalSensorsContent);
	} else {
		jQuery('#thermalSensorsContainer').html("No Thermal Sensors data available.<br /><br />");
		jQuery('<div/>').html(
91
				"<span>* You can configure a proper Thermal Sensor / Module under <br />" +
Ad Schellevis's avatar
Ad Schellevis committed
92 93 94 95 96 97 98 99 100 101 102
				"&nbsp;&nbsp;&nbsp;<a href='system_advanced_misc.php'>System &gt; Advanced &gt; Miscellaneous : Thermal Sensors section</a>.</span>"
				).appendTo('#thermalSensorsContainer');
	}
}

function buildThermalSensorsDataGraph(thermalSensorsData) {

	//local constants
	var normalColor = "LimeGreen";
	var normalColorShadowTop = "Lime";
	var normalColorShadowBottom = "Green";
103

Ad Schellevis's avatar
Ad Schellevis committed
104 105
	var warningColor = "Orange";
	var warningColorShadowBottom = "Chocolate";
106

Ad Schellevis's avatar
Ad Schellevis committed
107 108
	var criticalColor = "Red";
	var criticalColorShadowBottom = "DarkRed";
109

Ad Schellevis's avatar
Ad Schellevis committed
110 111 112 113
	//local variables
	var barBgColor = normalColor; //green/normal as default
	var barBgColorShadowTop = normalColorShadowTop; //green/normal as default
	var barBgColorShadowBottom = normalColorShadowBottom; //green/normal as default
114

Ad Schellevis's avatar
Ad Schellevis committed
115
	var thermalSensorsArray = new Array();
116

Ad Schellevis's avatar
Ad Schellevis committed
117 118 119 120 121 122
	if (thermalSensorsData && thermalSensorsData != ""){
		thermalSensorsArray = thermalSensorsData.split("|");
	}

	var thermalSensorsHTMLContent = "";
	var itemsToPulsate = new Array();
123

Ad Schellevis's avatar
Ad Schellevis committed
124 125
	//generate graph for each temperature sensor and append to thermalSensorsHTMLContent string
	for (var i = 0; i < thermalSensorsArray.length; i++) {
126

Ad Schellevis's avatar
Ad Schellevis committed
127 128 129 130 131 132
		var sensorDataArray = thermalSensorsArray[i].split(":");
		var sensorName = sensorDataArray[0].trim();
		var thermalSensorValue = getThermalSensorValue(sensorDataArray[1]);

		var pulsateTimes = 0;
		var pulsateDuration = 0;
133

Ad Schellevis's avatar
Ad Schellevis committed
134 135
		var warningTempThresholdPosition = 0;
		var criticalTempThresholdPosition = 0;
136 137 138

		//NOTE: the following variables are declared/set in "thermal_sensors.widget.php":
		//		thermal_sensors_widget_coreWarningTempThreshold, thermal_sensors_widget_coreCriticalTempThreshold,
Ad Schellevis's avatar
Ad Schellevis committed
139 140
		//		thermal_sensors_widget_zoneWarningTempThreshold, thermal_sensors_widget_zoneCriticalTempThreshold
		//		thermal_sensors_widget_pulsateWarning, thermal_sensors_widget_pulsateCritical
141

Ad Schellevis's avatar
Ad Schellevis committed
142 143
		//set graph color and pulsate parameters
		if (sensorName.indexOf("cpu") > -1) { //check CPU Threshold config settings
144

Ad Schellevis's avatar
Ad Schellevis committed
145 146
			warningTempThresholdPosition = thermal_sensors_widget_coreWarningTempThreshold;
			criticalTempThresholdPosition = thermal_sensors_widget_coreCriticalTempThreshold;
147

Ad Schellevis's avatar
Ad Schellevis committed
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
			if (thermalSensorValue < thermal_sensors_widget_coreWarningTempThreshold) {
				barBgColor = normalColor;
				barBgColorShadowTop = normalColorShadowTop;
				barBgColorShadowBottom = normalColorShadowBottom;
				pulsateTimes = 0;
				pulsateDuration = 0;
			} else if (thermalSensorValue >= thermal_sensors_widget_coreWarningTempThreshold && thermalSensorValue < thermal_sensors_widget_coreCriticalTempThreshold) {
				barBgColor = warningColor;
				barBgColorShadowTop = warningColor;
				barBgColorShadowBottom = warningColorShadowBottom;
				pulsateTimes = thermal_sensors_widget_pulsateWarning ? 4 : 0;
				pulsateDuration = thermal_sensors_widget_pulsateWarning ? 900 : 0;
			} else { // thermalSensorValue > thermal_sensors_widget_coreCriticalTempThreshold
				barBgColor = criticalColor;
				barBgColorShadowTop = criticalColor;
				barBgColorShadowBottom = criticalColorShadowBottom;
				pulsateTimes = thermal_sensors_widget_pulsateCritical ? 7 : 0;
				pulsateDuration = thermal_sensors_widget_pulsateCritical ? 900 : 0;
			}
		} else { //assuming sensor is for a zone, check Zone Threshold config settings
168

Ad Schellevis's avatar
Ad Schellevis committed
169 170 171 172
			warningTempThresholdPosition = thermal_sensors_widget_zoneWarningTempThreshold;
			criticalTempThresholdPosition = thermal_sensors_widget_zoneCriticalTempThreshold;

			if (thermalSensorValue < thermal_sensors_widget_zoneWarningTempThreshold) {
173

Ad Schellevis's avatar
Ad Schellevis committed
174 175 176 177 178 179
				barBgColor = normalColor;
				barBgColorShadowTop = normalColorShadowTop;
				barBgColorShadowBottom = normalColorShadowBottom;
				pulsateTimes = 0;
				pulsateDuration = 0;

180
			} else if (thermalSensorValue >= thermal_sensors_widget_zoneWarningTempThreshold
Ad Schellevis's avatar
Ad Schellevis committed
181
						&& thermalSensorValue < thermal_sensors_widget_zoneCriticalTempThreshold) {
182

Ad Schellevis's avatar
Ad Schellevis committed
183 184 185 186 187 188 189
				barBgColor = warningColor;
				barBgColorShadowTop = warningColor;
				barBgColorShadowBottom = warningColorShadowBottom;
				pulsateTimes = thermal_sensors_widget_pulsateWarning ? 4 : 0;
				pulsateDuration = thermal_sensors_widget_pulsateWarning ? 900 : 0;

			} else { // thermalSensorValue > thermal_sensors_widget_zoneCriticalTempThreshold
190

Ad Schellevis's avatar
Ad Schellevis committed
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
				barBgColor = criticalColor;
				barBgColorShadowTop = criticalColor;
				barBgColorShadowBottom = criticalColorShadowBottom;
				pulsateTimes = thermal_sensors_widget_pulsateCritical ? 7 : 0;
				pulsateDuration = thermal_sensors_widget_pulsateCritical ? 900 : 0;
			}
		}

		//NOTE: variable thermal_sensors_widget_showFullSensorName is declared/set in "thermal_sensors.widget.php"
		if (!thermal_sensors_widget_showFullSensorName) {
			sensorName = getSensorFriendlyName(sensorName);
		}

		//build temperature item/row for a sensor
		//NOTE: additional styles are set in 'thermal_sensors.widget.php'
206
		var thermalSensorRow =	"<div class='thermalSensorRow' id='thermalSensorRow" + i + "' >" +
Ad Schellevis's avatar
Ad Schellevis committed
207
							//sensor name and temperature value
208
							"	<div class='thermalSensorTextShell'><div class='thermalSensorText' id='thermalSensorText" + i + "'>" + sensorName + ": </div><div class='thermalSensorValue' id='thermalSensorValue" + i + "'>" + thermalSensorValue + " &deg;C</div></div>" +
Ad Schellevis's avatar
Ad Schellevis committed
209
							//temperature bar
210 211
							"	<div class='thermalSensorBarShell' id='thermalSensorBarShell" + i + "' >" +
							"		<div class='thermalSensorBar' id='thermalSensorBar" + i + "' style='background-color: " + barBgColor + "; border-top-color: " + barBgColorShadowTop + "; border-bottom-color: " + barBgColorShadowBottom + "; width:" + thermalSensorValue + "%;' ></div>" +
Ad Schellevis's avatar
Ad Schellevis committed
212
							//threshold targets (warning and critical)
213 214
							"		<div class='thermalSensorWarnThresh' id='thermalSensorWarnThresh" + i + "' style='left:" + warningTempThresholdPosition  + "%;' ></div>" +
							"		<div class='thermalSensorCritThresh' id='thermalSensorCritThresh" + i + "' style='left:" + criticalTempThresholdPosition + "%;' ></div>" +
Ad Schellevis's avatar
Ad Schellevis committed
215
							//temperature scale (max 100 C)
216 217 218 219 220 221 222 223 224 225 226 227 228
							"		<div class='thermal_sensors_widget_scale000'></div>" +
							"		<div class='thermal_sensors_widget_scale010'></div>" +
							"		<div class='thermal_sensors_widget_scale020'></div>" +
							"		<div class='thermal_sensors_widget_scale030'></div>" +
							"		<div class='thermal_sensors_widget_scale040'></div>" +
							"		<div class='thermal_sensors_widget_scale050'></div>" +
							"		<div class='thermal_sensors_widget_scale060'></div>" +
							"		<div class='thermal_sensors_widget_scale070'></div>" +
							"		<div class='thermal_sensors_widget_scale080'></div>" +
							"		<div class='thermal_sensors_widget_scale090'></div>" +
							"		<div class='thermal_sensors_widget_scale100'></div>" +
							"		<div class='thermal_sensors_widget_mark100'>100&deg;</div>" +
							"	</div>" +
Ad Schellevis's avatar
Ad Schellevis committed
229
							"</div>";
230

Ad Schellevis's avatar
Ad Schellevis committed
231 232 233 234 235
		//collect parameters for warning/critical items we need to pulsate
		if (pulsateTimes > 0) {
			var params = i + "|" + barBgColor + "|" + pulsateTimes + "|" + pulsateDuration;
			itemsToPulsate.push(params);
		}
236

Ad Schellevis's avatar
Ad Schellevis committed
237 238 239
		//append HTML item
		thermalSensorsHTMLContent = thermalSensorsHTMLContent + thermalSensorRow;
	}
240

Ad Schellevis's avatar
Ad Schellevis committed
241 242
	//load generated graph into thermalSensorsContainer (DIV defined in "thermal_sensors.widget.php")
	loadThermalSensorsContainer(thermalSensorsHTMLContent);
243

Ad Schellevis's avatar
Ad Schellevis committed
244 245 246 247 248 249 250
	if (itemsToPulsate.length > 0) {
		//pulsate/flash warning/critical items we collected
		pulsateThermalSensorsItems(itemsToPulsate);
	}
}

function pulsateThermalSensorsItems(itemsToPulsate) {
251

Ad Schellevis's avatar
Ad Schellevis committed
252 253
	//pulsate/flash warning/critical items we collected
	for (var i = 0; i < itemsToPulsate.length; i++) {
254

Ad Schellevis's avatar
Ad Schellevis committed
255 256 257 258 259 260 261 262
		var pulsateParams = itemsToPulsate[i].split("|");
		var rowNum = parseInt(pulsateParams[0]);
		//var textColor = pulsateParams[1];
		var pulsateTimes = parseInt(pulsateParams[2]);
		var pulsateDuration = parseInt(pulsateParams[3]);

		//pulsate temp Value
		var divThermalSensorValue = jQuery("#thermalSensorValue" + rowNum); //get temp value by id
263
		divThermalSensorValue.effect("pulsate", {
Ad Schellevis's avatar
Ad Schellevis committed
264
				 times: pulsateTimes
265
				,easing: 'linear' //'easeInExpo'
Ad Schellevis's avatar
Ad Schellevis committed
266 267
		}, pulsateDuration);
		////set Temp Value color
268 269
		//divThermalSensorValue.css( { color: textColor } );

Ad Schellevis's avatar
Ad Schellevis committed
270 271
		//pulsate temp Bar
		var divThermalSensorBar = jQuery("#thermalSensorBar" + rowNum); //get temp bar by id
272
		divThermalSensorBar.effect("pulsate", {
Ad Schellevis's avatar
Ad Schellevis committed
273
				 times: pulsateTimes
274
				,easing: 'linear' //'easeInExpo'
Ad Schellevis's avatar
Ad Schellevis committed
275
		}, pulsateDuration);
276

Ad Schellevis's avatar
Ad Schellevis committed
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
	}
}

function getSensorFriendlyName(sensorFullName){
	var rzone = /^hw\.acpi\.thermal\.tz([0-9]+)\.temperature$/;
	var rcore = /^dev\.cpu\.([0-9]+)\.temperature$/;

	if (rzone.test(sensorFullName))
		return "Zone " + rzone.exec(sensorFullName)[1];

	if (rcore.test(sensorFullName))
		return "Core " + rcore.exec(sensorFullName)[1];

	return sensorFullName;
}

function getThermalSensorValue(stringValue){
	return (+parseFloat(stringValue) || 0).toFixed(1);
}