修改字段

This commit is contained in:
qianhao.xu
2024-11-08 10:10:59 +08:00
parent 2fd4dafc1b
commit 2f5c090bc1
9 changed files with 213 additions and 12 deletions

View File

@@ -12,6 +12,7 @@ using DOAN.Common.DynamicApiSimple.Extens;
using DOAN.Infrastructure.WebExtensions; using DOAN.Infrastructure.WebExtensions;
using DOAN.ServiceCore.Signalr; using DOAN.ServiceCore.Signalr;
using DOAN.ServiceCore.SqlSugar; using DOAN.ServiceCore.SqlSugar;
using DOAN.ServiceCore;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
// NLog: Setup NLog for Dependency injection // NLog: Setup NLog for Dependency injection
@@ -89,6 +90,8 @@ builder.Services.AddHslCommunication();
// 添加本地化服务 // 添加本地化服务
builder.Services.AddLocalization(options => options.ResourcesPath = ""); builder.Services.AddLocalization(options => options.ResourcesPath = "");
//永驻线程
builder.Services.AddHostedService<DoanBackgroundService>();
var app = builder.Build(); var app = builder.Build();
InternalApp.ServiceProvider = app.Services; InternalApp.ServiceProvider = app.Services;

View File

@@ -53,7 +53,7 @@ namespace DOAN.Model.PBL
/// 版本 /// 版本
/// </summary> /// </summary>
[SugarColumn(ColumnName = "version")] [SugarColumn(ColumnName = "version")]
public int Version { get; set; } public string Version { get; set; }
/// <summary> /// <summary>

View File

@@ -29,7 +29,7 @@ namespace DOAN.Model.PBL.Dto
/// <summary> /// <summary>
/// 版本号 /// 版本号
/// </summary> /// </summary>
public int Version { get; set; } public string Version { get; set; }

View File

@@ -42,7 +42,7 @@ namespace DOAN.Model.PBL
/// 版本号 /// 版本号
/// </summary> /// </summary>
[SugarColumn(ColumnName = "Version")] [SugarColumn(ColumnName = "Version")]
public int Version { get; set; } public string Version { get; set; }
/// <summary> /// <summary>

View File

@@ -50,11 +50,17 @@ namespace DOAN.Model.PBL
/// <summary> /// <summary>
/// plc address ///PLC地址(亮灯拣货)
/// </summary> /// </summary>
[SugarColumn(ColumnName = "plc_address")] [SugarColumn(ColumnName = "plc_address")]
public string PlcAddress { get; set; } public string PlcAddress { get; set; }
/// <summary>
/// PLC地址(空箱补料)
/// </summary>
[SugarColumn(ColumnName = "plc_address_2")]
public string PlcAddress2 { get; set; }
/// <summary> /// <summary>
/// 创建人 /// 创建人
/// </summary> /// </summary>

View File

@@ -127,9 +127,7 @@ namespace DOAN.Service.PBL
light_Log.IsSuccess = isSuccess; light_Log.IsSuccess = isSuccess;
light_Log.Operationer = "PBL"; light_Log.Operationer = "PBL";
light_Log.CreatedTime = DateTime.Now; light_Log.CreatedTime = DateTime.Now;
int result = Context.Insertable(light_Log).ExecuteCommand(); int result = Context.Insertable(light_Log).ExecuteCommand();
return result > 0; return result > 0;
} }

View File

@@ -0,0 +1,96 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using DOAN.Infrastructure.PLC;
using DOAN.Model.PBL;
using DOAN.Model.System;
using Microsoft.Extensions.Hosting;
using SqlSugar;
using SqlSugar.IOC;
namespace DOAN.ServiceCore
{
public class DoanBackgroundService : IHostedService, IDisposable
{
private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
private Task _executingTask;
public Task StartAsync(CancellationToken cancellationToken)
{
PLCTool.ConnectPLC();
// 当服务开始时,启动后台任务
_executingTask = ExecuteAsync(_cancellationTokenSource.Token);
return _executingTask.IsCompleted ? _executingTask : Task.CompletedTask;
}
public async Task StopAsync(CancellationToken cancellationToken)
{
// 请求取消后台任务
_cancellationTokenSource.Cancel();
// 等待后台任务完成
await Task.WhenAny(_executingTask, Task.Delay(Timeout.Infinite, cancellationToken));
}
private async Task ExecuteAsync(CancellationToken stoppingToken)
{
// 获取所有传感器 地址
int index = 1;
while (!stoppingToken.IsCancellationRequested)
{
Storagelocation storagelocation = await DbScoped.SugarScope.CopyNew()
.Queryable<Storagelocation>().Where(it => it.Id == index).FirstAsync();
bool result = PLCTool.ReadBit(storagelocation.PlcAddress2);
// 写补料日志
Inventorylog inventorylog = new Inventorylog();
inventorylog.Id = SnowFlakeSingle.Instance.NextId().ToString();
inventorylog.RackCode = storagelocation.RackCode;
if (result)
{
//缺料
storagelocation.PackageNum = 2;
inventorylog.Operation = 1;
inventorylog.PackageNum = 2;
}
else
{
//补料
storagelocation.PackageNum = 4;
inventorylog.Operation = 2;
inventorylog.PackageNum = 4;
}
inventorylog.CreatedBy = "PLC";
inventorylog.CreatedTime = DateTime.Now.ToLocalTime();
await DbScoped.SugarScope.CopyNew().Insertable(storagelocation).ExecuteCommandAsync();
await Task.Delay(3000, stoppingToken);
index++;
if (index > 16)
{
index = 1;
}
}
}
public void Dispose()
{
_cancellationTokenSource.Cancel();
_executingTask.Wait();
_cancellationTokenSource.Dispose();
}
}
}

