-
Notifications
You must be signed in to change notification settings - Fork 0
/
StripListItem.cs
384 lines (341 loc) · 12.6 KB
/
StripListItem.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
//+--------------------------------------------------------------------------
//
// NightDriverDesktop - (c) 2024 Dave Plummer. All Rights Reserved.
//
// File: StripListView.cs
//
// Description:
//
// This file contains the implementation of a custom ListViewItem, specifically designed to represent
// individual LightStrip entries within the main list of LightStrips in the NightDriverDesktop application.
//
// The `StripListItem` class extends the basic functionality of a `ListViewItem` by associating it with
// a `LightStrip` object. This allows the program to display and manage various properties of each LightStrip,
// such as its host, connection status, Wi-Fi signal strength, power consumption, and more. Each column in
// the ListView corresponds to a specific attribute of the LightStrip, making it easier to monitor the state
// of each LightStrip at a glance.
//
// The file also defines an enum `StripListViewColumnIndex` that assigns integer indices to each property
// displayed in the ListView. These indices are used to reference the various columns when accessing or
// updating the ListView items.
//
// Additionally, the `StripListItemComparer` class is implemented to facilitate sorting of LightStrips
// based on the selected column. It supports both ascending and descending order sorts for various data types,
// including numeric values, IP addresses, and text.
//
// History: Dec-23-2023 Davepl Created
//
//---------------------------------------------------------------------------
using System.Collections;
using System.Diagnostics;
using System.Net;
namespace NightDriver
{
public enum StripListViewColumnIndex
{
INVALID = -1,
FIRST = 1,
iHost = 1,
iHasSocket,
iWiFiSignal,
iReadyForData,
iBytesPerSecond,
iCurrentClock,
iBufferPos,
iWatts,
iFpsDrawing,
iTimeOffset,
iConnects,
iQueueDepth,
iCurrentEffect,
MAX = iCurrentEffect
};
class StripListItem : ListViewItem
{
public string Host
{
get
{
return SubItems[(int)StripListViewColumnIndex.iHost].Text;
}
set
{
SubItems[(int)StripListViewColumnIndex.iHost].Text = value;
}
}
public string HasSocket
{
get
{
return SubItems[(int)StripListViewColumnIndex.iHasSocket].Text;
}
set
{
SubItems[(int)StripListViewColumnIndex.iHasSocket].Text = value;
}
}
public string WiFiSignal
{
get
{
return SubItems[(int)StripListViewColumnIndex.iWiFiSignal].Text;
}
set
{
SubItems[(int)StripListViewColumnIndex.iWiFiSignal].Text = value;
}
}
public string ReadyForData
{
get
{
return SubItems[(int)StripListViewColumnIndex.iReadyForData].Text;
}
set
{
SubItems[(int)StripListViewColumnIndex.iReadyForData].Text = value;
}
}
public string BytesPerSecond
{
get
{
return SubItems[(int)StripListViewColumnIndex.iBytesPerSecond].Text;
}
set
{
SubItems[(int)StripListViewColumnIndex.iBytesPerSecond].Text = value;
}
}
public string CurrentClock
{
get
{
return SubItems[(int)StripListViewColumnIndex.iCurrentClock].Text;
}
set
{
SubItems[(int)StripListViewColumnIndex.iCurrentClock].Text = value;
}
}
public string BufferPos
{
get
{
return SubItems[(int)StripListViewColumnIndex.iBufferPos].Text;
}
set
{
SubItems[(int)StripListViewColumnIndex.iBufferPos].Text = value;
}
}
public string Watts
{
get
{
return SubItems[(int)StripListViewColumnIndex.iWatts].Text;
}
set
{
SubItems[(int)StripListViewColumnIndex.iWatts].Text = value;
}
}
public string FpsDrawing
{
get
{
return SubItems[(int)StripListViewColumnIndex.iFpsDrawing].Text;
}
set
{
SubItems[(int)StripListViewColumnIndex.iFpsDrawing].Text = value;
}
}
public string TimeOffset
{
get
{
return SubItems[(int)StripListViewColumnIndex.iTimeOffset].Text;
}
set
{
SubItems[(int)StripListViewColumnIndex.iTimeOffset].Text = value;
}
}
public string Connects
{
get
{
return SubItems[(int)StripListViewColumnIndex.iConnects].Text;
}
set
{
SubItems[(int)StripListViewColumnIndex.iConnects].Text = value;
}
}
public string QueueDepth
{
get
{
return SubItems[(int)StripListViewColumnIndex.iQueueDepth].Text;
}
set
{
SubItems[(int)StripListViewColumnIndex.iQueueDepth].Text = value;
}
}
public string CurrentEffect
{
get
{
return SubItems[(int)StripListViewColumnIndex.iCurrentEffect].Text;
}
set
{
SubItems[(int)StripListViewColumnIndex.iCurrentEffect].Text = value;
}
}
public string Location
{
get
{
return Text;
}
}
// UpdateColumnText
public bool UpdateColumnText(StripListItem other, StripListViewColumnIndex idx = StripListViewColumnIndex.INVALID)
{
if (idx == StripListViewColumnIndex.INVALID)
{
bool bChanged = false;
for (int i = 0; i < SubItems.Count; i++)
bChanged |= UpdateColumnText(other, (StripListViewColumnIndex)i);
return bChanged;
}
else
{
if (SubItems[(int)idx].Text != other.SubItems[(int)idx].Text)
{
SubItems[(int)idx].Text = other.SubItems[(int)idx].Text;
return true;
}
return false;
}
}
public StripListItem(ListViewGroup group, String text, LightStrip? strip, bool enabled = true)
{
Text = text;
Group = group;
Tag = strip;
if (strip != null)
{
for (StripListViewColumnIndex i = 0; i < StripListViewColumnIndex.MAX; i++)
SubItems.Add(new ListViewItem.ListViewSubItem(this, "---"));
}
else
{
Checked = enabled;
}
}
public static StripListItem CreateForStrip(ListViewGroup group, LightStrip strip)
{
double epoch = (DateTime.UtcNow.Ticks - DateTime.UnixEpoch.Ticks) / (double)TimeSpan.TicksPerSecond;
var item = new StripListItem(group, strip.FriendlyName, strip);
item.Host = strip.HostName;
item.HasSocket = !strip.HasSocket ? "No" : "Open";
item.WiFiSignal = !strip.HasSocket ? "---" : strip.Response.wifiSignal.ToString();
item.ReadyForData = !strip.HasSocket ? "---" : strip.ReadyForData ? "Ready" : "No";
item.BytesPerSecond = !strip.HasSocket ? "---" : strip.BytesPerSecond.ToString();
item.CurrentClock = !strip.HasSocket ? "---" : strip.Response.currentClock > 8 ? (strip.Response.currentClock - epoch).ToString("F2") : "UNSET";
item.BufferPos = !strip.HasSocket ? "---" : strip.Response.bufferPos.ToString() + "/" + strip.Response.bufferSize.ToString();
item.Watts = !strip.HasSocket ? "---" : strip.Response.watts.ToString();
item.FpsDrawing = !strip.HasSocket ? "---" : strip.Response.fpsDrawing.ToString();
item.TimeOffset = !strip.HasSocket ? "---" : strip.TimeOffset.ToString();
item.Connects = strip.Connects.ToString();
item.QueueDepth = strip.QueueDepth.ToString();
item.CurrentEffect = strip.StripSite.CurrentEffectName;
item.Group = group;
Debug.Assert(item.Tag == strip);
return item;
}
}
public class StripListItemComparer : IComparer
{
internal StripListViewColumnIndex Column;
internal SortOrder SortOrder;
private static long IpToLong(string ip)
{
if (ip == "---")
return 0;
return BitConverter.ToInt32(IPAddress.Parse(ip).GetAddressBytes(), 0);
}
public StripListItemComparer(StripListViewColumnIndex column)
{
this.Column = column;
this.SortOrder = SortOrder.Ascending;
}
private static int CompareTextNumbers(StripListItem left, StripListItem right, StripListViewColumnIndex idx)
{
double leftVal, rightVal;
if (!Double.TryParse(left.SubItems[(int)idx].Text, out leftVal))
leftVal = 0;
if (!Double.TryParse(right.SubItems[(int)idx].Text, out rightVal))
rightVal = 0;
return leftVal.CompareTo(rightVal);
}
public int Compare(object x, object y)
{
var left = x as StripListItem;
var right = y as StripListItem;
if (left == null || right == null)
return 0;
var mult = (SortOrder == SortOrder.Ascending) ? 1 : -1;
switch (Column)
{
case 0:
return left.Location.CompareTo(right.Location) * mult;
case StripListViewColumnIndex.iWiFiSignal:
case StripListViewColumnIndex.iWatts:
case StripListViewColumnIndex.iBytesPerSecond:
case StripListViewColumnIndex.iCurrentClock:
case StripListViewColumnIndex.iFpsDrawing:
case StripListViewColumnIndex.iTimeOffset:
case StripListViewColumnIndex.iConnects:
case StripListViewColumnIndex.iQueueDepth:
return CompareTextNumbers(left, right, Column) * mult;
case StripListViewColumnIndex.iHost:
if (left.Tag == null && right.Tag == null) // Both are groups, compare by group name
{
return left.Group.Name.CompareTo(right.Group.Name) * mult;
}
if (left.Tag == null && right.Group == left.Group)
return -1; // Group is always above a strip that belongs to it
if (right.Tag == null && right.Group == left.Group)
return 1; // Strip is always greater than a group
if (left.Group == right.Group)
{
return left.Host.CompareTo(right.Host) * mult;
}
else
{
return left.Group.Name.CompareTo(right.Group.Name) * mult;
}
return 0;
case StripListViewColumnIndex.iHasSocket:
return left.HasSocket.CompareTo(right.HasSocket) * mult;
case StripListViewColumnIndex.iReadyForData:
return left.ReadyForData.CompareTo(right.ReadyForData) * mult;
case StripListViewColumnIndex.iBufferPos:
return left.BufferPos.CompareTo(right.BufferPos) * mult;
case StripListViewColumnIndex.iCurrentEffect:
return left.CurrentEffect.CompareTo(right.CurrentEffect) * mult;
default:
return 0;
}
}
public void ToggleSortOrder()
{
SortOrder = (SortOrder == SortOrder.Ascending) ? SortOrder.Descending : SortOrder.Ascending;
}
}
}