修改字段
This commit is contained in:
@@ -12,6 +12,7 @@ using DOAN.Common.DynamicApiSimple.Extens;
|
||||
using DOAN.Infrastructure.WebExtensions;
|
||||
using DOAN.ServiceCore.Signalr;
|
||||
using DOAN.ServiceCore.SqlSugar;
|
||||
using DOAN.ServiceCore;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
// NLog: Setup NLog for Dependency injection
|
||||
@@ -89,6 +90,8 @@ builder.Services.AddHslCommunication();
|
||||
|
||||
// 添加本地化服务
|
||||
builder.Services.AddLocalization(options => options.ResourcesPath = "");
|
||||
//永驻线程
|
||||
builder.Services.AddHostedService<DoanBackgroundService>();
|
||||
|
||||
var app = builder.Build();
|
||||
InternalApp.ServiceProvider = app.Services;
|
||||
|
||||
@@ -53,7 +53,7 @@ namespace DOAN.Model.PBL
|
||||
/// 版本
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "version")]
|
||||
public int Version { get; set; }
|
||||
public string Version { get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -29,7 +29,7 @@ namespace DOAN.Model.PBL.Dto
|
||||
/// <summary>
|
||||
/// 版本号
|
||||
/// </summary>
|
||||
public int Version { get; set; }
|
||||
public string Version { get; set; }
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -42,7 +42,7 @@ namespace DOAN.Model.PBL
|
||||
/// 版本号
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "Version")]
|
||||
public int Version { get; set; }
|
||||
public string Version { get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -50,11 +50,17 @@ namespace DOAN.Model.PBL
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// plc address
|
||||
///PLC地址(亮灯拣货)
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "plc_address")]
|
||||
public string PlcAddress { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// PLC地址(空箱补料)
|
||||
/// </summary>
|
||||
[SugarColumn(ColumnName = "plc_address_2")]
|
||||
public string PlcAddress2 { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 创建人
|
||||
/// </summary>
|
||||
|
||||
@@ -127,9 +127,7 @@ namespace DOAN.Service.PBL
|
||||
light_Log.IsSuccess = isSuccess;
|
||||
light_Log.Operationer = "PBL";
|
||||
light_Log.CreatedTime = DateTime.Now;
|
||||
int result = Context.Insertable(light_Log).ExecuteCommand();
|
||||
|
||||
|
||||
int result = Context.Insertable(light_Log).ExecuteCommand();
|
||||
return result > 0;
|
||||
|
||||
}
|
||||
|
||||
96
DOAN.ServiceCore/BackgroundService.cs
Normal file
96
DOAN.ServiceCore/BackgroundService.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
85
DOAN.ServiceCore/DoanBackgroundService.cs
Normal file
85
DOAN.ServiceCore/DoanBackgroundService.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,9 +17,9 @@ namespace DOAN.Infrastructure.PLC
|
||||
// private NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
||||
|
||||
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
|
||||
};
|
||||
@@ -46,13 +46,26 @@ namespace DOAN.Infrastructure.PLC
|
||||
/// <param name="addr"></param>
|
||||
/// <param name="value"></param>
|
||||
/// <returns></returns>
|
||||
public static bool WriteBit(string addr,bool value)
|
||||
public static bool WriteBit(string addr, bool value)
|
||||
{
|
||||
|
||||
|
||||
OperateResult write = siemensTcpNet.Write(addr, value);
|
||||
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()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user