forked from ArduPilot/MissionPlanner
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPacketInspector.cs
200 lines (167 loc) · 5.68 KB
/
PacketInspector.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
namespace MissionPlanner
{
/// <summary>
/// Used for packet collection to later display
/// </summary>
public class PacketInspector<T>
{
Dictionary<uint, Dictionary<uint, T>> _history = new Dictionary<uint, Dictionary<uint, T>>();
Dictionary<uint, Dictionary<uint, List<irate>>> _rate = new Dictionary<uint, Dictionary<uint, List<irate>>>();
Dictionary<uint, Dictionary<uint, List<irate>>> _bps = new Dictionary<uint, Dictionary<uint, List<irate>>>();
public int RateHistory { get; set; } = 200;
object _lock = new object();
public event EventHandler NewSysidCompid;
struct irate
{
internal DateTime dateTime;
internal int value;
internal irate(DateTime now, int v) : this()
{
dateTime = now;
value = v;
}
}
public List<byte> SeenSysid()
{
List<byte> sysids = new List<byte>();
foreach (var id in toArray(_history.Keys))
{
sysids.Add(GetFromID(id).sysid);
}
return sysids;
}
public List<byte> SeenCompid()
{
List<byte> compids = new List<byte>();
foreach (var id in toArray(_history.Keys))
{
compids.Add(GetFromID(id).compid);
}
return compids;
}
public double SeenRate(byte sysid, byte compid, uint msgid)
{
var id = GetID(sysid, compid);
var end = DateTime.Now;
var start = end.AddSeconds(-3);
var data = toArray(_rate[id][msgid]);
try
{
var starttime = data.First().dateTime;
starttime = starttime < start ? start : starttime;
var msgrate = data.Where(a =>
{
return (a.dateTime > start && a.dateTime < end);
}).Sum(a => a.value / (end - starttime).TotalSeconds);
return msgrate;
}
catch
{
return 0;
}
}
public double SeenBps(byte sysid, byte compid, uint msgid)
{
var id = GetID(sysid, compid);
var end = DateTime.Now;
var start = end.AddSeconds(-3);
var data = toArray(_bps[id][msgid]);
try
{
var starttime = data.First().dateTime;
starttime = starttime < start ? start : starttime;
var msgbps = data.Where(a =>
{
return (a.dateTime > start && a.dateTime < end);
}).Sum(a => a.value / (end - starttime).TotalSeconds);
return msgbps;
}
catch
{
return 0;
}
}
public void Add(byte sysid, byte compid, uint msgid, T message, int size)
{
var id = GetID(sysid, compid);
lock (_lock)
{
// must call clear on any new unseen item to init dictionarys
if (!_history.ContainsKey(id))
Clear(sysid, compid);
_history[id][msgid] = message;
if (!_bps[id].ContainsKey(msgid))
_bps[id][msgid] = new List<irate>();
_bps[id][msgid].Add(new irate(DateTime.Now, size));
if (!_rate[id].ContainsKey(msgid))
_rate[id][msgid] = new List<irate>();
_rate[id][msgid].Add(new irate(DateTime.Now, 1));
while (_rate[id][msgid].Count > RateHistory)
_rate[id][msgid].RemoveAt(0);
while (_bps[id][msgid].Count > RateHistory)
_bps[id][msgid].RemoveAt(0);
}
}
IEnumerable<T> toArray<T>(IEnumerable<T> input)
{
lock (_lock)
{
return input.ToArray();
}
}
public IEnumerable<T> GetPacketMessages()
{
foreach (var messages in toArray(_history.Values))
{
foreach (var msg in toArray(messages.Values))
{
yield return msg;
}
}
}
public void Clear()
{
lock (_lock)
{
_history = new Dictionary<uint, Dictionary<uint, T>>();
_rate = new Dictionary<uint, Dictionary<uint, List<irate>>>();
_bps = new Dictionary<uint, Dictionary<uint, List<irate>>>();
}
NewSysidCompid?.Invoke(this, null);
}
public void Clear(byte sysid, byte compid)
{
var id = GetID(sysid, compid);
lock (_lock)
{
_history[id] = new Dictionary<uint, T>();
_rate[id] = new Dictionary<uint, List<irate>>();
_bps[id] = new Dictionary<uint, List<irate>>();
}
NewSysidCompid?.Invoke(this, null);
}
public IEnumerable<T> this[byte sysid, byte compid]
{
get
{
var id = GetID(sysid, compid);
foreach (var msg in toArray(_history[id].Values))
{
yield return msg;
}
}
}
uint GetID(byte sysid, byte compid)
{
return sysid * 256u + compid;
}
(byte sysid, byte compid) GetFromID(uint id)
{
return ((byte)(id >> 8), (byte)(id & 0xff));
}
}
}