-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWriteLogSampleActionType.vb
More file actions
236 lines (210 loc) · 8.57 KB
/
WriteLogSampleActionType.vb
File metadata and controls
236 lines (210 loc) · 8.57 KB
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
Imports HomeSeer.Jui.Views
Imports HomeSeer.PluginSdk.Events
Imports HomeSeer.PluginSdk.Logging
''' <summary>
''' A sample event action type that writes a message to the HomeSeer log
''' </summary>
Public Class WriteLogSampleActionType
Inherits AbstractActionType
''' <summary>
''' The name of this action in the list of available actions on the event page
''' </summary>
Private Const ActionName As String = "Sample Plugin Action - Write to Log"
''' <summary>
''' The ID of the instructions label view
''' </summary>
Private ReadOnly Property InstructionsLabelId As String
Get
Return $"{PageId}-instructlabel"
End Get
End Property
''' <summary>
''' The value of the instructions label view
''' </summary>
Private Const InstructionsLabelValue As String = "Write a message to the log with a type of..."
''' <summary>
''' The ID of the log type select list view
''' </summary>
Private ReadOnly Property LogTypeSelectListId As String
Get
Return $"{PageId}-logtypesl"
End Get
End Property
''' <summary>
''' The ID of the log message input view
''' </summary>
Private ReadOnly Property LogMessageInputId As String
Get
Return $"{PageId}-messageinput"
End Get
End Property
Private mvarlogTypeOptions As List(Of String)
''' <summary>
''' The available log types to select from
''' </summary>
'''
Private ReadOnly Property _logTypeOptions As List(Of String)
Get
If mvarlogTypeOptions Is Nothing Then
mvarlogTypeOptions = New List(Of String) From {
"Trace",
"Debug",
"Info",
"Warning",
"Error"
}
End If
Return mvarlogTypeOptions
End Get
End Property
''' <summary>
''' The interface bridge to HSPI that enables proper encapsulation of access to the HomeSeer System
''' </summary>
Private ReadOnly Property Listener As IWriteLogActionListener
Get
Return TryCast(ActionListener, IWriteLogActionListener)
End Get
End Property
''' <inheritdoc />
''' <remarks>
''' All action types must implement this constructor
''' </remarks>
Public Sub New(ByVal id As Integer, ByVal eventRef As Integer, ByVal dataIn As Byte(), ByVal inListener As ActionTypeCollection.IActionTypeListener, Optional ByVal debugLog As Boolean = False)
MyBase.New(id, eventRef, dataIn, inListener, debugLog)
End Sub
''' <inheritdoc />
''' <remarks>
''' All action types must implement this constructor
''' </remarks>
Public Sub New()
End Sub
''' <inheritdoc />
''' <remarks>
''' Return the name of the action type
''' </remarks>
Protected Overrides Function GetName() As String
Return ActionName
End Function
''' <inheritdoc />
''' <remarks>
''' This action type always starts with a single select list.
''' </remarks>
Protected Overrides Sub OnNewAction()
Dim confPage = InitNewConfigPage()
ConfigPage = confPage.Page
End Sub
''' <inheritdoc />
''' <remarks>
''' This action type is only fully configured once a message is specified in the input view
''' </remarks>
Public Overrides Function IsFullyConfigured() As Boolean
Select Case ConfigPage.ViewCount
Case 3
Dim inputView = TryCast(ConfigPage.GetViewById(LogMessageInputId), InputView)
Return (If(inputView?.Value?.Length, 0)) > 0
Case Else
Return False
End Select
End Function
''' <inheritdoc />
''' <remarks>
''' This is where we validate data entry and update the <see cref="AbstractActionType.ConfigPage"/>
''' so that it represents the next state it should be in for configuration.
''' </remarks>
Protected Overrides Function OnConfigItemUpdate(ByVal configViewChange As AbstractView) As Boolean
If configViewChange.Id <> LogTypeSelectListId Then
'When the ID being changed is not the log type select list, always save and continue.
' No more configuration is needed
Return True
End If
'Log Type selection change
Dim changedLogTypeSl As SelectListView = TryCast(configViewChange, SelectListView)
'Make sure the change is to a select list view
If changedLogTypeSl Is Nothing Then
Return False
End If
Dim currentLogTypeSl As SelectListView = TryCast(ConfigPage.GetViewById(LogTypeSelectListId), SelectListView)
'Make sure the target select list view casts correctly
If currentLogTypeSl Is Nothing Then
Return False
End If
If currentLogTypeSl.Selection = changedLogTypeSl.Selection Then
'If the selection didn't change then return false because the user may still need to supply a message
Return False
End If
'Initialize the new state of the page so it asks for a message
Dim newConfPage = InitConfigPageWithInput()
ConfigPage = newConfPage.Page
'Save the change to the log type select list
Return True
End Function
''' <inheritdoc />
Public Overrides Function GetPrettyString() As String
Dim selectList = TryCast(ConfigPage.GetViewById(LogTypeSelectListId), SelectListView)
Dim message = If(ConfigPage?.GetViewById(LogMessageInputId)?.GetStringValue(), "Error retrieving log message")
Return $"write the message '{message}' to the log with the type of {If(selectList?.GetSelectedOption(), "Unknown Selection")}"
End Function
''' <inheritdoc />
''' <remarks>
''' This will call to HSPI through the <see cref="IWriteLogActionListener"/> interface to write a log message
''' </remarks>
Public Overrides Function OnRunAction() As Boolean
Dim iLogType = If((TryCast(ConfigPage?.GetViewById(LogTypeSelectListId), SelectListView))?.Selection, 0)
Dim logType As ELogType
Select Case iLogType
Case 0
logType = ELogType.Trace
Case 1
logType = ELogType.Debug
Case 2
logType = ELogType.Info
Case 3
logType = ELogType.Warning
Case 4
logType = ELogType.[Error]
Case Else
logType = ELogType.Info
End Select
Dim message = If(ConfigPage?.GetViewById(LogMessageInputId)?.GetStringValue(), "Error retrieving log message")
Listener?.WriteLog(logType, message)
Return True
End Function
''' <inheritdoc />
''' <remarks>
''' This action type does not do anything with devices/features; so we should always return false here.
''' </remarks>
Public Overrides Function ReferencesDeviceOrFeature(ByVal devOrFeatRef As Integer) As Boolean
Return False
End Function
''' <summary>
''' Initialize a new ConfigPage for initial setup of the action where the user must select the log type.
''' </summary>
''' <returns>A <see cref="PageFactory"/> representing the new ConfigPage</returns>
Private Function InitNewConfigPage() As PageFactory
Dim confPage = PageFactory.CreateEventActionPage(PageId, ActionName)
confPage.WithLabel(InstructionsLabelId, Nothing, InstructionsLabelValue)
confPage.WithDropDownSelectList(LogTypeSelectListId, "Log Type", _logTypeOptions)
Return confPage
End Function
''' <summary>
''' Initialize a new ConfigPage so the user can supply a message to write to the log
''' </summary>
''' <returns>A <see cref="PageFactory"/> representing the new ConfigPage</returns>
Private Function InitConfigPageWithInput() As PageFactory
Dim confPage = InitNewConfigPage()
confPage.WithInput(LogMessageInputId, "Message")
Return confPage
End Function
''' <summary>
''' An interface bridge to HSPI that enables proper encapsulation of access to the HomeSeer System
''' </summary>
Interface IWriteLogActionListener
Inherits ActionTypeCollection.IActionTypeListener
''' <summary>
''' Write a log message. Called by <see cref="WriteLogSampleActionType"/>.
''' </summary>
''' <param name="logType">The <see cref="ELogType"/> of the message.</param>
''' <param name="message">The message to write to the log.</param>
Sub WriteLog(ByVal logType As ELogType, ByVal message As String)
End Interface
End Class