forked from Nenkai/PDTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLiveTimingClient.cs
More file actions
140 lines (115 loc) · 4.74 KB
/
Copy pathLiveTimingClient.cs
File metadata and controls
140 lines (115 loc) · 4.74 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.WebSockets;
using System.Net;
using System.Text.Json;
using System.Threading;
using PDTools.LiveTimingApi.Entities;
namespace PDTools.LiveTimingApi;
public class LiveTimingClient : IDisposable
{
private string _host;
private ClientWebSocket _client;
public const int BufferSize = 0x2000;
public LiveTimingClient(string host)
{
_host = host;
}
public delegate void LiveTimingRaceStateDelegate(LiveTimingRaceState raceState);
/// <summary>
/// Fired when a race state was updated.
/// </summary>
public event LiveTimingRaceStateDelegate OnRaceStateUpdate;
public delegate void LiveTimingRaceInfoDelegate(LiveTimingRaceInfo raceInfo);
/// <summary>
/// Fired when a race information was updated.
/// </summary>
public event LiveTimingRaceInfoDelegate OnRaceInfoUpdate;
public delegate void LiveTimingEntryDelegate(LiveTimingEntry entry);
/// <summary>
/// Fired when an entry was updated.
/// </summary>
public event LiveTimingEntryDelegate OnEntryUpdate;
public delegate void LiveTimingConditionDelegate(LiveTimingCondition condition);
/// <summary>
/// Fired when course conditions were updated.
/// </summary>
public event LiveTimingConditionDelegate OnConditionUpdate;
public delegate void LiveTimingConsumeStateDelegate(LiveTimingConsumeState condition);
/// <summary>
/// Fired when an entry consumption state was updated.
/// </summary>
public event LiveTimingConsumeStateDelegate OnConsumeStateUpdate;
/// <summary>
/// Fired when an entry's best lap was updated.
/// </summary>
public event LiveTimingEntryDelegate OnBestLapEntryUpdate;
public delegate void LiveTimingJsonPayloadDelegate(JsonDocument payload);
/// <summary>
/// Fired when any Json payload is received for more manual control.
/// </summary>
public event LiveTimingJsonPayloadDelegate OnJsonPayloadReceived;
public async Task Start(CancellationToken token = default)
{
_client = new ClientWebSocket();
await _client.ConnectAsync(new Uri($"ws://{_host}/livetimingapi/"), token);
Memory<byte> buffer = new byte[BufferSize];
await _client.SendAsync(Array.Empty<byte>(), WebSocketMessageType.Text, false, token);
while (true)
{
ValueWebSocketReceiveResult r = await _client.ReceiveAsync(buffer, token);
if (_client.CloseStatus.HasValue)
{
Console.WriteLine($"Client closed ({_client.CloseStatus}).");
break;
}
JsonDocument document = JsonDocument.Parse(buffer.Slice(0, r.Count));
if (token.IsCancellationRequested)
token.ThrowIfCancellationRequested();
if (document.RootElement.TryGetProperty("RaceInfo", out JsonElement raceInfoProp))
{
var raceInfo = raceInfoProp.Deserialize<LiveTimingRaceInfo>();
this.OnRaceInfoUpdate?.Invoke(raceInfo);
}
else if (document.RootElement.TryGetProperty("RaceState", out JsonElement raceStateProp))
{
var raceState = raceStateProp.Deserialize<LiveTimingRaceState>();
this.OnRaceStateUpdate?.Invoke(raceState);
}
else if (document.RootElement.TryGetProperty("Entry", out JsonElement entryProp))
{
var entry = entryProp.Deserialize<LiveTimingEntry>();
this.OnEntryUpdate?.Invoke(entry);
}
else if (document.RootElement.TryGetProperty("Condition", out JsonElement conditionProp))
{
var condition = conditionProp.Deserialize<LiveTimingCondition>();
this.OnConditionUpdate?.Invoke(condition);
}
else if (document.RootElement.TryGetProperty("ConsumeState", out JsonElement consumeStateProp))
{
var consumeState = consumeStateProp.Deserialize<LiveTimingConsumeState>();
this.OnConsumeStateUpdate?.Invoke(consumeState);
}
else if (document.RootElement.TryGetProperty("BestlapEntry", out JsonElement bestLapEntryProp))
{
var bestLapEntry = bestLapEntryProp.Deserialize<LiveTimingEntry>();
this.OnBestLapEntryUpdate?.Invoke(bestLapEntry);
}
else
{
throw new Exception("Unsupported property");
}
this.OnJsonPayloadReceived?.Invoke(document);
}
}
public void Dispose()
{
_client.Dispose();
_client = null;
GC.SuppressFinalize(this);
}
}