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
/
PlugIn.cs
229 lines (197 loc) · 7.76 KB
/
PlugIn.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
using Hspi.Connector;
using Hspi.DeviceData;
using NullGuard;
using Scheduler.Classes;
using System;
using System.Collections.Generic;
using System.Text;
namespace Hspi
{
using Hspi.Utils;
using static System.FormattableString;
/// <summary>
/// Plugin class for AirVisual Node
/// </summary>
/// <seealso cref="Hspi.HspiBase" />
[NullGuard(ValidationFlags.Arguments | ValidationFlags.NonPublic)]
internal class Plugin : HspiBase
{
public Plugin()
: base(PluginData.PlugInName, supportConfigDevice: true)
{
}
public override string InitIO(string port)
{
string result = string.Empty;
try
{
pluginConfig = new PluginConfig(HS);
configPage = new ConfigPage(HS, pluginConfig);
LogInfo("Starting Plugin");
#if DEBUG
pluginConfig.DebugLogging = true;
#endif
pluginConfig.ConfigChanged += PluginConfig_ConfigChanged;
RegisterConfigPage();
RestartConnections();
LogInfo("Plugin Started");
}
catch (Exception ex)
{
result = Invariant($"Failed to initialize PlugIn With {ExceptionHelper.GetFullMessage(ex)}");
LogError(result);
}
return result;
}
private void RestartConnections()
{
lock (connectorManagerLock)
{
// This returns a new copy every time
var currentDevices = pluginConfig.Devices;
// Update changed or new
foreach (var device in pluginConfig.Devices)
{
if (connectorManager.TryGetValue(device.Key, out var oldConnector))
{
if (!device.Value.Equals(oldConnector.Device))
{
oldConnector.Dispose();
connectorManager[device.Key] = new AirVisualNodeConnectorManager(HS, device.Value, ShutdownCancellationToken);
}
}
else
{
connectorManager.Add(device.Key, new AirVisualNodeConnectorManager(HS, device.Value, ShutdownCancellationToken));
}
}
// Remove deleted
List<string> removalList = new List<string>();
foreach (var deviceKeyPair in connectorManager)
{
if (!currentDevices.ContainsKey(deviceKeyPair.Key))
{
deviceKeyPair.Value.Dispose();
removalList.Add(deviceKeyPair.Key);
}
}
foreach (var key in removalList)
{
connectorManager.Remove(key);
}
}
}
private void PluginConfig_ConfigChanged(object sender, EventArgs e)
{
RestartConnections();
}
public override void LogDebug(string message)
{
if ((pluginConfig != null) && (pluginConfig.DebugLogging))
{
base.LogDebug(message);
}
}
public override string GetPagePlugin(string page, [AllowNull]string user, int userRights, [AllowNull]string queryString)
{
if (page == ConfigPage.Name)
{
return configPage.GetWebPage(queryString);
}
return string.Empty;
}
public override string PostBackProc(string page, string data, [AllowNull]string user, int userRights)
{
if (page == ConfigPage.Name)
{
return configPage.PostBackProc(data, user, userRights);
}
return string.Empty;
}
public override string ConfigDevice(int deviceId, [AllowNull] string user, int userRights, bool newDevice)
{
if (newDevice)
{
return string.Empty;
}
try
{
DeviceClass deviceClass = (DeviceClass)HS.GetDeviceByRef(deviceId);
var deviceIdentifier = DeviceIdentifier.Identify(deviceClass);
foreach (var device in pluginConfig.Devices)
{
if (device.Key == deviceIdentifier.DeviceId)
{
StringBuilder stb = new StringBuilder();
stb.Append(@"<table style='width:100%;border-spacing:0px;'");
stb.Append("<tr height='5'><td style='width:25%'></td><td style='width:20%'></td><td style='width:55%'></td></tr>");
stb.Append(Invariant($"<tr><td class='tablecell'>Name:</td><td class='tablecell' colspan=2>{device.Value.Name}</td></tr>"));
stb.Append(Invariant($"<tr><td class='tablecell'>Device IP:</td><td class='tablecell' colspan=2>{device.Value.DeviceIP}</td></tr>"));
stb.Append(Invariant($"<tr><td class='tablecell'>Type:</td><td class='tablecell' colspan=2>{EnumHelper.GetDescription(deviceIdentifier.DeviceType)}</td></tr>"));
stb.Append(Invariant($"</td><td></td></tr>"));
stb.Append("<tr height='5'><td colspan=3></td></tr>");
stb.Append(@"</table>");
return stb.ToString();
}
}
return string.Empty;
}
catch (Exception ex)
{
LogError(Invariant($"ConfigDevice for {deviceId} With {ExceptionHelper.GetFullMessage(ex)}"));
return string.Empty;
}
}
private void RegisterConfigPage()
{
string link = ConfigPage.Name;
HS.RegisterPage(link, Name, string.Empty);
HomeSeerAPI.WebPageDesc wpd = new HomeSeerAPI.WebPageDesc()
{
plugInName = Name,
link = link,
linktext = "Configuration",
page_title = Invariant($"{PluginData.PlugInName} Configuration"),
};
Callback.RegisterConfigLink(wpd);
Callback.RegisterLink(wpd);
}
/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
protected override void Dispose(bool disposing)
{
if (!disposedValue)
{
if (pluginConfig != null)
{
pluginConfig.ConfigChanged -= PluginConfig_ConfigChanged;
}
if (configPage != null)
{
configPage.Dispose();
}
if (pluginConfig != null)
{
pluginConfig.Dispose();
}
lock (connectorManagerLock)
{
foreach (var deviceKeyPair in connectorManager)
{
deviceKeyPair.Value.Dispose();
}
connectorManager.Clear();
}
disposedValue = true;
}
base.Dispose(disposing);
}
private readonly object connectorManagerLock = new object();
private readonly IDictionary<string, AirVisualNodeConnectorManager> connectorManager = new Dictionary<string, AirVisualNodeConnectorManager>();
private ConfigPage configPage;
private PluginConfig pluginConfig;
private bool disposedValue;
}
}