Advertisement
Guest User

Untitled

a guest
May 7th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <html>
  2.     <head>
  3.  
  4.     <script src="bower_components/requirejs/require.js"></script>
  5.     <link rel="stylesheet" href="style.css">
  6.     </head>
  7.  
  8. <br />
  9.  
  10. <form>
  11.     <b>Record:</b><br>
  12.     <label class="switch">
  13.         <input type="checkbox" id="record">
  14.         <div class="slider round"></div>
  15.     </label>
  16.     <br />
  17.    
  18.     <b>Label:</b><br>
  19.     <select id="label"><br>
  20.         <option value="testing">Testing</option>
  21.         <option value="walking">Walking</option>
  22.         <option value="standing">Standing</option>
  23.         <option value="sitting">Sitting</option>
  24.     </select>
  25.    
  26.        
  27.     <b>Subject:</b><br>
  28.     <input type="text" id="subject"><br>
  29. </form>
  30.  
  31. <br />
  32.  
  33. <p id="debug">Not recording.</p>
  34.  
  35. <div class="main">
  36.     <h2 id="header">(Timestamp, Label)</h2>
  37.     <table>
  38.         <tr>
  39.             <td>alpha (rotation)</td>
  40.             <td id="alpha"></td>
  41.         </tr>
  42.         <tr>
  43.             <td>beta (rotation)</td>
  44.             <td id="beta"></td>
  45.         </tr>
  46.         <tr>
  47.             <td>gamma (rotation)</td>
  48.             <td id="gamma"></td>
  49.         </tr>
  50.         <tr>
  51.             <td>x (acceleration)</td>
  52.             <td id="x"></td>
  53.         </tr>
  54.         <tr>
  55.             <td>y (acceleration)</td>
  56.             <td id="y"></td>
  57.         </tr>
  58.         <tr>
  59.             <td>z (acceleration)</td>
  60.             <td id="z"></td>
  61.         </tr>
  62.     </table>
  63. </div>
  64.  
  65.  
  66. <script>
  67.  
  68. // Global variables to store current values.
  69. var c;
  70.  
  71. function Values() {return {
  72. alpha : [],
  73. beta : [],
  74. gamma : [],
  75. acc_x : [],
  76. acc_y : [],
  77. acc_z : [],
  78. n: 0
  79. }
  80. };
  81.  
  82. document.getElementById("subject").value=Math.floor((1 + Math.random()) * 0x10000).toString(16)
  83.  
  84. c=new Values();
  85.  
  86. // Measurement ID is the UNIX timestamp when recording is started.
  87. var measurement_label = "";
  88.  
  89. // How many data points have been recorded/uploaded so far?
  90. var data_count = 0;
  91.  
  92. // How many data points are uploaded per second?
  93. var UPLOAD_RATE = 20;
  94.  
  95. // Global var for setInterval setting/clearing.
  96. var interval;
  97. var noWrite=function () {}
  98. var write=noWrite()
  99. var writePoint=function(){noWrite()}
  100.  
  101. // Start upload function X times every second, but only if record switch is checked.
  102. function recordDataPoint() {
  103.    
  104.     if (document.getElementById('record').checked) {
  105.         writePoint();
  106.        
  107.     } else {
  108.         // Do nothing if record switch is off.
  109.     }
  110. }
  111.  
  112. // Stop/Start uploading depending on switch.
  113. document.getElementById('record').onchange = function() {
  114.     if(this.checked) {
  115.         label = document.getElementById("label").value;
  116.         subject = document.getElementById("subject").value;
  117.         writePoint=function(){write()}
  118.        
  119.         if (label) {
  120.             measurement_label = Math.floor(Date.now() / 1000) + "";
  121.             document.getElementById("debug").innerHTML = "Recording... (" + data_count + ")";
  122.             window.clearInterval(interval);
  123.             interval = window.setInterval(recordDataPoint, 1000 / UPLOAD_RATE);
  124.         } else {
  125.             this.checked = false;
  126.             document.getElementById("debug").innerHTML = "Choose label first."
  127.         }
  128.        
  129.     } else {
  130.         window.clearInterval(interval);
  131.         document.getElementById("debug").innerHTML = "Not recording."
  132.         data_count = 0;
  133.        
  134.         document.getElementById("header").innerHTML = "(Timestamp, Label)";
  135.         document.getElementById("alpha").innerHTML = "";
  136.         document.getElementById("beta").innerHTML = "";
  137.         document.getElementById("gamma").innerHTML = "";
  138.         document.getElementById("x").innerHTML = "";
  139.         document.getElementById("y").innerHTML = "";
  140.         document.getElementById("z").innerHTML = "";
  141.     }
  142. }
  143.  
  144. if (window.DeviceOrientationEvent) {
  145.   window.addEventListener('deviceorientation', function(rotation) {
  146.         // Read and store all acceleration and rotation values.
  147.        
  148.         c.alpha.push(rotation.alpha);
  149.         c.beta.push(rotation.beta);
  150.         c.gamma.push(rotation.gamma);
  151.     //  c.acc_x.push(acceleration.x);
  152.     //  c.acc_y.push(acceleration.y);
  153.     //  c.acc_z.push(acceleration.z);
  154.         c.n++;
  155.     }, false);
  156. } else {
  157.   document.getElementById("debug").innerHTML = "Not supported."
  158. }
  159.  
  160. require(["bower_components/influent/dist/influent"], function (influent) {
  161. // Function to write a single datapoint to the database.
  162.     influent
  163.         .createHttpClient({
  164.             server: [
  165.                 {
  166.                     protocol: "http",
  167.                     host:     "css18.teco.edu",
  168.                     port:     8086
  169.                 }
  170.             ],
  171.             username: "css18",
  172.             password: "css18",
  173.            
  174.             database: "css18"
  175.         })
  176.         .then(function(client) {
  177.             write=function()
  178.             {
  179.                 if(c.n>0)
  180.                 {
  181.                 data_count++;
  182.  
  183.  
  184.                 label = document.getElementById("label").value;
  185.                 subject = document.getElementById("subject").value;
  186.  
  187.                 // Set debug fields.
  188.                 document.getElementById("header").innerHTML = "(" + measurement_label + ", " + label + ")";
  189.                 document.getElementById("alpha").innerHTML = c.alpha;
  190.                 document.getElementById("beta").innerHTML = c.beta;
  191.                 document.getElementById("gamma").innerHTML = c.gamma;
  192.                 document.getElementById("x").innerHTML = c.acc_x;
  193.                 document.getElementById("y").innerHTML = c.acc_y;
  194.                 document.getElementById("z").innerHTML = c.acc_z;
  195.  
  196.                 var c0=c;
  197.                 c=new Values();
  198.                 client.write({
  199.                         key: "orientation",
  200.                         tags: {
  201.                             label: label,
  202.                             subject: subject
  203.                         },
  204.                         fields: {
  205.                             count: data_count,
  206.                             gamma: mean(c0.gamma),
  207.                             beta: mean(c0.beta),
  208.                             alpha: mean(c0.alpha),
  209.                         },
  210.                         timestamp: (Date.now() * 1000000)
  211.                     })
  212.                     .then(function() {
  213.                         document.getElementById("debug").innerHTML = "Recorded... (" + data_count + ")";
  214.                     });
  215.             }
  216.                 }
  217.             })
  218. })
  219.  
  220. function mean(values) {
  221.     var sum = values.reduce(function(a, b) { return a + b; });
  222.     return Math.floor(sum * 1000 / values.length);
  223. }
  224.  
  225. </script>
  226.  
  227. </html>
  228. <head>
  229.     <script>
  230.         var influx;
  231. requirejs(["bower_components/influent/dist/influent"],function(influent)
  232.     {
  233.         influx=influent;
  234.     }
  235. )
  236.     </script>
  237. </head>
  238. <body>
  239.     test7
  240. </body>
  241. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement