MainWindow.xaml.cs 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using Microsoft.Win32;
  2. using MQTTnet;
  3. using MQTTnet.Server;
  4. using MqttServerConsoleApp;
  5. using Newtonsoft.Json;
  6. using Newtonsoft.Json.Linq;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Runtime.InteropServices;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using System.Windows;
  15. using System.Windows.Controls;
  16. using System.Windows.Data;
  17. using System.Windows.Documents;
  18. using System.Windows.Input;
  19. using System.Windows.Media;
  20. using System.Windows.Media.Imaging;
  21. using System.Windows.Navigation;
  22. using System.Windows.Shapes;
  23. namespace MqttWpfApp
  24. {
  25. /// <summary>
  26. /// Interaction logic for MainWindow.xaml
  27. /// </summary>
  28. public partial class MainWindow : Window
  29. {
  30. public MainWindow()
  31. {
  32. InitializeComponent();
  33. //var str = Convert.FromBase64String("A2cnAQRoVw==");
  34. byte[] bytes = new byte[] { 0x03, 0x67, 0x27, 0x01, 0x04, 0x68, 0x57 };
  35. IntPtr intPtr = ToolLibrary.IntPtrSwitch.ByteToIntPtr(bytes);
  36. string resStr = "";
  37. var res = TransferDllResolving.sumTest2(intPtr, bytes.Length,1,ref resStr);
  38. }
  39. private async void Button_Click(object sender, RoutedEventArgs e)
  40. {
  41. var mqttFactory = new MqttFactory();
  42. // The port for the default endpoint is 1883.
  43. // The default endpoint is NOT encrypted!
  44. // Use the builder classes where possible.
  45. var mqttServerOptions = new MqttServerOptionsBuilder().WithDefaultEndpoint().Build();
  46. var mqttServer = mqttFactory.CreateMqttServer(mqttServerOptions);
  47. await mqttServer.StartAsync();
  48. mqttServer.SubscribeAsync("ug67", "uplink");
  49. mqttServer.SubscribeAsync("ug67", "downlink/$deveui");
  50. mqttServer.InterceptingPublishAsync += MqttServer_InterceptingPublishAsync;
  51. this.start.IsEnabled = false;
  52. // Stop and dispose the MQTT server if it is no longer needed!
  53. //await mqttServer.StopAsync();
  54. }
  55. private Task MqttServer_InterceptingPublishAsync(InterceptingPublishEventArgs arg)
  56. {
  57. var str = Encoding.UTF8.GetString(arg.ApplicationMessage.Payload);
  58. Dispatcher.Invoke(() => {
  59. this.list.Items.Add(str);
  60. var json = JObject.Parse(str);
  61. var datastr = json["data"].ToString();
  62. var bytes = Convert.FromBase64String(datastr);
  63. });
  64. return Task.CompletedTask;
  65. }
  66. private void store_Click(object sender, RoutedEventArgs e)
  67. {
  68. SaveFileDialog sfd = new SaveFileDialog();
  69. //设置这个对话框的起始保存路径
  70. sfd.InitialDirectory = @"D:\";
  71. //设置保存的文件的类型,注意过滤器的语法
  72. sfd.Filter = "JSON文件|*.json|文件文件|*.txt";
  73. //调用ShowDialog()方法显示该对话框,该方法的返回值代表用户是否点击了确定按钮
  74. if (sfd.ShowDialog() == true)
  75. {
  76. var filename = sfd.FileName;
  77. using (StreamWriter sw = new StreamWriter(filename))
  78. {
  79. foreach (var item in this.list.Items)
  80. {
  81. sw.WriteLine(item.ToString());
  82. }
  83. }
  84. MessageBox.Show("保存成功");
  85. }
  86. else
  87. {
  88. MessageBox.Show("取消保存");
  89. }
  90. }
  91. }
  92. }