Foxtable(狐表)用户栏目专家坐堂 → 阿里云物联网


  共有4558人关注过本帖树形打印复制链接

主题:阿里云物联网

帅哥哟,离线,有人找我吗?
machle
  1楼 | QQ | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:小狐 帖子:358 积分:4053 威望:0 精华:0 注册:2014/7/29 23:42:00
阿里云物联网  发帖心情 Post By:2022/12/23 0:10:00 [只看该作者]

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

 回到顶部
帅哥哟,离线,有人找我吗?
machle
  2楼 | QQ | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:小狐 帖子:358 积分:4053 威望:0 精华:0 注册:2014/7/29 23:42:00
回复:(machle)阿里云物联网  发帖心情 Post By:2022/12/23 0:14:00 [只看该作者]

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


 回到顶部
帅哥哟,离线,有人找我吗?
有点蓝
  3楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:111393 积分:567029 威望:0 精华:9 注册:2015/6/24 9:21:00
  发帖心情 Post By:2022/12/23 8:38:00 [只看该作者]

Namespace amqp以及前面的代码去掉,然后把这些命名空间放入代码里,比如:

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

 回到顶部