wankeda
2025-03-13 a6a33f6916afbf1fc629baecb772939cda2ee981
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HslCommunication;
using HslCommunication.MQTT;
using Newtonsoft.Json;
using WIDESEA_Common.AGVEunm;
using WIDESEA_Model.Models;
namespace WIDESEA_Tasks
{
    public partial class AGVjob
    {
 
        public void MqttExample()
        {
 
            // 创建MQTT客户端
            HslCommunication.MQTT.MqttClient mqttClient = new HslCommunication.MQTT.MqttClient(new MqttConnectionOptions()
            {
                IpAddress = "192.168.2.51",
                Port = 1883,
                KeepAlivePeriod = TimeSpan.FromSeconds(100),
                CleanSession = true,
                //Credentials = new MqttCredential("admin", "123456"),   // 设置了用户名和密码
 
            });
            // 连接
            OperateResult connect = mqttClient.ConnectServer();
            if (connect.IsSuccess)
            {
                //MessageBox.Show("Success");
            }
            else
            {
                //MessageBox.Show("Failed");
            }
            Thread.Sleep(2000);
            //原材料库库报警灯
            var taskout = _taskService.GetTasksout();
            if (taskout != null)
            {
                OperateResult send = mqttClient.SubscribeMessage(new string[] { "/cmd/get/1" });
                load load = new load();
                {
                    load.func = "setkeyval";
                    load.key = "R";
                    load.value = 2;
                }
                var posy = JsonConvert.SerializeObject(load);
                // 测试发布
                OperateResult result = mqttClient.PublishMessage(new MqttApplicationMessage()
                {
                    Topic = "/cmd/get/1",                                                      // 主题
                    QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce,     // 消息等级
                    Payload = Encoding.UTF8.GetBytes(posy),       // 数据
                    Retain = false,                                                   // 是否保留
                });
                load1 load1 = new load1();
                {
                    load1.func = "setkeyval";
                    load1.key = "BU";
                    load1.value = 1;
                }
                var posy1 = JsonConvert.SerializeObject(load1);
                OperateResult result1 = mqttClient.PublishMessage(new MqttApplicationMessage()
                {
                    Topic = "/cmd/get/1",                                                      // 主题
                    QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce,     // 消息等级
                    Payload = Encoding.UTF8.GetBytes(posy1),       // 数据
                    Retain = false,                                                   // 是否保留
                });
            }
            else
            {
                load load = new load();
                {
                    load.func = "setkeyval";
                    load.key = "R";
                    load.value = 0;
                }
                var posy = JsonConvert.SerializeObject(load);
                // 测试发布
                OperateResult result = mqttClient.PublishMessage(new MqttApplicationMessage()
                {
                    Topic = "/cmd/get/1",                                                      // 主题
                    QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce,     // 消息等级
                    Payload = Encoding.UTF8.GetBytes(posy),       // 数据
                    Retain = false,                                                   // 是否保留
                });
                load1 load1 = new load1();
                {
                    load1.func = "setkeyval";
                    load1.key = "BU";
                    load1.value = 0;
                }
                var posy1 = JsonConvert.SerializeObject(load1);
                OperateResult result1 = mqttClient.PublishMessage(new MqttApplicationMessage()
                {
                    Topic = "/cmd/get/1",                                                      // 主题
                    QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce,     // 消息等级
                    Payload = Encoding.UTF8.GetBytes(posy1),       // 数据
                    Retain = false,                                                   // 是否保留
                });
            }
            Thread.Sleep(2000);
            //成品库报警灯
            var taskin = _taskService.GetTasksoin();
            if (taskin != null)
            {
                OperateResult send1 = mqttClient.SubscribeMessage(new string[] { "/cmd/get/2" });//需换成品库订阅
                load load = new load();
                {
                    load.func = "setkeyval";
                    load.key = "R";
                    load.value = 2;
                }
                var posy = JsonConvert.SerializeObject(load);
                // 测试发布
                OperateResult result = mqttClient.PublishMessage(new MqttApplicationMessage()
                {
                    Topic = "/cmd/get/2",                                                      // 主题
                    QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce,     // 消息等级
                    Payload = Encoding.UTF8.GetBytes(posy),       // 数据
                    Retain = false,                                                   // 是否保留
                });
                load1 load1 = new load1();
                {
                    load1.func = "setkeyval";
                    load1.key = "BU";
                    load1.value = 1;
                }
                var posy1 = JsonConvert.SerializeObject(load1);
                OperateResult result1 = mqttClient.PublishMessage(new MqttApplicationMessage()
                {
                    Topic = "/cmd/get/2",                                                      // 主题
                    QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce,     // 消息等级
                    Payload = Encoding.UTF8.GetBytes(posy1),       // 数据
                    Retain = false,                                                   // 是否保留
                });
            }
            else
            {
                load load = new load();
                {
                    load.func = "setkeyval";
                    load.key = "R";
                    load.value = 0;
                }
                var posy = JsonConvert.SerializeObject(load);
                // 测试发布
                OperateResult result = mqttClient.PublishMessage(new MqttApplicationMessage()
                {
                    Topic = "/cmd/get/2",                                                      // 主题
                    QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce,     // 消息等级
                    Payload = Encoding.UTF8.GetBytes(posy),       // 数据
                    Retain = false,                                                   // 是否保留
                });
                load1 load1 = new load1();
                {
                    load1.func = "setkeyval";
                    load1.key = "BU";
                    load1.value = 0;
                }
                var posy1 = JsonConvert.SerializeObject(load1);
                OperateResult result1 = mqttClient.PublishMessage(new MqttApplicationMessage()
                {
                    Topic = "/cmd/get/2",                                                      // 主题
                    QualityOfServiceLevel = MqttQualityOfServiceLevel.AtMostOnce,     // 消息等级
                    Payload = Encoding.UTF8.GetBytes(posy1),       // 数据
                    Retain = false,                                                   // 是否保留
                });
            }
 
        }
        private HslCommunication.MQTT.MqttClient mqttClient = null;
 
        public partial class load
        {
            public string func { get; set; }
            public string key { get; set; }
            public int value { get; set; }
        }
        public partial class load1
        {
            public string func { get; set; }
            public string key { get; set; }
            public int value { get; set; }
        }
    }
}