This repository has been archived by the owner on Nov 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PluginConfig.cs
280 lines (243 loc) · 8.46 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
using HomeSeerAPI;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading;
namespace Hspi
{
using static System.FormattableString;
/// <summary>
/// Class to store PlugIn Configuration
/// </summary>
/// <seealso cref="System.IDisposable"/>
internal class PluginConfig : IDisposable
{
public event EventHandler<EventArgs> ConfigChanged;
/// <summary>
/// Initializes a new instance of the <see cref="PluginConfig"/> class.
/// </summary>
/// <param name="HS">The homeseer application.</param>
public PluginConfig(IHSApplication HS)
{
this.HS = HS;
debugLogging = GetValue(DebugLoggingKey, false);
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))
{
deviceIP = IPAddress.None;
}
string name = GetValue(NameKey, string.Empty, deviceId);
string username = GetValue(UserNameKey, string.Empty, deviceId);
string passwordEncrypted = GetValue(PasswordKey, string.Empty, deviceId);
string password = HS.DecryptString(passwordEncrypted, EncryptPassword);
devices.Add(deviceId, new AirVisualNode(deviceId, name, deviceIP, username, password));
}
}
/// <summary>
/// Gets or sets the devices
/// </summary>
/// <value>
/// The API key.
/// </value>
public IReadOnlyDictionary<string, AirVisualNode> Devices
{
get
{
configLock.EnterReadLock();
try
{
return new Dictionary<string, AirVisualNode>(devices);
}
finally
{
configLock.ExitReadLock();
}
}
}
/// <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
{
configLock.EnterReadLock();
try
{
return debugLogging;
}
finally
{
configLock.ExitReadLock();
}
}
set
{
configLock.EnterWriteLock();
try
{
SetValue(DebugLoggingKey, value);
debugLogging = value;
}
finally
{
configLock.ExitWriteLock();
}
}
}
public void AddDevice(AirVisualNode device)
{
configLock.EnterWriteLock();
try
{
devices[device.Id] = device;
SetValue(NameKey, device.Name, device.Id);
SetValue(IPAddressKey, device.DeviceIP.ToString(), device.Id);
SetValue(UserNameKey, device.Username, device.Id);
SetValue(PasswordKey, HS.EncryptString(device.Password, EncryptPassword), device.Id);
SetValue(DeviceIds, devices.Keys.Aggregate((x, y) => x + DeviceIdsSeparator + y));
}
finally
{
configLock.ExitWriteLock();
}
}
public void RemoveDevice(string deviceId)
{
configLock.EnterWriteLock();
try
{
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);
}
finally
{
configLock.ExitWriteLock();
}
}
private T GetValue<T>(string key, T defaultValue)
{
return GetValue(key, defaultValue, DefaultSection);
}
private T GetValue<T>(string key, 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, T value)
{
SetValue<T>(key, value, DefaultSection);
}
private void SetValue<T>(string key, 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);
}
}
public static string GetUnits(DeviceType deviceType)
{
switch (deviceType)
{
case DeviceType.CO2:
return "PPM";
case DeviceType.Humidity:
return "%";
case DeviceType.PM25AQICN:
return string.Empty;
case DeviceType.PM25AQI:
return string.Empty;
case DeviceType.OutsidePM25AQICN:
return string.Empty;
case DeviceType.OutsidePM25AQI:
return string.Empty;
case DeviceType.PM10:
return "ug/m3";
case DeviceType.PM25:
return "ug/m3";
case DeviceType.TemperatureC:
return "C";
case DeviceType.TemperatureF:
return "F";
default:
return string.Empty;
}
}
#region IDisposable Support
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
configLock.Dispose();
}
disposedValue = true;
}
}
// This code added to correctly implement the disposable pattern.
public void Dispose()
{
Dispose(true);
}
#endregion IDisposable Support
private const string NameKey = "Name";
private const string UserNameKey = "Username";
private const string PasswordKey = "Password";
private const string DeviceIds = "DevicesIds";
private const string DebugLoggingKey = "DebugLogging";
private readonly static string FileName = Invariant($"{Path.GetFileName(System.Reflection.Assembly.GetEntryAssembly().Location)}.ini");
private const string IPAddressKey = "IPAddress";
private const string DefaultSection = "Settings";
private const string EncryptPassword = "Not sure what is more secure";
private const char DeviceIdsSeparator = '|';
private readonly Dictionary<string, AirVisualNode> devices = new Dictionary<string, AirVisualNode>();
private readonly IHSApplication HS;
private bool debugLogging;
private bool disposedValue;
private readonly ReaderWriterLockSlim configLock = new ReaderWriterLockSlim();
};
}