diff --git a/SynPlc/Worker.cs b/SynPlc/Worker.cs index 7bd216c..1fb1620 100644 --- a/SynPlc/Worker.cs +++ b/SynPlc/Worker.cs @@ -34,7 +34,7 @@ public class Worker : BackgroundService XTrace.WriteException(e); } - _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now); + XTrace.WriteLine("Worker running at: {time}", DateTimeOffset.Now); await Task.Delay(1000, stoppingToken); } @@ -42,47 +42,39 @@ public class Worker : BackgroundService private async Task SynPlcFromDb(CancellationToken stoppingToken) { - try + var list = await DB.Queryable().ToListAsync(stoppingToken); + foreach(var e in list) { - var list = await DB.Queryable().ToListAsync(stoppingToken); - foreach(var e in list) + var a = await S7.ReadBoolAsync(e.Point); + if (a.IsSuccess) { - var a = await S7.ReadBoolAsync(e.Point); - if (a.IsSuccess) - { - e.UpdateTime = DateTime.Now; - e.PValue = a.Content; - await DB.Updateable(e).UpdateColumns(x => new { x.UpdateTime, x.PValue }).ExecuteCommandAsync(stoppingToken); - } - else - { - XTrace.WriteLine("{0} 读取异常",e.Point ); - } + e.UpdateTime = DateTime.Now; + e.PValue = a.Content; + await DB.Updateable(e).UpdateColumns(x => new { x.UpdateTime, x.PValue }).ExecuteCommandAsync(stoppingToken); } - - var listp = await DB.Queryable().ToListAsync(stoppingToken); - EPointDataEntity listd = new EPointDataEntity(); - foreach (var e in listp) + else { - var a = await S7.ReadDoubleAsync(e.Point); - if (a.IsSuccess) - { - listd.ID = Guid.NewGuid().ToString(); - listd.PID = e.ID; - listd.CreateTime = DateTime.Now; - listd.Data = a.Content.ToDecimal(); - await DB.Insertable(listd).ExecuteCommandAsync(stoppingToken); - } - else - { - XTrace.WriteLine("{0} 读取异常",e.Point ); - } + XTrace.WriteLine("{0} 读取异常",e.Point ); } } - catch (Exception e) + + var listp = await DB.Queryable().ToListAsync(stoppingToken); + EPointDataEntity listd = new EPointDataEntity(); + foreach (var e in listp) { - Console.WriteLine(e); - throw; + var a = await S7.ReadDoubleAsync(e.Point); + if (a.IsSuccess) + { + listd.ID = Guid.NewGuid().ToString(); + listd.PID = e.ID; + listd.CreateTime = DateTime.Now; + listd.Data = a.Content.ToDecimal(); + await DB.Insertable(listd).ExecuteCommandAsync(stoppingToken); + } + else + { + XTrace.WriteLine("{0} 读取异常",e.Point ); + } } } } \ No newline at end of file