generated from wenjy/Sln.Iot
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
64 lines
1.8 KiB
C#
64 lines
1.8 KiB
C#
using Sln.Iot.Business.Entity;
|
|
using Sln.Iot.Repository.service;
|
|
using System.Reflection;
|
|
using System.Text.Json;
|
|
|
|
namespace Sln.Iot.Business
|
|
{
|
|
/// <summary>
|
|
/// TCPRFID信息绑定
|
|
/// </summary>
|
|
public class TCPRFIDBinding
|
|
{
|
|
private static readonly Lazy<TCPRFIDBinding> lazy = new Lazy<TCPRFIDBinding>(() => new TCPRFIDBinding());
|
|
|
|
public static TCPRFIDBinding Instance
|
|
{
|
|
get
|
|
{
|
|
return lazy.Value;
|
|
}
|
|
}
|
|
|
|
public static Action<string>? GetNewMessage = new Action<string>((message) => { });
|
|
|
|
private readonly TrayBindingService _trayBinding = TrayBindingService.Instance;
|
|
|
|
public TCPRFIDBinding()
|
|
{
|
|
GetNewMessage += StoreBindingMessage;
|
|
}
|
|
|
|
private static readonly PropertyInfo[] _snProperties =
|
|
typeof(TCPRFIDBindingEntity).GetProperties()
|
|
.Where(p => p.Name.StartsWith("Sn", StringComparison.OrdinalIgnoreCase))
|
|
.ToArray();
|
|
|
|
|
|
/// <summary>
|
|
/// 数据绑定到数据库
|
|
/// </summary>
|
|
public void StoreBindingMessage(string message)
|
|
{
|
|
var entity = ParseMessage(message);
|
|
if (entity == null || string.IsNullOrWhiteSpace(entity.Rfid)) return;
|
|
|
|
var prodCodes = _snProperties
|
|
.Select(p => p.GetValue(entity) as string)
|
|
.Where(v => !string.IsNullOrWhiteSpace(v))
|
|
.ToArray();
|
|
|
|
if (prodCodes.Length == 0) return;
|
|
|
|
_trayBinding.TrayBindingRefresh(Guid.NewGuid(), entity.Rfid, prodCodes!);
|
|
}
|
|
|
|
private static TCPRFIDBindingEntity? ParseMessage(string message)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(message)) return null;
|
|
|
|
return JsonSerializer.Deserialize<TCPRFIDBindingEntity>(message);
|
|
}
|
|
}
|
|
}
|