使用websockets和PlotlyJs接收错误的项目

时间:2019-05-29 17:13:37

标签: javascript python websocket plotly

im试图实现一个从py中的websocket接收数据的图形:

async def wsjson(websocket, path):
    while True:

        data = datetime.datetime.now()

        randomSPO2 = random.randint(1,101)
        randomBPM =  random.randint(1,200)
        randomPI = random.randint(1, 20)
        randomDESAT = random.randint(1,101)
        sensors_data = {'type': {
                    'Saturimetro' :  {
                            'property': {
                                'SPO2': randomSPO2,
                                'BPM' : randomBPM,
                                'PI' : randomPI,
                                'DESAT' : 2,
                                'Port' : "",
                                'Datetime': data.strftime("%x"),
                                'status' : 1,
                                }
                            }
                        }       
                }         
        with open("websockets.json","w") as json_file:
            json.dump(sensors_data, json_file)
        print_json = json.dumps(sensors_data)
        await websocket.send(print_json)
        await asyncio.sleep(random.random() * 3)
start_server = websockets.serve(wsjson, '127.0.0.1', 5678)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()

这是我的js函数,用于接收数据并使用Plotly js实现RealTime Plot:

function createGraphFiO2(flag) {

  var ws = new WebSocket("ws://127.0.0.1:5678/");

  var fio2 = 0;
  var obj = [];
  if (!flag) {   
    ws.onmessage = function (event) {
      obj = JSON.parse(event.data);
      fio2 = parseFloat(obj.type.Ossimetro.property.O2);
    };
  }
  else {
    ws.close()
    fio2 = 0;
  }
  var time = new Date();

  FiO2data = [{
    x: [time],
    y: [fio2],
    mode: 'lines',
    line: { color: '#80CAF6' }
  }],

    FiO2layout = {
      autosize: true,
      //  width: 1200,
      // height: 400,
      /*   margin: {l: 50, r: 50, b: 20, t: 20, pad: 4 },*/
      yaxis: {
        fixedrange: true //disabilita zoom
      },
      xaxis: {
        fixedrange: true
      }

    };


  Plotly.plot('graphFiO2', FiO2data, FiO2layout);


    var cnt = 0;

    var interval = setInterval(function () {

      var time = new Date();

      var update = {
        x: [[time]],
        y: [[fio2]]
      }

      var olderTime = time.setMinutes(time.getMinutes() - 1);
      var futureTime = time.setMinutes(time.getMinutes() + 1);

      var minuteView = {
        xaxis: {
          type: 'date',
          range: [olderTime, futureTime]
        }
      };

      Plotly.relayout('graphFiO2', minuteView);
      Plotly.extendTraces('graphFiO2', update, [0])

      if (cnt === 100) clearInterval(interval);
    }, 6000);
}

使用html按钮单击调用:

<script type="text/javascript">

    flag = false;

    function startTest() {


      if (!flag) {
        document.getElementById('pauseBtn').classList.remove("disabled");
        createGraphFiO2(flag);
        flag = true;
        document.getElementById('startBtn').innerText = "Stop"
      }
      else {
        document.getElementById('pauseBtn').classList.add("disabled");

        createGraphFiO2(flag);
        flag = false;
        document.getElementById('startBtn').innerText = "Start"
      }

    }
  </script>

我知道有很多代码,但是我不明白问题出在哪里。

每次我单击“开始”按钮时,都会收到很多跟踪信息,而不是单个跟踪信息,并且当我单击“停止”时,websocket不会结束。

这是我的输出:

enter image description here

0 个答案:

没有答案