-
Notifications
You must be signed in to change notification settings - Fork 2
/
Program.cs
231 lines (207 loc) · 8.42 KB
/
Program.cs
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
using System;
using System.Collections.Generic;
using System.IO;
using System.ServiceModel.Web;
using NDesk.Options;
using Tobii.Eyetracking.Sdk;
using Tobii.Eyetracking.Sdk.Time;
namespace ATUAV_RT
{
/// <summary>
/// Processes ATUAV eyetracking data in real-time.
///
/// Requires:
/// - Tobii SDK 3.0 RC 1 http://www.tobii.com/en/eye-tracking-research/global/landingpages/analysis-sdk-30rc/
/// - FixDet http://www.sis.uta.fi/~csolsp/projects.php
/// - NDesk.Options http://www.ndesk.org/Options
/// </summary>
public class Program
{
private static Clock clock;
private static Database database = new Database();
private static Settings settings = new Settings();
private static readonly Dictionary<String, EmdatProcessor> processors = new Dictionary<String, EmdatProcessor>();
private static readonly Dictionary<String, Condition> conditions = new Dictionary<string, Condition>();
public static Dictionary<String, EmdatProcessor> Processors
{
get
{
return processors;
}
}
public static Dictionary<String, Condition> Conditions
{
get
{
return conditions;
}
}
public static Database Database
{
get
{
return database;
}
}
public static Settings Settings
{
get
{
return settings;
}
}
/// <summary>
/// Parses arguments and finds connected Eyetrackers.
/// </summary>
/// <param name="args">Command line arguments</param>
static void Main(string[] args)
{
if (parseArguments(args))
{
initTobiiSdk();
connectEyetrackers();
createWebService();
}
}
/// <summary>
/// Parses command line arguments.
/// </summary>
/// <param name="args"></param>
/// <returns>True if program should continue, false if arguments could not be parsed or help option was called.</returns>
private static bool parseArguments(string[] args)
{
string processorFilePath = null;
bool help = false;
var p = new OptionSet()
{
{ "b|baseAddress=", "{BASE_ADDRESS} for web service", (string v) => Settings.BaseAddress = new Uri(v)},
{ "p|processors=", "{FILEPATH} for processor definitions file", (string v) => processorFilePath = Path.GetFullPath(v)},
{ "h|help", v => help = v != null }
};
if (help)
{
ShowHelp(p);
return false;
}
try
{
List<string> extra = p.Parse(args);
// check if processorFilePath points to actual file
if (processorFilePath != null)
{
if (File.Exists(processorFilePath))
{
ReadProcessorDefinitions(processorFilePath);
}
else
{
throw new OptionException("Processor file does not exist. Verify file path.", "p");
}
}
// check for unparsed arguments
if (extra.Count > 0)
{
throw new OptionException("Unknown arguments.", "");
}
}
catch (OptionException e)
{
Console.WriteLine(e.Message);
Console.WriteLine("Try \'atuavrt --help\' for more information.");
Console.WriteLine();
return false;
}
return true;
}
private static void ReadProcessorDefinitions(string processorFilePath)
{
try
{
using (StreamReader sr = new StreamReader(processorFilePath))
{
while (!sr.EndOfStream)
{
string[] line = sr.ReadLine().Split("\t".ToCharArray(), 2);
string eyetrackerProductId = line[0];
string[] processorDefinitions = line[1].Split(",".ToCharArray());
List<EmdatProcessorSettings> processorSettings = new List<EmdatProcessorSettings>(processorDefinitions.Length);
foreach (string processorDefinition in processorDefinitions)
{
processorSettings.Add(new EmdatProcessorSettings(processorDefinition, processorDefinition.EndsWith("-c")));
}
Settings.ProcessorDefinitions[eyetrackerProductId] = processorSettings;
}
}
}
catch (Exception e)
{
Console.WriteLine("Processor definitions could not be read: " + e.Message);
}
}
/// <summary>
/// Writes usage to console.
/// </summary>
/// <param name="options">Parsed command line arguments</param>
private static void ShowHelp(OptionSet options)
{
Console.WriteLine("Usage: atuav_rt [OPTIONS]");
Console.WriteLine("Process eyetracking data in real time");
Console.WriteLine();
Console.WriteLine("Options:");
options.WriteOptionDescriptions(Console.Out);
}
private static void initTobiiSdk()
{
Library.Init();
clock = new Clock();
}
private static void connectEyetrackers()
{
EyetrackerBrowser browser = new EyetrackerBrowser(EventThreadingOptions.BackgroundThread);
browser.EyetrackerFound += EyetrackerFound;
browser.Start();
}
/// <summary>
/// Connects to found eyetrackers, synchronizes CPU and eyetracker clocks, and attaches event handlers.
/// </summary>
/// <param name="sender"></param>
/// <param name="e">Contains found EyetrackerInfo</param>
private static void EyetrackerFound(object sender, EyetrackerInfoEventArgs e)
{
EyetrackerConnector connector = new EyetrackerConnector(e.EyetrackerInfo);
connector.Connect();
// sync CPU and Eyetracker clocks
SyncManager syncManager = new SyncManager(clock, e.EyetrackerInfo, EventThreadingOptions.BackgroundThread);
// detect fixations
FixationDetector fixations = new FixationDetector(syncManager);
connector.Eyetracker.GazeDataReceived += fixations.GazeDataReceived;
if (Settings.ProcessorDefinitions.ContainsKey(connector.Info.ProductId))
{
foreach (EmdatProcessorSettings settings in Settings.ProcessorDefinitions[connector.Info.ProductId])
{
EmdatProcessor processor = new EmdatProcessor(syncManager);
processor.CumulativeData = settings.Cumulative;
connector.Eyetracker.GazeDataReceived += processor.GazeDataReceived;
fixations.FixDetector.FixationEnd += processor.FixationEnd;
Processors.Add(settings.ProcessorId, processor);
}
}
}
private static void createWebService()
{
using (WebServiceHost host = new WebServiceHost(typeof(AtuavWebServiceImp), Settings.BaseAddress))
{
// Open the ServiceHost to start listening for messages. Since
// no endpoints are explicitly configured, the runtime will create
// one endpoint per base address for each service contract implemented
// by the service.
host.Open();
Console.WriteLine("The service is ready at {0}", Settings.BaseAddress);
Console.WriteLine("Press <Enter> to stop the service.");
Console.ReadLine();
// Close the ServiceHost.
host.Close();
}
}
}
}