|
|
|
@ -1,5 +1,4 @@
|
|
|
|
|
using HslCommunication.Profinet.Siemens;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using NewLife.Log;
|
|
|
|
|
using SqlSugar;
|
|
|
|
|
using SynPlc.Entity;
|
|
|
|
@ -8,19 +7,17 @@ namespace SynPlc;
|
|
|
|
|
|
|
|
|
|
public class Worker : BackgroundService
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
SiemensS7Net S7 = PlcConnect.Instance;
|
|
|
|
|
private IServiceScopeFactory scopeFactory;
|
|
|
|
|
public Worker(IServiceScopeFactory _scopeFactory)
|
|
|
|
|
private readonly IServiceScopeFactory scopeFactory;
|
|
|
|
|
|
|
|
|
|
public Worker(IServiceScopeFactory scopeFactory)
|
|
|
|
|
{
|
|
|
|
|
this.scopeFactory=_scopeFactory;
|
|
|
|
|
this.scopeFactory = scopeFactory;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
|
|
|
|
{
|
|
|
|
|
while (!stoppingToken.IsCancellationRequested)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
SynPlcFromDb(stoppingToken);
|
|
|
|
@ -29,7 +26,7 @@ public class Worker : BackgroundService
|
|
|
|
|
{
|
|
|
|
|
XTrace.WriteException(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
XTrace.WriteLine("Worker running at: {05}", DateTimeOffset.Now);
|
|
|
|
|
|
|
|
|
|
await Task.Delay(1000, stoppingToken);
|
|
|
|
@ -38,51 +35,57 @@ public class Worker : BackgroundService
|
|
|
|
|
|
|
|
|
|
private async Task SynPlcFromDb(CancellationToken stoppingToken)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
using var scope = scopeFactory.CreateScope();
|
|
|
|
|
var services = scope.ServiceProvider;
|
|
|
|
|
var dbClient = services.GetService<ISqlSugarClient>();
|
|
|
|
|
|
|
|
|
|
var S7 = services.GetService<SiemensS7Net>();
|
|
|
|
|
var operateResult = await S7.ConnectServerAsync();
|
|
|
|
|
if (!operateResult.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
operateResult = await S7.ConnectServerAsync();
|
|
|
|
|
if (!operateResult.IsSuccess) XTrace.WriteLine("连接失败");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var list = await dbClient.Queryable<EEquipmentStateEntity>().ToListAsync(stoppingToken);
|
|
|
|
|
foreach(var e in list)
|
|
|
|
|
foreach (var e in list)
|
|
|
|
|
{
|
|
|
|
|
var a = await S7.ReadBoolAsync(e.Point);
|
|
|
|
|
if (a.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
e.UpdateTime = DateTime.Now;
|
|
|
|
|
e.PValue = a.Content;
|
|
|
|
|
await dbClient.Updateable(e).UpdateColumns(x => new { x.UpdateTime, x.PValue }).ExecuteCommandAsync(stoppingToken);
|
|
|
|
|
await dbClient.Updateable(e).UpdateColumns(x => new { x.UpdateTime, x.PValue })
|
|
|
|
|
.ExecuteCommandAsync(stoppingToken);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
XTrace.WriteLine("{0} 读取异常",e.Point );
|
|
|
|
|
XTrace.WriteLine("{0} 读取异常", e.Point);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var listp = await dbClient.Queryable<EPointsInfoEntity>().ToListAsync(stoppingToken);
|
|
|
|
|
|
|
|
|
|
List<EPointDataEntity> ls = new List<EPointDataEntity>();
|
|
|
|
|
var ls = new List<EPointDataEntity>();
|
|
|
|
|
foreach (var e in listp)
|
|
|
|
|
{
|
|
|
|
|
var a = await S7.ReadDoubleAsync(e.Point);
|
|
|
|
|
if (a.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
EPointDataEntity listd = new EPointDataEntity();
|
|
|
|
|
var listd = new EPointDataEntity();
|
|
|
|
|
listd.ID = Guid.NewGuid().ToString();
|
|
|
|
|
listd.PID = e.ID;
|
|
|
|
|
listd.CreateTime = DateTime.Now;
|
|
|
|
|
listd.Data = a.Content.ToDecimal();
|
|
|
|
|
ls.Add(listd);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
XTrace.WriteLine("{0} 读取异常",e.Point );
|
|
|
|
|
XTrace.WriteLine("{0} 读取异常", e.Point);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ls.Any())
|
|
|
|
|
{
|
|
|
|
|
await dbClient.Insertable(ls).ExecuteCommandAsync(stoppingToken);
|
|
|
|
|
}
|
|
|
|
|
if (ls.Any()) await dbClient.Insertable(ls).ExecuteCommandAsync(stoppingToken);
|
|
|
|
|
}
|
|
|
|
|
}
|