Public Class MessageProCrypto
    Private Declare Function WaitMessage Lib "user32.dll" () As Long
    Dim Session As NiApiLib.SrvrSession
    Dim Table As NiApiLib.Table

    Dim i, j, endTimer
    Dim connresult
    Dim disconn
    Dim tableName, FieldAcronym, Item, s As String

    Public Sub Wait(ByVal seconds)
        ' включение таймера
        '        Timer1.Enabled = True
        'установка интервала для таймера
        Timer1.Start()
        endTimer = 1
        Timer1.Interval = 1000 * seconds
        While endTimer = 1
            WaitMessage()
            Application.DoEvents()
        End While
        ' выключение таймера
        Timer1.Stop()
        '       Timer1.Enabled = False
    End Sub

    Private Sub Timer1_Timer()
        Timer1.Interval = 0
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        endTimer = 0
    End Sub

    Sub Session_OnLogin(ByVal LoginResult As Integer)
        lbConsole.Items.Add("Login with result " & LoginResult)
        lbConsole.SelectedIndex = lbConsole.Items.Count - 1
        connresult = LoginResult
        btnConnect.Enabled = False
        btnDisconnect.Enabled = True
    End Sub

    Sub Session_OnMessage(ByVal Message As Object)
        lbConsole.Items.Add(" Message( [" & Message.Count & "], " & Message.Command & ", " & Message.Value(1) & ")")
        lbConsole.SelectedIndex = lbConsole.Items.Count - 1
    End Sub

    Sub Session_OnStatusMessage(ByVal Message As Object)
        lbConsole.Items.Add(" StatusMessage( [" & Message.Code & "], " & Message.Class & ", " & Message.Description & ")")
        lbConsole.SelectedIndex = lbConsole.Items.Count - 1
    End Sub

    Sub Session_OnConnectionLost()
        lbConsole.Items.Add("Connection lost")
        lbConsole.SelectedIndex = lbConsole.Items.Count - 1
        btnConnect.Enabled = True
        btnDisconnect.Enabled = False
        Session.Dispose()
    End Sub

    Sub Table_OnData(ByVal Code As Integer, ByVal DataRowSet As Object, ByVal TableName As String)
        Dim Row As NiApiLib.DataRow
        lbConsole.Items.Add("OnData Received...")
        lbConsole.SelectedIndex = lbConsole.Items.Count - 1
        dgTable.RowCount = DataRowSet.Count
        Row = New NiApiLib.DataRow
        Row = DataRowSet.GetRow(0)
        For i = 0 To (DataRowSet.Count - 1)
            For j = 0 To (Row.Count - 1)
                FieldAcronym = Row.Structure.FieldAcronym(j)
                Row = DataRowSet.GetRow(i)
                Item = Row.Item(Row.Structure.FieldAcronym(j))
                dgTable.Item(FieldAcronym, i).Value = Item
            Next
        Next
    End Sub




    Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
        connresult = 1
        disconn = 0
        i = 0
        Session = New NiApiLib.SrvrSession
        Table = New NiApiLib.Table
        AddHandler Session.OnLogin, AddressOf Session_OnLogin
        AddHandler Session.OnConnectionLost, AddressOf Session_OnConnectionLost
        AddHandler Session.OnMessage, AddressOf Session_OnMessage
        AddHandler Session.OnStatusMessage, AddressOf Session_OnStatusMessage
        AddHandler Table.OnData, AddressOf Table_OnData

        Session.HostAddr = tbServer.Text
        Session.HostPort = tbPort.Text
        Session.Name = tbLogin.Text
        Session.Password = tbPassword.Text
        If tbJournal.Text <> "" Then
            Session.Journal = tbJournal.Text
        End If
        Session.CatalogName = "catalog.xml"
        If cbCrypto.Checked Then
            lbConsole.Items.Add("Calling CryptoStart")
            lbConsole.SelectedIndex = lbConsole.Items.Count - 1
            Session.CryptoStart("NiSecMP.dll", tbStorage.Text, tbUserCerID.Text, tbServerCerID.Text)
            lbConsole.Items.Add("CryptoStart executed")
            lbConsole.SelectedIndex = lbConsole.Items.Count - 1
        End If
        Session.Connect()
        Do While (connresult <> 0) And (i <= 10)
            lbConsole.Items.Add("Connection attempt " & i)
            lbConsole.SelectedIndex = lbConsole.Items.Count - 1
            Wait(1)
            i = i + 1
        Loop
        If (connresult <> 0) Then
            lbConsole.Items.Add("connection attempt failed. " & "I tried for " & (i - 1) & " times")
            lbConsole.SelectedIndex = lbConsole.Items.Count - 1
        Else
            Table.Open(Session, tbTableName.Text.ToUpper.Trim, NiApiLib.ETableType.tabAdrr)
            dgTable.Columns.Clear()
            For i = 0 To (Table.Structure.Count - 1)
                dgTable.Columns.Add(Table.Structure.FieldAcronym(i), Table.Structure.FieldDispName(i))
            Next
            Table.GetData()

        End If

    End Sub

    Private Sub btnDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisconnect.Click
        Session.Disconnect()
        Session.CryptoStop()
    End Sub

    Private Sub cbCrypto_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbCrypto.CheckedChanged
        If cbCrypto.Checked Then
            tbStorage.Enabled = True
            tbUserCerID.Enabled = True
            tbServerCerID.Enabled = True
        Else
            tbStorage.Enabled = False
            tbUserCerID.Enabled = False
            tbServerCerID.Enabled = False
        End If
    End Sub
End Class