View File

@@ -0,0 +1,85 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using DOAN.Infrastructure.PLC;
using DOAN.Model.PBL;
using DOAN.Model.System;
using Microsoft.Extensions.Hosting;
using SqlSugar;
using SqlSugar.IOC;
namespace DOAN.ServiceCore
{
public class BackgroundService : IHostedService, IDisposable
{
private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
private Task _executingTask;
public Task StartAsync(CancellationToken cancellationToken)
{
PLCTool.ConnectPLC();
// 当服务开始时,启动后台任务
_executingTask = ExecuteAsync(_cancellationTokenSource.Token);
return _executingTask.IsCompleted ? _executingTask : Task.CompletedTask;
}
public async Task StopAsync(CancellationToken cancellationToken)
{
// 请求取消后台任务
_cancellationTokenSource.Cancel();
// 等待后台任务完成
await Task.WhenAny(_executingTask, Task.Delay(Timeout.Infinite, cancellationToken));
}
private async Task ExecuteAsync(CancellationToken stoppingToken)
{
// 获取所有传感器 地址
int index = 1;
while (!stoppingToken.IsCancellationRequested)
{
Storagelocation storagelocation = await DbScoped.SugarScope.CopyNew()
.Queryable<Storagelocation>().Where(it => it.Id == index).FirstAsync();
bool result = PLCTool.ReadBit(storagelocation.PlcAddress2);
if (result)
{
storagelocation.PackageNum = 2;
}
else
{
storagelocation.PackageNum = 4;
}
await DbScoped.SugarScope.CopyNew().Insertable(storagelocation).ExecuteCommandAsync();
// 写补料日志
Inventorylog inventorylog = new Inventorylog();
inventorylog.Id = SnowFlakeSingle.Instance.NextId().ToString();
inventorylog.RackCode = storagelocation.RackCode;
index++;
if (index > 16)
{
index = 1;
}
}
}
public void Dispose()
{
_cancellationTokenSource.Cancel();
_executingTask.Wait();
_cancellationTokenSource.Dispose();
}
}
}

View File

@@ -17,9 +17,9 @@ namespace DOAN.Infrastructure.PLC
// private NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); // private NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
public static SiemensS7Net siemensTcpNet = null; public static SiemensS7Net siemensTcpNet = null;
public static bool ConnectPLC() public static bool ConnectPLC()
{ {
siemensTcpNet= new SiemensS7Net(SiemensPLCS.S200Smart, "192.168.2.1") siemensTcpNet = new SiemensS7Net(SiemensPLCS.S200Smart, "192.168.2.1")
{ {
ConnectTimeOut = 5000 ConnectTimeOut = 5000
}; };
@@ -46,13 +46,26 @@ namespace DOAN.Infrastructure.PLC
/// <param name="addr"></param> /// <param name="addr"></param>
/// <param name="value"></param> /// <param name="value"></param>
/// <returns></returns> /// <returns></returns>
public static bool WriteBit(string addr,bool value) public static bool WriteBit(string addr, bool value)
{ {
OperateResult write = siemensTcpNet.Write(addr, value); OperateResult write = siemensTcpNet.Write(addr, value);
return write.IsSuccess; return write.IsSuccess;
} }
/// <summary>
/// 读取bit
/// </summary>
/// <param name="addr"></param>
/// <returns></returns>
public static bool ReadBit(string addr)
{
bool M100_7 = siemensTcpNet.ReadBool(addr).Content;
return M100_7;
}
public static void ConnectClose() public static void ConnectClose()
{ {