summaryrefslogtreecommitdiff
path: root/schall/templates/index.html
blob: cd46a7ac6a67596742654591c65aae75373a4dfd (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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<html>
  <head>
    <script src="static/RGraph/libraries/RGraph.common.core.js" ></script>
    <script src="static/RGraph/libraries/RGraph.common.effects.js" ></script>
    <script src="static/RGraph/libraries/RGraph.led.js" ></script>
    <script src="static/RGraph/libraries/RGraph.gauge.js" ></script>
    <script src="static/RGraph/libraries/RGraph.line.js" ></script>
    <style>
#led {
  position: absolute;
  left: 118px;
  top: 95px;
  z-index: 1;
}
#gauge1 {
  position: absolute;
  left: 5px;
}
#chart {
  position: absolute;
  left: 260px;
}
#chartHistory {
  position: absolute;
  top: 280px;
}
    </style>
  </head>
  <body>
    <canvas width=" 40px" height=" 20px" id="led"></canvas>
    <canvas width="250px" height="250px" id="gauge1"></canvas>
    <canvas width="  0px" height="250px" id="chart"></canvas>
    <canvas width="  0px" height="250px" id="chartHistory"></canvas>
    <script>
window.onload = function () {
        document.getElementById("chart").width = window.innerWidth - document.getElementById("gauge1").width - 10;
        document.getElementById("chartHistory").width = window.innerWidth - 10;

        var data = {"value":null, ts:null};
        var chart_data = [[]];
        var GRAPH_MAX_POINTS = 1000;
        
        var gauge1 = new RGraph.Gauge('gauge1', 20, 70);
        gauge1.Set('chart.text.size', 8);
        gauge1.Set('chart.units.post', 'dbA');
        gauge1.Set('chart.green.color', 'lightGreen');
        gauge1.Set('chart.yellow.color', 'yellow');
        gauge1.Set('chart.red.color', 'red');
        gauge1.Set('chart.green.end', 40);
        gauge1.Set('chart.red.start', 60);
        gauge1.Draw();

        var led = new RGraph.LED('led', '00');
        led.Set('chart.light', 'red');
        led.Set('chart.zoom.background', false);
        led.Set('chart.dark', 'rgba(0,0,0,0)');
        led.Draw();

        var graph = new RGraph.Line('chart', [])
        graph.Set('chart.title.yaxis', 'dBA');
        graph.Set('chart.title.yaxis.pos', 0.4);
        graph.Set('chart.ylabels.count', 5);
        graph.Set('chart.gutter.top', 5);
        graph.Set('chart.gutter.bottom', 5);
        graph.Set('chart.gutter.left', 60);
        graph.Set('chart.gutter.right', 5);
        graph.Set('chart.linewidth', [3,1]);
        graph.Set('chart.colors', ["red", "black"]);
        graph.Set('chart.ymin', 20);
        graph.Set('chart.ymax', 70);
        graph.Set('chart.xticks', 25);

        chartHistory = new RGraph.Line('chartHistory', []);
        chartHistory.Set('chart.title.yaxis', 'dBA');
        chartHistory.Set('chart.title.yaxis.pos', 0.4);
        chartHistory.Set('chart.ylabels.count', 5);
        chartHistory.Set('chart.gutter.top', 5);
        chartHistory.Set('chart.gutter.bottom', 50);
        chartHistory.Set('chart.gutter.left', 60);
        chartHistory.Set('chart.gutter.right', 5);
        chartHistory.Set('chart.linewidth', [1]);
        chartHistory.Set('chart.colors', ["black"]);
        chartHistory.Set('chart.ymin', 20);
        chartHistory.Set('chart.ymax', 70);

 	function json(path, func) {
		var req = new XMLHttpRequest();
		req.open("GET", path, true);
		req.onreadystatechange = function() {
			if (req.readyState == 4)
				func( JSON.parse( req.responseText) );
		};
		req.send();
	}

        function startRefreshData() {
		var req_start = new Date().getTime();
		json("now", function(_data) {
			var req_end = new Date().getTime()
			var d = req_end - req_start;

			data = _data;

			window.setTimeout(startRefreshData, 500 - d);
		});
        }
	
	function startRefreshChartHistory() {
		var d = ( 1000 * 60 * 60 * 24 * 4 );
		var now = new Date().getTime();
		var from = now - d;
		var count = 1200;
		var interval = d / count;
		json("range/" + from + "/" + now + "/" + count, function (data) {
			var labels = [];
			var j = from + interval;
			for (var i in data) {
				var label = null;
				var thisDate = new Date(from + i*interval);
				if ( (from + i*interval) > j + 1000*60*60*6) {
					j = from + i*interval;
					label = ("" + (thisDate.getHours()<10?"0":"") + thisDate.getHours() + 
						 ":" + ( thisDate.getMinutes() < 10 ? "0" : "") + thisDate.getMinutes());
				}
				labels.push(label);
			}
			chartHistory.Set("chart.labels", labels.reverse());
			chartHistory.Set('chart.xticks', d / (1000*60*60*6));
			chartHistory.Set('chart.background.grid.autofit.numvlines', d/ (1000*60*60*6));

			RGraph.Clear(chartHistory.canvas);
			chartHistory.original_data = [ data ];
			chartHistory.Draw()

			window.setTimeout( startRefreshChartHistory, 60 * 1000);
		});
	}

	function redrawChart() {
		// Gauge
		gauge1.value = data['value'];
		gauge1.Draw();

		// LED
		led.text = '' + Math.round(data['value']);
		led.Draw()

		// Line Chart
		chart_data.unshift(data['value']);

		while (chart_data.length > GRAPH_MAX_POINTS) {
			chart_data.pop();
		}

		RGraph.Clear(graph.canvas);
		graph.original_data = [ chart_data ];
		graph.Draw();
	}


	json("range/" + (new Date().getTime() - (500 * 1500)), function (data) {
		chart_data = data;
		startRefreshData();
		startRefreshChartHistory();
		window.setInterval(redrawChart, 500);
	});
}
    </script>
  </body>
</html>