-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPluginConfig.cs
345 lines (315 loc) · 11.7 KB
/
PluginConfig.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
using HomeSeerAPI;
using Nito.AsyncEx;
using NullGuard;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using static System.FormattableString;
namespace Hspi
{
/// <summary>
/// Class to store PlugIn Configuration
/// </summary>
/// <seealso cref="System.IDisposable" />
[NullGuard(ValidationFlags.Arguments | ValidationFlags.NonPublic)]
internal sealed class PluginConfig
{
public event EventHandler<EventArgs> ConfigChanged;
/// <summary>
/// Initializes a new instance of the <see cref="PluginConfig"/> class.
/// </summary>
/// <param name="HS">The homeseer application.</param>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1806:DoNotIgnoreMethodResults", MessageId = "System.Net.IPAddress.TryParse(System.String,System.Net.IPAddress@)")]
public PluginConfig(IHSApplication HS)
{
this.HS = HS;
debugLogging = GetValue(DebugLoggingKey, false);
forwardSpeech = GetValue(ForwardSpeechKey, true);
sapiVoice = GetValue<string>(SapiVoiceKey, null);
string webServerIPAddressString = GetValue(WebServerIPAddressKey, string.Empty);
IPAddress.TryParse(webServerIPAddressString, out webServerIPAddress);
webServerPort = GetValue<ushort>(WebServerPortKey, 8081);
string deviceIdsConcatString = GetValue(DeviceIds, string.Empty);
var deviceIds = deviceIdsConcatString.Split(DeviceIdsSeparator);
foreach (var deviceId in deviceIds)
{
if (string.IsNullOrWhiteSpace(deviceId))
{
continue;
}
string ipAddressString = GetValue(IPAddressKey, string.Empty, deviceId);
if (IPAddress.TryParse(ipAddressString, out var deviceIP))
{
string name = GetValue(NameKey, string.Empty, deviceId);
var volume = GetValue<short>(VolumeKey, -1, deviceId);
devices.Add(deviceId, new ChromecastDevice(deviceId, name, deviceIP, volume == -1 ? null : (ushort?)volume));
}
}
// Auto create entries in INI File
WebServerPort = this.webServerPort;
}
/// <summary>
/// Gets or sets the devices
/// </summary>
/// <value>
/// The API key.
/// </value>
public ImmutableDictionary<string, ChromecastDevice> Devices
{
get
{
using (var sync = configLock.ReaderLock())
{
return devices.ToImmutableDictionary();
}
}
}
/// <summary>
/// Gets or sets a value indicating whether debug logging is enabled.
/// </summary>
/// <value>
/// <c>true</c> if [debug logging]; otherwise, <c>false</c>.
/// </value>
public bool DebugLogging
{
get
{
using (var sync = configLock.ReaderLock())
{
return debugLogging;
}
}
set
{
using (var sync = configLock.WriterLock())
{
SetValue(DebugLoggingKey, value);
debugLogging = value;
}
}
}
public bool ForwardSpeach
{
get
{
using (var sync = configLock.ReaderLock())
{
return forwardSpeech;
}
}
set
{
using (var sync = configLock.WriterLock())
{
SetValue(ForwardSpeechKey, value);
forwardSpeech = value;
}
}
}
public string SAPIVoice
{
get
{
using (var sync = configLock.ReaderLock())
{
return sapiVoice;
}
}
set
{
using (var sync = configLock.WriterLock())
{
SetValue(SapiVoiceKey, value);
sapiVoice = value;
}
}
}
public ushort WebServerPort
{
get
{
using (var sync = configLock.ReaderLock())
{
return webServerPort;
}
}
set
{
using (var sync = configLock.WriterLock())
{
SetValue(WebServerPortKey, value);
webServerPort = value;
}
}
}
public IPAddress WebServerIPAddress
{
get
{
using (var sync = configLock.ReaderLock())
{
return webServerIPAddress;
}
}
set
{
using (var sync = configLock.WriterLock())
{
SetValue(WebServerIPAddressKey, value);
webServerIPAddress = value;
}
}
}
public void AddDevice(ChromecastDevice device)
{
using (var sync = configLock.WriterLock())
{
devices[device.Id] = device;
SetValue(NameKey, device.Name, device.Id);
SetValue(IPAddressKey, device.DeviceIP.ToString(), device.Id);
SetValue(VolumeKey, device.Volume, device.Id);
SetValue(DeviceIds, devices.Keys.Aggregate((x, y) => x + DeviceIdsSeparator + y));
}
}
public void RemoveDevice(string deviceId)
{
using (var sync = configLock.WriterLock())
{
devices.Remove(deviceId);
if (devices.Count > 0)
{
SetValue(DeviceIds, devices.Keys.Aggregate((x, y) => x + DeviceIdsSeparator + y));
}
else
{
SetValue(DeviceIds, string.Empty);
}
HS.ClearINISection(deviceId, FileName);
}
}
public IPAddress CalculateServerIPAddress()
{
// if there is override address, use it
var overrideIPAddress = WebServerIPAddress;
if ((overrideIPAddress != null) && (!overrideIPAddress.Equals(IPAddress.Any)))
{
return overrideIPAddress;
}
var ipAddresses = GetIPv4Addresses(NetworkInterfaceType.Unknown, true, false);
var hsAddress = IPAddress.Parse(HS.GetIPAddress());
// if nothing is specified and hs address is in local addresses, us it
if (ipAddresses.Contains(hsAddress))
{
return hsAddress;
}
if (ipAddresses.Length == 0)
{
throw new IOException("No Local IP4 Address Found");
}
return ipAddresses.First();
}
private T GetValue<T>(string key, [AllowNull]T defaultValue)
{
return GetValue(key, defaultValue, DefaultSection);
}
private T GetValue<T>(string key, [AllowNull]T defaultValue, string section)
{
string stringValue = HS.GetINISetting(section, key, null, FileName);
if (stringValue != null)
{
try
{
T result = (T)System.Convert.ChangeType(stringValue, typeof(T), CultureInfo.InvariantCulture);
return result;
}
catch (Exception)
{
return defaultValue;
}
}
return defaultValue;
}
private void SetValue<T>(string key, [AllowNull]T value)
{
SetValue<T>(key, value, DefaultSection);
}
private void SetValue<T>(string key, [AllowNull]T value, string section)
{
string stringValue = System.Convert.ToString(value, CultureInfo.InvariantCulture);
HS.SaveINISetting(section, key, stringValue, FileName);
}
/// <summary>
/// Fires event that configuration changed.
/// </summary>
public void FireConfigChanged()
{
if (ConfigChanged != null)
{
var ConfigChangedCopy = ConfigChanged;
ConfigChangedCopy(this, EventArgs.Empty);
}
}
/// <summary>
/// Retrieves the local ip addresses.
/// </summary>
/// <param name="interfaceType">Type of the interface.</param>
/// <param name="skipTypeFilter">if set to <c>true</c> [skip type filter].</param>
/// <param name="includeLoopback">if set to <c>true</c> [include loopback].</param>
/// <returns>An array of local ip addresses</returns>
public static IPAddress[] GetIPv4Addresses(
NetworkInterfaceType interfaceType,
bool skipTypeFilter = false,
bool includeLoopback = false)
{
var addressList = new List<IPAddress>();
var interfaces = NetworkInterface.GetAllNetworkInterfaces()
.Where(ni =>
#if NET452
ni.IsReceiveOnly == false &&
#endif
(skipTypeFilter || ni.NetworkInterfaceType == interfaceType) &&
ni.OperationalStatus == OperationalStatus.Up)
.ToArray();
foreach (var networkInterface in interfaces)
{
var properties = networkInterface.GetIPProperties();
if (properties.GatewayAddresses.Any(g => g.Address.AddressFamily == AddressFamily.InterNetwork) ==
false)
continue;
addressList.AddRange(properties.UnicastAddresses
.Where(i => i.Address.AddressFamily == AddressFamily.InterNetwork)
.Select(i => i.Address));
}
if (includeLoopback || interfaceType == NetworkInterfaceType.Loopback)
addressList.Add(IPAddress.Loopback);
return addressList.ToArray();
}
private const string NameKey = "Name";
private const string DeviceIds = "DevicesIds";
private const string WebServerIPAddressKey = "WebServerIPAddress";
private const string DebugLoggingKey = "DebugLogging";
private const string ForwardSpeechKey = "FowardSpeech";
private const string WebServerPortKey = "WebServerPort";
private const string SapiVoiceKey = "SAPIVoice";
private readonly static string FileName = Invariant($"{Path.GetFileName(System.Reflection.Assembly.GetEntryAssembly().Location)}.ini");
private const string IPAddressKey = "IPAddress";
private const string VolumeKey = "Volume";
private const string DefaultSection = "Settings";
private const char DeviceIdsSeparator = '|';
private const char PortsEnabledSeparator = ',';
private readonly Dictionary<string, ChromecastDevice> devices = new Dictionary<string, ChromecastDevice>();
private readonly IHSApplication HS;
private ushort webServerPort;
private bool debugLogging;
private bool forwardSpeech;
private string sapiVoice;
private readonly AsyncReaderWriterLock configLock = new AsyncReaderWriterLock();
private IPAddress webServerIPAddress;
};
}