Mit dem Wissen das wir jetzt haben erstellen wir eine Klasse zu Kommunikation mit IPS.
Also fügen wir unserem Projekt eine neue Klasse hinzu. Diese Klasse nennen wir IPS_JSONRPC.
Als erstes brauchen wir einen Verweis auf „System.Web.Extensions“.
Dieser Verweis sollte ja noch bestehen! Für kommende Projekte ist
der natürlich zwingend erforderlich.
Oberhalb von „Public Class IPS_JSONRPC“ kommen die Imports:
Imports System.Net 'Namespace für HttpWeb
Imports System.Text 'Namespace für das Encoding
Imports System.IO 'Namespace für den Stream
Imports System.Web.Script.Serialization 'Namespace zum De/Serialisieren
In der Klasse kommen als erstes die Variablen die von "Aussen "erreichbar sein sollen.
'Deklaration
Public TimeStamp As Integer = 0
Public MSValidData As Boolean = False
Die Klasse hat zwei Funktionen. Als erstes kommt der " IPS_Request".
Public Function IPS_Request(ByVal IPS_IP As String, ByVal IPS_User As String, ByVal IPS_Password As String, ByVal IPS_Method As String, ByVal IPS_Params As String, ByVal SSL As Boolean) As Object
'Deklaration
Dim request As WebRequest
Dim response As WebResponse
Dim authInfo As String
Dim IPS_URI As String
Dim Send As String
Dim Rec As String
Dim jsDeserializer As New JavaScriptSerializer()
'Call-Back für SSL
ServicePointManager.ServerCertificateValidationCallback = New System.Net.Security.RemoteCertificateValidationCallback(AddressOf AcceptCert)
'erstellen der IPS-URI -> wenn SSL dann https
If SSL Then
IPS_URI = "https://" & IPS_IP & ":3777/api/"
Else
IPS_URI = "http://" & IPS_IP & ":3777/api/"
End If
'erstellen der Authorisierung
authInfo = IPS_User & ":" & IPS_Password
authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo))
'erstellen des Request Headers
request = WebRequest.Create(IPS_URI)
request.Headers("Authorization") = "Basic " & authInfo
request.Method = "Post"
'erstellen der JSON-RPC
'{"jsonrpc":"2.0","method":"IPS_RunScript","params":[10693],"id":"null"}
Send = "{" & Chr(34) & "jsonrpc" & Chr(34) & ":" & Chr(34) & "2.0" & Chr(34) & "," _
& Chr(34) & "method" & Chr(34) & ":" & Chr(34) & IPS_Method & Chr(34) & "," _
& Chr(34) & "params" & Chr(34) & ":[" & IPS_Params & "]," _
& Chr(34) & "id" & Chr(34) & ":" & Chr(34) & "null" & Chr(34) & "}"
Try
'Den request-Stream versenden
Dim StreamWriter As StreamWriter = New StreamWriter(request.GetRequestStream)
StreamWriter.Write(Send)
StreamWriter.Close()
'Den response-Stream empfangen
response = request.GetResponse
Dim StreamReader As StreamReader = New StreamReader(response.GetResponseStream)
Rec = StreamReader.ReadToEnd
StreamReader.Close()
response.Close()
Dim IPS_JSResponse As IPS_Request_Response = jsDeserializer.Deserialize(Of IPS_Request_Response)(Rec)
IPS_Request = IPS_JSResponse.result
Catch ex As Exception
IPS_Request = "IPS Comm. Fault"
End Try
End Function
Danach kommt die Funktion " IPS_MessageSink".
Public Function IPS_MessageSink(ByVal IPS_IP As String, ByVal IPS_User As String, ByVal IPS_Password As String, ByVal SSL As Boolean) As Object
'Deklaration
Dim request As WebRequest
Dim response As WebResponse
Dim authInfo As String
Dim IPS_URI As String
Dim Send As String
Dim Rec As String
Dim jsDeserializer As New JavaScriptSerializer()
'Call-Back für SSL
ServicePointManager.ServerCertificateValidationCallback = New System.Net.Security.RemoteCertificateValidationCallback(AddressOf AcceptCert)
'erstellen der IPS-URI -> wenn SSL dann https
If SSL Then
IPS_URI = "https://" & IPS_IP & ":3777/api/"
Else
IPS_URI = "http://" & IPS_IP & ":3777/api/"
End If
'erstellen der Authorisierung
authInfo = IPS_User & ":" & IPS_Password
authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo))
'erstellen des Request Headers
request = WebRequest.Create(IPS_URI)
request.Headers("Authorization") = "Basic " & authInfo
request.Method = "Post"
'erstellen der JSON-RPC
'{"jsonrpc":"2.0","method":"IPS_RunScript","params":[10693],"id":"null"}
Send = "{" & Chr(34) & "jsonrpc" & Chr(34) & ":" & Chr(34) & "2.0" & Chr(34) & "," _
& Chr(34) & "method" & Chr(34) & ":" & Chr(34) & "IPS_GetSnapshotChanges" & Chr(34) & "," _
& Chr(34) & "params" & Chr(34) & ":[" & TimeStamp & "]," _
& Chr(34) & "id" & Chr(34) & ":" & Chr(34) & "null" & Chr(34) & "}"
Try
'Den request-Stream versenden
Dim StreamWriter As StreamWriter = New StreamWriter(request.GetRequestStream)
StreamWriter.Write(Send)
StreamWriter.Close()
'Den response-Stream empfangen
response = request.GetResponse
Dim StreamReader As StreamReader = New StreamReader(response.GetResponseStream)
Rec = StreamReader.ReadToEnd
StreamReader.Close()
response.Close()
'Meldungsarray von IPS Deserialisieren
Dim IPS_JSResponse As IPS_SnapShot = jsDeserializer.Deserialize(Of IPS_SnapShot)(Rec)
'Bei einer Rückmeldung von IPS ist das Array gefüllt (mindestens ein Element)
If IPS_JSResponse.result.Length Then
'und kann ausgegeben werden
IPS_MessageSink = IPS_JSResponse.result
MSValidData = True
TimeStamp = IPS_JSResponse.result(IPS_JSResponse.result.Length - 1).TimeStamp
Else
IPS_MessageSink = vbNull
MSValidData = False
End If
Catch ex As Exception
IPS_MessageSink = vbNull
MSValidData = False
End Try
End Function
Danach kommt die Call-Back Funktion für den SSL - Betrieb.
'Wird benötigt für SSL (https)
Private Function AcceptCert(ByVal sender As Object, ByVal cert As System.Security.Cryptography.X509Certificates.X509Certificate, _
ByVal chain As System.Security.Cryptography.X509Certificates.X509Chain, _
ByVal errors As System.Net.Security.SslPolicyErrors) As Boolean
Return True
End Function
Unterhab der Klasse sind noch die Hilfsklassen zum Deserialisieren.
'Hilfsklasse zum Deserialisieren der Antwort
Public Class IPS_Request_Response
Private myresult As Object
Public Property result() As Object
Get
Return myresult
End Get
Set(ByVal value As Object)
myresult = value
End Set
End Property
Private myid As Object
Public Property id() As Object
Get
Return myid
End Get
Set(ByVal value As Object)
myid = value
End Set
End Property
Private myjsonrpc As String
Public Property jsonrpc() As String
Get
Return myjsonrpc
End Get
Set(ByVal value As String)
myjsonrpc = value
End Set
End Property
End Class
'Hilfsklasse zum Deserialisieren der Antwort
Public Class IPS_Result
Private myMessage As Integer
Public Property Message() As Integer
Get
Return myMessage
End Get
Set(ByVal value As Integer)
myMessage = value
End Set
End Property
Private mySenderID As Integer
Public Property SenderID() As Integer
Get
Return mySenderID
End Get
Set(ByVal value As Integer)
mySenderID = value
End Set
End Property
Private myTimeStamp As Integer
Public Property TimeStamp() As Integer
Get
Return myTimeStamp
End Get
Set(ByVal value As Integer)
myTimeStamp = value
End Set
End Property
Private myData As Object
Public Property Data() As Object
Get
Return myData
End Get
Set(ByVal value As Object)
myData = value
End Set
End Property
End Class
'Hilfsklasse zum Deserialisieren der Antwort
Public Class IPS_SnapShot
Private myresult As IPS_Result()
Public Property result() As IPS_Result()
Get
Return myresult
End Get
Set(ByVal value As IPS_Result())
myresult = value
End Set
End Property
Private myid As Object
Public Property id() As Object
Get
Return myid
End Get
Set(ByVal value As Object)
myid = value
End Set
End Property
Private myjsonrpc As String
Public Property jsonrpc() As String
Get
Return myjsonrpc
End Get
Set(ByVal value As String)
myjsonrpc = value
End Set
End Property
End Class
Einbinden der Klasse in unsere Form:
Nach „Public Class Form1“ kommt:
Dim IPS_Comm As New IPS_JSONRPC
Der Request:
Private Sub btSenden_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btSenden.Click
Receive.Text = IPS_Comm.IPS_Request(tbIPS_IP.Text, tbBenutzer.Text, tbPassword.Text, tbBefehl.Text, tbParameter.Text, cbSSL.Checked)
End Sub
Der MessageSink (Beispiel für eine Variable und Anzeige des letzten Zeitstempels):
Private Sub btStartStop_MessageSink_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btStartStop_MessageSink.Click
'MessageSink
Dim Result As Object = IPS_Comm.IPS_MessageSink(tbIPS_IP.Text, tbBenutzer.Text, tbPassword.Text, cbSSL.Checked)
'Bei validen Daten
If IPS_Comm.MSValidData Then
'umpacken in ein Array vom Typ IPS_Result
Dim IPS_MessageSink() As IPS_Result
IPS_MessageSink = Result
' Das Array durchlaufen
For Each msg As IPS_Result In IPS_MessageSink
'bei einer Variablenänderung
If msg.Message = 10603 Then
'bei einer bestimmten Variablen
If msg.SenderID = 23491 Then
'ablegen des "Neuwertes"
tbReceiveMS.Text = msg.Data(0)
End If
End If
Next
End If
'ablegen des aktuellen Zeitstempels
tbTimeStamp.Text = IPS_Comm.TimeStamp
End Sub
Das war’s!