EyeTrackerClient.cs 1.84 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tobii.Research;

namespace GazeWebSocketServer
{
    class EyeTrackerClient
    {
        private IEyeTracker eyeTracker;
        private GazeDataProcessor gazeDataProcessor;
14
        private List<GazeCoordinateBehavior> gazeCoordinateBehaviors;
15

16
        public EyeTrackerClient(ConfigurationData config)
17
        {
18
            gazeCoordinateBehaviors = new List<GazeCoordinateBehavior>();
19 20 21 22 23
            while (eyeTracker == null)
            {
                EyeTrackerCollection eyeTrackers = EyeTrackingOperations.FindAllEyeTrackers();
                if (eyeTrackers.Count > 0)
                {
24 25 26 27 28 29 30
                    foreach(IEyeTracker t in eyeTrackers)
                    {
                        if(config.TrackerSerialNumber == t.SerialNumber)
                        {
                            eyeTracker = t;
                        }
                    }
31 32 33
                }
            }
            gazeDataProcessor = new GazeDataProcessor();
34
            eyeTracker.SetGazeOutputFrequency(config.TrackerFrequency);
35 36
        }

37
        public void Start(GazeCoordinateBehavior gazeCoordinateBehavior)
38
        {
39
            gazeCoordinateBehaviors.Add(gazeCoordinateBehavior);
40 41 42 43 44 45 46 47 48 49
            eyeTracker.GazeDataReceived += GazeDataReceivedHandler;
        }

        public void Stop()
        {
            eyeTracker.GazeDataReceived -= GazeDataReceivedHandler;
        }

        private void GazeDataReceivedHandler(object sender, GazeDataEventArgs e)
        {
50 51 52 53 54 55
            GazeData gazeData = gazeDataProcessor.Extract(e);
            foreach(GazeCoordinateBehavior behavior in gazeCoordinateBehaviors)
            {
                behavior.Publish(gazeData);
            }
            Console.WriteLine(gazeData.ToString());
56 57 58
        }
    }
}