syc_zhaoqianyan 66fc5b3b19 1.扫码枪实现
2.体重秤实现
3.打印机实现
2025-07-01 14:20:07 +08:00

161 lines
5.9 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Seyounth.Auto.Hs.Runtime.Printer;
using Seyounth.Auto.Hs.Runtime.Scanner;
using Syc.Abp.Application.Contracts;
using Syc.Basic.Web.WMS.Entitys;
using Syc.Basic.Web.WMS.WebSocket;
using Syc.Core.Tools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Uow;
namespace Syc.Basic.Web.WMS
{
/// <summary>
/// 默认的扫码枪扫码触发事件处理
/// </summary>
public class DefaultScannerEventHandle : IScannerEventHandle
{
private readonly IRepository<Produce> produceRepository;
private readonly IPrinterService printerService;
private readonly IRepository<Box> boxRepository;
private readonly IRepository<Silk> silkRepository;
private readonly IUnitOfWorkManager uowm;
private readonly ILogger<DefaultScannerEventHandle> logger;
public DefaultScannerEventHandle(IRepository<Produce> produceRepository,IPrinterService printerService,IRepository<Box> boxRepository,IRepository<Silk> silkRepository,IUnitOfWorkManager unitOfWork,ILogger<DefaultScannerEventHandle> logger)
{
this.produceRepository = produceRepository;
this.printerService = printerService;
this.boxRepository = boxRepository;
this.silkRepository = silkRepository;
this.uowm = unitOfWork;
this.logger = logger;
}
/// <summary>
/// 扫码枪
/// </summary>
/// <param name="code"></param>
/// <param name="id"></param>
/// <returns></returns>
public async Task ExecAsync(string code, int id)
{
using (var uow = uowm.Reserve(UnitOfWork.UnitOfWorkReservationName))
{
try
{
if (id == 1)
await Yarn(code);
else
await Box(code);
await uow.CompleteAsync();
}
catch (Exception ex) when (ex is FriendlyException friendlyException)
{
logger.LogError(ex.GetBaseException(),"扫码报错");
await WebSocketManager.SocketManager.BroadcastAsync(friendlyException.Message);
await uow.RollbackAsync();
}
catch (Exception ex)
{
logger.LogError(ex.GetBaseException(), "扫码报错");
await uow.RollbackAsync();
}
}
}
/// <summary>
/// 整箱扫码
/// </summary>
/// <param name="code"></param>
/// <returns></returns>
public async Task Box(string code)
{
//if (!QueueManage.BoxQueue.Contains(code))
//{
// logger.LogInformation($"{code} 加入box集合");
// QueueManage.BoxQueue.Add(code);
//}
//else
//{
// logger.LogWarning($"{code}已在box集合中无需重复扫码");
//}
}
/// <summary>
/// 丝锭扫码
/// </summary>
/// <param name="code"></param>
/// <returns></returns>
public async Task Yarn(string code)
{
//throw Oops.Oh("异常返回前端测试");
logger.LogInformation($"接收到丝锭条码:{code}");
var produce = await produceRepository.FirstOrDefaultAsync(e => e.IfUse == 1 && e.IsDelete == 0);
var b = await silkRepository.AnyAsync(e => e.Code == code && e.IsDelete == 0);
if (produce is null)
{
throw Oops.Oh("无生产信息,请先设置生产信息再扫码");
}
if (b)
{
throw Oops.Oh("条码已存在");
}
logger.LogInformation($"当前生产信息:{produce.Name}|{produce.Spec}|{produce.Lot_No}|{produce.BoxSpec}|{produce.Qty}");
Silk silk = new Silk()
{
Code = code,
Length = produce.Length,
Lot_No = produce.Lot_No,
Name = produce.Name,
Date = DateTime.Now,
Type = produce.Type,
Status = 0,
Status_Details = "已扫码待称重",
Net_Weight = 0,
Createtime = DateTime.Now,
};
silk = await silkRepository.InsertAsync(silk);
logger.LogInformation($"丝锭:{code} 已添加到数据库");
}
public async Task ExecAsync2(string code, int id)
{
using (var uow = uowm.Reserve(UnitOfWork.UnitOfWorkReservationName))
{
try
{
/*
* 如果有其他的处理写在这里或者另外建一个类实现IScannerEventHandle接口
*/
var msg = new DeviceMessage(id, "扫码枪", code);
if (!code.IsNullOrWhiteSpace())
{
var result = await silkRepository.AnyAsync(x => x.Code == code);
if (result)
msg.Value = $"存在重复编号({code}";
}
await WebSocketManager.SocketManager.BroadcastAsync(msg.ToJsonString());
await uow.CompleteAsync();
}
catch (Exception ex) when (ex is FriendlyException friendlyException)
{
await WebSocketManager.SocketManager.BroadcastAsync(friendlyException.Message);
await uow.RollbackAsync();
}
catch (Exception ex)
{
await uow.RollbackAsync();
}
}
}
}
}