使用新框架与技术代替旧框架与技术,实现涂装车间后道标签扫码程序
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
using MQTTnet.Client;
|
||||
using Prism.Events;
|
||||
using Prism.Regions;
|
||||
using RIZO_Application.Core;
|
||||
using RIZO_Application.Core.Mvvm;
|
||||
using RIZO_Application.Infrastructure.Model;
|
||||
using RIZO_Helper.Tools;
|
||||
|
||||
namespace RIZO_Application.Modules.Base.ViewModels
|
||||
{
|
||||
public class MqttControlViewModel : RegionViewModelBase
|
||||
{
|
||||
private readonly IEventAggregator _eventAggregator;
|
||||
private MqttHelper? _mqttHepler;
|
||||
private SubscriptionToken _token;
|
||||
public MqttControlViewModel(
|
||||
IRegionManager regionManager,
|
||||
IEventAggregator eventAggregator)
|
||||
: base(regionManager)
|
||||
{
|
||||
_eventAggregator = eventAggregator;
|
||||
_token = _eventAggregator.GetEvent<ScanEvent>().Subscribe(OnScanEventReceived, ThreadOption.UIThread, true);
|
||||
Task.Run(Mqtt);
|
||||
}
|
||||
|
||||
public async Task Mqtt()
|
||||
{
|
||||
string serverUrl = "noConfig";
|
||||
string clientId = "noConfig";
|
||||
if (MqttConfigs.Current != null)
|
||||
{
|
||||
serverUrl = MqttConfigs.Current.ServerUrl ?? string.Empty;
|
||||
clientId = MqttConfigs.Current.ClientId ?? string.Empty;
|
||||
}
|
||||
_eventAggregator.GetEvent<SystemLogEvent>().Publish($"Mqtt初始化……");
|
||||
_mqttHepler = new MqttHelper(serverUrl, 1883, clientId);
|
||||
_mqttHepler.MessageReceived += HandleMqttMessage;
|
||||
await _mqttHepler.ConnectAsync();
|
||||
await _mqttHepler.SubscribeAsync($"shgg_mes/print/{clientId}");
|
||||
await _mqttHepler.PublishAsync($"shgg_mes/connect/{clientId}","已连接");
|
||||
// await _mqttHepler.SubscribeAsync("shgg_mes/backEnd/SiteComLabelCode");
|
||||
_eventAggregator.GetEvent<SystemLogEvent>().Publish($"Mqtt初始化完成");
|
||||
}
|
||||
// 定义事件处理方法
|
||||
public void HandleMqttMessage(object sender, MqttApplicationMessageReceivedEventArgs e)
|
||||
{
|
||||
// 获取消息的主题
|
||||
string topic = e.ApplicationMessage.Topic;
|
||||
// 获取消息的负载并转换为字符串
|
||||
string payload = System.Text.Encoding.UTF8.GetString(e.ApplicationMessage.Payload);
|
||||
// 打印接收到的消息信息
|
||||
_eventAggregator.GetEvent<SystemLogEvent>().Publish($"收到信息'{topic}': {payload}");
|
||||
// 控制打印
|
||||
PrintDto printMessage = new()
|
||||
{
|
||||
Path = "/",
|
||||
Name = "需要测试打印",
|
||||
Description = "123"
|
||||
};
|
||||
_eventAggregator.GetEvent<PrintEvent>().Publish(printMessage);
|
||||
}
|
||||
private async void OnScanEventReceived(string message)
|
||||
{
|
||||
if(_mqttHepler != null)
|
||||
{
|
||||
//TODO JSON重新构造发送信息
|
||||
|
||||
await _mqttHepler.PublishAsync($"shgg_mes/backEnd/SiteComLabelCode", message);
|
||||
}
|
||||
}
|
||||
|
||||
public void Destroy()
|
||||
{
|
||||
_token?.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
39
RIZO_Application.Modules.Base/ViewModels/PrintViewModel.cs
Normal file
39
RIZO_Application.Modules.Base/ViewModels/PrintViewModel.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Prism.Events;
|
||||
using Prism.Regions;
|
||||
using RIZO_Application.Core;
|
||||
using RIZO_Application.Core.Mvvm;
|
||||
|
||||
namespace RIZO_Application.Modules.Base.ViewModels
|
||||
{
|
||||
public class PrintViewModel : RegionViewModelBase
|
||||
{
|
||||
private readonly IEventAggregator _eventAggregator;
|
||||
private SubscriptionToken _token;
|
||||
|
||||
public PrintViewModel(
|
||||
IRegionManager regionManager,
|
||||
IEventAggregator eventAggregator)
|
||||
: base(regionManager)
|
||||
{
|
||||
_eventAggregator = eventAggregator;
|
||||
// 订阅事件,使用UI线程处理,保持强引用
|
||||
_token = _eventAggregator.GetEvent<SystemLogEvent>().Subscribe(OnReceived, ThreadOption.UIThread, true);
|
||||
}
|
||||
|
||||
private void OnReceived(string message)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void Destroy()
|
||||
{
|
||||
_token?.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using MQTTnet.Client;
|
||||
using Prism.Events;
|
||||
using Prism.Regions;
|
||||
using RIZO_Application.Core;
|
||||
using RIZO_Application.Core.Mvvm;
|
||||
using RIZO_Application.Infrastructure.Model;
|
||||
using RIZO_Helper.Tools;
|
||||
|
||||
namespace RIZO_Application.Modules.Base.ViewModels
|
||||
{
|
||||
public class ScanControlViewModel : RegionViewModelBase
|
||||
{
|
||||
private readonly IEventAggregator _eventAggregator;
|
||||
private ComScanHelper _scanHelper;
|
||||
|
||||
|
||||
|
||||
|
||||
public ScanControlViewModel(
|
||||
IRegionManager regionManager,
|
||||
IEventAggregator eventAggregator)
|
||||
: base(regionManager)
|
||||
{
|
||||
_eventAggregator = eventAggregator;
|
||||
Task.Run(StartComScan);
|
||||
}
|
||||
public async Task StartComScan()
|
||||
{
|
||||
string comName = "COM1";
|
||||
int baudRate = 9600;
|
||||
if (SerialConfigs.Current != null)
|
||||
{
|
||||
comName = SerialConfigs.Current.ComName ?? string.Empty;
|
||||
baudRate = SerialConfigs.Current.BaudRate ?? 9600;
|
||||
}
|
||||
_eventAggregator.GetEvent<SystemLogEvent>().Publish($"串口扫码枪初始化……串口:{comName}波特率:{baudRate}");
|
||||
_scanHelper = new ComScanHelper(comName, baudRate);
|
||||
if(_scanHelper == null)
|
||||
{
|
||||
Debug.WriteLine("串口打开异常!");
|
||||
return;
|
||||
}
|
||||
_scanHelper.DataReceived += HandleMessage;
|
||||
await _scanHelper.OpenAsync();
|
||||
_eventAggregator.GetEvent<SystemLogEvent>().Publish($"串口扫码初始化完成");
|
||||
}
|
||||
// 定义事件处理方法
|
||||
public void HandleMessage(string labelCode)
|
||||
{
|
||||
// 打印接收到的消息信息
|
||||
_eventAggregator.GetEvent<SystemLogEvent>().Publish($"收到串口信息:{labelCode}");
|
||||
_eventAggregator.GetEvent<ScanEvent>().Publish(labelCode);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using NLog;
|
||||
using Prism.Events;
|
||||
using Prism.Regions;
|
||||
using RIZO_Application.Core;
|
||||
using RIZO_Application.Core.Mvvm;
|
||||
|
||||
namespace RIZO_Application.Modules.Base.ViewModels
|
||||
{
|
||||
public class SystemLogViewModel : RegionViewModelBase
|
||||
{
|
||||
private readonly IEventAggregator _eventAggregator;
|
||||
private SubscriptionToken _token;
|
||||
private string _logMessage = "";
|
||||
private Logger _logger = LogManager.GetCurrentClassLogger();
|
||||
public string LogMessage
|
||||
{
|
||||
get { return _logMessage; }
|
||||
set { SetProperty(ref _logMessage, value); }
|
||||
}
|
||||
public SystemLogViewModel(
|
||||
IRegionManager regionManager,
|
||||
IEventAggregator eventAggregator)
|
||||
: base(regionManager)
|
||||
{
|
||||
OnLogReceived("初始化日志系统");
|
||||
_eventAggregator = eventAggregator;
|
||||
// 订阅事件,使用UI线程处理,保持强引用
|
||||
_token = _eventAggregator.GetEvent<SystemLogEvent>().Subscribe(OnLogReceived, ThreadOption.UIThread, true);
|
||||
|
||||
}
|
||||
|
||||
private void OnLogReceived(string message)
|
||||
{
|
||||
Debug.WriteLine(message);
|
||||
LogMessage += $"\n{DateTime.Now}: {message}";
|
||||
_logger.Info($"\n{DateTime.Now}: {message}");
|
||||
}
|
||||
|
||||
public void Destroy()
|
||||
{
|
||||
_token?.Dispose();
|
||||
}
|
||||
|
||||
public void AddTestLogMessage()
|
||||
{
|
||||
}
|
||||
public override void OnNavigatedTo(NavigationContext navigationContext)
|
||||
{
|
||||
//do something
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user