gazeclient.js 3.41 KB
Newer Older
Alexander Bazo committed
1
// GazeClient Version 1.0 - [https://lab.las3.de/gitlab/eye-tracking-classroom/gaze-client.js]
Alexander Bazo committed
2
(function () {
3
  'use strict';
Alexander Bazo committed
4

5
  class Observable {
Alexander Bazo committed
6

7 8 9
    constructor() {
      this.listeners = {};
    }
Alexander Bazo committed
10

11 12 13 14 15 16
    addEventListener(type, callback) {
      if (this.listeners[type] === undefined) {
        this.listeners[type] = [];
      }
      this.listeners[type].push(callback);
    }
Alexander Bazo committed
17

18 19 20 21 22 23 24 25
    notifyAll(event) {
      let listeners = this.listeners[event.type];
      if (listeners) {
        for (let i = 0; i < listeners.length; i++) {
          listeners[i](event);
        }
      }
    }
Alexander Bazo committed
26

27
  }
Alexander Bazo committed
28

Alexander Bazo committed
29 30 31 32 33 34 35 36
  class Event {

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

  }
Alexander Bazo committed
37

Alexander Bazo committed
38
  class DataEvent extends Event {
39
    constructor(data) {
Alexander Bazo committed
40
      super("dataavailable", data);
41 42
    }
  }
Alexander Bazo committed
43

Alexander Bazo committed
44
  class ConnectionOpenedEvent extends Event {
45
    constructor() {
Alexander Bazo committed
46
      super("connectionopened");
47 48
    }
  }
Alexander Bazo committed
49

Alexander Bazo committed
50 51 52 53 54 55 56 57 58 59 60 61
  class ConnectionClosedEvent extends Event {
    constructor() {
      super("connectionclosed");
    }
  }

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

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
  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(dataValues[0], dataValues[1], dataValues[2],
        dataValues[3], dataValues[4], dataValues[5]);
    }
  }

82 83 84 85 86 87 88 89 90
  class WebSocketClient extends Observable {

    constructor() {
      super();
    }

    connect(url) {
      this.ws = new WebSocket(url);
      this.ws.onopen = onOpen.bind(this);
Alexander Bazo committed
91 92
      this.ws.onclose = onClose.bind(this);
      this.ws.onerror = onError.bind(this);
93 94 95 96 97 98
      this.ws.onmessage = onMessage.bind(this);
    }

  }

  function onOpen() {
Alexander Bazo committed
99
    let connectionEvent = new ConnectionOpenedEvent();
100 101 102
    this.notifyAll(connectionEvent);
  }

Alexander Bazo committed
103 104 105 106 107 108 109 110 111 112
  function onClose() {
    let connectionEvent = new ConnectionClosedEvent();
    this.notifyAll(connectionEvent);
  }

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

113
  function onMessage(event) {
114 115
    let data = GazeData.fromDataString(event.data),
      dataEvent = new DataEvent(data);
Alexander Bazo committed
116
    this.notifyAll(dataEvent);
117 118 119 120 121 122 123 124 125 126 127
  }

  class GazeClient extends Observable {

    constructor() {
      super();
    }

    connect(url) {
      this.url = url;
      this.client = new WebSocketClient();
Alexander Bazo committed
128 129
      this.client.addEventListener("connectionopened", this.onConnected.bind(this));
      this.client.addEventListener("connectionclosed", this.onDisconnected.bind(this));
Alexander Bazo committed
130
      this.client.addEventListener("erroroccurred", this.onDisconnected.bind(this));
131
      this.client.addEventListener("dataavailable", this.onDataAvailable
132 133 134 135
        .bind(this));
      this.client.connect(url);
    }

Alexander Bazo committed
136 137
    onConnected(event) {
      this.notifyAll(event);
138 139
    }

Alexander Bazo committed
140 141
    onDisconnected(event) {
      this.notifyAll(event);
142 143
    }

Alexander Bazo committed
144 145 146 147
    onError(event) {
      this.notifyAll(event);
    }

Alexander Bazo committed
148
    onDataAvailable(event) {
149 150 151 152 153 154
      this.notifyAll(event);
    }

  }

  window.GazeClient = GazeClient;
Alexander Bazo committed
155 156

}());
Alexander Bazo committed
157
//# sourceMappingURL=gazeclient.js.map