-
Notifications
You must be signed in to change notification settings - Fork 1
/
modNanoprobes.vb
61 lines (57 loc) · 2.85 KB
/
modNanoprobes.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
Module modNanoprobes
Public Function ParseData(ByVal strNanoData As String)
Dim inputData() = strNanoData.Split(" ")
Dim intIndex As Integer = 0
While intIndex < inputData.Length
Select Case inputData(intIndex)
Case "NanoAPI"
My.Application.Log.WriteEntry("Nanoprobe API Version: " & inputData(intIndex + 1))
Case "TemperatureC"
My.Application.Log.WriteEntry("Temperature: " & inputData(intIndex + 1) & " C")
Case "RelativeH%"
My.Application.Log.WriteEntry("Relative Humity: " & inputData(intIndex + 1) & "%")
End Select
intIndex += 2
End While
Return 0
End Function
Public Function ReadNanoprobe(ByVal strDestination As String)
If modGlobal.IsOnline = True Then
My.Application.Log.WriteEntry("Reading nanoprobe at " & strDestination, TraceEventType.Verbose)
Dim Req As System.Net.HttpWebRequest
Dim TargetUri As New Uri("http://" & strDestination)
Dim Output As System.Net.HttpWebResponse
Req = DirectCast(System.Net.HttpWebRequest.Create(TargetUri), System.Net.HttpWebRequest)
Req.UserAgent = "HAController/" & My.Application.Info.Version.ToString
Req.KeepAlive = False
Req.Timeout = 10000
Req.Proxy = Nothing
Req.ServicePoint.ConnectionLeaseTimeout = 10000
Req.ServicePoint.MaxIdleTime = 10000
Try
Output = Req.GetResponse()
Using ResStream As System.IO.Stream = Output.GetResponseStream()
Dim Reader As System.IO.StreamReader = New System.IO.StreamReader(ResStream)
Dim OutputStream As String = Reader.ReadToEnd()
My.Application.Log.WriteEntry("Nanoprobe Response: " & CStr(CInt(Output.StatusCode)) & " " & Output.StatusCode.ToString & " " & OutputStream, TraceEventType.Verbose)
ParseData(OutputStream)
End Using
Output.Close()
Return "OK"
Catch WebEx As System.Net.WebException
If WebEx.Response IsNot Nothing Then
Using ResStream As System.IO.Stream = WebEx.Response.GetResponseStream()
Dim Reader As System.IO.StreamReader = New System.IO.StreamReader(ResStream)
Dim OutputStream As String = Reader.ReadToEnd()
My.Application.Log.WriteEntry("Nanoprobe Error: " & OutputStream, TraceEventType.Error)
End Using
Else
My.Application.Log.WriteException(WebEx)
End If
Return "Failed"
End Try
Else
Return "Offline"
End If
End Function
End Module