V1.0 加入报警上传云端
This commit is contained in:
parent
c5b2c70d84
commit
2dc7184746
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -71,6 +71,24 @@ namespace LeatherApp
|
||||
//材质Material
|
||||
public static string[] SuedeList = new string[0];
|
||||
|
||||
//云端
|
||||
public static string cloud_ip, cloud_username, cloud_password, CloudThisName;
|
||||
public static int cloud_port, cloud_open;
|
||||
|
||||
public static void LoadCloudConfig()
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(appBasePath))
|
||||
appBasePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
|
||||
|
||||
IniFile ini = new IniFile(appBasePath + "\\CloudConfig.ini");
|
||||
|
||||
cloud_ip = ini.ReadString("Cloud", "cloud_ip", "");
|
||||
cloud_port = ini.ReadInt("Cloud", "cloud_port", 0);
|
||||
cloud_open = ini.ReadInt("Cloud", "cloud_open", 0);
|
||||
cloud_username = ini.ReadString("Cloud", "cloud_username", "");
|
||||
cloud_password = ini.ReadString("Cloud", "cloud_password", "");
|
||||
CloudThisName = ini.ReadString("Cloud", "CloudThisName", "");
|
||||
}
|
||||
public static void LoadAllConfig()
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(appBasePath))
|
||||
|
158
LeatherProject/LeatherApp/Device/CloudMgr.cs
Normal file
158
LeatherProject/LeatherApp/Device/CloudMgr.cs
Normal file
@ -0,0 +1,158 @@
|
||||
using MQTTnet;
|
||||
using MQTTnet.Client;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace LeatherApp.Device
|
||||
{
|
||||
public class CloudlInfo
|
||||
{
|
||||
public string ProductKey = "";
|
||||
public string DeviceName = "";
|
||||
public string DeviceSecret = "";
|
||||
public string clientId = "";
|
||||
public string username = "";
|
||||
public string mqttHostUrl = "";
|
||||
public string passwd = "";
|
||||
public int port = 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 云端控制
|
||||
/// </summary>
|
||||
public class CloudMgr
|
||||
{
|
||||
private IMqttClient mqttClient = null;
|
||||
private CloudlInfo cloudlInfo = new CloudlInfo();
|
||||
/// <summary>
|
||||
/// 连接服务器
|
||||
/// </summary>
|
||||
/// <param name="path"></param>
|
||||
/// <returns></returns>
|
||||
public bool ConnectCloud(string ip, int port, string username, string password)
|
||||
{
|
||||
bool ret = true;
|
||||
cloudlInfo.mqttHostUrl = ip;
|
||||
cloudlInfo.port = port;
|
||||
cloudlInfo.username = username;
|
||||
cloudlInfo.passwd = password;
|
||||
cloudlInfo.clientId = username;
|
||||
//连接
|
||||
var option = new MqttClientOptions();
|
||||
option.ClientId = cloudlInfo.clientId;
|
||||
|
||||
//设置服务器地址与端口
|
||||
option.ChannelOptions = new MqttClientTcpOptions()
|
||||
{
|
||||
Server = cloudlInfo.mqttHostUrl,
|
||||
Port = cloudlInfo.port
|
||||
};
|
||||
//设置账号与密码
|
||||
option.Credentials = new MqttClientCredentials(cloudlInfo.username, Encoding.Default.GetBytes(cloudlInfo.passwd));
|
||||
option.CleanSession = true;
|
||||
|
||||
//保持期
|
||||
option.KeepAlivePeriod = TimeSpan.FromSeconds(100.5);
|
||||
|
||||
//构建客户端对象
|
||||
mqttClient = new MqttFactory().CreateMqttClient() as MqttClient;
|
||||
try
|
||||
{
|
||||
//绑定消息接收方法
|
||||
mqttClient.ApplicationMessageReceivedAsync += _client_ApplicationMessageReceived;
|
||||
//绑定连接成功状态接收方法
|
||||
mqttClient.ConnectedAsync += _client_Connected;
|
||||
//绑定连接断开状态接收方法
|
||||
mqttClient.DisconnectedAsync += _client_Disconnected;
|
||||
|
||||
//启动连接
|
||||
mqttClient.ConnectAsync(option);
|
||||
}
|
||||
catch
|
||||
{
|
||||
Console.WriteLine($"{DateTime.Now.ToString("HH:mm:ss")} 连接失败");
|
||||
ret = false;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
/// <summary>
|
||||
/// 客户端与服务端断开连接
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private Task _client_Disconnected(MqttClientDisconnectedEventArgs e)
|
||||
{
|
||||
Console.WriteLine($"{DateTime.Now.ToString("HH:mm:ss")} Mqtt客户端连接断开");
|
||||
|
||||
if (mqttClient == null || mqttClient.IsConnected == false)
|
||||
{
|
||||
Thread.Sleep(25 * 1000);
|
||||
Console.WriteLine($"{DateTime.Now.ToString("HH:mm:ss")} Mqtt客户端重连...");
|
||||
mqttClient.ReconnectAsync();
|
||||
Thread.Sleep(5 * 1000);
|
||||
}
|
||||
if (mqttClient.IsConnected)
|
||||
Console.WriteLine($"{DateTime.Now.ToString("HH:mm:ss")} Mqtt客户端重连成功");
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 客户端与服务端建立连接
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
|
||||
private Task _client_Connected(MqttClientConnectedEventArgs e)
|
||||
{
|
||||
Console.WriteLine($"{DateTime.Now.ToString("HH:mm:ss")} Mqtt客户端连接成功.");
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 客户端收到消息
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private Task _client_ApplicationMessageReceived(MqttApplicationMessageReceivedEventArgs e)
|
||||
{
|
||||
Console.WriteLine($"{DateTime.Now.ToString("HH:mm:ss")} Mqtt客户端收到消息:{e.ApplicationMessage}");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private class WarningInfo
|
||||
{
|
||||
public WarningEnum Warn;
|
||||
public string WarningString;
|
||||
}
|
||||
/// <summary>
|
||||
/// 发送topic信息
|
||||
/// </summary>
|
||||
/// <param name="topic"></param>
|
||||
/// <param name="payload"></param>
|
||||
public bool SendTopic(string topic, string payload)
|
||||
{
|
||||
try
|
||||
{
|
||||
var applicationMessage = new MqttApplicationMessageBuilder()
|
||||
.WithTopic(topic)
|
||||
.WithPayload(payload)
|
||||
.Build();
|
||||
Task<MqttClientPublishResult> task = mqttClient.PublishAsync(applicationMessage);
|
||||
task.Wait();
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
//throw new Exception("云端连接错误");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -154,6 +154,7 @@ namespace LeatherApp
|
||||
Task.Run(() =>
|
||||
{
|
||||
//ShowProcessForm(200);//等待动画
|
||||
Config.LoadCloudConfig();
|
||||
Config.LoadAllConfig();
|
||||
API.OutputDebugString(Config.DBConStr);
|
||||
Service.InitDB.initDB(Config.DBConStr);
|
||||
|
@ -68,6 +68,9 @@
|
||||
<Reference Include="Irony, Version=1.0.11.0, Culture=neutral, PublicKeyToken=ca48ace7223ead47, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Irony.NetCore.1.0.11\lib\net461\Irony.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="MQTTnet, Version=4.3.3.952, Culture=neutral, PublicKeyToken=fdb7629f2e364a63, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\MQTTnet.4.3.3.952\lib\net48\MQTTnet.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="MvCodeReaderSDK.Net">
|
||||
<HintPath>..\..\DOC\皮革\C#\BasicDemo\bin\win64\MvCodeReaderSDK.Net.dll</HintPath>
|
||||
</Reference>
|
||||
@ -189,6 +192,7 @@
|
||||
<Compile Include="Device\CamerUtil\BufferToImage.cs" />
|
||||
<Compile Include="Device\CamerUtil\IKDevice.cs" />
|
||||
<Compile Include="Device\CamerUtil\IKDeviceCL.cs" />
|
||||
<Compile Include="Device\CloudMgr.cs" />
|
||||
<Compile Include="Device\PLCDev.cs" />
|
||||
<Compile Include="Device\PhotoLib.cs" />
|
||||
<Compile Include="Device\DefectLib.cs" />
|
||||
|
@ -53,6 +53,10 @@ namespace LeatherApp.Page
|
||||
private Hashtable htTask = new Hashtable();//默认单线程写入不用lock, 多线程安全同步读取用Synchronized
|
||||
//无产品编码时加载
|
||||
FProductInfo frmProduct;
|
||||
|
||||
//云端
|
||||
private CloudMgr cloudMgr = new CloudMgr();
|
||||
private bool init_Cloud;
|
||||
public FHome(FProductInfo frm)
|
||||
{
|
||||
InitializeComponent();
|
||||
@ -475,6 +479,15 @@ namespace LeatherApp.Page
|
||||
//}));
|
||||
//日志滚动
|
||||
//lstLog.SelectedIndex = lstLog.Items.Count - 1;
|
||||
|
||||
//开启云端
|
||||
if ((init_Cloud) && (level != WarningEnum.Normal))
|
||||
{
|
||||
//上传报警状态和信息
|
||||
string statusStr = level == WarningEnum.Normal ? "正常" : level == WarningEnum.Low ? "警告" : "系统报警";
|
||||
cloudMgr.SendTopic("device/attributes", $"{{\"status\": \"{statusStr}\", \"alm\": \"{tag}-{msg}\", " +
|
||||
$"\"name\": \"{Config.CloudThisName}\"}}");
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
@ -501,6 +514,29 @@ namespace LeatherApp.Page
|
||||
ucColorListDefect.initData(Config.defectItemList);
|
||||
this.lineChartDefect.SetOption(new UILineOption());
|
||||
this.lineChartFaceWidth.SetOption(new UILineOption());
|
||||
|
||||
//判断是否连接云端
|
||||
if(Config.cloud_open >0)
|
||||
{
|
||||
AddTextEvent(DateTime.Now, "设备启动", $"连接云端");
|
||||
if (cloudMgr.ConnectCloud(Config.cloud_ip, Config.cloud_port,
|
||||
Config.cloud_username, Config.cloud_password))
|
||||
{
|
||||
init_Cloud = true;
|
||||
AddTextEvent(DateTime.Now, "云端数据", $"开启云端连接");
|
||||
|
||||
//开启云端
|
||||
if (init_Cloud)
|
||||
{
|
||||
//上传报警状态和信息
|
||||
string statusStr = "正常" ;
|
||||
cloudMgr.SendTopic("device/attributes", $"{{\"status\": \"{statusStr}\", \"alm\": \"系统运行-正常启动软件\", " +
|
||||
$"\"name\": \"{Config.CloudThisName}\"}}");
|
||||
}
|
||||
}
|
||||
else
|
||||
AddTextEvent(DateTime.Now, "云端数据", "云端连接失败!", WarningEnum.Low);
|
||||
}
|
||||
}
|
||||
private void FHome_Shown(object sender, EventArgs e)
|
||||
{
|
||||
|
7
LeatherProject/LeatherApp/bin/Debug/CloudConfig.ini
Normal file
7
LeatherProject/LeatherApp/bin/Debug/CloudConfig.ini
Normal file
@ -0,0 +1,7 @@
|
||||
[Cloud]
|
||||
cloud_ip=47.101.145.32
|
||||
cloud_port=1883
|
||||
cloud_username=2cd8254d-f97e-b307-33ee-2eee669f7ccb
|
||||
cloud_password=
|
||||
cloud_open=1
|
||||
CloudThisName=HeXin1#
|
7
LeatherProject/LeatherApp/bin/Debug/CloudConfig2.ini
Normal file
7
LeatherProject/LeatherApp/bin/Debug/CloudConfig2.ini
Normal file
@ -0,0 +1,7 @@
|
||||
[Cloud]
|
||||
cloud_ip=47.101.145.32
|
||||
cloud_port=1883
|
||||
cloud_username=f2a031f7-9eff-192a-89cd-72932b80c966
|
||||
cloud_password=
|
||||
cloud_open=1
|
||||
CloudThisName=HeXin2#
|
1771
LeatherProject/LeatherApp/bin/Debug/MQTTnet.xml
Normal file
1771
LeatherProject/LeatherApp/bin/Debug/MQTTnet.xml
Normal file
File diff suppressed because it is too large
Load Diff
@ -247,3 +247,5 @@ E:\CPL\迈沐智能项目\2023\革博士\源码\V1.0\LeatherProject\LeatherApp\o
|
||||
E:\CPL\迈沐智能项目\2023\革博士\源码\V1.0\LeatherProject\LeatherApp\obj\Debug\LeatherApp.csproj.CopyComplete
|
||||
E:\CPL\迈沐智能项目\2023\革博士\源码\V1.0\LeatherProject\LeatherApp\obj\Debug\革博士AI智能检测系统.exe
|
||||
E:\CPL\迈沐智能项目\2023\革博士\源码\V1.0\LeatherProject\LeatherApp\obj\Debug\革博士AI智能检测系统.pdb
|
||||
E:\CPL\迈沐智能项目\2023\革博士\源码\V1.0\LeatherProject\LeatherApp\bin\Debug\MQTTnet.dll
|
||||
E:\CPL\迈沐智能项目\2023\革博士\源码\V1.0\LeatherProject\LeatherApp\bin\Debug\MQTTnet.xml
|
||||
|
@ -5,6 +5,7 @@
|
||||
<package id="ExcelNumberFormat" version="1.1.0" targetFramework="net48" />
|
||||
<package id="Irony.NetCore" version="1.0.11" targetFramework="net48" />
|
||||
<package id="Microsoft.CSharp" version="4.7.0" targetFramework="net48" />
|
||||
<package id="MQTTnet" version="4.3.3.952" targetFramework="net48" />
|
||||
<package id="Newtonsoft.Json" version="13.0.3" targetFramework="net48" />
|
||||
<package id="OpenCvSharp4" version="4.8.0.20230708" targetFramework="net48" />
|
||||
<package id="OpenCvSharp4.Extensions" version="4.7.0.20230115" targetFramework="net48" />
|
||||
|
BIN
LeatherProject/packages/MQTTnet.4.3.3.952/MQTTnet.4.3.3.952.nupkg
vendored
Normal file
BIN
LeatherProject/packages/MQTTnet.4.3.3.952/MQTTnet.4.3.3.952.nupkg
vendored
Normal file
Binary file not shown.
1771
LeatherProject/packages/MQTTnet.4.3.3.952/lib/net452/MQTTnet.xml
vendored
Normal file
1771
LeatherProject/packages/MQTTnet.4.3.3.952/lib/net452/MQTTnet.xml
vendored
Normal file
File diff suppressed because it is too large
Load Diff
1771
LeatherProject/packages/MQTTnet.4.3.3.952/lib/net461/MQTTnet.xml
vendored
Normal file
1771
LeatherProject/packages/MQTTnet.4.3.3.952/lib/net461/MQTTnet.xml
vendored
Normal file
File diff suppressed because it is too large
Load Diff
1771
LeatherProject/packages/MQTTnet.4.3.3.952/lib/net48/MQTTnet.xml
vendored
Normal file
1771
LeatherProject/packages/MQTTnet.4.3.3.952/lib/net48/MQTTnet.xml
vendored
Normal file
File diff suppressed because it is too large
Load Diff
1766
LeatherProject/packages/MQTTnet.4.3.3.952/lib/net5.0/MQTTnet.xml
vendored
Normal file
1766
LeatherProject/packages/MQTTnet.4.3.3.952/lib/net5.0/MQTTnet.xml
vendored
Normal file
File diff suppressed because it is too large
Load Diff
1766
LeatherProject/packages/MQTTnet.4.3.3.952/lib/net6.0/MQTTnet.xml
vendored
Normal file
1766
LeatherProject/packages/MQTTnet.4.3.3.952/lib/net6.0/MQTTnet.xml
vendored
Normal file
File diff suppressed because it is too large
Load Diff
1766
LeatherProject/packages/MQTTnet.4.3.3.952/lib/net7.0/MQTTnet.xml
vendored
Normal file
1766
LeatherProject/packages/MQTTnet.4.3.3.952/lib/net7.0/MQTTnet.xml
vendored
Normal file
File diff suppressed because it is too large
Load Diff
1766
LeatherProject/packages/MQTTnet.4.3.3.952/lib/netcoreapp3.1/MQTTnet.xml
vendored
Normal file
1766
LeatherProject/packages/MQTTnet.4.3.3.952/lib/netcoreapp3.1/MQTTnet.xml
vendored
Normal file
File diff suppressed because it is too large
Load Diff
1754
LeatherProject/packages/MQTTnet.4.3.3.952/lib/netstandard1.3/MQTTnet.xml
vendored
Normal file
1754
LeatherProject/packages/MQTTnet.4.3.3.952/lib/netstandard1.3/MQTTnet.xml
vendored
Normal file
File diff suppressed because it is too large
Load Diff
1766
LeatherProject/packages/MQTTnet.4.3.3.952/lib/netstandard2.0/MQTTnet.xml
vendored
Normal file
1766
LeatherProject/packages/MQTTnet.4.3.3.952/lib/netstandard2.0/MQTTnet.xml
vendored
Normal file
File diff suppressed because it is too large
Load Diff
1766
LeatherProject/packages/MQTTnet.4.3.3.952/lib/netstandard2.1/MQTTnet.xml
vendored
Normal file
1766
LeatherProject/packages/MQTTnet.4.3.3.952/lib/netstandard2.1/MQTTnet.xml
vendored
Normal file
File diff suppressed because it is too large
Load Diff
BIN
LeatherProject/packages/MQTTnet.4.3.3.952/lib/uap10.0.10240/MQTTnet.pri
vendored
Normal file
BIN
LeatherProject/packages/MQTTnet.4.3.3.952/lib/uap10.0.10240/MQTTnet.pri
vendored
Normal file
Binary file not shown.
1760
LeatherProject/packages/MQTTnet.4.3.3.952/lib/uap10.0.10240/MQTTnet.xml
vendored
Normal file
1760
LeatherProject/packages/MQTTnet.4.3.3.952/lib/uap10.0.10240/MQTTnet.xml
vendored
Normal file
File diff suppressed because it is too large
Load Diff
BIN
LeatherProject/packages/MQTTnet.4.3.3.952/nuget.png
vendored
Normal file
BIN
LeatherProject/packages/MQTTnet.4.3.3.952/nuget.png
vendored
Normal file
Binary file not shown.
After Width: | Height: | Size: 4.0 KiB |
Loading…
Reference in New Issue
Block a user