paho架构_物联网架构成长之路(32)-SpringBoot集成MQTT客户端

1 usingMQTTClient.Model;2 usingMQTTnet;3 usingMQTTnet.Core;4 usingMQTTnet.Core.Client;5 usingNewtonsoft.Json;6 usingSystem;7 usingSystem.Collections.Generic;8 usingSystem.Text;9 usingSystem.Threading.Tasks;10 usingSystem.Windows.Forms;11

12 namespaceMQTTClient13 {14 public partial classMainPage : Form15 {16 publicMainPage()17 {18 InitializeComponent();19 init();20 }21 private voidinit()22 {23 txtusername.Text = "";24 txtpassword.Text = "";25 txtclientid.Text = "";26 txttopic.Text = "iot/UUID/device/devicepub/update";27 }28

29 IMqttClient client = null;30 private asyncTask ConnectMqttServerAsync()31 {32 if(client == null)33 {34 client = new MqttClientFactory().CreateMqttClient() asMqttClient;35 client.ApplicationMessageReceived +=mqttClientApplicationMessageReceived;36 client.Connected +=mqttClientConnected;37 client.Disconnected +=mqttClientDisconnected;38 }39 try

40 {41 awaitclient.DisconnectAsync();42 var option =getMQTTOption();43 awaitclient.ConnectAsync(option);44 }catch(Exception e)45 {46 Invoke((new Action(() =>

47 {48 lblStatus.Text = "连接服务器失败:" +e.Message;49 })));50 }51 }52 private void mqttClientDisconnected(objectsender, EventArgs e)53 {54 Invoke((new Action(() =>

55 {56 lblStatus.Text = "连接服务器失败: ERROR";57 })));58 }59 private void mqttClientConnected(objectsender, EventArgs e)60 {61 Invoke((new Action(() =>

62 {63 lblStatus.Text = "连接服务器成功";64 })));65 }66 private void mqttClientApplicationMessageReceived(objectsender, MqttApplicationMessageReceivedEventArgs e)67 {68 //本工具部收数据

69 throw newNotImplementedException();70 }71

72 private void btnconnect_Click(objectsender, EventArgs e)73 {74 Task.Run(async () => { awaitConnectMqttServerAsync(); });75 }76 private void btndisconnect_Click(objectsender, EventArgs e)77 {78 client.DisconnectAsync();79 }80 private void btnsendone_Click(objectsender, EventArgs e)81 {82 sendPayload();83 }84 private void btnsendts_Click(objectsender, EventArgs e)85 {86 timer1.Interval =Convert.ToInt32(txttime.Text);87 timer1.Enabled = true;88 }89 private void btnstopts_Click(objectsender, EventArgs e)90 {91 timer1.Enabled = false;92 }93 private void timer1_Tick(objectsender, EventArgs e)94 {95 sendPayload();96 }97 private intsendPayload()98 {99 if (client.IsConnected == false)100 {101 return -1;102 }103 PayloadModel payload =getPayload();104 string json =JsonConvert.SerializeObject(payload, Formatting.Indented);105 txtview.Text =json;106 string topic =txttopic.Text;107 var msg = newMqttApplicationMessage(topic, Encoding.Default.GetBytes(json),108 MQTTnet.Core.Protocol.MqttQualityOfServiceLevel.AtMostOnce, false);109 client.PublishAsync(msg);110 lblSendStatus.Text = "发送:" +DateTime.Now.ToLongTimeString();111 return 0;112 }113

114 privateMqttClientTcpOptions getMQTTOption()115 {116 MqttClientTcpOptions option = newMqttClientTcpOptions();117 string hostname =txthostname.Text;118 string[] host_port = hostname.Split(':');119 int port = 1883;120 if(host_port.Length >= 2)121 {122 hostname = host_port[0];123 port = Convert.ToInt32(host_port[1]);124 }125 option.Server =hostname;126 option.ClientId =txtclientid.Text;127 option.UserName =txtusername.Text;128 option.Password =txtpassword.Text;129 option.Port =port;130 option.CleanSession = true;131 returnoption;132 }133

134 privatePayloadModel getPayload()135 {136 PayloadModel payload = newPayloadModel();137 //略

138 returnpayload;139 }140

141 Random rand1 = newRandom(System.DateTime.Now.Millisecond);142 private intgetRandomNum()143 {144 int data = rand1.Next(0, 100);145 returndata;146 }147

148 int linenum = 0;149 Random rand2 = newRandom(System.DateTime.Now.Millisecond);150 private intgetLineNum()151 {152 int f = rand2.Next(0, 100);153 int data = rand2.Next(0, 5);154 if(f % 2 == 1)155 {156 linenum +=data;157 }158 else

159 {160 linenum -=data;161 }162 returnlinenum;163 }164

165 }166 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值