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; }
|
}
|
}
|
}
|