-
Notifications
You must be signed in to change notification settings - Fork 4
/
SampleTriggerType.cs
260 lines (237 loc) · 11.1 KB
/
SampleTriggerType.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
using System;
using System.Collections.Generic;
using System.Linq;
using HomeSeer.Jui.Views;
using HomeSeer.PluginSdk.Events;
namespace HSPI_HomeSeerSamplePlugin {
/// <summary>
/// A sample event trigger type fired from the Trigger Feature page.
/// </summary>
public class SampleTriggerType : AbstractTriggerType {
/// <summary>
/// The 1 based index of this trigger type. Used for reference in this class.
/// </summary>
public const int TriggerNumber = 1;
/// <summary>
/// The name of this trigger in the list of available triggers on the event page
/// </summary>
private const string TriggerName = "Sample Trigger";
/// <summary>
/// The ID of the option count select list
/// </summary>
private string OptionCountSlId => $"{PageId}-optioncountsl";
/// <summary>
/// The name of the option count select list
/// </summary>
private const string OptionCountSlName = "Number of Options Checked";
/// <summary>
/// The ID of the required option select list
/// </summary>
private string OptionNumSlId => $"{PageId}-optionnumsl";
/// <summary>
/// The name of the required option select list
/// </summary>
private const string OptionNumSlName = "Required Option";
/// <summary>
/// Determine whether the trigger should fire based on the specified option selections
/// </summary>
/// <param name="triggerOptions">
/// A boolean array describing the state of the options when the trigger button was clicked
/// </param>
/// <returns>
/// TRUE if the trigger should fire,
/// FALSE if it shouldn't
/// </returns>
public bool ShouldTriggerFire(params bool[] triggerOptions) {
switch (SelectedSubTriggerIndex) {
case 0:
var numRequiredOptions = GetSelectedOptionCount() + 1;
return numRequiredOptions != 0 && triggerOptions.Count(triggerOption => triggerOption) == numRequiredOptions;
case 1:
var specificRequiredOption = GetSelectedSpecificOptionNum();
if (triggerOptions.Length < specificRequiredOption + 1) {
return false;
}
return triggerOptions[specificRequiredOption];
case 2:
return !triggerOptions.Any(triggerOption => triggerOption);
case 3:
return triggerOptions.Any(triggerOption => triggerOption);
default:
return false;
}
}
/// <inheritdoc />
public SampleTriggerType(TrigActInfo trigInfo, TriggerTypeCollection.ITriggerTypeListener listener, bool logDebug = false) : base(trigInfo, listener, logDebug) { }
/// <inheritdoc />
/// <remarks>
/// All trigger types must implement this constructor
/// </remarks>
public SampleTriggerType(int id, int eventRef, int selectedSubTriggerIndex, byte[] dataIn, TriggerTypeCollection.ITriggerTypeListener listener, bool logDebug = false) : base(id, eventRef, selectedSubTriggerIndex, dataIn, listener, logDebug) { }
/// <inheritdoc />
/// <remarks>
/// All trigger types must implement this constructor
/// </remarks>
public SampleTriggerType() { }
/// <inheritdoc />
/// <remarks>
/// This trigger type has 4 subtypes.
/// </remarks>
protected override List<string> SubTriggerTypeNames { get; set; } = new List<string>
{
"Button click with X options checked",
"Button click with specific option checked",
"Button click with no options checked",
"Button click with any options checked"
};
/// <inheritdoc />
/// <remarks>
/// Return the name of the trigger type
/// </remarks>
protected override string GetName() => TriggerName;
/// <inheritdoc />
/// <remarks>
/// This trigger type has 3 states, 2 of which require additional configuration.
/// </remarks>
protected override void OnNewTrigger() {
switch (SelectedSubTriggerIndex) {
case 0:
ConfigPage = InitializeXOptionsPage().Page;
break;
case 1:
ConfigPage = InitializeSpecificOptionPage().Page;
break;
default:
ConfigPage = InitializeDefaultPage().Page;
break;
}
}
/// <inheritdoc />
/// <remarks>
/// This trigger type has 3 states, 2 of which require additional configuration.
/// </remarks>
public override bool IsFullyConfigured() {
switch (SelectedSubTriggerIndex) {
case 0:
//Check to see if the input for the number of options is valid
return GetSelectedOptionCount() >= 0;
case 1:
//Check to see if the input for the required option is valid
return GetSelectedSpecificOptionNum() >= 0;
default:
//The last two sub trigger types do not require any additional configuration
return true;
}
}
/// <inheritdoc />
/// <remarks>
/// Because all of the available configuration options are select lists, no data validation is needed.
/// This means that we can always return true here so that all changes are saved.
/// </remarks>
protected override bool OnConfigItemUpdate(AbstractView configViewChange) {
return true;
}
/// <inheritdoc />
public override string GetPrettyString() {
switch (SelectedSubTriggerIndex) {
case 0:
try {
var optionCountSl = ConfigPage?.GetViewById(OptionCountSlId) as SelectListView;
return $"the button on the Sample Plugin Trigger Feature page is clicked and {(optionCountSl?.GetSelectedOption() ?? "???")} options are checked";
}
catch (Exception exception) {
if (LogDebug) {
Console.WriteLine(exception);
}
return "the button on the Sample Plugin Trigger Feature page is clicked and ??? options are checked";
}
case 1:
try {
var optionNumSl = ConfigPage?.GetViewById(OptionNumSlId) as SelectListView;
return $"the button on the Sample Plugin Trigger Feature page is clicked and option number {(optionNumSl?.GetSelectedOption() ?? "???")} is checked";
}
catch (Exception exception) {
if (LogDebug) {
Console.WriteLine(exception);
}
return "the button on the Sample Plugin Trigger Feature page is clicked and option number ??? is checked";
}
case 2:
return "the button the Sample Plugin Trigger Feature page is clicked and no options are checked";
default:
return "the button the Sample Plugin Trigger Feature page is clicked";
}
}
/// <inheritdoc />
/// <remarks>
/// This trigger type is never used as a condition; so we can always return true here so manual trigger fires
/// are always executed.
/// </remarks>
public override bool IsTriggerTrue(bool isCondition) => true;
/// <inheritdoc />
/// <remarks>
/// This trigger type does not do anything with devices/features; so we should always return false here.
/// </remarks>
public override bool ReferencesDeviceOrFeature(int devOrFeatRef) => false;
/// <summary>
/// Initialize a new <see cref="AbstractTriggerType.ConfigPage"/> for a trigger based on a number of options
/// selected.
/// </summary>
/// <returns>A <see cref="PageFactory"/> initialized for the first subtype</returns>
private PageFactory InitializeXOptionsPage() {
var cpf = InitializeDefaultPage();
cpf.WithDropDownSelectList(OptionCountSlId, OptionCountSlName, new[] {"1", "2", "3", "4"}.ToList());
return cpf;
}
/// <summary>
/// Initialize a new <see cref="AbstractTriggerType.ConfigPage"/> for a trigger based on a specific option
/// being selected.
/// </summary>
/// <returns>A <see cref="PageFactory"/> initialized for the second subtype</returns>
private PageFactory InitializeSpecificOptionPage() {
var cpf = InitializeDefaultPage();
cpf.WithDropDownSelectList(OptionNumSlId, OptionNumSlName, new[] {"1", "2", "3", "4"}.ToList());
return cpf;
}
/// <summary>
/// Initialize a new <see cref="AbstractTriggerType.ConfigPage"/> for a trigger with no additional configurations.
/// </summary>
/// <returns>A <see cref="PageFactory"/> initialized for the third or fourth subtype</returns>
private PageFactory InitializeDefaultPage() {
var cpf = PageFactory.CreateEventTriggerPage(PageId, TriggerName);
return cpf;
}
/// <summary>
/// Get the currently selected specific option index
/// </summary>
/// <returns>The index of the required specific option</returns>
private int GetSelectedSpecificOptionNum() {
try {
var optionNumSl = ConfigPage?.GetViewById(OptionNumSlId) as SelectListView;
return optionNumSl?.Selection ?? -1;
}
catch (Exception exception) {
if (LogDebug) {
Console.WriteLine(exception);
}
return -1;
}
}
/// <summary>
/// Get the currently selected required option count
/// </summary>
/// <returns>The number of options that must be selected</returns>
private int GetSelectedOptionCount() {
try {
var optionCountSl = ConfigPage?.GetViewById(OptionCountSlId) as SelectListView;
return (optionCountSl?.Selection ?? -1);
}
catch (Exception exception) {
if (LogDebug) {
Console.WriteLine(exception);
}
return -1;
}
}
}
}