123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- Imports uPLibrary.Networking.M2Mqtt
- Imports uPLibrary.Networking.M2Mqtt.Messages
- Imports System.Security.Cryptography.X509Certificates
- Imports System.IO
- Imports System.Text.RegularExpressions
- Imports Newtonsoft.Json
-
- Public Class 測試頁面
- ' MQTT 客戶端變數
- Dim client As MqttClient
- Dim endpoint As String = "a3kltpd88hr7qj-ats.iot.ap-southeast-2.amazonaws.com" ' AWS IoT 端點
- Dim port As Integer = 8883
- Dim topic As String = "gcmserver"
- Dim pfxCertPath As String
- Dim caCertPath As String
- Dim pfxPassword As String = "1234"
- Dim IDname As String = Guid.NewGuid().ToString()
- Dim dataStringArray As String()
- Public Class MyMessage
- Public Property message As String
- Public Property number As String
- Public Property id As String
- Public Property data As List(Of Integer)
- End Class
- Private Sub 測試頁面_Load(sender As Object, e As EventArgs) Handles MyBase.Load
- ' 取得專案路徑
- Dim basePath As String = AppDomain.CurrentDomain.BaseDirectory
-
- ' 設定根證書 (CA證書) 和 pfx 檔案 (包含裝置證書和私鑰)
- caCertPath = Path.Combine(basePath, "gcm_CA.pem")
- pfxCertPath = Path.Combine(basePath, "gcm_unity_cert.pfx")
-
- ' 嘗試建立 MQTT 連接
- Try
- ' 載入 pfx 檔案並指定密碼
- Dim clientCert As New X509Certificate2(pfxCertPath, pfxPassword)
-
- ' 建立 MQTT 客戶端
- client = New MqttClient(endpoint, port, True, New X509Certificate(caCertPath), clientCert, MqttSslProtocols.TLSv1_2)
-
- ' 連接到 AWS IoT
- client.Connect("VBClient")
- If client.IsConnected Then
- Console.WriteLine("已連接到 AWS IoT")
-
- ' 訂閱主題
- client.Subscribe(New String() {topic}, New Byte() {MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE})
- Timer1.Interval = 3000 ' 3 秒
- Timer1.Start()
- ' 處理接收到的消息
- AddHandler client.MqttMsgPublishReceived, AddressOf OnMessageReceived
-
- Else
- Console.WriteLine("無法連接到 AWS IoT")
- End If
-
- Catch ex As Exception
- Console.WriteLine("連接失敗: " & ex.Message)
- End Try
- End Sub
-
- ' 當收到消息時觸發
- Private Sub OnMessageReceived(sender As Object, e As MqttMsgPublishEventArgs)
- Dim str As String = System.Text.Encoding.UTF8.GetString(e.Message)
- ' 步驟 1: 確認 str 不為空
- If Not String.IsNullOrEmpty(str) Then
- ' 步驟 2: 使用 Newtonsoft.Json 解析 JSON 字串
- Try
- ' 將 JSON 字串轉換為 MyMessage 物件
- Dim myMessage As MyMessage = JsonConvert.DeserializeObject(Of MyMessage)(str)
-
- ' 步驟 3: 確認 id 不為空且 message 為 "return",且 id 與 IDname 匹配
- If Not String.IsNullOrEmpty(myMessage.id) AndAlso myMessage.message = "return" AndAlso myMessage.id = IDname Then
- Console.WriteLine("已接收消息: " & str)
-
- ' 步驟 4: 直接從 myMessage.data 中獲取數據並進行處理
- If myMessage.data.Count >= 6 Then
- ' 獲取數值並進行計算
- Dim value3 As Decimal = myMessage.data(3) / 10
- Dim value4 As Decimal = myMessage.data(4) / 100
- Dim value5 As Decimal = myMessage.data(5) / 100
-
- ' 更新 TextBox 的內容
- If TextBox1.InvokeRequired Then
- ' 使用 Invoke 呼叫主執行緒更新 UI
- TextBox1.Invoke(Sub()
- TextBox1.Text = value3.ToString()
- End Sub)
- Else
- ' 如果已經在主執行緒,直接更新
- TextBox1.Text = value3.ToString()
- End If
-
- If TextBox2.InvokeRequired Then
- TextBox2.Invoke(Sub()
- TextBox2.Text = (value4 + value5).ToString()
- End Sub)
- Else
- TextBox2.Text = (value4 + value5).ToString()
- End If
- Else
- Console.WriteLine("data 中的元素不足")
- End If
- Else
- Console.WriteLine("條件不符合")
- End If
- Catch ex As Exception
- Console.WriteLine("JSON 解析失敗: " & ex.Message)
- End Try
- Else
- Console.WriteLine("收到的消息為空")
- End If
- End Sub
- ' 按鈕點擊事件,發送消息到 AWS IoT
- Private Sub timer_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
- ' 檢查是否已經連接到 AWS IoT
- If client IsNot Nothing AndAlso client.IsConnected Then
- ' 發送消息到 AWS IoT
- Dim message As String = "{""message"": ""send"", ""number"": ""EL-00000001-GCM"", ""id"": """ & IDname & """}"
- client.Publish(topic, System.Text.Encoding.UTF8.GetBytes(message), MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE, False)
- Console.WriteLine("已發送消息: " & message)
- End If
- End Sub
- End Class
|