-
Notifications
You must be signed in to change notification settings - Fork 0
/
CanTerminal.cs
214 lines (203 loc) · 7.63 KB
/
CanTerminal.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
using System.Diagnostics;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
public class CanTerminal
{
CanWifiClient client;
bool consolePrint = false;
bool filter = false;
bool onlyNew = false;
long messageReceived = 0;
Stopwatch stopwatch = new Stopwatch();
private Dictionary<ulong, ulong> idsMap = new Dictionary<ulong, ulong>();
private HashSet<ulong> OnlyIds = new HashSet<ulong>();
private HashSet<int> uniqueMessagesHashes = new HashSet<int>();
private HashSet<int> ignoreMessagesHashes = new HashSet<int>();
private CanTerminal(CanWifiClient client)
{
this.client = client;
}
public static async Task<CanTerminal> Create(IPAddress ip, int port)
{
var client = await CanWifiClient.Connect(ip, port);
Console.WriteLine ($"Connected successfully to: {ip}:{port}");
return new CanTerminal(client);
}
private async Task HandleNextMessage()
{
try
{
var message = await client.ReceiveMessageAsync();
ulong count;
idsMap.TryGetValue(message.ID, out count);
idsMap[message.ID] = ++count;
bool isFirstTimeID = idsMap[message.ID] == 1;
int messageHash = message.GetHashCode();
bool isFirstTimeMessage = uniqueMessagesHashes.Add(messageHash);
bool filtered = (filter == true) && OnlyIds.Contains(message.ID);
bool ignore = ignoreMessagesHashes.Contains(messageHash);
messageReceived++;
if (!ignore && (consolePrint || isFirstTimeID || isFirstTimeMessage || filtered || (onlyNew && !ignore)))
{
if (isFirstTimeID || isFirstTimeMessage)
{
Console.ForegroundColor = ConsoleColor.Green;
}
if (filtered)
{
Console.ForegroundColor = ConsoleColor.Yellow;
}
Console.WriteLine($"{stopwatch.ElapsedMilliseconds}:{count}:{message}");
Console.ResetColor();
}
}
catch (InvalidOperationException e)
{
Console.WriteLine($"Reading from socket failed: {e}");
throw;
}
catch (Exception e)
{
Console.WriteLine($"Parsing failed: {e}");
}
Task ignoredTask = HandleNextMessage();
}
private async Task HandleTimer()
{
await Task.Delay(TimeSpan.FromSeconds(10));
Console.WriteLine($"Messages received: {messageReceived}, Channel connected: {client.Connected}");
Task ignoredTask = HandleTimer();
}
//Split by spaces but keep together qouted text. Authored by ChatGPT: "c# regex match quoted string with spaces"
private IList<string> splitInput(String input)
{
// var result = Regex.Matches(input, @"(?<="")[^""]+(?="")|(?<=')[^']+(?=')|[^'""\s]+");
// List<string> list = result.Select(m => m.Value).ToList();
var list = input.Split(" ");
return list;
}
private async Task HandleConsoleInput(Stream stream)
{
try
{
var buffer = new byte[100];
var bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length);
var input = Encoding.UTF8.GetString(buffer, 0, bytesRead).Trim();
var argc = splitInput(input);
if(argc.Count == 3 && argc[0] == "dump" && argc[1] == "start")
{
var fileName = argc[2];
Console.WriteLine($"Dumping to: {fileName}");
client.StartDump(fileName);
}
else if(argc.Count == 2 && argc[0] == "dump" && argc[1] == "stop")
{
Console.WriteLine($"Dump stopped");
client.StopDump();
}
else if(argc.Count == 2 && argc[0] == "print" && argc[1] == "start")
{
Console.WriteLine($"Console print started");
consolePrint = true;
}
else if(argc.Count == 2 && argc[0] == "print" && argc[1] == "stop")
{
Console.WriteLine($"Console print stopped");
consolePrint = false;
}
else if(argc.Count == 1 && argc[0] == "clean")
{
Console.WriteLine($"Clean history");
idsMap.Clear();
uniqueMessagesHashes.Clear();
}
else if(argc.Count == 1 && argc[0] == "onlynew")
{
Console.WriteLine($"Only new");
onlyNew = true;
ignoreMessagesHashes = new HashSet<int>(uniqueMessagesHashes);
}
else if(argc.Count == 2 && argc[0] == "filter" && argc[1] == "off")
{
Console.WriteLine($"Console filter off");
filter = false;
}
else if(argc.Count == 2 && argc[0] == "filter" && argc[1] == "on")
{
Console.WriteLine($"Console filter on");
filter = true;
}
else if(argc.Count == 2 && argc[0] == "filter" && argc[1] == "clear")
{
Console.WriteLine($"Filter reset");
OnlyIds.Clear();
}
else if(argc.Count == 2 && argc[0] == "only")
{
ulong val = ulong.Parse(argc[1]);
if (OnlyIds.Contains(val))
{
Console.WriteLine($"only {val} removed");
OnlyIds.Remove(val);
}
else
{
Console.WriteLine($"only {val} added");
OnlyIds.Add(val);
}
}
else if(argc.Count == 2 && argc[0] == "filter")
{
var canMessage = CanMessage.Deserialize(argc[1]);
var hash = canMessage.GetHashCode();
if (ignoreMessagesHashes.Contains(hash))
{
Console.WriteLine($"Filter {hash} removed");
ignoreMessagesHashes.Remove(hash);
}
else
{
Console.WriteLine($"Filter {hash} added");
ignoreMessagesHashes.Add(hash);
}
}
else if(input.StartsWith("{") && input.EndsWith("}"))
{
var canMessage = CanMessage.Deserialize(input);
await client.SendMessageAsync(canMessage);
}
else
{
Console.WriteLine("Available options:");
Console.WriteLine(" dump start <file name>");
Console.WriteLine(" dump stop");
Console.WriteLine(" print start");
Console.WriteLine(" print stop");
Console.WriteLine(" filter on");
Console.WriteLine(" filter off");
Console.WriteLine(" filter clear");
Console.WriteLine(" only <id>");
Console.WriteLine(" filter <json message>");
Console.WriteLine(" onlynew");
Console.WriteLine(" {json CAN message} to send");
}
}
catch (Exception e)
{
Console.WriteLine($"Failed: {e}");
}
Task ignoredTask = HandleConsoleInput(stream);
}
public async Task Loop()
{
stopwatch.Start();
Task receiveTask = HandleNextMessage();
Task timerTask = HandleTimer();
using (var stream = Console.OpenStandardInput())
{
Task consoleInputTask = HandleConsoleInput(stream);
await Task.Delay(1000*100000);
}
}
}