-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.vb
120 lines (96 loc) · 4.21 KB
/
Main.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
Imports Scheduler
Imports HomeSeerAPI
Imports HSCF.Communication.Scs.Communication.EndPoints.Tcp
Imports HSCF.Communication.ScsServices.Client
Imports HSCF.Communication.ScsServices.Service
Module Main
Public WithEvents client As HSCF.Communication.ScsServices.Client.IScsServiceClient(Of IHSApplication)
Dim WithEvents clientCallback As HSCF.Communication.ScsServices.Client.IScsServiceClient(Of IAppCallbackAPI)
Private host As HomeSeerAPI.IHSApplication
Private gAppAPI As PluginAPI
Public plugin As New HSPI ' real plugin functions, user supplied
Public sIp As String = "127.0.0.1"
' Public ThermostatPlugin As Thermostat
Friend colTrigs_Sync As System.Collections.SortedList
Friend colTrigs As System.Collections.SortedList
Friend colActs_Sync As System.Collections.SortedList
Friend colActs As System.Collections.SortedList
Sub Main()
Dim argv As System.Collections.ObjectModel.ReadOnlyCollection(Of String)
argv = My.Application.CommandLineArgs
Dim sCmd As String
For Each sCmd In argv
Dim ch(0) As String
ch(0) = "="
Dim parts() As String = sCmd.Split(ch, StringSplitOptions.None)
Select Case parts(0).ToLower
Case "server" : sIp = parts(1)
Case "instance"
Try
Instance = parts(1)
Catch ex As Exception
Instance = ""
End Try
End Select
Next
gAppAPI = New PluginAPI
Console.WriteLine("Connecting to server at " & sIp & " instance " & Instance & "...")
client = ScsServiceClientBuilder.CreateClient(Of IHSApplication)(New ScsTcpEndPoint(sIp, 10400), gAppAPI)
clientCallback = ScsServiceClientBuilder.CreateClient(Of IAppCallbackAPI)(New ScsTcpEndPoint(sIp, 10400), gAppAPI)
Dim Attempts As Integer = 1
TryAgain:
Try
client.Connect()
clientCallback.Connect()
host = client.ServiceProxy
Dim APIVersion As Double = host.APIVersion ' will cause an error if not really connected
callback = clientCallback.ServiceProxy
APIVersion = callback.APIVersion ' will cause an error if not really connected
Catch ex As Exception
Console.WriteLine("Cannot connect attempt " & Attempts.ToString & ": " & ex.Message)
If ex.Message.ToLower.Contains("timeout occurred.") Then
Attempts += 1
If Attempts < 6 Then GoTo TryAgain
End If
If client IsNot Nothing Then
client.Dispose()
client = Nothing
End If
If clientCallback IsNot Nothing Then
clientCallback.Dispose()
clientCallback = Nothing
End If
wait(4)
Return
End Try
Try
' connect to HS so it can register a callback to us
' host.Connect(IFACE_NAME, "")
host.Connect(IFACE_NAME, Instance)
' create the user object that is the real plugin, accessed from the pluginAPI wrapper
callback = callback
hs = host
OurInstanceFriendlyName = instance
Console.WriteLine("Connected, waiting to be initialized...")
Do
Threading.Thread.Sleep(30)
Loop While client.CommunicationState = HSCF.Communication.Scs.Communication.CommunicationStates.Connected And Not bShutDown
Console.WriteLine("Connection lost, exiting")
' disconnect from server for good here
client.Disconnect()
clientCallback.Disconnect()
wait(2)
End
Catch ex As Exception
Console.WriteLine("Cannot connect(2): " & ex.Message)
wait(2)
Return
End Try
End Sub
Private Sub client_Disconnected(ByVal sender As Object, ByVal e As System.EventArgs) Handles client.Disconnected
Console.WriteLine("Disconnected from server - client")
End Sub
Private Sub wait(ByVal secs As Integer)
Threading.Thread.Sleep(secs * 1000)
End Sub
End Module