-
Notifications
You must be signed in to change notification settings - Fork 1
/
modGlobal.vb
192 lines (165 loc) · 9.79 KB
/
modGlobal.vb
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
Imports System.IO
Imports System.Xml.Serialization
' modGlobal cannot be disabled, Disable and Enable methods will be skipped
Public Module modGlobal
Public HomeStatus As String
Public IsOnline As Boolean = True
Public DeviceCollection As New ArrayList
Sub LoadModules()
If My.Settings.Global_LoadModulesAsync = True Then
My.Application.Log.WriteEntry("Loading modules asynchronously")
Dim LoadModuleTasks As System.Collections.Concurrent.ConcurrentBag(Of Task) = New System.Collections.Concurrent.ConcurrentBag(Of Task)()
LoadModuleTasks.Add(Task.Run(Function() modDatabase.Load())) 'Dependencies: None
LoadModuleTasks.Add(Task.Run(Function() modPing.Load())) 'Dependencies: None
LoadModuleTasks.Add(Task.Run(Function() modSpeech.Load())) 'Dependencies: None
LoadModuleTasks.Add(Task.Run(Function() modMapQuest.Load())) 'Dependencies: None
LoadModuleTasks.Add(Task.Run(Function() modWolframAlpha.Load())) 'Dependencies: None
LoadModuleTasks.Add(Task.Run(Function() modDreamCheeky.Load())) 'Dependencies: None
LoadModuleTasks.Add(Task.Run(Function() modStreamDeck.Load())) 'Dependencies: None
LoadModuleTasks.Add(Task.Run(Function() modMusic.Load())) 'Dependencies: None
LoadModuleTasks.Add(Task.Run(Function() modPihole.Load())) 'Dependencies: None
LoadModuleTasks.Add(Task.Run(Function() modLibrary.Load())) 'Dependencies: None
Task.WaitAll(LoadModuleTasks.ToArray())
LoadModuleTasks.Add(Task.Run(Function() modComputer.Load())) 'Dependencies: Database
LoadModuleTasks.Add(Task.Run(Function() modMail.Load())) 'Dependencies: Database
LoadModuleTasks.Add(Task.Run(Function() modOpenWeatherMap.Load())) 'Dependencies: Database
LoadModuleTasks.Add(Task.Run(Function() modMatrixLCD.Load())) 'Dependencies: Speech
LoadModuleTasks.Add(Task.Run(Function() modGPS.Load())) 'Dependencies: Database
LoadModuleTasks.Add(Task.Run(Function() modSync.Load())) 'Dependencies: Database
Task.WaitAll(LoadModuleTasks.ToArray())
LoadModuleTasks.Add(Task.Run(Function() modInsteon.Load())) 'Dependencies: Database, Mail
Task.WaitAll(LoadModuleTasks.ToArray())
Else
My.Application.Log.WriteEntry("Loading modules")
modDatabase.Load() 'Dependencies: None
modPing.Load() 'Dependencies: None
modMail.Load() 'Dependencies: Database
modInsteon.Load() 'Dependencies: Database, Mail
modSpeech.Load() 'Dependencies: None
modOpenWeatherMap.Load() 'Dependencies: Database
modMatrixLCD.Load() 'Dependencies: Speech
modGPS.Load() 'Dependencies: Database
modMapQuest.Load() 'Dependencies: None
modWolframAlpha.Load() 'Dependencies: None
modDreamCheeky.Load() 'Dependencies: None
modStreamDeck.Load() 'Dependencies: None
modMusic.Load() 'Dependencies: None
modComputer.Load() 'Dependencies: None
modPihole.Load() 'Dependencies: None
modLibrary.Load() 'Dependencies: None
modSync.Load() 'Dependencies: None
End If
My.Application.Log.WriteEntry("Module loading complete")
End Sub
Sub SaveCollection()
Dim targetFile As New StreamWriter("C:\HAC\DeviceCollection.xml")
Dim serializedString As String = ""
serializedString = XmlSerializerHelper.Current.SerializeToXml(DeviceCollection, True)
targetFile.Write(serializedString)
targetFile.Close()
End Sub
Sub SetHomeStatus(ByVal ChangeHomeStatus As String)
' TODO: This could probably use some sort of change countdown with the scheduler
modGlobal.HomeStatus = ChangeHomeStatus
My.Application.Log.WriteEntry("Home status changed to " + HomeStatus)
My.Settings.Global_LastHomeStatus = HomeStatus
End Sub
Sub UnloadModules()
modMail.Unload()
modSync.Unload()
modOpenWeatherMap.Unload()
modPing.Unload()
modMatrixLCD.Unload()
modStreamDeck.Unload()
modMusic.Unload()
modGPS.Unload()
modInsteon.Unload()
modComputer.Unload()
modDatabase.Unload()
End Sub
Function CheckLogFileSize()
Dim LogFile As New System.IO.FileInfo(My.Settings.Global_LogFileURI)
My.Application.Log.WriteEntry("Log file is " & LogFile.Length & " bytes")
If LogFile.Length > (50 * 1024 * 1024) Then
My.Application.Log.WriteEntry("Log file exceeds 50 MB, consider clearing", TraceEventType.Warning)
End If
Return LogFile.Length
End Function
Function RunUpdate() As String
' HAController isn't fully functional in ClickOnce mode, this will almost never run
Dim info As System.Deployment.Application.UpdateCheckInfo = Nothing
If (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed) Then
My.Application.Log.WriteEntry("Application is a ClickOnce application", TraceEventType.Information)
Dim AD As System.Deployment.Application.ApplicationDeployment = System.Deployment.Application.ApplicationDeployment.CurrentDeployment
Try
info = AD.CheckForDetailedUpdate()
Catch DDEExcep As System.Deployment.Application.DeploymentDownloadException
My.Application.Log.WriteException(DDEExcep)
Return "Cannot download update at this time"
Catch IOEExcep As InvalidOperationException
My.Application.Log.WriteException(IOEExcep)
Return "Cannot be updated"
End Try
If (info.UpdateAvailable) Then
modSpeech.Say("Update found", False)
Try
AD.Update()
Catch DDEExcep As System.Deployment.Application.DeploymentDownloadException
My.Application.Log.WriteException(DDEExcep)
Return "Cannot install update at this time"
End Try
modSpeech.Say("Restarting application", False)
Application.Restart()
Return " "
Else
My.Application.Log.WriteEntry("No update available")
Return "No update available"
End If
Else
My.Application.Log.WriteEntry("Application is not a ClickOnce application", TraceEventType.Information)
Dim UpdateDownloadClient As System.Net.WebClient = New System.Net.WebClient
Try
Dim strLatestVersion As String = UpdateDownloadClient.DownloadString(My.Settings.Global_UpdateCheckURI)
My.Application.Log.WriteEntry("Application version is " & My.Application.Info.Version.Revision.ToString & ". Latest version is " & strLatestVersion)
If My.Application.Info.Version.Revision.ToString = strLatestVersion Then
Return "No update available"
ElseIf CInt(My.Application.Info.Version.Revision.ToString) < CInt(strLatestVersion) Then
My.Application.Log.WriteEntry("Newer version available")
Try
modSpeech.Say("Downloading update", False)
My.Computer.Network.DownloadFile(My.Settings.Global_UpdateFileURI, My.Settings.Global_ScriptsFolderURI & "update.zip")
My.Application.Log.WriteEntry("Decompressing update", TraceEventType.Information)
System.IO.Directory.CreateDirectory(My.Settings.Global_ScriptsFolderURI & "temp")
System.IO.Compression.ZipFile.ExtractToDirectory(My.Settings.Global_ScriptsFolderURI & "update.zip", My.Settings.Global_ScriptsFolderURI & "temp")
System.IO.File.Delete(My.Settings.Global_ScriptsFolderURI & "update.zip")
modSpeech.Say("Restarting application", False)
modComputer.RunScript("updateoverwrite")
Return " "
Catch NetExcep As System.Net.WebException
My.Application.Log.WriteException(NetExcep)
Return "Unable to download updates"
End Try
Else
Return "Application version is newer than online."
End If
Catch NetExcep As System.Net.WebException
My.Application.Log.WriteException(NetExcep)
Return "Unable to check for updates"
End Try
End If
End Function
Function SendStatusReport() As String
Dim strStatusReport As String = "The current time is " & Now() & vbCrLf & vbCrLf & "Home monitoring status is set to " & modGlobal.HomeStatus & "." & vbCrLf & vbCrLf & "The inside temperature read " & My.Settings.Global_LastKnownInsideTemp & " F at " & My.Settings.Global_TimeThermostatLastUpdated.ToShortTimeString & " on " & My.Settings.Global_TimeThermostatLastUpdated.ToShortDateString & "."
If My.Settings.Insteon_ThermostatSlaveAddr <> "" Then
strStatusReport = strStatusReport & " (Second reading: " & My.Settings.Global_LastKnownInsideTemp2nd & " F)"
End If
If My.Settings.Global_TimeDoorLastOpened > DateTime.MinValue Then
strStatusReport = strStatusReport & vbCrLf & vbCrLf & "The door was last opened at " & My.Settings.Global_TimeDoorLastOpened.ToShortTimeString & " on " & My.Settings.Global_TimeDoorLastOpened.ToShortDateString
End If
If My.Settings.Pihole_Enable = True Then
strStatusReport = strStatusReport & vbCrLf & vbCrLf & modPihole.CheckPiholeStatus()
End If
modMail.Send("HAController Status Report", strStatusReport)
Return "Sending status report"
End Function
End Module