metastream_sample.html 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <!--
  2. * Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in
  4. * compliance with the License. A copy of the License is located at
  5. * http://aws.amazon.com/apache2.0/
  6. * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS,
  7. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  8. * specific language governing permissions and limitations under the License.
  9. *
  10. -->
  11. <!DOCTYPE html>
  12. <html>
  13. <head>
  14. <meta charset="utf-8">
  15. <title>Metastream Sample</title>
  16. <link href="https://fonts.googleapis.com/css?family=Roboto+Condensed" rel="stylesheet">
  17. <link href="metastream_sample.css" rel="stylesheet">
  18. </head>
  19. <body>
  20. <div class="container">
  21. <div id="position-text"></div>
  22. <div id="direction-text"></div>
  23. <div class="map">
  24. <div class="position">
  25. <div class="arrow"></div>
  26. </div>
  27. </div>
  28. </div>
  29. <script src="http://code.jquery.com/jquery-3.1.1.min.js"></script>
  30. <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.14.0/babel.min.js"></script>
  31. <script type="text/babel">
  32. class SampleOverlay {
  33. constructor(pollRate = 200) {
  34. this.state = {
  35. pollRate: pollRate, // How often to poll for data in milliseconds
  36. position: [0,0,0],
  37. forward: [0,0,0],
  38. };
  39. this._bind(
  40. "pollData"
  41. );
  42. // Start polling for data after a short delay
  43. setTimeout(this.pollData, 1000);
  44. }
  45. _bind(...methods) {
  46. methods.forEach( (method) => this[method] = this[method].bind(this) );
  47. }
  48. pollData() {
  49. var url = "/data";
  50. var query = "?table=sample&key=";
  51. query += "position,";
  52. query += "forward";
  53. $.get(url + query, function(json) {
  54. var data;
  55. try {
  56. data = $.parseJSON(json);
  57. } catch(e) {
  58. // returned JSON wasn't well formed... 404 maybe?
  59. return;
  60. }
  61. var position = data["position"];
  62. if(position) {
  63. this.state.position = position;
  64. }
  65. var forward = data["forward"];
  66. if(forward) {
  67. this.state.forward = forward;
  68. }
  69. this.update();
  70. }.bind(this)).always(function() {
  71. setTimeout(this.pollData, this.state.pollRate);
  72. }.bind(this));
  73. }
  74. update() {
  75. var xpos = (this.state.position[0] / 128.0) * 100.0;
  76. var ypos = (this.state.position[1] / 128.0) * 100.0;
  77. var angle = Math.atan2(this.state.forward[0], this.state.forward[1]) + Math.PI/4;
  78. var positionStyle = {
  79. bottom: "calc(" + ypos + "% - 16px)",
  80. left: "calc(" + xpos + "% - 16px)",
  81. transform: "rotate(" + angle + "rad)",
  82. };
  83. $("#position-text").html("Jack Position: " + JSON.stringify(this.state.position));
  84. $("#direction-text").html("Jack Direction: " + JSON.stringify(this.state.forward));
  85. $(".position").css(positionStyle);
  86. }
  87. }
  88. // Create and start overlay
  89. var overlay = new SampleOverlay();
  90. </script>
  91. </body>
  92. </html>