PLC 交互

This commit is contained in:
qianhao.xu
2024-11-08 08:42:33 +08:00
parent 74c849149e
commit 47855d3f54
7 changed files with 104 additions and 48 deletions

View File

@@ -3,6 +3,7 @@ using DOAN.Model.PBL.Dto;
using DOAN.Model.PBL;
using DOAN.Service.PBL.IService;
using DOAN.Admin.WebApi.Filters;
using DOAN.ServiceCore.Middleware;
//创建时间2024-09-23
namespace DOAN.Admin.WebApi.Controllers.PBL
@@ -23,6 +24,7 @@ namespace DOAN.Admin.WebApi.Controllers.PBL
//TODO 接受工单 亮灯
[HttpPost("mes_light_up")]
[DoanPlcActionFilter]
public IActionResult MESLightUp([FromBody] LightUpDto light)
{
var response= mesInteraction.MESLightUp(light);
@@ -32,21 +34,16 @@ namespace DOAN.Admin.WebApi.Controllers.PBL
//TODO 扫码灭灯
[HttpGet("mes_light_down")]
[DoanPlcActionFilter]
public IActionResult MESLightDown(string scan_code)
{
var response = mesInteraction.MESLightDown(scan_code);
return SUCCESS(response);
}
[HttpGet("test_plc")]
public IActionResult TestPLC(int num)
{
var response = mesInteraction.TestPLC(num);
return SUCCESS(response);
}
}

View File

