<!DOCTYPE html>
 <meta charset="utf-8" />
 <title>Quantified Other: Heart Edition</title>
 <link rel="stylesheet" media="screen" href="https://fontlibrary.org/face/d-din" type="text/css"/>
 <style media="screen">
   body{
     font-family: 'DDINRegular', Helvetica, sans-serif;
   }
   #graph{
     height: calc(100vh);
     width:100vw;
     position: absolute;
     top:0;
     left:0;
     z-index: -1;
   }
   h1{
     color: darkblue;
   }
  #downloads {
    border: solid #000 1px;
    padding: 10px;
    position: absolute;
    top: 160px;
    right: 10px;
    background: darkblue;
    font-weight: bold;
    width: 200px;
    color: white;
  }
  #downloads a{
    color: yellow;
  }
  #title {
    background: darkblue;
    width: 200px;
    height: 130px;
    display: flex;
    justify-content: center;
    align-items: center;
    color: white;
    font-size: 20pt;
    padding: 10px;
    position: absolute;
    top: 10px;
    right: 10px;
    border: solid 1px #000;
    text-shadow: 1px 1px #000, -1px -1px #000,1px -1px #000, -1px 1px #000;
    margin-top:0;
  }
  #value {
    padding-left: 6px;
    font-size: 60px;
    display: block;
    float: right;
    width: 50%;
    text-align: right;
  }
  #downloads span {
    font-weight: normal;
    margin-left: 20px;
  }

  #projecttitle{
    position: absolute;
    top: 0px;
    left: 10px;
  }

  #me{
    font-style: italic;
    position: absolute;
    bottom: 10px;
    right:10px;
    font-size: 70%;
  }

  .gtitle{
    transform: translateY(47px);
  }

 </style>
 <script src="https://cdn.plot.ly/plotly-1.5.0.min.js"></script>
 <script language="javascript" type="text/javascript">

 var url = window.location.toString().replace('http','ws');
 var wsUri = url.substring(0, url.lastIndexOf("/")) + "/ws";
 console.log(wsUri)

 </script>

<h1 id="projecttitle">Data Double Black Market - Redeem code {{ code }}</h1>
<div id="me">A project by <a href="https://rubenvandeven.com">Ruben van de Ven</a></div>
 <h2 id="title">live bpm: <span id='value'>...</span></h2>
 <div id='downloads'>
   <div class='description>'>Get as <a href="/{{ code }}.csv">csv</a></div>
 </div>
<div id="graph">

</div>
<script type="text/javascript">
var valueEl = document.getElementById('value');
var audio = new Audio('/assets/heartbeat.mp3');
Plotly.d3.csv("/{{ code }}.csv", function(err, rows){
  console.log(rows);
   function unpack(rows, key) {
     return rows.map(function(row) { return row[key]; });
   }


   var trace1 = {
     type: "scatter",
     mode: "lines",
     name: 'BPM',
     x: unpack(rows, 'timestamp'),
     y: unpack(rows, 'bpm'),
     line: {color: '#02028b'}//17BECF
   }

   var data = [trace1];

   var layout = {
     title: 'Redeem heartbeat: Ruben van de Ven',
     xaxis: {

       type: 'date',
       range: [ new Date("{{ startDate }}").getTime(), new Date("{{ endDate }}").getTime() ],
       autorange: false
     }
   };

   var currentBpm = null;

   Plotly.newPlot('graph', data, layout);

   var wsUri = window.location.toString().replace('http','ws') + "ws";

   var connectSocket = function() {
     websocket = new WebSocket(wsUri);
     websocket.onopen = function(evt) {
       console.log("opened");
     };
     websocket.onclose = function(evt) {
       console.log("closed", evt);
       valueEl.innerHTML = '...';
       setTimeout(connectSocket, 1000 * 10);
     };
     websocket.onmessage = function(evt) {
       data = JSON.parse(evt.data);
        var update = {
          x: [[data['timestamp']]],
          y: [[data['bpm']]]
        }

        console.log("update", update)
        valueEl.innerHTML = data['bpm'];
        currentBpm = data['bpm'];

        Plotly.extendTraces('graph', update, [0])
     };
     websocket.onerror = function(evt) {

     };
   }
   connectSocket();

   let tick = function() {
     var duration = 10000;
     if(currentBpm != null) {
       audio.play();
       duration = 60000 / currentBpm;
     }
     setTimeout(tick, duration);
   }
   tick();

});

</script>