Clean up duplicate php code to inc file
[ampy.git] / html / test_chuncked.php
1 <!DOCTYPE html>
2 <meta charset="UTF-8">
3
4 <fieldset>
5   <legend>debug messages</legend>
6   <span id=debugg></span>
7 </fieldset>
8
9 Puissance instannĂ©e: <span id=currentpower>Unknown</span> watts<br>
10
11 <script>
12 function debug(str) {
13         document.getElementById('debugg').innerHTML += "" + str + "<br>";
14 }
15 xhr = new XMLHttpRequest();
16 xhr.onreadystatechange = function() {
17         switch (xhr.readyState) {
18         case 0: /* unsent */
19         case 1: /* opened */
20         case 2: /* header received */
21                 return;
22         case 3:
23                 switch(xhr.status) {
24                 case 200:
25                         response = xhr.responseText;
26                         //debug("responseText:"+response);
27                         new_text = response.substring(xhr.ampy_len);
28                         xhr.ampy_len = response.length;
29                         //debug("new_text: "+new_text);
30
31                         try {
32                                 data = JSON.parse(new_text);
33                         } catch(SyntaxError) {
34                                 debug("Error decoding json: "+new_text);
35                                 break;
36                         }
37                         document.getElementById('currentpower').innerHTML = data.watts.toFixed(1);
38                         break;
39                 default:
40                         debug("XMLHttpRequest.status=="+xhr.status+" ("+xhr.statusText+")");
41                         break;
42                 }
43                 break;
44         case 4: /* done */
45                 debug("XMLHttpRequest.readyState==4. Connection was lost.");
46                 /* TODO: reconnect */
47                 break;
48         }
49 }
50 xhr.ampy_len=0;
51 xhr.open('GET', 'currentjson_chunked.php', true /*async*/);
52 xhr.send(null);
53
54 </script>
55
56
57 <!--img src="graph.php"-->