-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
337 lines (304 loc) · 12.3 KB
/
Form1.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.WebSockets;
using System.Diagnostics;
using System.Windows.Forms;
using System.Threading;
namespace algetiming_streamer
{
public partial class TimeStreamerForm : Form
{
private Alge.TimyUsb timyUsb;
private ClientWebSocket webSocket;
#if DEBUG
static String WSURI = "ws://127.0.0.1:8080/time";
#else
private static String WSURI = "ws://default-dot-streaming-clock.appspot.com/time";
#endif
//file to write resuts, just in case
private String fileName = "climbing_results.txt";
private System.IO.StreamWriter resultsFile;
//keepalive timer, so the connection won't stop
private System.Windows.Forms.Timer keepaliveTimer;
public static int SERVERKEEPALIVE = 60 * 1000;
//retry-system for failed messages
public static int RETRYTIME = 500;
private List<String> failedMessageList;
private System.Windows.Forms.Timer retryTimer;
//the connection seeems to be alive for an hour, reconstruct it again before it goes bad
public static int SERVERRECONNECTTIMER = 55 * 60 * 1000 + 500;
private System.Windows.Forms.Timer serverReconnectTimer;
public TimeStreamerForm()
{
InitializeComponent();
failedMessageList = new List<String>();
}
private void Form1_Load(object sender, EventArgs e)
{
String filename = System.IO.Path.GetDirectoryName(Application.ExecutablePath) +"\\"+ fileName;
if (!File.Exists(filename))
{
resultsFile = File.CreateText(filename);
}
else
{
resultsFile = File.AppendText(filename);
}
webSocket = new ClientWebSocket();
webSocket.ConnectAsync(new Uri(WSURI), CancellationToken.None);
//start the timyUSB stuff
timyUsb = new Alge.TimyUsb(this);
timyUsb.Start();
timyUsb.DeviceConnected += new EventHandler<Alge.DeviceChangedEventArgs>(timyUsb_DeviceConnected);
timyUsb.DeviceDisconnected += new EventHandler<Alge.DeviceChangedEventArgs>(timyUsb_DeviceDisconnected);
timyUsb.PnPDeviceAttached += new EventHandler(timyUsb_PnPDeviceAttached);
timyUsb.PnPDeviceDetached += new EventHandler(timyUsb_PnPDeviceDetached);
timyUsb.LineReceived += new EventHandler<Alge.DataReceivedEventArgs>(timyUsb_LineReceived);
timyUsb.HeartbeatReceived += new EventHandler<Alge.HeartbeatReceivedEventArgs>(timyUsb_HeartbeatReceived);
keepaliveTimer = new System.Windows.Forms.Timer();
keepaliveTimer.Interval = SERVERKEEPALIVE;
keepaliveTimer.Tick += new EventHandler(keepaliveTimer_Tick);
keepaliveTimer.Start();
retryTimer = new System.Windows.Forms.Timer();
retryTimer.Interval = RETRYTIME;
retryTimer.Tick += new EventHandler(retryTick);
serverReconnectTimer = new System.Windows.Forms.Timer();
serverReconnectTimer.Interval = SERVERRECONNECTTIMER;
serverReconnectTimer.Tick += new EventHandler(keepTheConnectionAlive);
serverReconnectTimer.Start();
#if DEBUG
AddLogLine("Starting app in DEBUG");
#else
AddLogLine("Starting app in RELEASE");
#endif
AddLogLine("Process is " + (IntPtr.Size == 8 ? "x64" : "x86"));
}
private void reConnectWebSocket()
{
try
{
//socket was not healthy
webSocket.Dispose();
webSocket = new ClientWebSocket();
webSocket.ConnectAsync(new Uri(WSURI), CancellationToken.None);
AddLogLine("Created a new websocket");
}
catch (Exception ex)
{
AddLogLine("reconnecting the websocket failed" + ex.Message);
}
}
private void keepTheConnectionAlive(object sender, EventArgs e)
{
//the connection must be re-created at sometimes to keep things on-going
reConnectWebSocket();
}
private void retryTick(object sender, EventArgs e)
{
AddLogLine("Failed stuff, retrying");
if (webSocket.State != WebSocketState.Open)
{
return;
}
if (failedMessageList.Count() > 0)
{
String message = failedMessageList[0];
byte[] sendBody = Encoding.UTF8.GetBytes(message);
try
{
webSocket.SendAsync(new ArraySegment<byte>(sendBody), WebSocketMessageType.Text, true, CancellationToken.None);
}
catch (Exception ex)
{
AddLogLine("Retry send message exception: " + ex.Message);
}
//ok, message sent, remove it from the LIST
AddLogLine("RetrySent for message " + message+ " succeeded");
failedMessageList.RemoveAt(0);
}
else
{
//no data in the buffer, stop timer
retryTimer.Stop();
}
}
private void keepaliveTimer_Tick(object sender, EventArgs e)
{
//Just write something to keep the connection alive,
//for PING I would have to read for PONG, I don't want to overcomplicate this C# implementation
var message = "PING";
byte[] sendBody = Encoding.UTF8.GetBytes(message);
if (webSocket.State != WebSocketState.Open)
{
reConnectWebSocket();
return; //no need to send anything this time
}
try
{
webSocket.SendAsync(new ArraySegment<byte>(sendBody), WebSocketMessageType.Text, true, CancellationToken.None);
AddLogLine("PING");
}
catch(Exception ex)
{
AddLogLine("keepalive exception: "+ ex.Message);
failedMessageList.Add(message);
retryTimer.Start();
}
}
void timyUsb_HeartbeatReceived(object sender, Alge.HeartbeatReceivedEventArgs e)
{
Debug.WriteLine("Heartbeat: " + e.Time.ToString());
}
void timyUsb_PnPDeviceDetached(object sender, EventArgs e)
{
AddLogLine("Device detached");
}
void timyUsb_PnPDeviceAttached(object sender, EventArgs e)
{
AddLogLine("Device attached");
}
void timyUsb_LineReceived(object sender, Alge.DataReceivedEventArgs e)
{
Debug.WriteLine("Device " + e.Device.Id + " Line: " + e.Data);
String webMessage = null;
String data = e.Data;
String bib;
String time = null;
//if data starts with n, then it's bib update
if (data.StartsWith("n"))
{
bib = data.Substring(1, 4);
bib = bib.TrimStart('0');
if (data.EndsWith("r"))
{
webMessage = "sbr" + bib;
}
else if (data.EndsWith("l"))
{
webMessage = "sbl" + bib;
}
} else
{
bib = data.Substring(1, 4);
String cmd = data.Substring(5, 3);
Debug.WriteLine("bib: " + bib + "cmd: " + cmd);
//was it start:
if (cmd.Equals("rC0")) {
//it is START
webMessage = "srs";
} else if (cmd.Equals("lC3"))
{
//started already on right-start
return;
} else if (cmd.Equals("rC2"))
{
//right side FALSE START
webMessage = "srf";
writeResults("bib: " + bib + " false start");
} else if (cmd.Equals("lC5"))
{
//left side FALSE START
webMessage = "slf";
writeResults("bib: " + bib + " false start");
}
else if (cmd.Equals("rc1"))
{
//right side finished
time = getTimeinMS(data.Substring(10,13));
webMessage = "ssr"+time+"b"+bib;
Debug.WriteLine(time);
writeResults("bib: " + bib + " time: "+ data.Substring(10, 13));
}
else if(cmd.Equals("lc4"))
{
//left side finished
time = getTimeinMS(data.Substring(10, 13));
webMessage = "ssl" + time + "b" + bib;
Debug.WriteLine(time);
writeResults("bib: " + bib + " time: " + data.Substring(10, 13));
}
}
//did we actually construct a command
if (webMessage != null)
{
byte[] sendBody = Encoding.UTF8.GetBytes(webMessage);
if (webSocket.State != WebSocketState.Open)
{
reConnectWebSocket();
}
try
{
//Send the message
webSocket.SendAsync(new ArraySegment<byte>(sendBody), WebSocketMessageType.Text, true, CancellationToken.None);
resetTimer(); //connection is alive, so reset the timer
} catch (Exception ex)
{
AddLogLine("Websocket send mesage failed: " + ex.Message);
failedMessageList.Add(webMessage);
retryTimer.Start();
}
}
}
private void resetTimer()
{
keepaliveTimer.Stop();
keepaliveTimer.Start();
}
private void ProcessProgramResponse(string line)
{
String programString = line.Substring(6);
AddLogLine("Active program: '" + programString + "'");
}
void timyUsb_DeviceDisconnected(object sender, Alge.DeviceChangedEventArgs e)
{
AddLogLine("Device " + e.Device.Id + " disconnected, " + timyUsb.ConnectedDevicesCount + " total connected");
}
void timyUsb_DeviceConnected(object sender, Alge.DeviceChangedEventArgs e)
{
AddLogLine("Device " + e.Device.Id + " connected, " + timyUsb.ConnectedDevicesCount + " total connected");
}
void AddLogLine(String str)
{
status.Items.Insert(0, str);
}
private void writeResults(String str)
{
results.Items.Insert(0, str);
resultsFile.WriteLine(str);
resultsFile.Flush();
}
private String getTimeinMS(String time)
{
String min = time.Substring(3, 2);
String sec = time.Substring(6, 2);
String rest = time.Substring(9, 3);
int total = Int32.Parse(min) * 60 * 1000 + Int32.Parse(sec)*1000 + Int32.Parse(rest);
return total.ToString();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
timyUsb.DeviceConnected -= new EventHandler<Alge.DeviceChangedEventArgs>(timyUsb_DeviceConnected);
timyUsb.DeviceDisconnected -= new EventHandler<Alge.DeviceChangedEventArgs>(timyUsb_DeviceDisconnected);
timyUsb.LineReceived -= new EventHandler<Alge.DataReceivedEventArgs>(timyUsb_LineReceived);
timyUsb.PnPDeviceAttached -= new EventHandler(timyUsb_PnPDeviceAttached);
timyUsb.PnPDeviceDetached -= new EventHandler(timyUsb_PnPDeviceDetached);
timyUsb.HeartbeatReceived -= new EventHandler<Alge.HeartbeatReceivedEventArgs>(timyUsb_HeartbeatReceived);
resultsFile.Flush();
resultsFile.Close();
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void listBox1_SelectedIndexChanged_1(object sender, EventArgs e)
{
}
}
}