summaryrefslogtreecommitdiff
path: root/kvmd/web/js/atx.js
blob: 0167ae6fbacf8218c2660f3e05d87bec8d3f4a7b (plain)
1
2
3
4
5
6
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
39
40
41
42
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
var atx = new function() {
	this.loadInitialState = function() {
		var http = tools.makeRequest("GET", "/kvmd/atx", function() {
			if (http.readyState === 4) {
				if (http.status === 200) {
					atx.setButtonsBusy(JSON.parse(http.responseText).result.busy);
				} else {
					setTimeout(atx.loadInitialState, 1000);
				}
			}
		});
	};

	this.setState = function(state) {
		$("atx-power-led").className = (state.leds.power ? "led-on" : "led-off");
		$("atx-hdd-led").className = (state.leds.hdd ? "led-hdd-busy" : "led-off");
	};

	this.clearState = function() {
		[
			"atx-power-led",
			"atx-hdd-led",
		].forEach(function(name) {
			$(name).className = "led-off";
		});
	};

	this.clickButton = function(el) {
		var button = null;
		var confirm_msg = null;
		var timeout = null;

		switch (el.id) {
			case "atx-power-button":
				var button = "power";
				var confirm_msg = "Are you sure to click the power button?";
				break;
			case "atx-power-button-long":
				var button = "power_long";
				var confirm_msg = "Are you sure to perform the long press of the power button?";
				var timeout = 15000;
				break;
			case "atx-reset-button":
				var button = "reset";
				var confirm_msg = "Are you sure to reboot the server?";
				break;
		}

		if (button && confirm(confirm_msg)) {
			// atx.setButtonsBusy(true);
			var http = tools.makeRequest("POST", "/kvmd/atx/click?button=" + button, function() {
				if (http.readyState === 4) {
					if (http.status === 409) {
						alert("Performing another ATX operation for other client, please try again later");
					} else if (http.status !== 200) {
						alert("Click error:", http.responseText);
					}
					// atx.setButtonsBusy(false);
				}
			}, timeout);
		}
	};

	this.setButtonsBusy = function(busy) {
		[
			"atx-power-button",
			"atx-power-button-long",
			"atx-reset-button",
		].forEach(function(name) {
			tools.setButtonBusy(document.getElementById(name), busy);
		});
	};
};