以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.net/bbs/index.asp)
--  专家坐堂  (http://foxtable.net/bbs/list.asp?boardid=2)
----  阿里云物联网  (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=184692)

--  作者:machle
--  发布时间:2022/12/23 0:10:00
--  阿里云物联网
阿里云物联网有个服务端订阅消息,里面设备上报的信息可以通过它转发到数据库。但是从.net设备接入,代码转成VB,然后再用狐表调用的话,难度比较大。老师们能不能帮帮忙,给一个可以直接用的代码。谢谢!
[此贴子已经被作者于2022/12/23 0:12:19编辑过]

--  作者:machle
--  发布时间:2022/12/23 0:14:00
--  回复:(machle)阿里云物联网
https://help.aliyun.com/document_detail/143598.html

从.net 转成VB.net ,谢谢!!!


Imports System
Imports System.Text
Imports Amqp
Imports Amqp.Sasl
Imports Amqp.Framing
Imports System.Threading
Imports System.Security.Cryptography.X509Certificates
Imports System.Net.Security
Imports System.Security.Cryptography

Namespace amqp
    Class MainClass
        Shared Host As String = "${YourHost}"
        Shared Port As Integer = 5671
        Shared AccessKey As String = "${YourAccessKey}"
        Shared AccessSecret As String = "${YourAccessSecret}"
        Shared consumerGroupId As String = "${YourConsumerGroupId}"
        Shared clientId As String = "${YourClientId}"
        Shared iotInstanceId As String = "${YourIotInstanceId}"
        Shared Count As Integer = 0
        Shared IntervalTime As Integer = 10000
        Shared address As Address

        Public Shared Sub Main(ByVal args As String())
            Dim timestamp As Long = GetCurrentMilliseconds()
            Dim param As String = "authId=" & AccessKey & "&timestamp=" & timestamp
            Dim userName As String = clientId & "|authMode=aksign,signMethod=hmacmd5,c,iotInstanceId=" & iotInstanceId & ",authId=" & AccessKey & ",timestamp=" & timestamp & "|"
            Dim password As String = doSign(param, AccessSecret, "HmacMD5")
            DoConnectAmqp(userName, password)
            Dim resetEvent As ManualResetEvent = New ManualResetEvent(False)
            resetEvent.WaitOne()
        End Sub

        Private Shared Sub DoConnectAmqp(ByVal userName As String, ByVal password As String)
            address = New Address(Host, Port, userName, password)
            Dim cf As ConnectionFactory = New ConnectionFactory()
            cf.SASL.Profile = SaslProfile.External
            cf.AMQP.IdleTimeout = 120000
            cf.AMQP.C
            cf.AMQP.HostName = "contoso.com"
            cf.AMQP.MaxFrameSize = 8 * 1024
            Dim connection = cf.CreateAsync(address).Result
            connection.AddClosedCallback(AddressOf ConnClosed)
            DoReceive(connection)
        End Sub

        Private Shared Sub DoReceive(ByVal connection As Connection)
            Dim session = New Session(connection)
            Dim receiver = New ReceiverLink(session, "queueName", Nothing)
            receiver.Start(20, Function(link, message)
                                   Dim messageId As Object = message.ApplicationProperties("messageId")
                                   Dim topic As Object = message.ApplicationProperties("topic")
                                   Dim body As String = Encoding.UTF8.GetString(CType(message.Body, Byte()))
                                   Console.WriteLine("receive message, topic=" & topic & ", messageId=" & messageId & ", body=" & body)
                                   link.Accept(message)
                               End Function)
        End Sub

        Private Shared Sub ConnClosed(ByVal __ As IAmqpObject, ByVal e As [Error])
            Console.WriteLine("ocurr error: " & e)

            If Count < 3 Then
                Count += 1
                Thread.Sleep(IntervalTime * Count)
            Else
                Thread.Sleep(120000)
            End If

            DoConnectAmqp(address.User, address.Password)
        End Sub

        Private Shared Function GetCert() As X509Certificate
            Dim certPath As String = Environment.CurrentDirectory & "/root.crt"
            Dim crt As X509Certificate = New X509Certificate(certPath)
            Return crt
        End Function

        Private Shared Function ValidateServerCertificate(ByVal sender As Object, ByVal certificate As X509Certificate, ByVal chain As X509Chain, ByVal sslPolicyErrors As SslPolicyErrors) As Boolean
            Return True
        End Function

        Private Shared Function GetCurrentMilliseconds() As Long
            Dim dt1970 As DateTime = New DateTime(1970, 1, 1)
            Dim current As DateTime = DateTime.Now
            Return CLng((current - dt1970).TotalMilliseconds)
        End Function

        Private Shared Function doSign(ByVal param As String, ByVal accessSecret As String, ByVal signMethod As String) As String
            Dim key As Byte() = Encoding.UTF8.GetBytes(accessSecret)
            Dim signContent As Byte() = Encoding.UTF8.GetBytes(param)
            Dim hmac = New HMACMD5(key)
            Dim hashBytes As Byte() = hmac.ComputeHash(signContent)
            Return Convert.ToBase64String(hashBytes)
        End Function
    End Class
End Namespace


--  作者:有点蓝
--  发布时间:2022/12/23 8:38:00
--  
Namespace amqp以及前面的代码去掉,然后把这些命名空间放入代码里,比如:

Shared address As Amqp.Sasl.Address \'这里是不是Amqp.Sasl我也不知道,只是举个例子,哪个类型是哪个命名空间的,自己去看sdk源码,或者到vs里打开dll查看。改完后把代码放到全局代码使用即可