summaryrefslogtreecommitdiff
path: root/schall/templates/index.html
blob: 7ed729fb3b564bb9d68380fa4f83e6b9bcbdb590 (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<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 graph_data = [[],[]];
	var data_max = 0;
	var req_start = 0;

        var GRAPH_MAX_POINTS = 1000;
        for (var i=0; i< GRAPH_MAX_POINTS; ++i) {
                graph_data[0].push(null);
                graph_data[1].push(null);
        }
        
        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_data[0], graph_data[1]);
        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', 5);
        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);
        chartHistory.Set('chart.xticks', 25);

 
        function load() {
                var http = new XMLHttpRequest();
                http.open("GET", "now", true);
                http.onreadystatechange = function() {
                        if (http.readyState == 4) {
				var req_end = ( new Date ).getTime()
                                data = JSON.parse(http.responseText);
				
				var d = req_end - req_start;
                                window.setTimeout(load, 500 - d);
                        } 
                }
		req_start = ( new Date ).getTime();
                http.send(null);
        }

	var httpinit = new XMLHttpRequest();
	httpinit.open("GET", "range/", true);
	httpinit.onreadystatechange = function(){
		if (httpinit.readyState == 4) {
			graph_data[1] = JSON.parse(httpinit.responseText);
			// Start polling
			init();
		}
	}
	httpinit.send()

	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 refreshChartHistory() {
		var now = new Date().getTime();
		var from = now - ( 1000 * 60 * 60 * 24 * 2 );
		var count = 1200;
		json("range/" + from + "/" + now + "/" + count, function (data) {
			RGraph.Clear(chartHistory.canvas);
			chartHistory.original_data = [ data ];
			chartHistory.Draw()

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

	function init() {
		load();
		refreshChartHistory();

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

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

				// Line Chart
				if (data['value'] > data_max) {
					data_max = data['value'];
				}
				graph_data[0].unshift(data['value']);

				while (graph_data[0].length > GRAPH_MAX_POINTS) {
					graph_data[0].pop();
				}

				RGraph.Clear(graph.canvas);
				graph.original_data = graph_data;
				graph.Draw();

		}, 500);
		window.setInterval(function() {
				// Line Chart - Max
				graph_data[1].unshift(data_max);
				data_max = 0;
				while (graph_data[1].length > GRAPH_MAX_POINTS) {
					graph_data[1].pop();
				}
		}, 10000);
	}
}
    </script>
  </body>
</html>