gazeclient.js 3.47 KB
Newer Older
Alexander Bazo 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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
// GazeClient Version 1.0 - [https://lab.las3.de/gitlab/eye-tracking-classroom/gaze-client.js]
(function () {
  'use strict';

  class Observable {

    constructor() {
      this.listeners = {};
    }

    addEventListener(type, callback) {
      if (this.listeners[type] === undefined) {
        this.listeners[type] = [];
      }
      this.listeners[type].push(callback);
    }

    notifyAll(event) {
      let listeners = this.listeners[event.type];
      if (listeners) {
        for (let i = 0; i < listeners.length; i++) {
          listeners[i](event);
        }
      }
    }

  }

  class Event {

    constructor(type, data) {
      this.type = type;
      this.data = data;
    }

  }

  class DataEvent extends Event {
    constructor(data) {
      super("dataavailable", data);
    }
  }

  class ConnectionOpenedEvent extends Event {
    constructor() {
      super("connectionopened");
    }
  }

  class ConnectionClosedEvent extends Event {
    constructor() {
      super("connectionclosed");
    }
  }

  class ConnectionErrorEvent extends Event {
    constructor() {
      super("erroroccurred");
    }
  }

  const DATA_SEPERATOR = ";";

  class GazeData {
    constructor(leftEyeX, leftEyeY, rightEyeX, rightEyeY, trackerTimeStamp,
      systemTimeStamp) {
      this.leftEyeX = leftEyeX;
      this.leftEyeY = leftEyeY;
      this.rightEyeX = rightEyeX;
      this.rightEyeY = rightEyeY;
      this.trackerTimeStamp = trackerTimeStamp;
      this.systemTimeStamp = systemTimeStamp;
    }

    static fromDataString(dataString) {
      let dataValues = dataString.split(DATA_SEPERATOR);
      return new GazeData(parseFloat(dataValues[0]), parseFloat(dataValues[1]), parseFloat(dataValues[2]),
        parseFloat(dataValues[3]), parseInt(dataValues[4]), parseInt(dataValues[5]));
    }
  }

  class WebSocketClient extends Observable {

    constructor() {
      super();
    }

    connect(url) {
      this.ws = new WebSocket(url);
      this.ws.onopen = onOpen.bind(this);
      this.ws.onclose = onClose.bind(this);
      this.ws.onerror = onError.bind(this);
      this.ws.onmessage = onMessage.bind(this);
    }

  }

  function onOpen() {
    let connectionEvent = new ConnectionOpenedEvent();
    this.notifyAll(connectionEvent);
  }

  function onClose() {
    let connectionEvent = new ConnectionClosedEvent();
    this.notifyAll(connectionEvent);
  }

  function onError() {
    let connectionEvent = new ConnectionErrorEvent();
    this.notifyAll(connectionEvent);
  }

  function onMessage(event) {
    let data = GazeData.fromDataString(event.data),
      dataEvent = new DataEvent(data);
    this.notifyAll(dataEvent);
  }

  class GazeClient extends Observable {

    constructor() {
      super();
    }

    connect(url) {
      this.url = url;
      this.client = new WebSocketClient();
      this.client.addEventListener("connectionopened", this.onConnected.bind(this));
      this.client.addEventListener("connectionclosed", this.onDisconnected.bind(this));
      this.client.addEventListener("erroroccurred", this.onDisconnected.bind(this));
      this.client.addEventListener("dataavailable", this.onDataAvailable
        .bind(this));
      this.client.connect(url);
    }

    onConnected(event) {
      this.notifyAll(event);
    }

    onDisconnected(event) {
      this.notifyAll(event);
    }

    onError(event) {
      this.notifyAll(event);
    }

    onDataAvailable(event) {
      this.notifyAll(event);
    }

  }

  window.GazeClient = GazeClient;

}());
//# sourceMappingURL=gazeclient.js.map