-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathHelpers.cs
268 lines (239 loc) · 9.7 KB
/
Helpers.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Reflection;
using System.Text;
namespace Dynaframe3
{
public static class Helpers
{
// Fisher Yates shuffle - source: https://stackoverflow.com/questions/273313/randomize-a-listt/4262134#4262134
// Modified on 3/11 based on A380Coding's feedback. Long lists aren't getting as well shuffled (thousands of images) towards the end with
// the original algo.
public static IList<T> Shuffle<T>(this IList<T> list, Random rnd)
{
for (var i = list.Count-1; i > 0; i--)
list.Swap(i, rnd.Next(list.Count-1));
return list;
}
public static void Swap<T>(this IList<T> list, int i, int j)
{
var temp = list[i];
list[i] = list[j];
list[j] = temp;
}
/// <summary>
/// Gets the ip address of the system. Dynaframe uses this to help the user locate thier device
/// on the network
/// </summary>
/// <returns></returns>
public static string GetIPString()
{
string returnval = "";
string version = Assembly.GetExecutingAssembly().GetName().Version.ToString();
returnval += "(Version: " + version + ")\r\n";
NetworkInterface[] nets = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();
if (nets.Length > 0)
{
foreach (NetworkInterface net in nets)
{
try
{
var addresses = net.GetIPProperties().UnicastAddresses;
for (int i = 0; i < addresses.Count; i++)
{
string ip = addresses[i].Address.ToString();
// Filter out IPV6, local, loopback, etc.
if ((!ip.StartsWith("169.")) && (!ip.StartsWith("127.")) && (!ip.Contains("::")))
returnval += "http://" + ip + ":8000 "+ Environment.NewLine;
}
}
catch (Exception exc)
{
Logger.LogComment("Exception in GetIPString() : " + exc.ToString());
}
}
}
return returnval;
}
/// <summary>
/// Gets the ip address of the system (only IP, not whole string).
/// </summary>
/// <returns></returns>
public static string GetIP()
{
string returnval = "";
NetworkInterface[] nets = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();
if (nets.Length > 0)
{
foreach (NetworkInterface net in nets)
{
try
{
var addresses = net.GetIPProperties().UnicastAddresses;
for (int i = 0; i < addresses.Count; i++)
{
string ip = addresses[i].Address.ToString();
// Filter out IPV6, local, loopback, etc.
if ((!ip.StartsWith("169.")) && (!ip.StartsWith("127.")) && (!ip.Contains("::")))
returnval += ip + ":8000 ";
}
}
catch (Exception exc)
{
Logger.LogComment("Exception in GetIPString() : " + exc.ToString());
}
}
}
return returnval.Trim();
}
/// <summary>
/// Runs a process and exits. If there is a failure or exception, returns false
/// </summary>
/// <param name="process">Process path and name</param>
/// <param name="args">any arguments to pass</param>
/// <returns>Process ID if it runs, else -1 if something fails</returns>
public static int RunProcess(string patoToProcess, string args)
{
try
{
ProcessStartInfo info = new ProcessStartInfo(patoToProcess);
if (!String.IsNullOrEmpty(args))
{
info.Arguments = args;
}
Process process = new Process();
process.StartInfo = info;
process.Start();
return process.Id;
}
catch (Exception)
{
return -1;
}
}
/// <summary>
/// NYI - Future work to get the temp and give warnings, or to build a 'pi dashboard'
/// </summary>
public static void GetTemp()
{
// to get temp in C
// vcgencmd measure_temp | egrep -o '[0-9]*\.[0-9]*'
// 9/5 * C +32 = Farenheight
}
/// <summary>
/// Parses out a value as an int and sets it based on the query string.
/// </summary>
/// <param name="querystring"></param>
/// <param name="property"></param>
/// <returns>0 if a value was not found, 1 if a value was set</returns>
public static int SetIntAppSetting(string querystring, string property)
{
if (querystring != null)
{
int i = 0;
if (int.TryParse(querystring, out i))
{
typeof(AppSettings).GetProperty(property).SetValue(AppSettings.Default, i);
}
return 1;
}
return 0;
}
/// <summary>
/// Parses out a value as an int and sets it based on the query string.
/// </summary>
/// <param name="querystring"></param>
/// <param name="property"></param>
/// <returns>0 if a value was not found, 1 if a value was set</returns>
public static int SetFloatAppSetting(string querystring, string property)
{
if (querystring != null)
{
float i = 0;
if (float.TryParse(querystring, out i))
{
typeof(AppSettings).GetProperty(property).SetValue(AppSettings.Default, i);
}
return 1;
}
return 0;
}
/// <summary>
/// Parses out a value as an int and sets it based on the query string.
/// </summary>
/// <param name="querystring"></param>
/// <param name="property"></param>
/// <returns>0 if a value was not found, 1 if a value was set</returns>
public static int SetDoubleAppSetting(string querystring, string property)
{
if (querystring != null)
{
double i = 0;
if (double.TryParse(querystring, out i))
{
typeof(AppSettings).GetProperty(property).SetValue(AppSettings.Default, i);
}
return 1;
}
return 0;
}
/// <summary>
/// Takes in a string value and returns a 0/1 based on if a setting was found
/// </summary>
/// <param name="querystring"></param>
/// <param name="property"></param>
/// <returns>0 if no setting was changed, 1 if it was</returns>
public static int SetStringAppSetting(string querystring, string property)
{
if (querystring != null)
{
typeof(AppSettings).GetProperty(property).SetValue(AppSettings.Default, querystring);
return 1;
}
return 0;
}
/// <summary>
/// Sets a boolean value based on the query string.
/// </summary>
/// <param name="querystring"></param>
/// <param name="property"></param>
/// <returns>0 if no setting was passed, 1 if it was.</returns>
public static int SetBoolAppSetting(string querystring, string property)
{
if (querystring != null)
{
if (querystring.ToUpper() == "ON")
{
typeof(AppSettings).GetProperty(property).SetValue(AppSettings.Default, true);
}
else
{
typeof(AppSettings).GetProperty(property).SetValue(AppSettings.Default, false);
}
return 1;
}
return 0;
}
public static void DumpAppSettingsToLogger()
{
Logger.LogComment("Current App Settings");
Logger.LogComment("FadeTransition: " + AppSettings.Default.FadeTransitionTime);
Logger.LogComment("SlideshowTransitionTime: " + AppSettings.Default.SlideshowTransitionTime);
Logger.LogComment("FontSize: " + AppSettings.Default.InfoBarFontSize);
Logger.LogComment("FontFamily: " + AppSettings.Default.DateTimeFontFamily);
Logger.LogComment("DateTimeFormat: " + AppSettings.Default.DateTimeFormat);
Logger.LogComment("Rotation: " + AppSettings.Default.Rotation);
Logger.LogComment("Shuffle: " + AppSettings.Default.Shuffle);
Logger.LogComment("ImageStretch: " + AppSettings.Default.ImageStretch);
Logger.LogComment("VideoStretch: " + AppSettings.Default.VideoStretch);
Logger.LogComment("VideoVolume: " + AppSettings.Default.VideoVolume);
Logger.LogComment("isSyncEnabled: " + AppSettings.Default.IsSyncEnabled);
Logger.LogComment("Number of Sync Clients: " + AppSettings.Default.RemoteClients.Count);
}
}
}