forked from HomeSeer/Sample-Plugin-CS
/
HSPI.cs
464 lines (406 loc) · 22.4 KB
/
HSPI.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HomeSeer.Jui.Types;
using HomeSeer.Jui.Views;
using HomeSeer.PluginSdk;
using HomeSeer.PluginSdk.Logging;
using HSPI_HomeSeerSamplePlugin.Features;
using Newtonsoft.Json;
namespace HSPI_HomeSeerSamplePlugin {
/// <inheritdoc cref="AbstractPlugin"/>
/// <summary>
/// The plugin class for HomeSeer Sample Plugin that implements the <see cref="AbstractPlugin"/> base class.
/// </summary>
/// <remarks>
/// This class is accessed by HomeSeer and requires that its name be "HSPI" and be located in a namespace
/// that corresponds to the name of the executable. For this plugin, "HomeSeerSamplePlugin" the executable
/// file is "HSPI_HomeSeerSamplePlugin.exe" and this class is HSPI_HomeSeerSamplePlugin.HSPI
/// <para>
/// If HomeSeer is unable to find this class, the plugin will not start.
/// </para>
/// </remarks>
// ReSharper disable once InconsistentNaming
public class HSPI : AbstractPlugin, WriteLogSampleActionType.IWriteLogActionListener {
// Local vars
// speaker client instance
private SpeakerClient _speakerClient;
#region Properties
/// <inheritdoc />
/// <remarks>
/// This ID is used to identify the plugin and should be unique across all plugins
/// <para>
/// This must match the MSBuild property $(PluginId) as this will be used to copy
/// all of the HTML feature pages located in .\html\ to a relative directory
/// within the HomeSeer html folder.
/// </para>
/// <para>
/// The relative address for all of the HTML pages will end up looking like this:
/// ..\Homeseer\Homeseer\html\HomeSeerSamplePlugin\
/// </para>
/// </remarks>
public override string Id { get; } = "HomeSeerSamplePlugin";
/// <inheritdoc />
/// <remarks>
/// This is the readable name for the plugin that is displayed throughout HomeSeer
/// </remarks>
public override string Name { get; } = "Sample Plugin";
/// <inheritdoc />
protected override string SettingsFileName { get; } = "HomeSeerSamplePlugin.ini";
#endregion
public HSPI() {
//Initialize the plugin
//Enable internal debug logging to console
LogDebug = true;
//Setup anything that needs to be configured before a connection to HomeSeer is established
// like initializing the starting state of anything needed for the operation of the plugin
//Such as initializing the settings pages presented to the user (currently saved state is loaded later)
InitializeSettingsPages();
//Or adding an event action or trigger type definition to the list of types supported by your plugin
ActionTypes.AddActionType(typeof(WriteLogSampleActionType));
TriggerTypes.AddTriggerType(typeof(SampleTriggerType));
}
/// <summary>
/// Initialize the starting state of the settings pages for the HomeSeerSamplePlugin.
/// This constructs the framework that the user configurable settings for the plugin live in.
/// Any saved configuration options are loaded later in <see cref="Initialize"/> using
/// <see cref="AbstractPlugin.LoadSettingsFromIni"/>
/// </summary>
/// <remarks>
/// For ease of use throughout the plugin, all of the view IDs, names, and values (non-volatile data)
/// are stored in the <see cref="HSPI_HomeSeerSamplePlugin.Constants.Settings"/> static class.
/// </remarks>
private void InitializeSettingsPages() {
//Initialize the first settings page
// This page is used to manipulate the behavior of the sample plugin
//Start a PageFactory to construct the Page
var settingsPage1 = PageFactory.CreateSettingsPage(Constants.Settings.SettingsPage1Id,
Constants.Settings.SettingsPage1Name);
//Add a LabelView to the page
settingsPage1.WithLabel(Constants.Settings.Sp1ColorLabelId, null,
Constants.Settings.Sp1ColorLabelValue);
//Create a list of ToggleViews using the keys and values stored in Constants.Settings.ColorMap as
// the IDs and Names respectively
var colorToggles = Constants.Settings.ColorMap
.Select(kvp => new ToggleView(kvp.Key, kvp.Value, true)
{ToggleType = EToggleType.Checkbox})
.ToList();
//Add a ViewGroup containing all of the ToggleViews to the page
settingsPage1.WithGroup(Constants.Settings.Sp1ColorGroupId,
Constants.Settings.Sp1ColorGroupName,
colorToggles);
//Create 2 ToggleViews for controlling the visibility of the other two settings pages
var pageToggles = new List<ToggleView> {
new ToggleView(Constants.Settings.Sp1PageVisToggle1Id, Constants.Settings.Sp1PageVisToggle1Name, true),
new ToggleView(Constants.Settings.Sp1PageVisToggle2Id, Constants.Settings.Sp1PageVisToggle2Name, true),
};
//Add a ViewGroup containing all of the ToggleViews to the page
settingsPage1.WithGroup(Constants.Settings.Sp1PageToggleGroupId,
Constants.Settings.Sp1PageToggleGroupName,
pageToggles);
//Add the first page to the list of plugin settings pages
Settings.Add(settingsPage1.Page);
//Initialize the second settings page
// This page is used to visually demonstrate all of the available JUI views except for InputViews.
// None of these views interact with the plugin and are merely for show.
//Start a PageFactory to construct the Page
var settingsPage2 = PageFactory.CreateSettingsPage(Constants.Settings.SettingsPage2Id,
Constants.Settings.SettingsPage2Name);
//Add a LabelView with a title to the page
settingsPage2.WithLabel(Constants.Settings.Sp2LabelWTitleId,
Constants.Settings.Sp2LabelWTitleName,
Constants.Settings.Sp2LabelWTitleValue);
//Add a LabelView without a title to the page
settingsPage2.WithLabel(Constants.Settings.Sp2LabelWoTitleId,
null,
Constants.Settings.Sp2LabelWoTitleValue);
//Add a toggle switch to the page
settingsPage2.WithToggle(Constants.Settings.Sp2SampleToggleId, Constants.Settings.Sp2SampleToggleName);
//Add a checkbox to the page
settingsPage2.WithCheckBox(Constants.Settings.Sp2SampleCheckBoxId, Constants.Settings.Sp2SampleCheckBoxName);
//Add a drop down select list to the page
settingsPage2.WithDropDownSelectList(Constants.Settings.Sp2SelectListId,
Constants.Settings.Sp2SelectListName,
Constants.Settings.Sp2SelectListOptions);
//Add a radio select list to the page
settingsPage2.WithRadioSelectList(Constants.Settings.Sp2RadioSlId,
Constants.Settings.Sp2RadioSlName,
Constants.Settings.Sp2SelectListOptions);
//Add the second page to the list of plugin settings pages
Settings.Add(settingsPage2.Page);
//Initialize the third settings page
// This page is used to visually demonstrate the different types of JUI InputViews.
//Start a PageFactory to construct the Page
var settingsPage3 = PageFactory.CreateSettingsPage(Constants.Settings.SettingsPage3Id, Constants.Settings.SettingsPage3Name);
//Add a text InputView to the page
settingsPage3.WithInput(Constants.Settings.Sp3SampleInput1Id,
Constants.Settings.Sp3SampleInput1Name);
//Add a number InputView to the page
settingsPage3.WithInput(Constants.Settings.Sp3SampleInput2Id,
Constants.Settings.Sp3SampleInput2Name,
EInputType.Number);
//Add an email InputView to the page
settingsPage3.WithInput(Constants.Settings.Sp3SampleInput3Id,
Constants.Settings.Sp3SampleInput3Name,
EInputType.Email);
//Add a URL InputView to the page
settingsPage3.WithInput(Constants.Settings.Sp3SampleInput4Id,
Constants.Settings.Sp3SampleInput4Name,
EInputType.Url);
//Add a password InputView to the page
settingsPage3.WithInput(Constants.Settings.Sp3SampleInput5Id,
Constants.Settings.Sp3SampleInput5Name,
EInputType.Password);
//Add a decimal InputView to the page
settingsPage3.WithInput(Constants.Settings.Sp3SampleInput6Id,
Constants.Settings.Sp3SampleInput6Name,
EInputType.Decimal);
//Add the third page to the list of plugin settings pages
Settings.Add(settingsPage3.Page);
}
protected override void Initialize() {
//Load the state of Settings saved to INI if there are any.
LoadSettingsFromIni();
if (LogDebug) {
Console.WriteLine("Registering feature pages");
}
//Initialize feature pages
HomeSeerSystem.RegisterFeaturePage(Id, "sample-guided-process.html", "Sample Guided Process");
HomeSeerSystem.RegisterFeaturePage(Id, "sample-blank.html", "Sample Blank Page");
HomeSeerSystem.RegisterFeaturePage(Id, "sample-trigger-feature.html", "Trigger Feature Page");
HomeSeerSystem.RegisterDeviceIncPage(Id, "add-sample-device.html", "Add Sample Device");
// If a speaker client is needed that handles sending speech to an audio device, initialize that here.
// If you are supporting multiple speak devices such as multiple speakers, you would make this call
// in your reoutine that initializes each speaker device. Create a new instance of the speaker client
// for each speaker. We simply initalize one here as a sample implementation
_speakerClient = new SpeakerClient(Name);
// if the HS system has the setting "No password required for local subnet" enabled, the user/pass passed to Connect are ignored
// if the connection is from the local subnet, else the user/pass passed here are must exist as a user in the system
// You will need to allow the user to supply a user/pass in your plugin settings
// This functions connects your speaker client to the system. Your client will then appear as a speaker client in the system
// and can be selected as a target for speech and audio in event actions.
// When the system speaks to your client, your SpeakText function is called in SpeakerClient class
_speakerClient.Connect("default", "default");
Console.WriteLine("Initialized");
Status = PluginStatus.Ok();
}
protected override bool OnSettingChange(string pageId, AbstractView currentView, AbstractView changedView) {
//React to the toggles that control the visibility of the last 2 settings pages
if (changedView.Id == Constants.Settings.Sp1PageVisToggle1Id) {
//Make sure the changed view is a ToggleView
if (!(changedView is ToggleView tView)) {
return false;
}
//Show/Hide the second page based on the new state of the toggle
if (tView.IsEnabled) {
Settings.ShowPageById(Constants.Settings.SettingsPage2Id);
}
else {
Settings.HidePageById(Constants.Settings.SettingsPage2Id);
}
}
else if (changedView.Id == Constants.Settings.Sp1PageVisToggle2Id) {
//Make sure the changed view is a ToggleView
if (!(changedView is ToggleView tView)) {
return false;
}
//Show/Hide the second page based on the new state of the toggle
if (tView.IsEnabled) {
Settings.ShowPageById(Constants.Settings.SettingsPage3Id);
}
else {
Settings.HidePageById(Constants.Settings.SettingsPage3Id);
}
}
else {
if (LogDebug) {
Console.WriteLine($"View ID {changedView.Id} does not match any views on the page.");
}
}
return true;
}
/// <inheritdoc />
/// <remarks>
/// This plugin does not have a shifting operational state; so this method is not used.
/// </remarks>
protected override void BeforeReturnStatus() {}
/// <inheritdoc />
/// <remarks>
/// Process any HTTP POST requests targeting pages registered to your plugin.
/// <para>
/// This is a very flexible process that does not have a predefined structure. The form <see cref="data"/> sends
/// from a page is entirely up to you and what works for you. JSON and Base64 strings are encouraged because
/// of how readily available resources are to translate to/from these types. In Javascript, see JSON.stringify();
/// and window.btoa();
/// </para>
/// </remarks>
public override string PostBackProc(string page, string data, string user, int userRights) {
if (LogDebug) {
Console.WriteLine("PostBack");
}
var response = "";
switch(page) {
case "sample-trigger-feature.html":
//Handle the Trigger Feature page
try {
var triggerOptions = JsonConvert.DeserializeObject<List<bool>>(data);
//Get all triggers configured on the HomeSeer system that are of the SampleTriggerType
var configuredTriggers = HomeSeerSystem.GetTriggersByType(Name, SampleTriggerType.TriggerNumber);
if (configuredTriggers.Length == 0) {
return "No triggers configured to fire.";
}
//Handle each trigger that matches
foreach (var configuredTrigger in configuredTriggers) {
var trig = new SampleTriggerType(configuredTrigger, this, LogDebug);
if (trig.ShouldTriggerFire(triggerOptions.ToArray())) {
HomeSeerSystem.TriggerFire(Name, configuredTrigger);
}
}
}
catch (JsonSerializationException exception) {
if (LogDebug) {
Console.WriteLine(exception);
}
response = $"Error while deserializing data: {exception.Message}";
}
break;
case "sample-guided-process.html":
//Handle the Guided Process page
try {
var postData = JsonConvert.DeserializeObject<SampleGuidedProcessData>(data);
if (LogDebug) {
Console.WriteLine("Post back from sample-guided-process page");
}
response = postData.GetResponse();
}
catch (JsonSerializationException exception) {
if (LogDebug) {
Console.WriteLine(exception.Message);
}
response = "error";
}
break;
case "add-sample-device.html":
try {
var postData = JsonConvert.DeserializeObject<DeviceAddPostData>(data);
if (LogDebug) {
Console.WriteLine("Post back from add-sample-device page");
}
if (postData.Action == "verify") {
response = JsonConvert.SerializeObject(postData.Device);
}
else {
var deviceData = postData.Device;
var device = deviceData.BuildDevice(Id);
var devRef = HomeSeerSystem.CreateDevice(device);
deviceData.Ref = devRef;
response = JsonConvert.SerializeObject(deviceData);
}
}
catch (Exception exception) {
if (LogDebug) {
Console.WriteLine(exception.Message);
}
response = "error";
}
break;
default:
response = "error";
break;
}
return response;
}
/// <summary>
/// Called by the sample guided process feature page through a liquid tag to provide the list of available colors
/// <para>
/// {{plugin_function 'HomeSeerSamplePlugin' 'GetSampleSelectList' []}}
/// </para>
/// </summary>
/// <returns>The HTML for the list of select list options</returns>
public string GetSampleSelectList() {
if (LogDebug) {
Console.WriteLine("Getting sample select list for sample-guided-process page");
}
var sb = new StringBuilder("<select class=\"mdb-select md-form\" id=\"step3SampleSelectList\">");
sb.Append(Environment.NewLine);
sb.Append("<option value=\"\" disabled selected>Color</option>");
sb.Append(Environment.NewLine);
var colorList = new List<string>();
try {
var colorSettings = Settings["settings-page2"].GetViewById("settings-page2.colorgroup");
if (!(colorSettings is ViewGroup colorViewGroup)) {
throw new ViewTypeMismatchException("No View Group found containing colors");
}
foreach (var view in colorViewGroup.Views) {
if (!(view is ToggleView colorView)) {
continue;
}
colorList.Add(colorView.IsEnabled ? colorView.Name : "");
}
}
catch (Exception exception) {
if (LogDebug) {
Console.WriteLine(exception);
}
colorList = Constants.Settings.ColorMap.Values.ToList();
}
for (var i = 0; i < colorList.Count; i++) {
var color = colorList[i];
if (string.IsNullOrEmpty(color)) {
continue;
}
sb.Append("<option value=\"");
sb.Append(i);
sb.Append("\">");
sb.Append(color);
sb.Append("</option>");
sb.Append(Environment.NewLine);
}
sb.Append("</select>");
return sb.ToString();
}
/// <summary>
/// Called by the sample trigger feature page to get the HTML for a list of checkboxes to use a trigger options
/// <para>
/// {{list=plugin_function 'HomeSeerSamplePlugin' 'GetTriggerOptionsHtml' [2]}}
/// </para>
/// </summary>
/// <param name="numTriggerOptions">The number of checkboxes to generate</param>
/// <returns>
/// A List of HTML strings representing checkbox input elements
/// </returns>
public List<string> GetTriggerOptionsHtml(int numTriggerOptions) {
var triggerOptions = new List<string>();
for (var i = 1; i <= numTriggerOptions; i++) {
var cbTrigOpt = new ToggleView($"checkbox-triggeroption{i}", $"Trigger Option {i}")
{
ToggleType = EToggleType.Checkbox
};
triggerOptions.Add(cbTrigOpt.ToHtml());
}
return triggerOptions;
}
/// <summary>
/// Called by the sample trigger feature page to get trigger option items as a list to populate HTML on the page.
/// <para>
/// {{list2=plugin_function 'HomeSeerSamplePlugin' 'GetTriggerOptions' [2]}}
/// </para>
/// </summary>
/// <param name="numTriggerOptions">The number of trigger options to generate.</param>
/// <returns>
/// A List of <see cref="TriggerOptionItem"/>s used for checkbox input HTML element IDs and Names
/// </returns>
public List<TriggerOptionItem> GetTriggerOption(int numTriggerOptions) {
var triggerOptions = new List<TriggerOptionItem>();
for (var i = 1; i <= numTriggerOptions; i++) {
triggerOptions.Add(new TriggerOptionItem(i, $"Trigger Option {i}"));
}
return triggerOptions;
}
/// <inheritdoc />
public void WriteLog(ELogType logType, string message) {
HomeSeerSystem.WriteLog(logType, message, Name);
}
}
}