-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathTools.cs
executable file
·375 lines (338 loc) · 14.5 KB
/
Tools.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
/* License
* ------------------------------------------------------------------------------
* Copyright (c) 2019 UX Digital Systems Ltd
*
* Permission is hereby granted, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software
* for the continued use and development of the system on which it was installed,
* and to permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* Any persons obtaining the software have no rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software without
* written persmission from UX Digital Systems Ltd, if it is not for use on the
* system on which it was originally installed.
* ------------------------------------------------------------------------------
* UX.Digital
* ----------
* http://ux.digital
*/
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using Crestron.SimplSharp;
using Crestron.SimplSharp.CrestronIO;
using Crestron.SimplSharp.CrestronXml;
using Crestron.SimplSharp.CrestronXmlLinq;
using Crestron.SimplSharp.Reflection;
using Crestron.SimplSharpPro.DM;
using UX.Lib2.Models;
namespace UX.Lib2
{
/// <summary>
/// A generic static class for useful tools
/// </summary>
public static class Tools
{
/// <summary>
/// Print the version of an assembly with the branded header to console. Use on program boot at start of Ctor()
/// </summary>
/// <param name="projectAssembly">The assembly to get the version info from</param>
public static void PrintLibInfo(Assembly projectAssembly)
{
var info = GetProgramInfo();
CrestronConsole.PrintLine("");
CrestronConsole.PrintLine(@"_________________________________________________________");
CrestronConsole.PrintLine(@" ");
CrestronConsole.Print(Debug.AnsiCyan);
CrestronConsole.PrintLine(@" _ ___ __ ____ _ _ _ _ ");
CrestronConsole.PrintLine(@" | | | \ \/ / | _ \(_) __ _(_) |_ __ _| | ");
CrestronConsole.PrintLine(@" | | | |\ / | | | | |/ _` | | __/ _` | | ");
CrestronConsole.PrintLine(@" | |_| |/ \ _| |_| | | (_| | | || (_| | | ");
CrestronConsole.PrintLine(@" \___//_/\_(_)____/|_|\__, |_|\__\__,_|_| ");
CrestronConsole.PrintLine(@" |___/ ");
CrestronConsole.Print(Debug.AnsiReset);
CrestronConsole.PrintLine(@" ");
CrestronConsole.PrintLine(@" UX Digital Systems Limited ");
CrestronConsole.Print(Debug.AnsiPurple);
CrestronConsole.PrintLine(@" http://ux.digital ");
CrestronConsole.Print(Debug.AnsiReset);
CrestronConsole.PrintLine(@"_________________________________________________________");
CrestronConsole.PrintLine(@"");
CrestronConsole.PrintLine("System Name: {0}", info["System Name"]);
CrestronConsole.PrintLine(GetAssemblyNameAndVersionString(projectAssembly));
CrestronConsole.PrintLine(@"_________________________________________________________");
CrestronConsole.PrintLine(@"");
foreach (var pair in info)
{
CrestronConsole.PrintLine("{0}: {1}", pair.Key, pair.Value);
}
CrestronConsole.PrintLine(@"_________________________________________________________");
}
public static string GetAssemblyNameAndVersionString(Assembly assembly)
{
var version = assembly.GetName().Version;
return string.Format("{0} v{1}.{2:D2}.{3:D2} ({4})", assembly.GetName().Name,
version.Major, version.Minor, version.Build, version.Revision);
}
public static ReadOnlyDictionary<string, string> GetProgramInfo()
{
using (
var stream =
new FileStream(
string.Format("{0}\\ProgramInfo.config", InitialParametersClass.ProgramDirectory),
FileMode.Open))
{
using (var xml = new XmlReader(stream))
{
var doc = XDocument.Load(xml);
var dict = new Dictionary<string, string>();
var requiredInfo = doc.Root.Element("RequiredInfo");
dict["System Name"] = requiredInfo.Element("SystemName").Value;
var optionalInfo = doc.Root.Element("OptionalInfo");
try
{
dict["Programmer Name"] = optionalInfo.Element("ProgrammerName").Value;
}
catch
{
dict["Programmer Name"] = "Unknown";
}
dict["App Build Date"] = optionalInfo.Element("CompiledOn").Value;
dict["App Version"] = optionalInfo.Element("CompilerRev").Value;
var plugin = doc.Root.Element("Plugin");
dict["SIMPLSharp Version"] = plugin.Element("Version").Value;
try
{
dict["Include4.dat Version"] = plugin.Element("Include4.dat").Value;
}
catch
{
dict["Include4.dat Version"] = "Unknown";
}
return new ReadOnlyDictionary<string, string>(dict);
}
}
}
/// <summary>
/// Print an array of bytes to the console
/// </summary>
/// <param name="bytes">Byte array to print</param>
/// <param name="startIndex">Start index of the array</param>
/// <param name="length">Count of bytes to print</param>
public static void PrintBytes(byte[] bytes, int startIndex, int length)
{
PrintBytes(bytes, startIndex, length, false);
}
/// <summary>
/// Print a collection of bytes to the console for debugging
/// </summary>
/// <param name="bytes">Byte array to print</param>
/// <param name="startIndex">Start index of the array</param>
/// <param name="length">Number of bytes to print</param>
/// <param name="showReadable">true if you want to display readable characters as normal ascii</param>
public static void PrintBytes(byte[] bytes, int startIndex, int length, bool showReadable)
{
CrestronConsole.PrintLine(GetBytesAsReadableString(bytes, startIndex, length, showReadable));
}
public static string GetBytesAsReadableString(byte[] bytes, int startIndex, int length, bool showReadable)
{
var result = string.Empty;
for (int i = startIndex; i < length; i++)
{
if (showReadable && bytes[i] >= 32 && bytes[i] < 127)
{
result = result + string.Format("{0}", (char)bytes[i]);
}
else
{
result = result + @"\x" + bytes[i].ToString("X2");
}
}
return result;
}
/// <summary>
/// Scale a number range
/// </summary>
/// <param name="value">Number to scale</param>
/// <param name="fromMinValue">The current min value of the number</param>
/// <param name="fromMaxValue">The current max value of the number</param>
/// <param name="toMinValue">The new min value of the new range</param>
/// <param name="toMaxValue">The new max value of the new range</param>
/// <returns></returns>
public static double ScaleRange(double value,
double fromMinValue, double fromMaxValue,
double toMinValue, double toMaxValue)
{
try
{
return (value - fromMinValue) *
(toMaxValue - toMinValue) /
(fromMaxValue - fromMinValue) + toMinValue;
}
catch
{
return double.NaN;
}
}
public static void Increment(this IAudioLevelControl level, uint percentage)
{
if (percentage > 100)
{
throw new IndexOutOfRangeException("perecentage out of range");
}
var scaledLevel = ScaleRange(level.Level, ushort.MinValue, ushort.MaxValue, 0, 100);
scaledLevel = scaledLevel + percentage;
if (scaledLevel > 100)
{
scaledLevel = 100;
}
level.Level = (ushort) ScaleRange(scaledLevel, 0, 100, ushort.MinValue, ushort.MaxValue);
}
public static void Decrement(this IAudioLevelControl level, uint percentage)
{
if (percentage > 100)
{
throw new IndexOutOfRangeException("perecentage out of range");
}
var scaledLevel = ScaleRange(level.Level, ushort.MinValue, ushort.MaxValue, 0, 100);
scaledLevel = scaledLevel - percentage;
if (scaledLevel < 0)
{
scaledLevel = 0;
}
level.Level = (ushort)ScaleRange(scaledLevel, 0, 100, ushort.MinValue, ushort.MaxValue);
}
public static ushort GetPercentageLevel(this IAudioLevelControl control)
{
return (ushort) ScaleRange(control.Level, ushort.MinValue, ushort.MaxValue, 0, 100);
}
/// <summary>
/// Save a stream as a file
/// </summary>
/// <param name="input">The stream to save</param>
/// <param name="filePath">The full file path where it will be saved to</param>
public static void CreateFileFromResourceStream(Crestron.SimplSharp.CrestronIO.Stream input, string filePath)
{
using (Crestron.SimplSharp.CrestronIO.Stream output = Crestron.SimplSharp.CrestronIO.File.Create(filePath))
{
CopyStream(input, output);
}
}
/// <summary>
/// Copy a stream byte for byte
/// </summary>
/// <param name="input"></param>
/// <param name="output"></param>
public static void CopyStream(Crestron.SimplSharp.CrestronIO.Stream input, Crestron.SimplSharp.CrestronIO.Stream output)
{
// Insert null checking here for production
var buffer = new byte[8192];
int bytesRead;
while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, bytesRead);
}
}
private static readonly string[] SizeSuffixes =
{"bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"};
/// <summary>
/// Get a string showing size from bytes
/// </summary>
/// <param name="value">number of bytes</param>
/// <param name="decimalPlaces">number of decimal places</param>
public static string PrettyByteSize(Int64 value, int decimalPlaces)
{
var i = 0;
var dValue = (decimal) value;
while (Math.Round(dValue, decimalPlaces) >= 1000)
{
dValue /= 1024;
i++;
}
return string.Format("{0:n" + decimalPlaces + "} {1}", dValue, SizeSuffixes[i]);
}
public static IEnumerable<IpTableResult> GetIpTable()
{
var reply = string.Empty;
if (CrestronConsole.SendControlSystemCommand("ipt -t", ref reply) == false)
{
return new IpTableResult[]
{
};
}
var matches = Regex.Matches(reply,
@"\ *(\w+)\ *\|(\w+)\ *\|(\w+)\ *\|(\w+)?\ *\|(\d+)\ *\|([\d\.]+)\ *\|\ *([\w\-\ ]+)\ *?\|\ *([\w ]+)");
return new ReadOnlyCollection<IpTableResult>((from Match match in matches
select
new IpTableResult(int.Parse(match.Groups[1].Value.Trim(), NumberStyles.HexNumber), match.Groups[3].Value.Trim() == "ONLINE",
int.Parse(match.Groups[5].Value.Trim()), match.Groups[6].Value.Trim(),
match.Groups[7].Value.Trim())).ToList());
}
public static string GetDmInputEventIdName(int value)
{
var type = typeof (DMInputEventIds).GetCType();
foreach (var field in type.GetFields())
{
if (field.FieldType != typeof (int)) continue;
var v = (int) field.GetValue(null);
if (v == value) return field.Name;
}
return "Unknown ID " + value;
}
public static string GetDmOutputEventIdName(int value)
{
var type = typeof(DMOutputEventIds).GetCType();
foreach (var field in type.GetFields())
{
if (field.FieldType != typeof(int)) continue;
var v = (int)field.GetValue(null);
if (v == value) return field.Name;
}
return "Unknown ID " + value;
}
}
public class IpTableResult
{
private readonly int _cipId;
private readonly bool _online;
private readonly int _port;
private readonly string _ipAddress;
private readonly string _modelName;
public IpTableResult(int cipId, bool online, int port, string ipAddress, string modelName)
{
_cipId = cipId;
_online = online;
_port = port;
_ipAddress = ipAddress;
_modelName = modelName;
}
public int CipId
{
get { return _cipId; }
}
public bool Online
{
get { return _online; }
}
public int Port
{
get { return _port; }
}
public string IPAddress
{
get { return _ipAddress; }
}
public string ModelName
{
get { return _modelName; }
}
}
}