Friend NotInheritable Class ClsConnexion

#Region "Shared"

    Private Const cFileName As String = "Connexions.xml"


    Private Shared msLstConnexion As List(Of ClsConnexion)


    Sub New()
        Dim lFileName As String = IO.Path.Combine(My.Application.Info.DirectoryPath, cFileName)

        msLstConnexion = Nothing

        If Not IO.File.Exists(lFileName) Then
            Try
                IO.File.WriteAllText(lFileName, My.Resources.NewFile, System.Text.Encoding.UTF8)
            Catch ex As Exception
                MessageBox.Show("Erreur lors de la création du fichier de la base de données : " + ex.Message, "Base de données", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Return
            End Try
        End If

        Dim lDocument As New Xml.XmlDocument

        Try
            lDocument.Load(lFileName)
        Catch ex As Exception
            MessageBox.Show("Erreur lors de l'ouverture du fichier de la base de données : " + ex.Message + vbCrLf + "Verifiez l'intégrité du fichier.", "Base de données", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return
        End Try

        msLstConnexion = New List(Of ClsConnexion)

        With lDocument.DocumentElement
            If .GetAttribute("version") <> "1.0" Then
                msLstConnexion = Nothing
                MessageBox.Show("Version de base de données incorrecte.", "Base de données", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Return
            End If

            For Each lNode As Xml.XmlNode In .ChildNodes
                If lNode.Name = "connexion" Then
                    Dim lConnexion As ClsConnexion = New ClsConnexion(lNode)
                End If
            Next
        End With
    End Sub


#End Region

#Region "non shared"


    Private mConnexion As Modbus.IAnimCom

    Private mName As String

    Private mCommentaires As String

    Private mDelegate As New Modbus.SendingFrame.DgtSent(AddressOf EvtSent)

    Private mSlaves As New List(Of ClsSlave)

    Private mFrames As New Dictionary(Of Modbus.Frame.ClsFrameModbusBase, Modbus.Frame.ClsFrameModbusBase)


    Public Sub New(ByVal pNode As Xml.XmlNode)

        '<connexion name="Nom" type="1" params="192.168.1.1:502">
        '<comments>Commentaires</comments>
        '<slave name="nom" privateID="1" publicID="5">

        With DirectCast(pNode, Xml.XmlElement)
            mName = .GetAttribute("name")
            Select Case DirectCast(CInt(.GetAttribute("type")), Modbus.IAnimCom.EConnexType)
                Case Modbus.IAnimCom.EConnexType.COM
                    mConnexion = New Modbus.Serie.MonitorMaster(.GetAttribute("params"))
                Case Modbus.IAnimCom.EConnexType.TCP
                    mConnexion = New Modbus.TCP.MonitorClient(.GetAttribute("params"))
                Case Else
                    mConnexion = Nothing
            End Select

            For Each lNode As Xml.XmlNode In .ChildNodes
                Select Case lNode.Name
                    Case "comments"
                        mCommentaires = lNode.InnerText
                    Case "slave"
                        mSlaves.Add(New ClsSlave(lNode))
                End Select
            Next
        End With

        If mConnexion IsNot Nothing Then
            mConnexion.Open()
        End If
    End Sub


    Private Function EvtSent(ByVal pSender As Modbus.IAnimCom, ByVal pFrameSent As Modbus.Frame.ClsFrameModbusBase, ByVal pFrameReceived As Modbus.Frame.ClsFrameModbusBase, ByVal pID As Integer) As Modbus.SendingFrame
        mFrames.Add(pFrameSent, pFrameReceived)
        Return Nothing
    End Function


    Public Function SendFrame(ByVal pFrame As Modbus.Frame.ClsFrameModbusBase, ByVal pPrio As Boolean) As Modbus.Frame.ClsFrameModbusBase
        Dim lSleep As Integer = IIf(pPrio, 50, 200)
        If mConnexion Is Nothing Then
            Return Nothing
        End If
        If (mConnexion.State And (Modbus.IAnimCom.EState.NotConnectable Or Modbus.IAnimCom.EState.NotConnectedConnectable)) <> Modbus.IAnimCom.EState.Unknow Then
            Return Nothing
        End If

        mConnexion.NonCyclicAdd(New Modbus.SendingFrame(pFrame, mDelegate, Modbus.SendingFrame.ESlaveLock.NoLock, IIF(pPrio, 0, 3)), IIF(pPrio, 0, 1))

        Do


        Loop While mFrames.ContainsKey(pFrame)

        If Not mFrames.TryGetValue(pFrame, pFrame) Then
#If DEBUG Then
            Stop
#End If
        End If

        Return pFrame
    End Function


#End Region

End Class
