Imports Microsoft.VisualBasic
Imports System
Imports System.Linq
Imports System.Text
Imports System.ComponentModel
Imports System.Collections.Generic
Imports System.Xml
Imports RabbitMQ.Client
Imports System.Diagnostics
Imports System.Reflection
Imports System.Configuration

Public Module SISVersion
    Public Sub SendSISVersion(_XML As System.Xml.XmlDocument, _BasicProperties As IBasicProperties)
        Dim _ReplyTo As String = _BasicProperties.ReplyTo
        Dim _RoutingKey As String = ""
        Dim _Excahnge As String = ""
        Dim _MessageBodyId As String = _XML.SelectSingleNode("JsonXML/id").InnerText.ToString
        If _ReplyTo.Contains("@") Then
            Dim _Data() As String = _ReplyTo.Split(New Char() {"@"})
            _RoutingKey = _Data(0)
            _Excahnge = _Data(1)
            If String.IsNullOrEmpty(_Excahnge) Then
                _Excahnge = ConfigurationManager.AppSettings("Exchange_Name")
            End If
        Else
            _RoutingKey = _ReplyTo
            _Excahnge = ConfigurationManager.AppSettings("Exchange_Name")
        End If

        Dim _factory As ConnectionFactory
        Dim _connection As IConnection
        Dim _channel As IModel

        _factory = New ConnectionFactory
        _factory.HostName = ConfigurationManager.AppSettings("Factory_HostName")
        _factory.UserName = ConfigurationManager.AppSettings("Factory_UserName")
        _factory.Password = ConfigurationManager.AppSettings("Factory_Password")
        _factory.VirtualHost = ConfigurationManager.AppSettings("Factory_VirtualHost")

        _connection = _factory.CreateConnection
        _channel = _connection.CreateModel

        Dim _IBasicProperties As IBasicProperties = _channel.CreateBasicProperties

        Dim _UserId As String = System.Configuration.ConfigurationManager.AppSettings("Factory_UserName")
        Dim _CorrelationId As String = _BasicProperties.CorrelationId
        Dim uTime As Integer = (DateTime.UtcNow - New DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds
        Dim _Expiration As String = "17200000"
        Dim _MessageID As String = Guid.NewGuid.ToString

        With _IBasicProperties
            .UserId = _UserId
            .Timestamp = New AmqpTimestamp(uTime)
            .MessageId = _MessageID
            .Headers = New Collections.Generic.Dictionary(Of String, Object)
            .CorrelationId = _CorrelationId
            .ContentType = "application/json"
            .ContentEncoding = "UTF-8"
            With .Headers
                .Add("mq3c-message-type", "MQ3CMessage_JSON_RPC")
                .Add("mq3clib-version", "1.0")
                .Add("mq3clib-appname", "SISBusiness")
                .Add("mq3clib-workername", "")
                .Add("mq3clib-workerid", "")
                .Add("mq3clib-environment", "testing/production")
                .Add("mq3clib-appversion", "1.0 RC")
                .Add("mq3clib-runtime", "")
                .Add("mq3clib-uniquid", Guid.NewGuid.ToString)
            End With
        End With

        Dim _MessageText As String = GetSISVersion(_MessageBodyId)

        _channel.BasicPublish(_Excahnge, _RoutingKey, _IBasicProperties, StrToByteArray(_MessageText))
    End Sub
    Public Function GetSISVersion(_MessageBodyId As String) As String
        Dim _Message As String
        _Message = "{""jsonrpc"": ""2.0"",""id"": """ + _MessageBodyId
        _Message += """,""result"": """
        Dim myFileVersionInfo As FileVersionInfo = FileVersionInfo.GetVersionInfo([Assembly].GetExecutingAssembly().Location)
        _Message += "ProductName: " + myFileVersionInfo.ProductName + "; "
        _Message += "FileVersion: " + myFileVersionInfo.FileVersion + "; "
        _Message += "ProductVersion: " + myFileVersionInfo.ProductVersion + "; "
        _Message += "OSVersion: " + Environment.OSVersion.VersionString + "; "
        _Message += "OSPlatform" + Environment.OSVersion.Platform.ToString + "; "
        _Message += "OSServicePack: " + Environment.OSVersion.ServicePack + ";"
        _Message += """,""MQ3CMessage_JSON_RPC"": {}}"

        Return _Message
    End Function
End Module
