chore: 初始化合力驱动桥MES采集程序
This commit is contained in:
56
Common/00MesWorkMqtt/MesWorkMqtt/00MesWorkMqtt.csproj
Normal file
56
Common/00MesWorkMqtt/MesWorkMqtt/00MesWorkMqtt.csproj
Normal file
@@ -0,0 +1,56 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>{F55DA1A0-A982-4479-8C47-0D9A5AAF3E18}</ProjectGuid>
|
||||
<OutputType>Library</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>MesWorkMqtt</RootNamespace>
|
||||
<AssemblyName>00MesWorkMqtt</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<Deterministic>true</Deterministic>
|
||||
<TargetFrameworkProfile />
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>..\..\dll\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="MQTTnet">
|
||||
<HintPath>..\..\dll\MQTTnet.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="MQTTnet.Extensions.ManagedClient">
|
||||
<HintPath>..\..\dll\MQTTnet.Extensions.ManagedClient.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Configuration" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Net.Http" />
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Mqtt.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
</Project>
|
||||
275
Common/00MesWorkMqtt/MesWorkMqtt/Mqtt.cs
Normal file
275
Common/00MesWorkMqtt/MesWorkMqtt/Mqtt.cs
Normal file
@@ -0,0 +1,275 @@
|
||||
|
||||
using MQTTnet;
|
||||
using MQTTnet.Client;
|
||||
using MQTTnet.Client.Connecting;
|
||||
using MQTTnet.Client.Disconnecting;
|
||||
using MQTTnet.Client.Options;
|
||||
using MQTTnet.Client.Receiving;
|
||||
using MQTTnet.Extensions.ManagedClient;
|
||||
using MQTTnet.Formatter;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MesWorkMqtt
|
||||
{
|
||||
public class Mqtt
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public static IManagedMqttClient mqttClient = null;
|
||||
public static string MqttUrl = ConfigurationManager.AppSettings["MqttUrl"];
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public static string topic = "";
|
||||
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="uri"></param>
|
||||
/// <returns></returns>
|
||||
public static async Task StartAsync()
|
||||
{
|
||||
string uri = MqttUrl;
|
||||
//127.0.0.1:18083,123
|
||||
var array = uri.Split(new char[] { ':', ',' });
|
||||
string mqttUser = "MisDataFun";
|
||||
string mqttPassword = "";
|
||||
var options = new MqttClientOptions
|
||||
{
|
||||
ClientId = "MisDataFun-200170511",
|
||||
ProtocolVersion = MqttProtocolVersion.V311,
|
||||
ChannelOptions = new MqttClientTcpOptions { Server = array[0], Port = Convert.ToInt32(array[1]) },
|
||||
CleanSession = false,
|
||||
SessionExpiryInterval = 300,
|
||||
KeepAlivePeriod = TimeSpan.FromSeconds(20),
|
||||
Credentials = new MqttClientCredentials
|
||||
{
|
||||
Username = mqttUser,
|
||||
Password = Encoding.UTF8.GetBytes(mqttPassword)
|
||||
}
|
||||
};
|
||||
mqttClient = new MqttFactory().CreateManagedMqttClient();
|
||||
|
||||
//mqttClient.ConnectedHandler = new MqttClientConnectedHandlerDelegate(OnSubscriberConnected);
|
||||
//mqttClient.DisconnectedHandler = new MqttClientDisconnectedHandlerDelegate(OnSubscriberDisconnected);
|
||||
//mqttClient.ApplicationMessageReceivedHandler = new MqttApplicationMessageReceivedHandlerDelegate(OnSubscriberMessageReceived);
|
||||
await mqttClient.StartAsync(new ManagedMqttClientOptions { ClientOptions = options });
|
||||
topic = array[2];
|
||||
await mqttClient.SubscribeAsync(new MqttTopicFilter { Topic = topic });
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="publish_topic"></param>
|
||||
/// <param name="publish_msg"></param>
|
||||
/// <returns></returns>
|
||||
public static async Task Publish(string publish_topic, string publish_msg)
|
||||
{
|
||||
if (mqttClient != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
await mqttClient.PublishAsync(new MqttApplicationMessageBuilder().WithTopic(publish_topic).WithPayload(publish_msg).WithExactlyOnceQoS().Build());
|
||||
}
|
||||
catch (Exception err)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="publish_topic"></param>
|
||||
/// <param name="publish_msg"></param>
|
||||
/// <returns></returns>
|
||||
public static async Task PublishQos0(string publish_topic, string publish_msg)
|
||||
{
|
||||
if (mqttClient != null)
|
||||
{
|
||||
await mqttClient.PublishAsync(new MqttApplicationMessageBuilder().WithTopic(publish_topic).WithPayload(publish_msg).WithAtLeastOnceQoS().Build());
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static async Task StopAsync()
|
||||
{
|
||||
if (mqttClient != null)
|
||||
{
|
||||
await mqttClient.UnsubscribeAsync(new string[] { topic });
|
||||
await mqttClient.StopAsync();
|
||||
}
|
||||
}
|
||||
///// <summary>
|
||||
/////
|
||||
///// </summary>
|
||||
///// <param name="x"></param>
|
||||
//private static void OnSubscriberConnected(MqttClientConnectedEventArgs x)
|
||||
//{
|
||||
// var rec = x.AuthenticateResult.ResultCode.ToString();
|
||||
// // label_State.Text = rec;
|
||||
//}
|
||||
///// <summary>
|
||||
/////
|
||||
///// </summary>
|
||||
///// <param name="x"></param>
|
||||
//private static void OnSubscriberDisconnected(MqttClientDisconnectedEventArgs x)
|
||||
//{
|
||||
// //var rec = x.AuthenticateResult.ResultCode.ToString();
|
||||
// //label_State.Text = rec;
|
||||
//}
|
||||
///// <summary>
|
||||
/////
|
||||
///// </summary>
|
||||
///// <param name="x"></param>
|
||||
//private static void OnSubscriberMessageReceived(MqttApplicationMessageReceivedEventArgs x)
|
||||
//{
|
||||
// var rec = x.ApplicationMessage.ConvertPayloadToString();
|
||||
// // txt_Rec.Text = txt_Rec.Text + rec + System.Environment.NewLine;
|
||||
|
||||
//}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
public class Mqtt2
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public IManagedMqttClient mqttClient = null;
|
||||
public string MqttUrl;
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public string topic = "";
|
||||
|
||||
public Mqtt2()
|
||||
{
|
||||
mqttClient = new MqttFactory().CreateManagedMqttClient();
|
||||
}
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="uri"></param>
|
||||
/// <returns></returns>
|
||||
public async Task StartAsync()
|
||||
{
|
||||
string uri = MqttUrl;
|
||||
//127.0.0.1:18083,123
|
||||
var array = uri.Split(new char[] { ':', ',' });
|
||||
string mqttUser = "";
|
||||
string mqttPassword = "";
|
||||
var options = new MqttClientOptions
|
||||
{
|
||||
ClientId = Guid.NewGuid().ToString(),
|
||||
ProtocolVersion = MqttProtocolVersion.V311,
|
||||
ChannelOptions = new MqttClientTcpOptions { Server = array[0], Port = Convert.ToInt32(array[1]) },
|
||||
CleanSession = true,
|
||||
KeepAlivePeriod = TimeSpan.FromSeconds(100),
|
||||
Credentials = new MqttClientCredentials
|
||||
{
|
||||
Username = mqttUser,
|
||||
Password = Encoding.UTF8.GetBytes(mqttPassword)
|
||||
}
|
||||
};
|
||||
//mqttClient.ConnectedHandler = new MqttClientConnectedHandlerDelegate(OnSubscriberConnected);
|
||||
//mqttClient.DisconnectedHandler = new MqttClientDisconnectedHandlerDelegate(OnSubscriberDisconnected);
|
||||
//mqttClient.ApplicationMessageReceivedHandler = new MqttApplicationMessageReceivedHandlerDelegate(OnSubscriberMessageReceived);
|
||||
await mqttClient.StartAsync(new ManagedMqttClientOptions { ClientOptions = options });
|
||||
//topic = array[2];
|
||||
await mqttClient.SubscribeAsync(new MqttTopicFilter { Topic = topic });
|
||||
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="publish_topic"></param>
|
||||
/// <param name="publish_msg"></param>
|
||||
/// <returns></returns>
|
||||
public async Task Publish(string publish_topic, string publish_msg)
|
||||
{
|
||||
if (mqttClient != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
await mqttClient.PublishAsync(new MqttApplicationMessageBuilder().WithTopic(publish_topic).WithPayload(publish_msg).WithExactlyOnceQoS().Build());
|
||||
|
||||
}
|
||||
catch (Exception err)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
//static public async Task Publish(IManagedMqttClient mqttClient_1, string publish_topic, string publish_msg)
|
||||
//{
|
||||
// if (mqttClient_1 != null)
|
||||
// {
|
||||
// try
|
||||
// {
|
||||
// await mqttClient_1.PublishAsync(new MqttApplicationMessageBuilder().WithTopic(publish_topic).WithPayload(publish_msg).WithExactlyOnceQoS().Build());
|
||||
|
||||
// }
|
||||
// catch (Exception err)
|
||||
// {
|
||||
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task StopAsync()
|
||||
{
|
||||
if (mqttClient != null)
|
||||
{
|
||||
await mqttClient.UnsubscribeAsync(new string[] { topic });
|
||||
await mqttClient.StopAsync();
|
||||
}
|
||||
}
|
||||
///// <summary>
|
||||
/////
|
||||
///// </summary>
|
||||
///// <param name="x"></param>
|
||||
//private static void OnSubscriberConnected(MqttClientConnectedEventArgs x)
|
||||
//{
|
||||
// var rec = x.AuthenticateResult.ResultCode.ToString();
|
||||
// // label_State.Text = rec;
|
||||
//}
|
||||
///// <summary>
|
||||
/////
|
||||
///// </summary>
|
||||
///// <param name="x"></param>
|
||||
//private static void OnSubscriberDisconnected(MqttClientDisconnectedEventArgs x)
|
||||
//{
|
||||
// //var rec = x.AuthenticateResult.ResultCode.ToString();
|
||||
// //label_State.Text = rec;
|
||||
//}
|
||||
///// <summary>
|
||||
/////
|
||||
///// </summary>
|
||||
///// <param name="x"></param>
|
||||
//private static void OnSubscriberMessageReceived(MqttApplicationMessageReceivedEventArgs x)
|
||||
//{
|
||||
// var rec = x.ApplicationMessage.ConvertPayloadToString();
|
||||
// // txt_Rec.Text = txt_Rec.Text + rec + System.Environment.NewLine;
|
||||
|
||||
//}
|
||||
}
|
||||
}
|
||||
36
Common/00MesWorkMqtt/MesWorkMqtt/Properties/AssemblyInfo.cs
Normal file
36
Common/00MesWorkMqtt/MesWorkMqtt/Properties/AssemblyInfo.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// 有关程序集的一般信息由以下
|
||||
// 控制。更改这些特性值可修改
|
||||
// 与程序集关联的信息。
|
||||
[assembly: AssemblyTitle("MesWorkMqtt")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("MesWorkMqtt")]
|
||||
[assembly: AssemblyCopyright("Copyright © 2022")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// 将 ComVisible 设置为 false 会使此程序集中的类型
|
||||
//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型
|
||||
//请将此类型的 ComVisible 特性设置为 true。
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
|
||||
[assembly: Guid("f55da1a0-a982-4479-8c47-0d9a5aaf3e18")]
|
||||
|
||||
// 程序集的版本信息由下列四个值组成:
|
||||
//
|
||||
// 主版本
|
||||
// 次版本
|
||||
// 生成号
|
||||
// 修订号
|
||||
//
|
||||
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值
|
||||
//通过使用 "*",如下所示:
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
||||
Reference in New Issue
Block a user