index.html 4.6 KB
Newer Older
nextime's avatar
nextime committed
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
 <html>
   <head>
      <title>Autobahn Serial2Ws</title>

      <script src="http://autobahn.s3.amazonaws.com/js/autobahn.min.js"></script>
      <script src="smoothie.js"></script>

      <script>

         var sess = null;
         var wsuri = "ws://" + window.location.hostname + ":9000";
         var retryCount = 0;
         var retryDelay = 2;

         var analog0 = null;
         var analog1 = null;

         var analog0_last = null;
         var analog1_last = null;

         var line0 = new TimeSeries();
         var line1 = new TimeSeries();

         var eventCnt = 0;
         var eventCntUpdateInterval = 2;

         function onAnalogValue(topicUri, event) {
            eventCnt += 1;
            event.value = event.value / 400 * 100;
            event.value = event.value.toFixed(2);
            switch (event.id) {
               case 0:
                  analog0.innerHTML = event.value;
                  if (analog0_last !== null) {
                     line0.append(new Date().getTime(), analog0_last);
                  }
                  analog0_last = event.value;
                  line0.append(new Date().getTime(), event.value);
                  break;
               case 1:
                  analog1.innerHTML = event.value;
                  if (analog1_last !== null) {
                     line1.append(new Date().getTime(), analog1_last);
                  }
                  analog1_last = event.value;
                  line1.append(new Date().getTime(), event.value);
                  break;
               default:
                  break;
            }
         }

         function controlLed(status) {
            sess.call("rpc:control-led", status).always(ab.log);
         }

         function updateEventCnt() {
            document.getElementById("event-cnt").innerHTML = Math.round(eventCnt/eventCntUpdateInterval) + " events/s";
            eventCnt = 0;
         }

         function connect() {

            statusline = document.getElementById('statusline');

            sess = new ab.Session(wsuri,
               function() {

                  statusline.innerHTML = "Connected to " + wsuri;
                  retryCount = 0;

                  sess.prefix("event", "http://example.com/mcu#");
                  sess.subscribe("event:analog-value", onAnalogValue);

                  sess.prefix("rpc", "http://example.com/mcu-control#");

                  eventCnt = 0;

                  window.setInterval(updateEventCnt, eventCntUpdateInterval * 1000);
               },
               function() {
                  console.log(retryCount);
                  retryCount = retryCount + 1;
                  statusline.innerHTML = "Connection lost. Reconnecting (" + retryCount + ") in " + retryDelay + " secs ..";
                  window.setTimeout(connect, retryDelay * 1000);
               }
            );
         }


         window.onload = function ()
         {
            analog0 = document.getElementById('analog0');
            analog1 = document.getElementById('analog1');

            var smoothie = new SmoothieChart({grid: {strokeStyle: 'rgb(125, 0, 0)',
                                                     fillStyle: 'rgb(60, 0, 0)',
                                                     lineWidth: 1,
                                                     millisPerLine: 250,
                                                     verticalSections: 6},
                                              minValue: 0,
                                              maxValue: 100,
                                              resetBounds: false,
                                              //interpolation: "line"
                                              });

            smoothie.addTimeSeries(line0, { strokeStyle: 'rgb(0, 255, 0)', fillStyle: 'rgba(0, 255, 0, 0.4)', lineWidth: 3 });
            smoothie.addTimeSeries(line1, { strokeStyle: 'rgb(255, 0, 255)', fillStyle: 'rgba(255, 0, 255, 0.3)', lineWidth: 3 });

            smoothie.streamTo(document.getElementById("mycanvas"));

            connect();
         };
     </script>
   </head>
   <body>
      <h1>Autobahn WebSockets: MCU to WS Gateway Demo</h1>

      <h2>Control LED</h2>
      <button onclick="controlLed(false)">LED Off</button>
      <button onclick="controlLed(true)">LED On</button>

      <h2>Analog Values</h2>
      <p>Analog 0 : <span id="analog0">-</span></p>
      <p>Analog 1 : <span id="analog1">-</span></p>

      <div id="event-cnt" style="color: #f60; font-weight: 700; font-size: 24px;">-</div>

      <canvas id="mycanvas" width="800" height="400"></canvas>

      <h2>Status</h2>
      <div id="statusline"></div>
   </body>
 </html>