@@ -10,7 +10,7 @@ namespace DOAN.Model.PBL
/// 库存日志
/// </summary>
[SugarTable("light_log")]
public class Light_Log
public class Light_Log
{
/// <summary>
/// 雪花
@@ -29,16 +29,19 @@ namespace DOAN.Model.PBL
/// 货架号
/// </summary>
[SugarColumn(ColumnName = "shelf_code")]
public string ShelfCode { get; set; }
public string ShelfCode { get; set; }
/// <summary>
/// 操作者
/// </summary>
[SugarColumn(ColumnName = "operationer")]
public string Operationer { get; set; }
public string Operationer { get; set; }
/// <summary>
/// 是否plc成功操作
/// </summary>
[SugarColumn(ColumnName = "is_success")]
public bool IsSuccess { get; set; }
/// <summary>
/// 创建时间
/// </summary>

View File

@@ -47,6 +47,14 @@ namespace DOAN.Model.PBL
[SugarColumn(ColumnName = "is_light")]
public int? IsLight { get; set; }
/// <summary>
/// plc address
/// </summary>
[SugarColumn(ColumnName = "plc_address")]
public string PlcAddress { get; set; }
/// <summary>
/// 创建人
/// </summary>

View File

@@ -10,7 +10,7 @@ namespace DOAN.Service.PBL.IService
{
public interface IMESInteractionServcie
{
int TestPLC(int num);
bool MESLightUp(LightUpDto light);
bool MESLightDown(string scan_code);
}

View File

@@ -19,14 +19,7 @@ namespace DOAN.Service.PBL
[AppService(ServiceType = typeof(IMESInteractionServcie), ServiceLifetime = LifeTime.Transient)]
public class MESInteractionServcie : BaseService<Storagelocation>, IMESInteractionServcie
{
public int TestPLC(int num)
{
PCLTool.ConnectPLC();
int result = PCLTool.Write("VB100", num);
PCLTool.ConnectClose();
return result;
}
public bool MESLightUp(LightUpDto light)
{
@@ -50,13 +43,17 @@ namespace DOAN.Service.PBL
//TODO PLC 交互
// 对应料架 亮灯字段
MirrorshellShelf.IsLight = 1;
MirrorshellBody.IsLight = 1;
Context.Insertable(MirrorshellShelf).ExecuteCommand();
Context.Insertable(MirrorshellBody).ExecuteCommand();
// 通知PLC
bool isSucesss = PLCTool.WriteBit(MirrorshellShelf.PlcAddress, true);
if (isSucesss)
{
MirrorshellShelf.IsLight = 1;
MirrorshellBody.IsLight = 1;
Context.Insertable(MirrorshellShelf).ExecuteCommand();
Context.Insertable(MirrorshellBody).ExecuteCommand();
}
//亮灯日志
Light_Log light_Log = new Light_Log();
light_Log.Id = XUEHUA;
@@ -69,6 +66,9 @@ namespace DOAN.Service.PBL
light_Log.Operationer = "PBL";
light_Log.CreatedTime = DateTime.Now;
light_Log.IsSuccess = isSucesss;
Light_Log light_Log2 = new Light_Log();
light_Log2.Id = XUEHUA;
light_Log2.LightOperation = 1;
@@ -80,6 +80,7 @@ namespace DOAN.Service.PBL
light_Log2.Operationer = "PBL";
light_Log2.CreatedTime = DateTime.Now;
light_Log2.IsSuccess = isSucesss;
Context.Insertable(light_Log2).ExecuteCommand();
int result = Context.Insertable(light_Log).ExecuteCommand();
return result > 0;
@@ -87,6 +88,11 @@ namespace DOAN.Service.PBL
}
/// <summary>
/// 灭灯
/// </summary>
/// <param name="scan_code"></param>
/// <returns></returns>
public bool MESLightDown(string scan_code)
{
// 1.记录MES交互记录
@@ -97,15 +103,20 @@ namespace DOAN.Service.PBL
Context.Insertable(item).ExecuteCommand();
//2 找到对应的料架 灭灯
Storagelocation storagelocation = Context.Queryable<Storagelocation>().Where(it => it.Partnumber == scan_code).First();
//TODO PLC 交互
bool isSuccess= PLCTool.WriteBit(storagelocation.PlcAddress, false);
//亮灯日志
if (isSuccess)
{
storagelocation.IsLight = 1;
storagelocation.PackageNum =storagelocation.PackageNum - 1;
//3 扣减对应的库存
Context.Insertable(storagelocation).ExecuteCommand();
}
//灭灯日志
Light_Log light_Log = new Light_Log();
light_Log.Id = XUEHUA;
light_Log.LightOperation = 2;
@@ -113,12 +124,14 @@ namespace DOAN.Service.PBL
{
light_Log.ShelfCode = storagelocation.RackCode;
}
light_Log.IsSuccess = isSuccess;
light_Log.Operationer = "PBL";
light_Log.CreatedTime = DateTime.Now;
int result = Context.Insertable(light_Log).ExecuteCommand();
//3 扣减对应的库存
return result > 0;

View File

@@ -0,0 +1,39 @@
using DOAN.Infrastructure.PLC;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
namespace DOAN.ServiceCore.Middleware
{
public class DoanPlcActionFilter : ActionFilterAttribute
{
///// <summary>
///// PLC地址
///// </summary>
//private readonly string addr;
//private readonly bool value;
//public DoanPlcActionFilter(string addr,string value)
//{
// addr = addr;
// value = value;
//}
public override void OnActionExecuting(ActionExecutingContext context)
{
//建立plC 连接
PLCTool.ConnectPLC();
base.OnActionExecuting(context);
}
public override void OnActionExecuted(ActionExecutedContext context)
{
// 关闭pLc连接
PLCTool.ConnectClose();
base.OnActionExecuted(context);
}
}
}

View File

@@ -10,7 +10,7 @@ using System.Threading.Tasks;
namespace DOAN.Infrastructure.PLC
{
public class PCLTool
public class PLCTool
{
@@ -40,22 +40,18 @@ namespace DOAN.Infrastructure.PLC
}
public static int Write(string addr,int num)
/// <summary>
/// 写入bit
/// </summary>
/// <param name="addr"></param>
/// <param name="value"></param>
/// <returns></returns>
public static bool WriteBit(string addr,bool value)
{
OperateResult write = siemensTcpNet.Write(addr, (byte)num);
if (write.IsSuccess)
{
Console.WriteLine("Write [v100] success");
return 1;
}
else
{
Console.WriteLine("Write [v100] failed: " + write.Message);
return 0;
}
OperateResult write = siemensTcpNet.Write(addr, value);
return write.IsSuccess;
}
public static void ConnectClose()