-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSafetyMonitorDriver.vb
More file actions
398 lines (278 loc) · 9.3 KB
/
Copy pathSafetyMonitorDriver.vb
File metadata and controls
398 lines (278 loc) · 9.3 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
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
Imports System
Imports System.IO
Imports System.Collections
Imports System.Runtime.InteropServices
Imports ASCOM.DeviceInterface
Imports ASCOM.Utilities
<Guid("0A886EE8-A907-4B11-8D7C-D615ECD1BF1D")>
<ClassInterface(ClassInterfaceType.None)>
<ComVisible(True)>
Public Class SafetyMonitor
Implements ISafetyMonitorV3
#Region "Driver Constants"
Friend Shared driverID As String =
"ASCOM.WeatherWatcher.SafetyMonitor"
Private Shared ReadOnly driverDescription As String =
"WeatherWatcher SafetyMonitor"
Friend Shared FileLocationProfileName As String =
"Data File"
Friend Shared TraceStateProfileName As String =
"Trace Level"
Friend Shared DataFileDefault As String =
"C:\ProgramData\WeatherWatcher2\weatherdata.txt"
Friend Shared TraceStateDefault As String =
"False"
#End Region
#Region "Private Variables"
Friend Shared DataFile As String
Friend Shared traceState As Boolean
Private connectedState As Boolean
Private utilities As Util
Private TL As TraceLogger
#End Region
#Region "Constructor"
Public Sub New()
ReadProfile()
TL = New TraceLogger("", "WeatherWatcher")
TL.Enabled = traceState
TL.LogMessage("Constructor",
"Starting initialization")
connectedState = False
utilities = New Util()
TL.LogMessage("Constructor",
"Initialization complete")
End Sub
#End Region
#Region "Connection"
Public Property Connected As Boolean _
Implements ISafetyMonitorV3.Connected
Get
TL.LogMessage("Connected Get",
connectedState.ToString())
Return connectedState
End Get
Set(value As Boolean)
If value = connectedState Then Return
connectedState = value
If value Then
TL.LogMessage("Connected Set",
"Connected")
Else
TL.LogMessage("Connected Set",
"Disconnected")
End If
End Set
End Property
Public ReadOnly Property Connecting As Boolean _
Implements ISafetyMonitorV3.Connecting
Get
Return False
End Get
End Property
Public Sub Connect() _
Implements ISafetyMonitorV3.Connect
Connected = True
End Sub
Public Sub Disconnect() _
Implements ISafetyMonitorV3.Disconnect
Connected = False
End Sub
#End Region
#Region "Safety"
Public ReadOnly Property IsSafe As Boolean _
Implements ISafetyMonitorV3.IsSafe
Get
Try
TL.LogMessage("IsSafe",
"Checking safe state")
If String.IsNullOrWhiteSpace(DataFile) Then
TL.LogMessage("IsSafe",
"Data file path is blank")
Return False
End If
If Not File.Exists(DataFile) Then
TL.LogMessage("IsSafe",
"Data file not found")
Return False
End If
Dim fileInfo As New FileInfo(DataFile)
Dim age As TimeSpan =
DateTime.Now - fileInfo.LastWriteTime
TL.LogMessage("IsSafe",
"File age = " &
age.TotalSeconds.ToString("F0") &
" seconds")
If age.TotalSeconds > 120 Then
TL.LogMessage("IsSafe",
"File is stale")
Return False
End If
Dim contents As String =
File.ReadAllText(DataFile).Trim()
If String.IsNullOrWhiteSpace(contents) Then
TL.LogMessage("IsSafe",
"File empty")
Return False
End If
Dim lastChar As Char =
contents(contents.Length - 1)
TL.LogMessage("IsSafe",
"Last character = " &
lastChar)
If lastChar = "0"c Then
TL.LogMessage("IsSafe",
"SAFE")
Return True
Else
TL.LogMessage("IsSafe",
"UNSAFE")
Return False
End If
Catch ex As Exception
TL.LogMessageCrLf("IsSafe Error",
ex.ToString())
Return False
End Try
End Get
End Property
#End Region
#Region "Common ASCOM Properties"
Public ReadOnly Property Name As String _
Implements ISafetyMonitorV3.Name
Get
Return "WeatherWatcher Safety Monitor"
End Get
End Property
Public ReadOnly Property Description As String _
Implements ISafetyMonitorV3.Description
Get
Return driverDescription
End Get
End Property
Public ReadOnly Property DriverInfo As String _
Implements ISafetyMonitorV3.DriverInfo
Get
Return "WeatherWatcher SafetyMonitor Driver"
End Get
End Property
Public ReadOnly Property DriverVersion As String _
Implements ISafetyMonitorV3.DriverVersion
Get
Return Reflection.Assembly.
GetExecutingAssembly().
GetName().
Version.
ToString(2)
End Get
End Property
Public ReadOnly Property InterfaceVersion As Short _
Implements ISafetyMonitorV3.InterfaceVersion
Get
Return 3
End Get
End Property
#End Region
#Region "Required ISafetyMonitorV3 Members"
Public Sub SetupDialog() _
Implements ISafetyMonitorV3.SetupDialog
Using f As New SetupDialogForm()
If f.ShowDialog() = Windows.Forms.DialogResult.OK Then
ReadProfile()
End If
End Using
End Sub
Public ReadOnly Property SupportedActions As ArrayList _
Implements ISafetyMonitorV3.SupportedActions
Get
Return New ArrayList()
End Get
End Property
Public Function Action(ActionName As String,
ActionParameters As String) As String _
Implements ISafetyMonitorV3.Action
Throw New ASCOM.ActionNotImplementedException(
"Action " & ActionName)
End Function
Public Sub CommandBlind(Command As String,
Optional Raw As Boolean = False) _
Implements ISafetyMonitorV3.CommandBlind
Throw New ASCOM.MethodNotImplementedException(
"CommandBlind")
End Sub
Public Function CommandBool(Command As String,
Optional Raw As Boolean = False) As Boolean _
Implements ISafetyMonitorV3.CommandBool
Throw New ASCOM.MethodNotImplementedException(
"CommandBool")
End Function
Public Function CommandString(Command As String,
Optional Raw As Boolean = False) As String _
Implements ISafetyMonitorV3.CommandString
Throw New ASCOM.MethodNotImplementedException(
"CommandString")
End Function
Public ReadOnly Property DeviceState As IStateValueCollection _
Implements ISafetyMonitorV3.DeviceState
Get
Dim states As New StateValueCollection()
states.Add(
New StateValue(
"IsSafe",
IsSafe))
Return states
End Get
End Property
#End Region
#Region "Dispose"
Public Sub Dispose() _
Implements ISafetyMonitorV3.Dispose
TL?.Dispose()
TL = Nothing
utilities?.Dispose()
utilities = Nothing
End Sub
#End Region
#Region "ASCOM Registration"
Private Shared Sub RegUnregASCOM(ByVal register As Boolean)
Using profile As New ASCOM.Utilities.Profile()
profile.DeviceType = "SafetyMonitor"
If register Then
profile.Register(
driverID,
driverDescription)
Else
profile.Unregister(
driverID)
End If
End Using
End Sub
<ComRegisterFunction()>
Public Shared Sub RegisterASCOM(ByVal t As Type)
RegUnregASCOM(True)
End Sub
<ComUnregisterFunction()>
Public Shared Sub UnregisterASCOM(ByVal t As Type)
RegUnregASCOM(False)
End Sub
#End Region
#Region "Profile"
Friend Sub ReadProfile()
Using profile As New Profile()
profile.DeviceType = "SafetyMonitor"
traceState =
Convert.ToBoolean(
profile.GetValue(
driverID,
TraceStateProfileName,
"",
TraceStateDefault))
DataFile =
profile.GetValue(
driverID,
FileLocationProfileName,
"",
DataFileDefault)
End Using
End Sub
#End Region
End Class